Commit 5266865f authored by Tsaiilin(蔡依林)'s avatar Tsaiilin(蔡依林)

Merge branch 'transfer' into 'master'

Transfer

See merge request !13
parents f5abc227 ec9ec7e2
...@@ -46,7 +46,7 @@ func init() { ...@@ -46,7 +46,7 @@ func init() {
func initial() { func initial() {
if global.Config.PprofPort > 0 { if global.Config.PprofPort > 0 {
safe.SafeGo(func() { safe.Go(func() {
addr := fmt.Sprintf("127.0.0.1:%d", global.Config.PprofPort) addr := fmt.Sprintf("127.0.0.1:%d", global.Config.PprofPort)
log.Run().Infof("enable pprof: %s", addr) log.Run().Infof("enable pprof: %s", addr)
log.Run().Error(http.ListenAndServe(addr, nil)) log.Run().Error(http.ListenAndServe(addr, nil))
......
...@@ -12,14 +12,13 @@ import ( ...@@ -12,14 +12,13 @@ import (
type Client struct { type Client struct {
config *model.Configure config *model.Configure
host string host string
port int port int
localAddr net.Addr localAddr net.Addr
natTunnel getty.Client natTunnel getty.Client
session getty.Session session getty.Session
connStore sync.Map transferStore sync.Map
sessionStore sync.Map dnsCache *freecache.Cache
dnsCache *freecache.Cache
} }
func NewClientWithConf(cfg *model.Configure, host string, port int) *Client { func NewClientWithConf(cfg *model.Configure, host string, port int) *Client {
...@@ -35,13 +34,12 @@ func NewCli(cfg *model.Configure, host string, port int) *Client { ...@@ -35,13 +34,12 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
} }
} }
client := &Client{ client := &Client{
config: cfg, config: cfg,
host: host, host: host,
port: port, port: port,
localAddr: localAddr, localAddr: localAddr,
connStore: sync.Map{}, transferStore: sync.Map{},
sessionStore: sync.Map{}, dnsCache: freecache.NewCache(1024),
dnsCache: freecache.NewCache(1024),
} }
return client return client
...@@ -50,4 +48,3 @@ func NewCli(cfg *model.Configure, host string, port int) *Client { ...@@ -50,4 +48,3 @@ func NewCli(cfg *model.Configure, host string, port int) *Client {
func (client *Client) StartUp() { func (client *Client) StartUp() {
client.connect() client.connect()
} }
...@@ -23,7 +23,7 @@ type ClusterClient struct { ...@@ -23,7 +23,7 @@ type ClusterClient struct {
func (c *ClusterClient) Start() { func (c *ClusterClient) Start() {
c.check() c.check()
safe.SafeGo(func() { safe.Go(func() {
var timer = time.NewTimer(5 * time.Minute) var timer = time.NewTimer(5 * time.Minute)
for { for {
c.connectNatServers() c.connectNatServers()
...@@ -32,7 +32,7 @@ func (c *ClusterClient) Start() { ...@@ -32,7 +32,7 @@ func (c *ClusterClient) Start() {
} }
}) })
if global.Config.Redial.Valid() { if global.Config.Redial.Valid() {
safe.SafeGo(func() { safe.Go(func() {
// 加上随机 防止vps在同时间重启 // 加上随机 防止vps在同时间重启
duration := c.randomDuration() duration := c.randomDuration()
log.Run().Infof("Redial interval %+v", duration) log.Run().Infof("Redial interval %+v", duration)
...@@ -142,7 +142,7 @@ func (c *ClusterClient) check() { ...@@ -142,7 +142,7 @@ func (c *ClusterClient) check() {
url = global.Config.NetCheckUrl url = global.Config.NetCheckUrl
} }
safe.SafeGo(func() { safe.Go(func() {
var timer = time.NewTimer(interval) var timer = time.NewTimer(interval)
for { for {
timer.Reset(interval) timer.Reset(interval)
......
This diff is collapsed.
...@@ -75,7 +75,7 @@ func (client *Client) Redial(tag string) { ...@@ -75,7 +75,7 @@ func (client *Client) Redial(tag string) {
if !client.config.Redial.Valid() { if !client.config.Redial.Valid() {
return return
} }
log.Run().Infof("[Redial %s] Send offline message", tag) log.Run().Infof("[Redial %s] seed offline message", tag)
if _, _, err := client.session.WritePkg(OfflinePacket, 0); err != nil { if _, _, err := client.session.WritePkg(OfflinePacket, 0); err != nil {
log.Run().Errorf("[Redial %s] write offline to server error %s", tag, err.Error()) log.Run().Errorf("[Redial %s] write offline to server error %s", tag, err.Error())
} }
......
package client
import (
"errors"
"fmt"
"net"
"sync"
"time"
"virjar.com/majora-go/common"
"virjar.com/majora-go/log"
"virjar.com/majora-go/protocol"
"virjar.com/majora-go/safe"
"virjar.com/majora-go/trace"
)
type Transfer struct {
serialNumber int64
client *Client
upstreamConn *net.TCPConn
recorder trace.Recorder
transferChan chan *protocol.MajoraPacket
transferToUpstreamFunc func(t *Transfer, p *protocol.MajoraPacket)
transferToDownstreamFunc func(t *Transfer, data []byte, err error)
once sync.Once
cancel chan struct{}
}
func NewTransfer(serialNumber int64, client *Client, conn *net.TCPConn, recorder trace.Recorder) *Transfer {
return &Transfer{
serialNumber: serialNumber,
client: client,
upstreamConn: conn,
recorder: recorder,
transferChan: make(chan *protocol.MajoraPacket, 10),
once: sync.Once{},
cancel: make(chan struct{}, 0),
}
}
func (t *Transfer) SetTransferToUpstreamFunc(f func(t *Transfer, p *protocol.MajoraPacket)) {
t.transferToUpstreamFunc = f
}
func (t *Transfer) SetTransferToDownstreamFunc(f func(t *Transfer, data []byte, err error)) {
t.transferToDownstreamFunc = f
}
func (t *Transfer) TransferToUpstream(p *protocol.MajoraPacket) {
t.transferChan <- p
}
func (t *Transfer) Start() {
if t.transferToUpstreamFunc == nil {
panic(errors.New("transferToUpstreamFunc is nil"))
}
if t.transferToDownstreamFunc == nil {
panic(errors.New("transferToDownstreamFunc is nil"))
}
safe.Go(func() {
for {
select {
case p := <-t.transferChan:
t.transferToUpstreamFunc(t, p)
case <-t.cancel:
return
}
}
})
safe.Go(func() {
traceRecorder := t.recorder
traceRecorder.RecordEvent(trace.UpStreamEvent, fmt.Sprintf("Ready read from upstream (sn:%d)", t.serialNumber))
log.Run().Debugf("[handleUpStream] %d-> handleUpStream start...", t.serialNumber)
for {
buf := make([]byte, common.BufSize)
cnt, err := t.upstreamConn.Read(buf)
t.transferToDownstreamFunc(t, buf[0:cnt], err)
if err != nil {
break
}
}
})
}
func (t *Transfer) Close() {
t.once.Do(func() {
readDeadLine := time.Now().Add(3 * time.Millisecond)
t.recorder.RecordEvent(trace.DisconnectEvent, fmt.Sprintf("Set upstream read deadline:%s (sn:%d)",
readDeadLine.Format("2006-01-02 15:04:05.000000"), t.serialNumber))
err := t.upstreamConn.SetReadDeadline(readDeadLine)
if err != nil {
t.recorder.RecordErrorEvent(trace.DisconnectEvent,
fmt.Sprintf("Set upstream read deadline failed (sn:%d)", t.serialNumber), err)
_ = t.upstreamConn.Close()
}
close(t.cancel)
})
}
tunnel_addr: majora-vps-zj.virjar.com:5879 tunnel_addr: 127.0.0.1:5879
dns_server: 114.114.114.114:53 dns_server: 114.114.114.114:53
daemon: true #daemon: true
log_level: debug log_level: debug
log_path: ./majora-log/ log_path: ./majora-log/
reconn_intervalz: 5s reconn_intervalz: 5s
...@@ -8,11 +8,11 @@ net_check_interval: 5s ...@@ -8,11 +8,11 @@ net_check_interval: 5s
dns_cache_duration: 10m dns_cache_duration: 10m
net_check_url: https://www.baidu.com[extra] net_check_url: https://www.baidu.com[extra]
redial: #redial:
command: /bin/bash # command: /bin/bash
exec_path: /root/ppp_redial.sh # exec_path: /root/ppp_redial.sh
redial_duration: 5m # redial_duration: 5m
wait_time: 10s # wait_time: 10s
extra: extra:
account: superman account: superman
...@@ -2,7 +2,7 @@ package safe ...@@ -2,7 +2,7 @@ package safe
import "virjar.com/majora-go/log" import "virjar.com/majora-go/log"
func SafeGo(f func()) { func Go(f func()) {
go func() { go func() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
......
...@@ -22,7 +22,7 @@ var ( ...@@ -22,7 +22,7 @@ var (
) )
func init() { func init() {
safe.SafeGo(func() { safe.Go(func() {
for { for {
e := <-sessionEventChan e := <-sessionEventChan
if e.Err != nil { if e.Err != nil {
...@@ -133,15 +133,9 @@ func acquireRecorder(sessionId string, host, user string, enable bool) Recorder ...@@ -133,15 +133,9 @@ func acquireRecorder(sessionId string, host, user string, enable bool) Recorder
} }
type Session struct { func NewRecorder(sessionId string, host string, user string, enable bool) Recorder {
Recorder Recorder
}
func NewSession(sessionId string, host string, user string, enable bool) *Session {
if len(sessionId) == 0 { if len(sessionId) == 0 {
sessionId = sessionIdNop sessionId = sessionIdNop
} }
return &Session{ return acquireRecorder(sessionId, host, user, enable)
Recorder: acquireRecorder(sessionId, host, user, enable),
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment