nps/client/client.go

292 lines
7.4 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-04-21 08:03:58 -07:00
"bufio"
2019-12-01 09:47:22 -08:00
"bytes"
2019-08-09 20:15:25 -07:00
"net"
"net/http"
"strconv"
"time"
"github.com/astaxie/beego/logs"
"github.com/xtaci/kcp-go"
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/common"
2019-03-07 02:07:53 -08:00
"github.com/cnlh/nps/lib/config"
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/conn"
2019-04-21 08:03:58 -07:00
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/mux"
2018-11-04 07:19:22 -08:00
)
type TRPClient struct {
2019-02-09 01:07:47 -08:00
svrAddr string
bridgeConnType string
2019-02-16 04:43:26 -08:00
proxyUrl string
vKey string
2019-04-21 08:03:58 -07:00
p2pAddr map[string]string
2019-03-02 01:43:21 -08:00
tunnel *mux.Mux
signal *conn.Conn
2019-03-19 07:41:40 -07:00
ticker *time.Ticker
2019-03-07 02:07:53 -08:00
cnf *config.Config
2018-11-04 07:19:22 -08:00
}
2019-01-03 08:21:23 -08:00
//new client
2019-03-07 02:07:53 -08:00
func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl string, cnf *config.Config) *TRPClient {
return &TRPClient{
2019-02-09 01:07:47 -08:00
svrAddr: svraddr,
2019-04-21 08:03:58 -07:00
p2pAddr: make(map[string]string, 0),
2019-02-09 01:07:47 -08:00
vKey: vKey,
bridgeConnType: bridgeConnType,
2019-02-16 04:43:26 -08:00
proxyUrl: proxyUrl,
2019-03-07 02:07:53 -08:00
cnf: cnf,
}
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-10-12 07:56:37 -07:00
if c == nil {
logs.Error("Error data from server, and will be reconnected in five seconds")
time.Sleep(time.Second * 5)
goto retry
}
2019-02-23 07:29:48 -08:00
logs.Info("Successful connection with server %s", s.svrAddr)
2019-04-08 02:01:08 -07:00
//monitor the connection
2019-03-02 01:43:21 -08:00
go s.ping()
s.signal = c
2019-04-08 02:01:08 -07:00
//start a channel connection
go s.newChan()
//start health check if the it's open
2019-03-14 23:03:49 -07:00
if s.cnf != nil && len(s.cnf.Healths) > 0 {
go heathCheck(s.cnf.Healths, s.signal)
}
2019-04-08 02:01:08 -07:00
//msg connection, eg udp
s.handleMain()
}
//handle main connection
func (s *TRPClient) handleMain() {
2018-11-04 07:19:22 -08:00
for {
2019-04-08 02:01:08 -07:00
flags, err := s.signal.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-26 06:40:28 -08:00
case common.NEW_UDP_CONN:
2019-04-08 02:01:08 -07:00
//read server udp addr and password
if lAddr, err := s.signal.GetShortLenContent(); err != nil {
logs.Warn(err)
2019-02-26 06:40:28 -08:00
return
2019-04-08 02:01:08 -07:00
} else if pwd, err := s.signal.GetShortLenContent(); err == nil {
2019-04-21 08:03:58 -07:00
var localAddr string
//The local port remains unchanged for a certain period of time
if v, ok := s.p2pAddr[crypt.Md5(string(pwd)+strconv.Itoa(int(time.Now().Unix()/100)))]; !ok {
tmpConn, err := common.GetLocalUdpAddr()
if err != nil {
logs.Error(err)
return
}
localAddr = tmpConn.LocalAddr().String()
} else {
localAddr = v
}
go s.newUdpConn(localAddr, string(lAddr), string(pwd))
2019-02-26 06:40:28 -08:00
}
2018-11-04 07:19:22 -08:00
}
}
2019-02-12 11:54:00 -08:00
s.Close()
}
2019-02-12 11:54:00 -08:00
2019-04-21 08:03:58 -07:00
func (s *TRPClient) newUdpConn(localAddr, rAddr string, md5Password string) {
2019-04-08 02:01:08 -07:00
var localConn net.PacketConn
var err error
var remoteAddress string
2019-04-21 08:03:58 -07:00
if remoteAddress, localConn, err = handleP2PUdp(localAddr, rAddr, md5Password, common.WORK_P2P_PROVIDER); err != nil {
logs.Error(err)
return
}
l, err := kcp.ServeConn(nil, 150, 3, localConn)
2019-02-17 03:36:48 -08:00
if err != nil {
logs.Error(err)
2019-02-17 03:36:48 -08:00
return
}
2019-04-08 02:01:08 -07:00
logs.Trace("start local p2p udp listen, local address", localConn.LocalAddr().String())
for {
udpTunnel, err := l.AcceptKCP()
if err != nil {
logs.Error(err)
l.Close()
return
}
2019-04-08 02:01:08 -07:00
if udpTunnel.RemoteAddr().String() == string(remoteAddress) {
conn.SetUdpSession(udpTunnel)
2019-04-08 02:01:08 -07:00
logs.Trace("successful connection with client ,address %s", udpTunnel.RemoteAddr().String())
//read link info from remote
2019-04-21 08:03:58 -07:00
conn.Accept(mux.NewMux(udpTunnel, s.bridgeConnType), func(c net.Conn) {
go s.handleChan(c)
})
break
2019-02-17 03:36:48 -08:00
}
}
2019-02-17 03:36:48 -08:00
}
//mux tunnel
2019-04-08 02:01:08 -07:00
func (s *TRPClient) newChan() {
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
}
2019-03-19 07:41:40 -07:00
s.tunnel = mux.NewMux(tunnel.Conn, s.bridgeConnType)
for {
src, err := s.tunnel.Accept()
if err != nil {
logs.Warn(err)
s.Close()
break
}
2019-04-08 02:01:08 -07:00
go s.handleChan(src)
2019-03-19 07:41:40 -07:00
}
2019-02-12 11:54:00 -08:00
}
2019-02-23 21:17:43 -08:00
2019-04-08 02:01:08 -07:00
func (s *TRPClient) handleChan(src net.Conn) {
lk, err := conn.NewConn(src).GetLinkInfo()
if err != nil || lk == nil {
src.Close()
logs.Error("get connection info from server error ", err)
return
}
2019-03-02 01:43:21 -08:00
//host for target processing
lk.Host = common.FormatAddress(lk.Host)
2019-04-21 08:03:58 -07:00
//if Conn type is http, read the request and log
if lk.ConnType == "http" {
if targetConn, err := net.DialTimeout(common.CONN_TCP, lk.Host, lk.Option.Timeout); err != nil {
2019-04-21 08:03:58 -07:00
logs.Warn("connect to %s error %s", lk.Host, err.Error())
src.Close()
} else {
2019-04-25 05:13:07 -07:00
srcConn := conn.GetConn(src, lk.Crypt, lk.Compress, nil, false)
2019-04-21 08:03:58 -07:00
go func() {
2019-04-25 05:13:07 -07:00
common.CopyBuffer(srcConn, targetConn)
srcConn.Close()
2019-04-21 08:03:58 -07:00
targetConn.Close()
}()
for {
2019-04-25 05:13:07 -07:00
if r, err := http.ReadRequest(bufio.NewReader(srcConn)); err != nil {
srcConn.Close()
2019-04-21 08:03:58 -07:00
targetConn.Close()
break
} else {
logs.Trace("http request, method %s, host %s, url %s, remote address %s", r.Method, r.Host, r.URL.Path, r.RemoteAddr)
r.Write(targetConn)
}
}
}
return
}
if lk.ConnType == "udp5" {
2019-12-01 09:47:22 -08:00
logs.Trace("new %s connection with the goal of %s, remote address:%s", lk.ConnType, lk.Host, lk.RemoteAddr)
s.handleUdp(src)
}
2019-04-21 08:03:58 -07:00
//connect to target if conn type is tcp or udp
if targetConn, err := net.DialTimeout(lk.ConnType, lk.Host, lk.Option.Timeout); err != nil {
logs.Warn("connect to %s error %s", lk.Host, err.Error())
src.Close()
} else {
logs.Trace("new %s connection with the goal of %s, remote address:%s", lk.ConnType, lk.Host, lk.RemoteAddr)
2019-03-25 05:40:22 -07:00
conn.CopyWaitGroup(src, targetConn, lk.Crypt, lk.Compress, nil, nil, false, nil)
2019-02-23 21:17:43 -08:00
}
}
2019-03-02 01:43:21 -08:00
2019-12-01 09:47:22 -08:00
func (s *TRPClient) handleUdp(serverConn net.Conn) {
// bind a local udp port
local, err := net.ListenUDP("udp", nil)
defer local.Close()
defer serverConn.Close()
if err != nil {
logs.Error("bind local udp port error ", err.Error())
return
}
go func() {
defer serverConn.Close()
b := common.BufPoolUdp.Get().([]byte)
defer common.BufPoolUdp.Put(b)
for {
n, raddr, err := local.ReadFrom(b)
if err != nil {
logs.Error("read data from remote server error", err.Error())
}
buf := bytes.Buffer{}
dgram := common.NewUDPDatagram(common.NewUDPHeader(0, 0, common.ToSocksAddr(raddr)), b[:n])
dgram.Write(&buf)
2019-12-02 08:46:30 -08:00
b, err := conn.GetLenBytes(buf.Bytes())
if err != nil {
logs.Warn("get len bytes error", err.Error())
continue
}
if _, err := serverConn.Write(b); err != nil {
2019-12-01 09:47:22 -08:00
logs.Error("write data to remote error", err.Error())
return
}
}
}()
b := common.BufPoolUdp.Get().([]byte)
defer common.BufPoolUdp.Put(b)
for {
n, err := serverConn.Read(b)
if err != nil {
logs.Error("read udp data from server error ", err.Error())
return
}
udpData, err := common.ReadUDPDatagram(bytes.NewReader(b[:n]))
if err != nil {
logs.Error("unpack data error", err.Error())
return
}
raddr, err := net.ResolveUDPAddr("udp", udpData.Header.Addr.String())
if err != nil {
logs.Error("build remote addr err", err.Error())
continue // drop silently
}
_, err = local.WriteTo(udpData.Data, raddr)
if err != nil {
logs.Error("write data to remote ", raddr.String(), "error", err.Error())
return
}
}
}
2019-04-21 08:03:58 -07:00
// Whether the monitor channel is closed
2019-03-02 01:43:21 -08:00
func (s *TRPClient) ping() {
2019-03-19 07:41:40 -07:00
s.ticker = time.NewTicker(time.Second * 5)
2019-03-02 01:43:21 -08:00
loop:
for {
select {
2019-03-19 07:41:40 -07:00
case <-s.ticker.C:
2019-04-08 02:01:08 -07:00
if s.tunnel != nil && s.tunnel.IsClose {
2019-03-02 01:43:21 -08:00
s.Close()
break loop
}
}
}
}
2019-03-23 07:19:59 -07:00
func (s *TRPClient) Close() {
if s.tunnel != nil {
s.tunnel.Close()
}
if s.signal != nil {
s.signal.Close()
}
if s.ticker != nil {
s.ticker.Stop()
}
2019-03-23 07:19:59 -07:00
}