From 6a978515caab304cfbffc81a05b641e6a2b4aa17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Sat, 13 Apr 2019 19:48:34 +0800 Subject: [PATCH] p2p secret --- bridge/bridge.go | 6 ++++-- client/control.go | 30 ++++++++++++++++++++++-------- client/local.go | 11 +++++++++-- server/server.go | 5 +---- web/views/index/add.html | 2 +- web/views/index/edit.html | 2 +- web/views/index/list.html | 2 +- 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/bridge/bridge.go b/bridge/bridge.go index d0d86e9..e50e2ea 100755 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -248,6 +248,8 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) { case common.WORK_SECRET: if b, err := c.GetShortContent(32); err == nil { s.SecretChan <- conn.NewSecret(string(b), c) + } else { + logs.Error("secret error, failed to match the key successfully") } case common.WORK_FILE: muxConn := mux.NewMux(c.Conn, s.tunnelType) @@ -257,9 +259,9 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) { case common.WORK_P2P: //read md5 secret if b, err := c.GetShortContent(32); err != nil { - return + logs.Error("p2p error,", err.Error()) } else if t := file.GetDb().GetTaskByMd5Password(string(b)); t == nil { - return + logs.Error("p2p error, failed to match the key successfully") } else { if v, ok := s.Client.Load(t.Client.Id); !ok { return diff --git a/client/control.go b/client/control.go index b28339e..b7d9abc 100644 --- a/client/control.go +++ b/client/control.go @@ -357,19 +357,30 @@ func sendP2PTestMsg(remoteAddr string, localAddr string) (string, error) { return "", err } localConn, err := newUdpConnByAddr(localAddr) - defer localConn.Close() if err != nil { return "", err } - buf := make([]byte, 10) - for i := 20; i > 0; i-- { - logs.Trace("try send test packet to target %s", remoteAddr) - if _, err := localConn.WriteTo([]byte(common.WORK_P2P_CONNECT), remoteUdpAddr); err != nil { - return "", err + defer localConn.Close() + ticker := time.NewTicker(time.Millisecond * 500) + go func(ticker *time.Ticker) { + for { + select { + case <-ticker.C: + logs.Trace("try send test packet to target %s", remoteAddr) + if _, err := localConn.WriteTo([]byte(common.WORK_P2P_CONNECT), remoteUdpAddr); err != nil { + return + } + } } - localConn.SetReadDeadline(time.Now().Add(time.Millisecond * 500)) + }(ticker) + buf := make([]byte, 10) + for { + localConn.SetReadDeadline(time.Now().Add(time.Second * 30)) n, addr, err := localConn.ReadFromUDP(buf) localConn.SetReadDeadline(time.Time{}) + if err != nil { + break + } switch string(buf[:n]) { case common.WORK_P2P_SUCCESS: for i := 20; i > 0; i-- { @@ -391,9 +402,12 @@ func sendP2PTestMsg(remoteAddr string, localAddr string) (string, error) { time.Sleep(time.Second) } }() + default: + continue } + ticker.Stop() } - localConn.Close() + ticker.Stop() return "", errors.New("connect to the target failed, maybe the nat type is not support p2p") } diff --git a/client/local.go b/client/local.go index 4e484ab..480e0a9 100644 --- a/client/local.go +++ b/client/local.go @@ -20,6 +20,7 @@ var ( muxSession *mux.Mux fileServer []*http.Server lock sync.Mutex + hasP2PTry bool ) func CloseLocalServer() { @@ -81,12 +82,18 @@ func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *con restart: lock.Lock() if udpConn == nil { - newUdpConn(config, l) + if !hasP2PTry { + hasP2PTry = true + newUdpConn(config, l) + } if udpConn == nil { lock.Unlock() + logs.Notice("new conn, P2P can not penetrate successfully, traffic will be transferred through the server") + handleSecret(localTcpConn, config, l) return + } else { + muxSession = mux.NewMux(udpConn, "kcp") } - muxSession = mux.NewMux(udpConn, "kcp") } lock.Unlock() logs.Trace("start trying to connect with the server") diff --git a/server/server.go b/server/server.go index b382401..e5c7f9d 100644 --- a/server/server.go +++ b/server/server.go @@ -66,10 +66,7 @@ func DealBridgeTask() { case s := <-Bridge.SecretChan: logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr()) if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil { - if !t.Client.GetConn() { - logs.Info("Connections exceed the current client %d limit", t.Client.Id) - s.Conn.Close() - } else if t.Status { + if t.Status { go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow, t.Target.LocalProxy) } else { s.Conn.Close() diff --git a/web/views/index/add.html b/web/views/index/add.html index 7d049eb..c2208a7 100755 --- a/web/views/index/add.html +++ b/web/views/index/add.html @@ -121,7 +121,7 @@ arr["socks5"] = ["type", "port", "compress", "u", "p", "socks5代理模式,内网socks5代理,配合proxifer,可如同使用vpn一样访问内网设备或资源,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置socks5代理,即访问内网设备或者资源 "] arr["httpProxy"] = ["type", "port", "compress", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置http代理,即访问内网站点"] arr["secret"] = ["type", "target", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置http代理,即访问内网站点"] - arr["p2p"] = ["type", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置http代理,即访问内网站点"] + arr["p2p"] = ["type", "target", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置http代理,即访问内网站点"] arr["file"] = ["type", "strip_pre", "local_path", "port", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key
在内网机器执行./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口
建立成功后,在外网环境下本机配置http代理,即访问内网站点"] arrClientHide = ["compress", "u", "p", "crypt", "mux"] diff --git a/web/views/index/edit.html b/web/views/index/edit.html index dd5f72b..ef48ad0 100755 --- a/web/views/index/edit.html +++ b/web/views/index/edit.html @@ -120,7 +120,7 @@ arr["socks5"] = ["type", "port", "compress", "u", "p"] arr["httpProxy"] = ["type", "port", "compress", "u", "p"] arr["secret"] = ["type", "target", "compress", "u", "p", "password"] - arr["p2p"] = ["type", "password"] + arr["p2p"] = ["type", "target", "password"] arr["file"] = ["type", "port", "local_path", "strip_pre"] arrClientHide = ["compress", "u", "p", "crypt", "mux"] diff --git a/web/views/index/list.html b/web/views/index/list.html index ae00607..90d6742 100755 --- a/web/views/index/list.html +++ b/web/views/index/list.html @@ -70,7 +70,7 @@ + 'basic权限认证用户名:' + row.Client.Cnf.U + `       ` + 'basic权限认证密码:' + row.Client.Cnf.P + `       ` if (row.Mode == "p2p") { - return tmp + "

" + '访问端命令:' + "./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -target=your target" + "" + return tmp + "

" + '访问端命令:' + "./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -target=" + row.Target.TargetStr + "" } if (row.Mode = "secret") { return tmp + "

" + '访问端命令:' + "./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -local_type=secret" + ""