nps/client/client.go

153 lines
3.1 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-01-09 04:33:00 -08:00
"github.com/cnlh/easyProxy/utils"
2018-11-04 07:19:22 -08:00
"log"
"net"
"sync"
"sync/atomic"
2018-11-04 07:19:22 -08:00
"time"
)
type TRPClient struct {
svrAddr string
tcpNum int
2019-01-27 22:45:55 -08:00
connPoolSize int
tunnelNum int64
tunnel chan bool
serverStatus bool
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
2018-12-11 00:37:12 -08:00
func NewRPClient(svraddr string, tcpNum int, vKey string) *TRPClient {
2018-11-04 07:19:22 -08:00
c := new(TRPClient)
c.svrAddr = svraddr
c.tcpNum = tcpNum
2018-12-11 00:37:12 -08:00
c.vKey = vKey
c.tunnel = make(chan bool)
2019-01-27 22:45:55 -08:00
c.connPoolSize = 5
2018-11-04 07:19:22 -08:00
return c
}
2019-01-03 08:21:23 -08:00
//start
2018-11-29 03:55:24 -08:00
func (s *TRPClient) Start() error {
for i := 0; i < s.tcpNum; i++ {
2019-01-09 04:33:00 -08:00
go s.NewConn()
2018-11-04 07:19:22 -08:00
}
for i := 0; i < 5; i++ {
go s.dealChan()
}
go s.session()
2018-11-04 07:19:22 -08:00
return nil
}
2018-11-29 03:55:24 -08:00
//新建
2019-01-09 04:33:00 -08:00
func (s *TRPClient) NewConn() error {
2018-11-29 03:55:24 -08:00
s.Lock()
s.serverStatus = false
2018-11-29 03:55:24 -08:00
conn, err := net.Dial("tcp", s.svrAddr)
2018-11-04 07:19:22 -08:00
if err != nil {
log.Println("连接服务端失败,五秒后将重连")
time.Sleep(time.Second * 5)
2018-11-29 03:55:24 -08:00
s.Unlock()
2019-01-09 04:33:00 -08:00
go s.NewConn()
2018-11-04 07:19:22 -08:00
return err
}
2018-11-29 03:55:24 -08:00
s.Unlock()
return s.processor(utils.NewConn(conn))
2018-11-04 07:19:22 -08:00
}
2019-01-03 08:21:23 -08:00
//处理
func (s *TRPClient) processor(c *utils.Conn) error {
s.serverStatus = true
2018-11-29 03:55:24 -08:00
c.SetAlive()
2019-01-09 04:33:00 -08:00
if _, err := c.Write([]byte(utils.Getverifyval(s.vKey))); err != nil {
2018-11-04 07:19:22 -08:00
return err
}
2019-01-09 04:33:00 -08:00
c.WriteMain()
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 {
log.Println("服务端断开,五秒后将重连", err)
2019-01-09 04:33:00 -08:00
go s.NewConn()
2018-11-29 03:55:24 -08:00
break
2018-11-04 07:19:22 -08:00
}
switch flags {
2019-01-09 04:33:00 -08:00
case utils.VERIFY_EER:
2018-12-11 00:37:12 -08:00
log.Fatalln("vkey:", s.vKey, "不正确,服务端拒绝连接,请检查")
2019-01-09 04:33:00 -08:00
case utils.WORK_CHAN: //隧道模式每次开启10个加快连接速度
2019-01-27 22:45:55 -08:00
case utils.RES_CLOSE:
log.Fatal("该vkey被另一客户连接")
2019-01-09 04:33:00 -08:00
case utils.RES_MSG:
2018-11-04 07:19:22 -08:00
log.Println("服务端返回错误。")
default:
2018-12-11 00:37:12 -08:00
log.Println("无法解析该错误。", flags)
2018-11-04 07:19:22 -08:00
}
}
return nil
}
2018-11-29 03:55:24 -08:00
//隧道模式处理
2019-01-06 09:52:54 -08:00
func (s *TRPClient) dealChan() {
var err error
2018-11-29 03:55:24 -08:00
//创建一个tcp连接
conn, err := net.Dial("tcp", s.svrAddr)
if err != nil {
2019-01-02 09:44:45 -08:00
log.Println("connect to ", s.svrAddr, "error:", err)
2019-01-06 09:52:54 -08:00
return
}
2018-11-29 03:55:24 -08:00
//验证
2019-01-09 04:33:00 -08:00
if _, err := conn.Write([]byte(utils.Getverifyval(s.vKey))); err != nil {
2019-01-02 09:44:45 -08:00
log.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-01-09 04:33:00 -08:00
c := utils.NewConn(conn)
2018-11-29 03:55:24 -08:00
c.SetAlive()
//写标志
2019-01-09 04:33:00 -08:00
c.WriteChan()
2019-01-05 11:16:46 -08:00
re:
atomic.AddInt64(&s.tunnelNum, 1)
//获取连接的host type(tcp or udp)
2019-01-05 11:16:46 -08:00
typeStr, host, en, de, crypt, mux, err := c.GetHostFromConn()
s.tunnel <- true
atomic.AddInt64(&s.tunnelNum, -1)
2018-11-05 04:39:31 -08:00
if err != nil {
2019-01-06 09:52:54 -08:00
c.Close()
return
2018-11-05 04:39:31 -08:00
}
s.ConnectAndCopy(c, typeStr, host, en, de, crypt, mux)
2019-01-05 11:16:46 -08:00
if mux {
2019-01-09 04:33:00 -08:00
utils.FlushConn(conn)
2019-01-05 11:16:46 -08:00
goto re
2019-01-06 09:52:54 -08:00
} else {
c.Close()
2019-01-05 11:16:46 -08:00
}
}
2018-11-29 03:55:24 -08:00
func (s *TRPClient) session() {
t := time.NewTicker(time.Millisecond * 1000)
for {
select {
case <-s.tunnel:
case <-t.C:
}
if s.serverStatus && s.tunnelNum < 5 {
go s.dealChan()
}
}
}
func (s *TRPClient) ConnectAndCopy(c *utils.Conn, typeStr, host string, en, de int, crypt, mux bool) {
2019-01-09 04:33:00 -08:00
//与目标建立连接,超时时间为3
server, err := net.DialTimeout(typeStr, host, time.Second*3)
2018-11-05 04:39:31 -08:00
if err != nil {
2019-01-09 04:33:00 -08:00
log.Println("connect to ", host, "error:", err, mux)
c.WriteFail()
return
2018-11-05 04:39:31 -08:00
}
2019-01-09 04:33:00 -08:00
c.WriteSuccess()
2019-01-27 22:45:55 -08:00
utils.ReplayWaitGroup(c.Conn, server, en, de, crypt, mux, nil)
2018-11-05 04:39:31 -08:00
}