mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 11:56:53 +00:00
P2p、install、log package
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -16,6 +20,7 @@ type TRPClient struct {
|
||||
bridgeConnType string
|
||||
proxyUrl string
|
||||
vKey string
|
||||
p2pAddr map[string]string
|
||||
tunnel *mux.Mux
|
||||
signal *conn.Conn
|
||||
ticker *time.Ticker
|
||||
@@ -26,6 +31,7 @@ type TRPClient struct {
|
||||
func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl string, cnf *config.Config) *TRPClient {
|
||||
return &TRPClient{
|
||||
svrAddr: svraddr,
|
||||
p2pAddr: make(map[string]string, 0),
|
||||
vKey: vKey,
|
||||
bridgeConnType: bridgeConnType,
|
||||
proxyUrl: proxyUrl,
|
||||
@@ -71,18 +77,30 @@ func (s *TRPClient) handleMain() {
|
||||
logs.Warn(err)
|
||||
return
|
||||
} else if pwd, err := s.signal.GetShortLenContent(); err == nil {
|
||||
go s.newUdpConn(string(lAddr), string(pwd))
|
||||
var localAddr string
|
||||
//The local port remains unchanged for a certain period of time
|
||||
if v, ok := s.p2pAddr[crypt.Md5(string(pwd)+strconv.Itoa(int(time.Now().Unix()/100)))]; !ok {
|
||||
tmpConn, err := common.GetLocalUdpAddr()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
localAddr = tmpConn.LocalAddr().String()
|
||||
} else {
|
||||
localAddr = v
|
||||
}
|
||||
go s.newUdpConn(localAddr, string(lAddr), string(pwd))
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Close()
|
||||
}
|
||||
|
||||
func (s *TRPClient) newUdpConn(rAddr string, md5Password string) {
|
||||
func (s *TRPClient) newUdpConn(localAddr, rAddr string, md5Password string) {
|
||||
var localConn net.PacketConn
|
||||
var err error
|
||||
var remoteAddress string
|
||||
if remoteAddress, localConn, err = handleP2PUdp(rAddr, md5Password, common.WORK_P2P_PROVIDER); err != nil {
|
||||
if remoteAddress, localConn, err = handleP2PUdp(localAddr, rAddr, md5Password, common.WORK_P2P_PROVIDER); err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
@@ -92,7 +110,6 @@ func (s *TRPClient) newUdpConn(rAddr string, md5Password string) {
|
||||
return
|
||||
}
|
||||
logs.Trace("start local p2p udp listen, local address", localConn.LocalAddr().String())
|
||||
//接收新的监听,得到conn,
|
||||
for {
|
||||
udpTunnel, err := l.AcceptKCP()
|
||||
if err != nil {
|
||||
@@ -104,14 +121,10 @@ func (s *TRPClient) newUdpConn(rAddr string, md5Password string) {
|
||||
conn.SetUdpSession(udpTunnel)
|
||||
logs.Trace("successful connection with client ,address %s", udpTunnel.RemoteAddr().String())
|
||||
//read link info from remote
|
||||
l := mux.NewMux(udpTunnel, s.bridgeConnType)
|
||||
for {
|
||||
connMux, err := l.Accept()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
go s.handleChan(connMux)
|
||||
}
|
||||
conn.Accept(mux.NewMux(udpTunnel, s.bridgeConnType), func(c net.Conn) {
|
||||
go s.handleChan(c)
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,7 +157,31 @@ func (s *TRPClient) handleChan(src net.Conn) {
|
||||
}
|
||||
//host for target processing
|
||||
lk.Host = common.FormatAddress(lk.Host)
|
||||
//connect to target
|
||||
//if Conn type is http, read the request and log
|
||||
if lk.ConnType == "http" {
|
||||
if targetConn, err := net.Dial(common.CONN_TCP, lk.Host); err != nil {
|
||||
logs.Warn("connect to %s error %s", lk.Host, err.Error())
|
||||
src.Close()
|
||||
} else {
|
||||
go func() {
|
||||
common.CopyBuffer(src, targetConn)
|
||||
src.Close()
|
||||
targetConn.Close()
|
||||
}()
|
||||
for {
|
||||
if r, err := http.ReadRequest(bufio.NewReader(src)); err != nil {
|
||||
src.Close()
|
||||
targetConn.Close()
|
||||
break
|
||||
} else {
|
||||
logs.Trace("http request, method %s, host %s, url %s, remote address %s", r.Method, r.Host, r.URL.Path, r.RemoteAddr)
|
||||
r.Write(targetConn)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
//connect to target if conn type is tcp or udp
|
||||
if targetConn, err := net.Dial(lk.ConnType, lk.Host); err != nil {
|
||||
logs.Warn("connect to %s error %s", lk.Host, err.Error())
|
||||
src.Close()
|
||||
@@ -154,6 +191,7 @@ func (s *TRPClient) handleChan(src net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
// Whether the monitor channel is closed
|
||||
func (s *TRPClient) ping() {
|
||||
s.ticker = time.NewTicker(time.Second * 5)
|
||||
loop:
|
||||
|
Reference in New Issue
Block a user