2018-11-04 07:19:22 -08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2018-11-29 03:55:24 -08:00
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2018-11-04 07:19:22 -08:00
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type TRPClient struct {
|
|
|
|
|
svrAddr string
|
|
|
|
|
tcpNum int
|
|
|
|
|
sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRPClient(svraddr string, tcpNum int) *TRPClient {
|
|
|
|
|
c := new(TRPClient)
|
|
|
|
|
c.svrAddr = svraddr
|
|
|
|
|
c.tcpNum = tcpNum
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 03:55:24 -08:00
|
|
|
|
func (s *TRPClient) Start() error {
|
|
|
|
|
for i := 0; i < s.tcpNum; i++ {
|
|
|
|
|
go s.newConn()
|
2018-11-04 07:19:22 -08:00
|
|
|
|
}
|
|
|
|
|
for {
|
2018-11-29 03:55:24 -08:00
|
|
|
|
time.Sleep(time.Second * 5)
|
2018-11-04 07:19:22 -08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 03:55:24 -08:00
|
|
|
|
//新建
|
|
|
|
|
func (s *TRPClient) newConn() error {
|
|
|
|
|
s.Lock()
|
|
|
|
|
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()
|
|
|
|
|
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.process(NewConn(conn))
|
2018-11-04 07:19:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 03:55:24 -08:00
|
|
|
|
func (s *TRPClient) process(c *Conn) error {
|
|
|
|
|
c.SetAlive()
|
|
|
|
|
if _, err := c.Write(getverifyval()); err != nil {
|
2018-11-04 07:19:22 -08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
c.wMain()
|
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)
|
|
|
|
|
time.Sleep(5 * time.Second)
|
2018-11-29 03:55:24 -08:00
|
|
|
|
go s.newConn()
|
|
|
|
|
break
|
2018-11-04 07:19:22 -08:00
|
|
|
|
}
|
|
|
|
|
switch flags {
|
2018-11-29 03:55:24 -08:00
|
|
|
|
case VERIFY_EER:
|
2018-11-04 07:19:22 -08:00
|
|
|
|
log.Fatal("vkey不正确,请检查配置文件")
|
2018-11-29 03:55:24 -08:00
|
|
|
|
case RES_SIGN: //代理请求模式
|
|
|
|
|
if err := s.dealHttp(c); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
case WORK_CHAN: //隧道模式,每次开启10个,加快连接速度
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
go s.dealChan()
|
|
|
|
|
}
|
|
|
|
|
case RES_MSG:
|
2018-11-04 07:19:22 -08:00
|
|
|
|
log.Println("服务端返回错误。")
|
|
|
|
|
default:
|
|
|
|
|
log.Println("无法解析该错误。")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
|
|
|
|
|
//隧道模式处理
|
|
|
|
|
func (s *TRPClient) dealChan() error {
|
|
|
|
|
//创建一个tcp连接
|
|
|
|
|
conn, err := net.Dial("tcp", s.svrAddr)
|
|
|
|
|
//验证
|
|
|
|
|
if _, err := conn.Write(getverifyval()); err != nil {
|
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
//默认长连接保持
|
|
|
|
|
c := NewConn(conn)
|
|
|
|
|
c.SetAlive()
|
|
|
|
|
//写标志
|
|
|
|
|
c.wChan()
|
|
|
|
|
//获取连接的host
|
|
|
|
|
host, err := c.GetHostFromConn()
|
2018-11-05 04:39:31 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
//与目标建立连接
|
|
|
|
|
server, err := net.Dial("tcp", host)
|
2018-11-05 04:39:31 -08:00
|
|
|
|
if err != nil {
|
2018-11-06 00:43:06 -08:00
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
//创建成功后io.copy
|
|
|
|
|
go io.Copy(server, c)
|
|
|
|
|
io.Copy(c, server)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//http模式处理
|
|
|
|
|
func (s *TRPClient) dealHttp(c *Conn) error {
|
|
|
|
|
nlen, err := c.GetLen()
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.wError()
|
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
raw, err := c.ReadLen(int(nlen))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.wError()
|
2018-11-06 00:43:06 -08:00
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
req, err := DecodeRequest(raw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.wError()
|
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
respBytes, err := GetEncodeResponse(req)
|
2018-11-05 04:39:31 -08:00
|
|
|
|
if err != nil {
|
2018-11-29 03:55:24 -08:00
|
|
|
|
c.wError()
|
2018-11-06 00:43:06 -08:00
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
2018-11-29 03:55:24 -08:00
|
|
|
|
n, err := c.Write(respBytes)
|
2018-11-05 04:39:31 -08:00
|
|
|
|
if err != nil {
|
2018-11-06 00:43:06 -08:00
|
|
|
|
return err
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
|
|
|
|
if n != len(respBytes) {
|
2018-11-29 03:55:24 -08:00
|
|
|
|
return errors.New(fmt.Sprintf("发送数据长度错误,已经发送:%dbyte,总字节长:%dbyte\n", n, len(respBytes)))
|
2018-11-05 04:39:31 -08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|