nps/client/client.go

230 lines
5.3 KiB
Go
Raw Normal View History

2019-01-09 04:33:00 -08:00
package client
2018-11-04 07:19:22 -08:00
import (
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/mux"
2019-02-23 07:29:48 -08:00
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
2019-02-26 06:40:28 -08:00
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
2018-11-04 07:19:22 -08:00
"net"
2019-02-23 07:29:48 -08:00
"os"
2018-11-04 07:19:22 -08:00
"time"
)
type TRPClient struct {
2019-02-09 01:07:47 -08:00
svrAddr string
bridgeConnType string
2019-02-12 11:54:00 -08:00
stop chan bool
2019-02-16 04:43:26 -08:00
proxyUrl string
vKey string
2019-03-02 01:43:21 -08:00
tunnel *mux.Mux
signal *conn.Conn
2018-11-04 07:19:22 -08:00
}
2019-01-03 08:21:23 -08:00
//new client
2019-02-16 04:43:26 -08:00
func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl string) *TRPClient {
return &TRPClient{
2019-02-09 01:07:47 -08:00
svrAddr: svraddr,
vKey: vKey,
bridgeConnType: bridgeConnType,
2019-02-12 11:54:00 -08:00
stop: make(chan bool),
2019-02-16 04:43:26 -08:00
proxyUrl: proxyUrl,
}
2018-11-04 07:19:22 -08:00
}
2019-01-03 08:21:23 -08:00
//start
2019-02-12 11:54:00 -08:00
func (s *TRPClient) Start() {
retry:
2019-02-16 04:43:26 -08:00
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN, s.proxyUrl)
2018-11-04 07:19:22 -08:00
if err != nil {
2019-02-23 07:29:48 -08:00
logs.Error("The connection server failed and will be reconnected in five seconds")
2018-11-04 07:19:22 -08:00
time.Sleep(time.Second * 5)
goto retry
2018-11-04 07:19:22 -08:00
}
2019-03-02 01:43:21 -08:00
2019-02-23 07:29:48 -08:00
logs.Info("Successful connection with server %s", s.svrAddr)
2019-03-02 01:43:21 -08:00
go s.ping()
2019-02-12 11:54:00 -08:00
s.processor(c)
}
func (s *TRPClient) Close() {
2019-03-02 01:43:21 -08:00
s.signal.Close()
2018-11-04 07:19:22 -08:00
}
2019-01-03 08:21:23 -08:00
//处理
2019-02-09 01:07:47 -08:00
func (s *TRPClient) processor(c *conn.Conn) {
2019-03-02 01:43:21 -08:00
s.signal = c
go s.dealChan()
2018-11-04 07:19:22 -08:00
for {
2018-11-29 03:55:24 -08:00
flags, err := c.ReadFlag()
2018-11-04 07:19:22 -08:00
if err != nil {
2019-02-23 07:29:48 -08:00
logs.Error("Accept server data error %s, end this service", err.Error())
2018-11-29 03:55:24 -08:00
break
2018-11-04 07:19:22 -08:00
}
switch flags {
2019-02-09 01:07:47 -08:00
case common.VERIFY_EER:
2019-02-23 07:29:48 -08:00
logs.Error("VKey:%s is incorrect, the server refuses to connect, please check", s.vKey)
os.Exit(0)
2019-02-09 01:07:47 -08:00
case common.RES_CLOSE:
2019-02-23 07:29:48 -08:00
logs.Error("The authentication key is connected by another client or the server closes the client.")
os.Exit(0)
2019-02-09 01:07:47 -08:00
case common.RES_MSG:
2019-02-23 07:29:48 -08:00
logs.Error("Server-side return error")
break
2019-02-26 06:40:28 -08:00
case common.NEW_UDP_CONN:
//读取服务端地址、密钥 继续做处理
if lAddr, err := c.GetShortLenContent(); err != nil {
logs.Warn(err)
2019-02-26 06:40:28 -08:00
return
} else if pwd, err := c.GetShortLenContent(); err == nil {
2019-02-26 06:40:28 -08:00
go s.newUdpConn(string(lAddr), string(pwd))
}
2018-11-04 07:19:22 -08:00
default:
2019-02-23 07:29:48 -08:00
logs.Warn("The error could not be resolved")
break
2018-11-04 07:19:22 -08:00
}
}
2019-02-12 11:54:00 -08:00
c.Close()
s.Close()
}
2019-02-12 11:54:00 -08:00
2019-02-26 06:40:28 -08:00
func (s *TRPClient) newUdpConn(rAddr string, md5Password string) {
tmpConn, err := common.GetLocalUdpAddr()
if err != nil {
logs.Error(err)
return
}
2019-02-26 06:40:28 -08:00
localAddr, _ := net.ResolveUDPAddr("udp", tmpConn.LocalAddr().String())
localConn, err := net.ListenUDP("udp", localAddr)
if err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
}
localKcpConn, err := kcp.NewConn(rAddr, nil, 150, 3, localConn)
if err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
}
conn.SetUdpSession(localKcpConn)
2019-02-26 06:40:28 -08:00
localToolConn := conn.NewConn(localKcpConn)
//写入密钥、provider身份
if _, err := localToolConn.Write([]byte(md5Password)); err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
}
if _, err := localToolConn.Write([]byte(common.WORK_P2P_PROVIDER)); err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
}
//接收服务端传的visitor地址
var b []byte
if b, err = localToolConn.GetShortLenContent(); err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
2019-02-15 06:59:28 -08:00
}
//向visitor地址发送测试消息
visitorAddr, err := net.ResolveUDPAddr("udp", string(b))
2019-02-26 06:40:28 -08:00
if err != nil {
logs.Error(err)
2019-02-26 06:40:28 -08:00
return
}
//向目标IP发送探测包
if _, err := localConn.WriteTo([]byte("test"), visitorAddr); err != nil {
logs.Error(err)
return
}
//给服务端发反馈
if _, err := localToolConn.Write([]byte(common.VERIFY_SUCCESS)); err != nil {
logs.Error(err)
return
}
//关闭与服务端的连接
localConn.Close()
//关闭与服务端udp conn建立新的监听
if localConn, err = net.ListenUDP("udp", localAddr); err != nil {
logs.Error(err)
return
}
l, err := kcp.ServeConn(nil, 150, 3, localConn)
2019-02-17 03:36:48 -08:00
if err != nil {
logs.Error(err)
2019-02-17 03:36:48 -08:00
return
}
//接收新的监听得到conn
for {
udpTunnel, err := l.AcceptKCP()
if err != nil {
logs.Error(err)
l.Close()
return
}
if udpTunnel.RemoteAddr().String() == string(b) {
conn.SetUdpSession(udpTunnel)
//读取link,设置msgCh 设置msgConn消息回传响应机制
l := mux.NewMux(udpTunnel)
for {
connMux, err := l.Accept()
if err != nil {
continue
2019-02-17 03:36:48 -08:00
}
go s.srcProcess(connMux)
2019-02-17 03:36:48 -08:00
}
}
}
2019-02-17 03:36:48 -08:00
}
//mux tunnel
2019-01-06 09:52:54 -08:00
func (s *TRPClient) dealChan() {
tunnel, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN, s.proxyUrl)
if err != nil {
2019-02-23 07:29:48 -08:00
logs.Error("connect to ", s.svrAddr, "error:", err)
2019-01-06 09:52:54 -08:00
return
}
go func() {
2019-03-02 01:43:21 -08:00
s.tunnel = mux.NewMux(tunnel.Conn)
for {
2019-03-02 01:43:21 -08:00
src, err := s.tunnel.Accept()
if err != nil {
logs.Warn(err)
break
}
go s.srcProcess(src)
}
}()
2019-02-12 11:54:00 -08:00
<-s.stop
}
2019-02-23 21:17:43 -08:00
func (s *TRPClient) srcProcess(src net.Conn) {
lk, err := conn.NewConn(src).GetLinkInfo()
if err != nil {
src.Close()
logs.Error("get connection info from server error ", err)
return
}
2019-03-02 01:43:21 -08:00
//host for target processing
lk.Host = common.FormatAddress(lk.Host)
//connect to target
if targetConn, err := net.Dial(lk.ConnType, lk.Host); err != nil {
logs.Warn("connect to %s error %s", lk.Host, err.Error())
src.Close()
} else {
logs.Trace("new %s connection with the goal of %s, remote address:%s", lk.ConnType, lk.Host, lk.RemoteAddr)
2019-03-04 17:23:18 -08:00
conn.CopyWaitGroup(src, targetConn, lk.Crypt, lk.Compress, nil, nil, false)
2019-02-23 21:17:43 -08:00
}
}
2019-03-02 01:43:21 -08:00
func (s *TRPClient) ping() {
ticker := time.NewTicker(time.Second * 5)
loop:
for {
select {
case <-ticker.C:
if s.tunnel.IsClose {
s.Close()
ticker.Stop()
break loop
}
}
}
}