From 064d52eae2f67dfb2d41efc0a69e411d17634a41 Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 27 Aug 2021 12:23:45 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/pool/conn.go | 10 +--------- code/client/tunnel/link.go | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/code/client/pool/conn.go b/code/client/pool/conn.go index 6184526..cf1c82d 100644 --- a/code/client/pool/conn.go +++ b/code/client/pool/conn.go @@ -114,15 +114,7 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) { if ch == nil { ch = conn.unknownRead } - select { - case ch <- msg: - case <-time.After(conn.parent.cfg.ReadTimeout): - logging.Error("write read channel for link %s timeouted", linkID) - if ch == conn.unknownRead { - continue - } - close(ch) - } + ch <- msg } } diff --git a/code/client/tunnel/link.go b/code/client/tunnel/link.go index 82305fd..5d9d062 100644 --- a/code/client/tunnel/link.go +++ b/code/client/tunnel/link.go @@ -89,7 +89,7 @@ func (link *Link) localRead() { if !link.closeFromRemote { link.remote.SendDisconnect(link.target, link.targetIdx, link.id) } - // logging.Error("read data on tunnel %s link %s failed, err=%v", link.parent.Name, link.id, err) + logging.Error("read data on tunnel %s link %s failed, err=%v", link.parent.Name, link.id, err) return } if n == 0 { From 7523efe6ef31407689feac5b86a5645a34dea55e Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 27 Aug 2021 14:15:38 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/server/handler/handler.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index 41b7b58..15c0c6b 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -160,6 +160,21 @@ func (h *Handler) msgHook(msg *network.Msg, from, to *client) { h.lockLinks.Lock() h.links[id] = pair h.lockLinks.Unlock() + logging.Info("link %s name %s request from %s-%d to %s-%d", + id, msg.GetCreq().GetName(), from.parent.id, from.idx, to.parent.id, to.idx) + case network.Msg_connect_rep: + rep := msg.GetCrep() + if rep.GetOk() { + logging.Info("link %s from %s-%d to %s-%d connect successed", + rep.GetId(), from.parent.id, from.idx, to.parent.id, to.idx) + } else { + logging.Info("link %s from %s-%d to %s-%d connect failed, %s", + rep.GetId(), from.parent.id, from.idx, to.parent.id, to.idx, rep.GetMsg()) + } + case network.Msg_forward: + data := msg.GetXData() + logging.Debug("link %s forward %d bytes from %s-%d to %s-%d", + data.GetLid(), len(data.GetData()), from.parent.id, from.idx, to.parent.id, to.idx) case network.Msg_disconnect: id := msg.GetXDisconnect().GetId() if from != nil { @@ -171,6 +186,9 @@ func (h *Handler) msgHook(msg *network.Msg, from, to *client) { h.lockLinks.Lock() delete(h.links, id) h.lockLinks.Unlock() + disconnect := msg.GetXDisconnect() + logging.Info("link %s disconnect from %s-%d to %s-%d", + disconnect.GetId(), from.parent.id, from.idx, to.parent.id, to.idx) } msg.From = from.parent.id msg.FromIdx = from.idx From 1eaea40a50475bd6e96c792d0ff4acb4ae1481ce Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 27 Aug 2021 14:21:33 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E5=B9=B6=E5=8F=91=E8=BF=9E=E6=8E=A5ID=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/pool/pool.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/code/client/pool/pool.go b/code/client/pool/pool.go index 2334403..bd2f3dd 100644 --- a/code/client/pool/pool.go +++ b/code/client/pool/pool.go @@ -5,6 +5,7 @@ import ( "natpass/code/client/global" "natpass/code/network" "sync" + "sync/atomic" "time" "github.com/lwch/logging" @@ -58,12 +59,12 @@ func (p *Pool) Get(id ...string) *Conn { return conn } - p.idx++ - conn := p.connect(p.idx) + idx := atomic.AddUint32(&p.idx, 1) + conn := p.connect(idx) if conn == nil { return nil } - c := newConn(p, conn, p.idx) + c := newConn(p, conn, idx) p.Lock() p.conns[c.Idx] = c From 9e99ccbd4ab516d30b36520f8ea264ad50d61a58 Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 27 Aug 2021 14:26:23 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=9F=E5=AD=90?= =?UTF-8?q?=E5=8A=A0=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/server/handler/clients.go | 7 ++++--- code/server/handler/handler.go | 2 -- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/code/server/handler/clients.go b/code/server/handler/clients.go index 21453c9..9d4601c 100644 --- a/code/server/handler/clients.go +++ b/code/server/handler/clients.go @@ -3,6 +3,7 @@ package handler import ( "natpass/code/network" "sync" + "sync/atomic" "time" "github.com/lwch/logging" @@ -13,7 +14,7 @@ type clients struct { parent *Handler id string data map[uint32]*client // idx => client - idx int + idx uint32 } func newClients(parent *Handler, id string) *clients { @@ -48,8 +49,8 @@ func (cs *clients) next() *client { } cs.RUnlock() if len(list) > 0 { - cli := list[cs.idx%len(list)] - cs.idx++ + idx := atomic.AddUint32(&cs.idx, 1) + cli := list[int(idx)%len(list)] return cli } return nil diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index 15c0c6b..9c25307 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -18,7 +18,6 @@ type Handler struct { clients map[string]*clients // client id => client lockLinks sync.RWMutex links map[string][2]*client // link id => endpoints - idx int } // New create handler @@ -27,7 +26,6 @@ func New(cfg *global.Configure) *Handler { cfg: cfg, clients: make(map[string]*clients), links: make(map[string][2]*client), - idx: 0, } } From 8dbc51cb80c04db4f7975128238d20b0c3a044b4 Mon Sep 17 00:00:00 2001 From: lwch Date: Wed, 1 Sep 2021 11:14:28 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/main.go | 3 ++- code/client/tunnel/link.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/code/client/main.go b/code/client/main.go index 50950c9..4892f4c 100644 --- a/code/client/main.go +++ b/code/client/main.go @@ -99,7 +99,8 @@ func main() { linkID = msg.GetXData().GetLid() } if len(linkID) > 0 { - logging.Error("link of %s not found, type=%s", linkID, msg.GetXType().String()) + logging.Error("link of %s on connection %d not found, type=%s", + linkID, conn.Idx, msg.GetXType().String()) continue } } diff --git a/code/client/tunnel/link.go b/code/client/tunnel/link.go index 5d9d062..8775832 100644 --- a/code/client/tunnel/link.go +++ b/code/client/tunnel/link.go @@ -23,7 +23,8 @@ type Link struct { func NewLink(parent *Tunnel, id, target string, local net.Conn, remote *pool.Conn) *Link { remote.AddLink(id) - logging.Info("create link %s for tunnel %s", id, parent.Name) + logging.Info("create link %s for tunnel %s on connection %d", + id, parent.Name, remote.Idx) return &Link{ parent: parent, id: id, From 2c7f67dce245f59c9e90a683a4b6186b6a979b26 Mon Sep 17 00:00:00 2001 From: lwch Date: Wed, 1 Sep 2021 11:27:35 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/pool/conn.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/client/pool/conn.go b/code/client/pool/conn.go index cf1c82d..269da6f 100644 --- a/code/client/pool/conn.go +++ b/code/client/pool/conn.go @@ -128,6 +128,10 @@ func (conn *Conn) loopWrite(cancel context.CancelFunc) { } msg.From = conn.parent.cfg.ID msg.FromIdx = conn.Idx + if msg.XType == network.Msg_forward { + logging.Info("forward %s from %s-%d to %s-%d", msg.GetXData().Lid, + msg.From, msg.FromIdx, msg.To, msg.ToIdx) + } err := conn.conn.WriteMessage(msg, conn.parent.cfg.WriteTimeout) if err != nil { logging.Error("write message error on %s-%d: %v", From 8baae350141b8eeb528154c579f1b9df228e49bc Mon Sep 17 00:00:00 2001 From: lwch Date: Wed, 1 Sep 2021 11:33:31 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E4=BF=AE=E6=AD=A3server=E7=AB=AF?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=93=BE=E6=8E=A5=E6=97=B6=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/server/handler/client.go | 4 ++++ code/server/handler/handler.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/code/server/handler/client.go b/code/server/handler/client.go index e131d71..5c58c95 100644 --- a/code/server/handler/client.go +++ b/code/server/handler/client.go @@ -86,3 +86,7 @@ func (c *client) closeLink(id string) { delete(c.links, id) c.Unlock() } + +func (c *client) is(id string, idx uint32) bool { + return c.parent.id == id && c.idx == idx +} diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index 9c25307..b24e709 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -99,10 +99,10 @@ func (h *Handler) getClient(linkID, to string, toIdx uint32) *client { pair := h.links[linkID] h.lockLinks.RUnlock() - if pair[0] != nil && pair[0].idx == toIdx { + if pair[0] != nil && pair[0].is(to, toIdx) { return pair[0] } - if pair[1] != nil && pair[1].idx == toIdx { + if pair[1] != nil && pair[1].is(to, toIdx) { return pair[1] } From 7d26ada8c0610650a02e72150b6a1785d266a134 Mon Sep 17 00:00:00 2001 From: lwch Date: Wed, 1 Sep 2021 11:46:53 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BF=AE=E6=AD=A3connect=E5=90=8E?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=E6=8D=AEtoidx=E5=AD=97=E6=AE=B5=E4=B8=BA0=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/main.go | 1 + code/client/tunnel/link.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/code/client/main.go b/code/client/main.go index 4892f4c..0bfd67b 100644 --- a/code/client/main.go +++ b/code/client/main.go @@ -135,6 +135,7 @@ func connect(pool *pool.Pool, conn *pool.Conn, from, to string, fromIdx, toIdx u RemotePort: uint16(req.GetPort()), }) lk := tunnel.NewLink(tn, req.GetId(), from, link, conn) + lk.SetTargetIdx(fromIdx) conn.SendConnectOK(from, fromIdx, req.GetId()) lk.Forward() lk.OnWork <- struct{}{} diff --git a/code/client/tunnel/link.go b/code/client/tunnel/link.go index 8775832..b1467e6 100644 --- a/code/client/tunnel/link.go +++ b/code/client/tunnel/link.go @@ -100,3 +100,7 @@ func (link *Link) localRead() { link.remote.SendData(link.target, link.targetIdx, link.id, buf[:n]) } } + +func (link *Link) SetTargetIdx(idx uint32) { + link.targetIdx = idx +} From 00d9f68275ce3dc7beac89ae18fc583f8d6ec29a Mon Sep 17 00:00:00 2001 From: lwch Date: Wed, 1 Sep 2021 11:48:59 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/pool/conn.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/code/client/pool/conn.go b/code/client/pool/conn.go index 269da6f..cf1c82d 100644 --- a/code/client/pool/conn.go +++ b/code/client/pool/conn.go @@ -128,10 +128,6 @@ func (conn *Conn) loopWrite(cancel context.CancelFunc) { } msg.From = conn.parent.cfg.ID msg.FromIdx = conn.Idx - if msg.XType == network.Msg_forward { - logging.Info("forward %s from %s-%d to %s-%d", msg.GetXData().Lid, - msg.From, msg.FromIdx, msg.To, msg.ToIdx) - } err := conn.conn.WriteMessage(msg, conn.parent.cfg.WriteTimeout) if err != nil { logging.Error("write message error on %s-%d: %v", From 3df70780a172e2561b4b34b57ccaf19165398710 Mon Sep 17 00:00:00 2001 From: lwch Date: Thu, 2 Sep 2021 18:26:42 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d0cf87e..3c54b0c 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,10 @@ server端配置(10.0.1.1): # natpass10路复用,读写均为1s超时 [ ID] Interval Transfer Bitrate Retr - [ 5] 0.00-60.00 sec 70.0 MBytes 9.79 Mbits/sec 28 sender - [ 5] 0.00-60.01 sec 58.4 MBytes 8.16 Mbits/sec receiver + [ 5] 0.00-60.00 sec 70.0 MBytes 9.79 Mbits/sec 18 sender + [ 5] 0.00-60.30 sec 58.2 MBytes 8.10 Mbits/sec receiver # frp10路复用stcp,tls [ ID] Interval Transfer Bitrate Retr - [ 5] 0.00-60.00 sec 67.4 MBytes 9.43 Mbits/sec 21 sender - [ 5] 0.00-60.29 sec 58.6 MBytes 8.15 Mbits/sec receiver \ No newline at end of file + [ 5] 0.00-60.00 sec 66.2 MBytes 9.26 Mbits/sec 31 sender + [ 5] 0.00-60.10 sec 57.7 MBytes 8.05 Mbits/sec receiver \ No newline at end of file From 4df4db7186393a35fd55371519a4787a9ab69934 Mon Sep 17 00:00:00 2001 From: lwch Date: Thu, 2 Sep 2021 18:33:33 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3c54b0c..87fcf95 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,8 @@ server端配置(10.0.1.1): # natpass10路复用,读写均为1s超时 [ ID] Interval Transfer Bitrate Retr - [ 5] 0.00-60.00 sec 70.0 MBytes 9.79 Mbits/sec 18 sender - [ 5] 0.00-60.30 sec 58.2 MBytes 8.10 Mbits/sec receiver + [ 5] 0.00-60.00 sec 70.0 MBytes 9.79 Mbits/sec 22 sender + [ 5] 0.00-60.02 sec 57.9 MBytes 8.10 Mbits/sec receiver # frp10路复用stcp,tls [ ID] Interval Transfer Bitrate Retr