nps/client/client.go

191 lines
4.0 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/pool"
2019-02-23 07:29:48 -08:00
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
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
"sync"
"time"
)
type TRPClient struct {
2019-02-09 01:07:47 -08:00
svrAddr string
linkMap map[int]*conn.Link
tunnel *conn.Conn
2019-02-17 03:36:48 -08:00
msgTunnel *conn.Conn
2019-02-09 01:07:47 -08:00
bridgeConnType string
2019-02-12 11:54:00 -08:00
stop chan bool
2019-02-16 04:43:26 -08:00
proxyUrl 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-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,
linkMap: make(map[int]*conn.Link),
Mutex: sync.Mutex{},
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-02-23 07:29:48 -08:00
logs.Info("Successful connection with server %s", s.svrAddr)
2019-02-12 11:54:00 -08:00
s.processor(c)
}
func (s *TRPClient) Close() {
s.tunnel.Close()
s.stop <- true
for _, v := range s.linkMap {
2019-02-15 06:59:28 -08:00
if v.Conn != nil {
v.Conn.Close()
}
2019-02-12 11:54:00 -08:00
}
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) {
go s.dealChan()
2019-02-17 03:36:48 -08:00
go s.getMsgStatus()
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.NEW_CONN:
if link, err := c.GetLinkInfo(); err != nil {
break
} else {
s.Lock()
s.linkMap[link.Id] = link
s.Unlock()
2019-02-17 03:36:48 -08:00
link.MsgConn = s.msgTunnel
go s.linkProcess(link, c)
2019-02-17 03:36:48 -08:00
link.Run(false)
}
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
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-09 01:07:47 -08:00
func (s *TRPClient) linkProcess(link *conn.Link, c *conn.Conn) {
2019-02-15 06:59:28 -08:00
link.Host = common.FormatAddress(link.Host)
//与目标建立连接
server, err := net.DialTimeout(link.ConnType, link.Host, time.Second*3)
if err != nil {
c.WriteFail(link.Id)
2019-02-23 07:29:48 -08:00
logs.Warn("connect to ", link.Host, "error:", err)
return
}
c.WriteSuccess(link.Id)
2019-02-15 06:59:28 -08:00
link.Conn = conn.NewConn(server)
buf := pool.BufPoolCopy.Get().([]byte)
for {
if n, err := server.Read(buf); err != nil {
s.tunnel.SendMsg([]byte(common.IO_EOF), link)
break
} else {
if _, err := s.tunnel.SendMsg(buf[:n], link); err != nil {
c.Close()
break
}
2019-02-17 03:36:48 -08:00
if link.ConnType == common.CONN_UDP {
break
}
}
2019-02-17 03:36:48 -08:00
<-link.StatusCh
2019-02-15 06:59:28 -08:00
}
pool.PutBufPoolCopy(buf)
s.Lock()
2019-02-17 09:05:05 -08:00
//TODO 删除map
2019-02-15 06:59:28 -08:00
s.Unlock()
2018-11-04 07:19:22 -08:00
}
2018-11-29 03:55:24 -08:00
2019-02-17 03:36:48 -08:00
func (s *TRPClient) getMsgStatus() {
var err error
s.msgTunnel, err = NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_SEND_STATUS, s.proxyUrl)
if err != nil {
2019-02-23 07:29:48 -08:00
logs.Error("connect to ", s.svrAddr, "error:", err)
2019-02-17 03:36:48 -08:00
return
}
go func() {
for {
if id, err := s.msgTunnel.GetLen(); err != nil {
break
} else {
s.Lock()
if v, ok := s.linkMap[id]; ok {
s.Unlock()
v.StatusCh <- true
} else {
s.Unlock()
}
}
}
}()
<-s.stop
}
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-16 04:43:26 -08:00
s.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() {
for {
if id, err := s.tunnel.GetLen(); err != nil {
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
pool.PutBufPoolCopy(content)
break
} else {
2019-02-17 03:36:48 -08:00
v.MsgCh <- content
}
} else {
s.Unlock()
}
}
}
}()
2019-02-12 11:54:00 -08:00
<-s.stop
}