mirror of https://github.com/qwqdanchun/nps.git
p2p secret
This commit is contained in:
parent
0cc0e82c5d
commit
6a978515ca
|
@ -248,6 +248,8 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) {
|
||||||
case common.WORK_SECRET:
|
case common.WORK_SECRET:
|
||||||
if b, err := c.GetShortContent(32); err == nil {
|
if b, err := c.GetShortContent(32); err == nil {
|
||||||
s.SecretChan <- conn.NewSecret(string(b), c)
|
s.SecretChan <- conn.NewSecret(string(b), c)
|
||||||
|
} else {
|
||||||
|
logs.Error("secret error, failed to match the key successfully")
|
||||||
}
|
}
|
||||||
case common.WORK_FILE:
|
case common.WORK_FILE:
|
||||||
muxConn := mux.NewMux(c.Conn, s.tunnelType)
|
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:
|
case common.WORK_P2P:
|
||||||
//read md5 secret
|
//read md5 secret
|
||||||
if b, err := c.GetShortContent(32); err != nil {
|
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 {
|
} else if t := file.GetDb().GetTaskByMd5Password(string(b)); t == nil {
|
||||||
return
|
logs.Error("p2p error, failed to match the key successfully")
|
||||||
} else {
|
} else {
|
||||||
if v, ok := s.Client.Load(t.Client.Id); !ok {
|
if v, ok := s.Client.Load(t.Client.Id); !ok {
|
||||||
return
|
return
|
||||||
|
|
|
@ -357,19 +357,30 @@ func sendP2PTestMsg(remoteAddr string, localAddr string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
localConn, err := newUdpConnByAddr(localAddr)
|
localConn, err := newUdpConnByAddr(localAddr)
|
||||||
defer localConn.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
buf := make([]byte, 10)
|
defer localConn.Close()
|
||||||
for i := 20; i > 0; i-- {
|
ticker := time.NewTicker(time.Millisecond * 500)
|
||||||
logs.Trace("try send test packet to target %s", remoteAddr)
|
go func(ticker *time.Ticker) {
|
||||||
if _, err := localConn.WriteTo([]byte(common.WORK_P2P_CONNECT), remoteUdpAddr); err != nil {
|
for {
|
||||||
return "", err
|
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)
|
n, addr, err := localConn.ReadFromUDP(buf)
|
||||||
localConn.SetReadDeadline(time.Time{})
|
localConn.SetReadDeadline(time.Time{})
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
switch string(buf[:n]) {
|
switch string(buf[:n]) {
|
||||||
case common.WORK_P2P_SUCCESS:
|
case common.WORK_P2P_SUCCESS:
|
||||||
for i := 20; i > 0; i-- {
|
for i := 20; i > 0; i-- {
|
||||||
|
@ -391,9 +402,12 @@ func sendP2PTestMsg(remoteAddr string, localAddr string) (string, error) {
|
||||||
time.Sleep(time.Second)
|
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")
|
return "", errors.New("connect to the target failed, maybe the nat type is not support p2p")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ var (
|
||||||
muxSession *mux.Mux
|
muxSession *mux.Mux
|
||||||
fileServer []*http.Server
|
fileServer []*http.Server
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
hasP2PTry bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func CloseLocalServer() {
|
func CloseLocalServer() {
|
||||||
|
@ -81,12 +82,18 @@ func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *con
|
||||||
restart:
|
restart:
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
if udpConn == nil {
|
if udpConn == nil {
|
||||||
newUdpConn(config, l)
|
if !hasP2PTry {
|
||||||
|
hasP2PTry = true
|
||||||
|
newUdpConn(config, l)
|
||||||
|
}
|
||||||
if udpConn == nil {
|
if udpConn == nil {
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
logs.Notice("new conn, P2P can not penetrate successfully, traffic will be transferred through the server")
|
||||||
|
handleSecret(localTcpConn, config, l)
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
muxSession = mux.NewMux(udpConn, "kcp")
|
||||||
}
|
}
|
||||||
muxSession = mux.NewMux(udpConn, "kcp")
|
|
||||||
}
|
}
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
logs.Trace("start trying to connect with the server")
|
logs.Trace("start trying to connect with the server")
|
||||||
|
|
|
@ -66,10 +66,7 @@ func DealBridgeTask() {
|
||||||
case s := <-Bridge.SecretChan:
|
case s := <-Bridge.SecretChan:
|
||||||
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
|
logs.Trace("New secret connection, addr", s.Conn.Conn.RemoteAddr())
|
||||||
if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil {
|
if t := file.GetDb().GetTaskByMd5Password(s.Password); t != nil {
|
||||||
if !t.Client.GetConn() {
|
if t.Status {
|
||||||
logs.Info("Connections exceed the current client %d limit", t.Client.Id)
|
|
||||||
s.Conn.Close()
|
|
||||||
} else 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)
|
go proxy.NewBaseServer(Bridge, t).DealClient(s.Conn, t.Client, t.Target.TargetStr, nil, common.CONN_TCP, nil, t.Flow, t.Target.LocalProxy)
|
||||||
} else {
|
} else {
|
||||||
s.Conn.Close()
|
s.Conn.Close()
|
||||||
|
|
|
@ -121,7 +121,7 @@
|
||||||
arr["socks5"] = ["type", "port", "compress", "u", "p", "socks5代理模式,内网socks5代理,配合proxifer,可如同使用vpn一样访问内网设备或资源,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置socks5代理,即访问内网设备或者资源 "]
|
arr["socks5"] = ["type", "port", "compress", "u", "p", "socks5代理模式,内网socks5代理,配合proxifer,可如同使用vpn一样访问内网设备或资源,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置socks5代理,即访问内网设备或者资源 "]
|
||||||
arr["httpProxy"] = ["type", "port", "compress", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
arr["httpProxy"] = ["type", "port", "compress", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
||||||
arr["secret"] = ["type", "target", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
arr["secret"] = ["type", "target", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
||||||
arr["p2p"] = ["type", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
arr["p2p"] = ["type", "target", "compress", "password", "u", "p", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
||||||
arr["file"] = ["type", "strip_pre", "local_path", "port", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
arr["file"] = ["type", "strip_pre", "local_path", "port", " http代理模式,内网http代理,可访问内网网站,添加后会自动生成一个客户端验证key<br>在内网机器执行<span style='color: red'>./easyProxy -vkey=生成的key -server=公网服务器ip:下面设定的端口</span><br>建立成功后,在外网环境下本机配置http代理,即访问内网站点"]
|
||||||
arrClientHide = ["compress", "u", "p", "crypt", "mux"]
|
arrClientHide = ["compress", "u", "p", "crypt", "mux"]
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,7 @@
|
||||||
arr["socks5"] = ["type", "port", "compress", "u", "p"]
|
arr["socks5"] = ["type", "port", "compress", "u", "p"]
|
||||||
arr["httpProxy"] = ["type", "port", "compress", "u", "p"]
|
arr["httpProxy"] = ["type", "port", "compress", "u", "p"]
|
||||||
arr["secret"] = ["type", "target", "compress", "u", "p", "password"]
|
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"]
|
arr["file"] = ["type", "port", "local_path", "strip_pre"]
|
||||||
arrClientHide = ["compress", "u", "p", "crypt", "mux"]
|
arrClientHide = ["compress", "u", "p", "crypt", "mux"]
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@
|
||||||
+ '<b langtag="info-web-auth-username">basic权限认证用户名</b>:' + row.Client.Cnf.U + `       `
|
+ '<b langtag="info-web-auth-username">basic权限认证用户名</b>:' + row.Client.Cnf.U + `       `
|
||||||
+ '<b langtag="info-web-auth-password">basic权限认证密码</b>:' + row.Client.Cnf.P + `       `
|
+ '<b langtag="info-web-auth-password">basic权限认证密码</b>:' + row.Client.Cnf.P + `       `
|
||||||
if (row.Mode == "p2p") {
|
if (row.Mode == "p2p") {
|
||||||
return tmp + "<br/><br>" + '<b langtag="info-command">访问端命令</b>:' + "<code>./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -target=your target" + "</code>"
|
return tmp + "<br/><br>" + '<b langtag="info-command">访问端命令</b>:' + "<code>./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -target=" + row.Target.TargetStr + "</code>"
|
||||||
}
|
}
|
||||||
if (row.Mode = "secret") {
|
if (row.Mode = "secret") {
|
||||||
return tmp + "<br/><br>" + '<b langtag="info-command">访问端命令</b>:' + "<code>./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -local_type=secret" + "</code>"
|
return tmp + "<br/><br>" + '<b langtag="info-command">访问端命令</b>:' + "<code>./npc{{.win}} -server={{.ip}}:{{.p}} -vkey=" + row.Client.VerifyKey + " -type=" +{{.bridgeType}} +" -password=" + row.Password + " -local_type=secret" + "</code>"
|
||||||
|
|
Loading…
Reference in New Issue