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/kcp"
|
|
|
|
"github.com/cnlh/nps/lib/lg"
|
|
|
|
"github.com/cnlh/nps/lib/pool"
|
2018-11-04 07:19:22 -08:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TRPClient struct {
|
2019-02-09 01:07:47 -08:00
|
|
|
svrAddr string
|
|
|
|
linkMap map[int]*conn.Link
|
|
|
|
stop chan bool
|
|
|
|
tunnel *conn.Conn
|
|
|
|
bridgeConnType string
|
2018-11-04 07:19:22 -08:00
|
|
|
sync.Mutex
|
2018-12-11 00:37:12 -08:00
|
|
|
vKey string
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
|
|
|
|
2019-01-03 08:21:23 -08:00
|
|
|
//new client
|
2019-02-09 01:07:47 -08:00
|
|
|
func NewRPClient(svraddr string, vKey string, bridgeConnType string) *TRPClient {
|
2019-01-31 10:06:30 -08:00
|
|
|
return &TRPClient{
|
2019-02-09 01:07:47 -08:00
|
|
|
svrAddr: svraddr,
|
|
|
|
linkMap: make(map[int]*conn.Link),
|
|
|
|
stop: make(chan bool),
|
|
|
|
Mutex: sync.Mutex{},
|
|
|
|
vKey: vKey,
|
|
|
|
bridgeConnType: bridgeConnType,
|
2019-01-31 10:06:30 -08:00
|
|
|
}
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
|
|
|
|
2019-01-03 08:21:23 -08:00
|
|
|
//start
|
2018-11-29 03:55:24 -08:00
|
|
|
func (s *TRPClient) Start() error {
|
2019-01-31 10:06:30 -08:00
|
|
|
s.NewConn()
|
2018-11-04 07:19:22 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-29 03:55:24 -08:00
|
|
|
//新建
|
2019-01-31 10:06:30 -08:00
|
|
|
func (s *TRPClient) NewConn() {
|
2019-02-09 01:07:47 -08:00
|
|
|
var err error
|
|
|
|
var c net.Conn
|
2019-01-31 10:06:30 -08:00
|
|
|
retry:
|
2019-02-09 01:07:47 -08:00
|
|
|
if s.bridgeConnType == "tcp" {
|
|
|
|
c, err = net.Dial("tcp", s.svrAddr)
|
|
|
|
} else {
|
|
|
|
var sess *kcp.UDPSession
|
|
|
|
sess, err = kcp.DialWithOptions(s.svrAddr, nil, 150, 3)
|
|
|
|
conn.SetUdpSession(sess)
|
|
|
|
c = sess
|
|
|
|
}
|
2018-11-04 07:19:22 -08:00
|
|
|
if err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("连接服务端失败,五秒后将重连")
|
2018-11-04 07:19:22 -08:00
|
|
|
time.Sleep(time.Second * 5)
|
2019-01-31 10:06:30 -08:00
|
|
|
goto retry
|
|
|
|
return
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
s.processor(conn.NewConn(c))
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
2019-01-31 10:06:30 -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) {
|
|
|
|
c.SetAlive(s.bridgeConnType)
|
|
|
|
if _, err := c.Write([]byte(common.Getverifyval(s.vKey))); err != nil {
|
2019-01-31 10:06:30 -08:00
|
|
|
return
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
2019-01-09 04:33:00 -08:00
|
|
|
c.WriteMain()
|
2019-01-31 10:06:30 -08:00
|
|
|
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-09 01:07:47 -08:00
|
|
|
lg.Println("服务端断开,正在重新连接")
|
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:
|
|
|
|
lg.Fatalf("vKey:%s不正确,服务端拒绝连接,请检查", s.vKey)
|
|
|
|
case common.NEW_CONN:
|
2019-01-31 10:06:30 -08:00
|
|
|
if link, err := c.GetLinkInfo(); err != nil {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
s.Lock()
|
|
|
|
s.linkMap[link.Id] = link
|
|
|
|
s.Unlock()
|
|
|
|
go s.linkProcess(link, c)
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
case common.RES_CLOSE:
|
|
|
|
lg.Fatalln("该vkey被另一客户连接")
|
|
|
|
case common.RES_MSG:
|
|
|
|
lg.Println("服务端返回错误,重新连接")
|
2019-01-31 10:06:30 -08:00
|
|
|
break
|
2018-11-04 07:19:22 -08:00
|
|
|
default:
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("无法解析该错误,重新连接")
|
2019-01-31 10:06:30 -08:00
|
|
|
break
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 10:06:30 -08:00
|
|
|
s.stop <- true
|
2019-02-09 01:07:47 -08:00
|
|
|
s.linkMap = make(map[int]*conn.Link)
|
2019-01-31 10:06:30 -08:00
|
|
|
go s.NewConn()
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
func (s *TRPClient) linkProcess(link *conn.Link, c *conn.Conn) {
|
2019-01-31 10:06:30 -08:00
|
|
|
//与目标建立连接
|
|
|
|
server, err := net.DialTimeout(link.ConnType, link.Host, time.Second*3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.WriteFail(link.Id)
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("connect to ", link.Host, "error:", err)
|
2019-01-31 10:06:30 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.WriteSuccess(link.Id)
|
|
|
|
|
2019-02-09 01:07:47 -08:00
|
|
|
link.Conn = conn.NewConn(server)
|
|
|
|
buf := pool.BufPoolCopy.Get().([]byte)
|
2019-01-31 10:06:30 -08:00
|
|
|
for {
|
|
|
|
if n, err := server.Read(buf); err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
s.tunnel.SendMsg([]byte(common.IO_EOF), link)
|
2019-01-31 10:06:30 -08:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if _, err := s.tunnel.SendMsg(buf[:n], link); err != nil {
|
|
|
|
c.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
pool.PutBufPoolCopy(buf)
|
2019-01-31 10:06:30 -08:00
|
|
|
s.Lock()
|
|
|
|
delete(s.linkMap, link.Id)
|
|
|
|
s.Unlock()
|
2018-11-04 07:19:22 -08:00
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
|
|
|
//隧道模式处理
|
2019-01-06 09:52:54 -08:00
|
|
|
func (s *TRPClient) dealChan() {
|
|
|
|
var err error
|
2019-02-09 01:07:47 -08:00
|
|
|
var c net.Conn
|
|
|
|
var sess *kcp.UDPSession
|
|
|
|
if s.bridgeConnType == "tcp" {
|
|
|
|
c, err = net.Dial("tcp", s.svrAddr)
|
|
|
|
} else {
|
|
|
|
sess, err = kcp.DialWithOptions(s.svrAddr, nil, 10, 3)
|
|
|
|
conn.SetUdpSession(sess)
|
|
|
|
c = sess
|
|
|
|
}
|
2018-12-03 07:03:25 -08:00
|
|
|
if err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("connect to ", s.svrAddr, "error:", err)
|
2019-01-06 09:52:54 -08:00
|
|
|
return
|
2018-12-03 07:03:25 -08:00
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
//验证
|
2019-02-09 01:07:47 -08:00
|
|
|
if _, err := c.Write([]byte(common.Getverifyval(s.vKey))); err != nil {
|
|
|
|
lg.Println("connect to ", s.svrAddr, "error:", err)
|
2019-01-06 09:52:54 -08:00
|
|
|
return
|
2018-11-05 04:39:31 -08:00
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
//默认长连接保持
|
2019-02-09 01:07:47 -08:00
|
|
|
s.tunnel = conn.NewConn(c)
|
|
|
|
s.tunnel.SetAlive(s.bridgeConnType)
|
2018-11-29 03:55:24 -08:00
|
|
|
//写标志
|
2019-01-31 10:06:30 -08:00
|
|
|
s.tunnel.WriteChan()
|
2018-11-29 03:55:24 -08:00
|
|
|
|
2019-01-31 10:06:30 -08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if id, err := s.tunnel.GetLen(); err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("get msg id error")
|
2019-01-31 10:06:30 -08:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
s.Lock()
|
|
|
|
if v, ok := s.linkMap[id]; ok {
|
|
|
|
s.Unlock()
|
|
|
|
if content, err := s.tunnel.GetMsgContent(v); err != nil {
|
2019-02-09 01:07:47 -08:00
|
|
|
lg.Println("get msg content error:", err, id)
|
|
|
|
pool.PutBufPoolCopy(content)
|
2019-01-31 10:06:30 -08:00
|
|
|
break
|
|
|
|
} else {
|
2019-02-09 01:07:47 -08:00
|
|
|
if len(content) == len(common.IO_EOF) && string(content) == common.IO_EOF {
|
2019-01-31 10:06:30 -08:00
|
|
|
v.Conn.Close()
|
|
|
|
} else if v.Conn != nil {
|
|
|
|
v.Conn.Write(content)
|
|
|
|
}
|
2019-02-09 01:07:47 -08:00
|
|
|
pool.PutBufPoolCopy(content)
|
2019-01-31 10:06:30 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
2019-01-31 10:06:30 -08:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
2019-02-09 01:07:47 -08:00
|
|
|
break
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
}
|