nps/client/client.go

282 lines
6.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-12 11:54:00 -08:00
"errors"
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/common"
2019-02-12 11:54:00 -08:00
"github.com/cnlh/nps/lib/config"
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/pool"
2019-02-16 04:43:26 -08:00
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"github.com/cnlh/nps/vender/golang.org/x/net/proxy"
2019-02-12 11:54:00 -08:00
"io/ioutil"
2018-11-04 07:19:22 -08:00
"net"
2019-02-16 04:43:26 -08:00
"net/url"
2019-02-12 11:54:00 -08:00
"path/filepath"
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
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-12 11:54:00 -08:00
lg.Println("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-12 11:54:00 -08:00
lg.Printf("Successful connection with server %s", s.svrAddr)
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()
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-12 11:54:00 -08:00
lg.Printf("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-12 11:54:00 -08:00
lg.Fatalf("VKey:%s is incorrect, the server refuses to connect, please check", s.vKey)
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()
go s.linkProcess(link, c)
}
2019-02-09 01:07:47 -08:00
case common.RES_CLOSE:
2019-02-12 11:54:00 -08:00
lg.Fatalln("The authentication key is connected by another client or the server closes the client.")
2019-02-09 01:07:47 -08:00
case common.RES_MSG:
2019-02-12 11:54:00 -08:00
lg.Println("Server-side return error")
break
2018-11-04 07:19:22 -08:00
default:
2019-02-12 11:54:00 -08:00
lg.Println("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-09 01:07:47 -08:00
lg.Println("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-16 07:18:58 -08:00
lg.Println("send ok", link.Id)
}
2019-02-15 06:59:28 -08:00
}
pool.PutBufPoolCopy(buf)
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-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-09 01:07:47 -08:00
lg.Println("connect to ", s.svrAddr, "error:", err)
2019-01-06 09:52:54 -08:00
return
}
2018-11-29 03:55:24 -08:00
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
lg.Println("get msg content error:", err, id)
pool.PutBufPoolCopy(content)
break
} else {
2019-02-09 01:07:47 -08:00
if len(content) == len(common.IO_EOF) && string(content) == common.IO_EOF {
v.Conn.Close()
} else if v.Conn != nil {
v.Conn.Write(content)
}
2019-02-09 01:07:47 -08:00
pool.PutBufPoolCopy(content)
}
} else {
s.Unlock()
}
}
}
}()
2019-02-12 11:54:00 -08:00
<-s.stop
}
var errAdd = errors.New("The server returned an error, which port or host may have been occupied or not allowed to open.")
func StartFromFile(path string) {
first := true
cnf, err := config.NewConfig(path)
if err != nil {
lg.Fatalln(err)
}
lg.Printf("Loading configuration file %s successfully", path)
re:
if first || cnf.CommonConfig.AutoReconnection {
if !first {
lg.Println("Reconnecting...")
time.Sleep(time.Second * 5)
}
} else {
return
}
first = false
2019-02-16 04:43:26 -08:00
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
2019-02-12 11:54:00 -08:00
if err != nil {
lg.Println(err)
goto re
}
if _, err := c.SendConfigInfo(cnf.CommonConfig.Cnf); err != nil {
lg.Println(err)
goto re
}
var b []byte
if b, err = c.ReadLen(16); err != nil {
lg.Println(err)
goto re
} else {
ioutil.WriteFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt"), []byte(string(b)), 0600)
}
if !c.GetAddStatus() {
lg.Println(errAdd)
goto re
}
for _, v := range cnf.Hosts {
if _, err := c.SendHostInfo(v); err != nil {
lg.Println(err)
goto re
}
if !c.GetAddStatus() {
lg.Println(errAdd, v.Host)
goto re
}
}
for _, v := range cnf.Tasks {
if _, err := c.SendTaskInfo(v); err != nil {
lg.Println(err)
goto re
}
if !c.GetAddStatus() {
2019-02-16 04:43:26 -08:00
lg.Println(errAdd, v.Ports)
2019-02-12 11:54:00 -08:00
goto re
}
}
2019-02-12 11:54:00 -08:00
c.Close()
2019-02-16 04:43:26 -08:00
NewRPClient(cnf.CommonConfig.Server, string(b), cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl).Start()
2019-02-12 11:54:00 -08:00
goto re
}
//Create a new connection with the server and verify it
2019-02-16 04:43:26 -08:00
func NewConn(tp string, vkey string, server string, connType string, proxyUrl string) (*conn.Conn, error) {
2019-02-12 11:54:00 -08:00
var err error
var connection net.Conn
var sess *kcp.UDPSession
if tp == "tcp" {
2019-02-16 04:43:26 -08:00
if proxyUrl != "" {
u, er := url.Parse(proxyUrl)
if er != nil {
return nil, er
}
n, er := proxy.FromURL(u, nil)
if er != nil {
return nil, er
}
connection, err = n.Dial("tcp", server)
} else {
connection, err = net.Dial("tcp", server)
}
2019-02-12 11:54:00 -08:00
} else {
sess, err = kcp.DialWithOptions(server, nil, 10, 3)
conn.SetUdpSession(sess)
connection = sess
}
if err != nil {
return nil, err
}
c := conn.NewConn(connection)
if _, err := c.Write([]byte(common.Getverifyval(vkey))); err != nil {
lg.Println(err)
}
if s, err := c.ReadFlag(); err != nil {
lg.Println(err)
} else if s == common.VERIFY_EER {
lg.Fatalf("Validation key %s incorrect", vkey)
}
if _, err := c.Write([]byte(connType)); err != nil {
lg.Println(err)
}
c.SetAlive(tp)
return c, nil
}