pingtunnel/client.go

711 lines
17 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
2019-10-26 07:01:30 +03:00
"github.com/esrrhs/go-engine/src/common"
2019-10-16 16:09:21 +03:00
"github.com/esrrhs/go-engine/src/loggo"
2019-10-22 16:04:25 +03:00
"github.com/golang/protobuf/proto"
2018-12-17 10:21:15 +03:00
"golang.org/x/net/icmp"
2018-12-21 10:52:34 +03:00
"math"
"math/rand"
2018-12-17 10:21:15 +03:00
"net"
2019-10-30 14:45:19 +03:00
"sync"
2018-12-17 14:06:46 +03:00
"time"
2018-12-17 10:21:15 +03:00
)
2019-10-22 16:04:25 +03:00
const (
SEND_PROTO int = 8
RECV_PROTO int = 0
)
2019-10-22 16:27:21 +03:00
func NewClient(addr string, server string, target string, timeout int, key int,
2019-10-28 06:58:01 +03:00
tcpmode int, tcpmode_buffersize int, tcpmode_maxwin int, tcpmode_resend_timems int, tcpmode_compress int,
2019-10-31 16:16:14 +03:00
tcpmode_stat int, open_sock5 int, maxconn int) (*Client, error) {
2018-12-17 10:21:15 +03:00
2019-10-16 16:09:21 +03:00
var ipaddr *net.UDPAddr
var tcpaddr *net.TCPAddr
var err error
2019-10-20 11:27:03 +03:00
if tcpmode > 0 {
2019-10-16 16:09:21 +03:00
tcpaddr, err = net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
} else {
ipaddr, err = net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
2018-12-17 10:21:15 +03:00
}
2018-12-18 06:39:16 +03:00
ipaddrServer, err := net.ResolveIPAddr("ip", server)
2018-12-17 10:21:15 +03:00
if err != nil {
return nil, err
}
2018-12-21 10:52:34 +03:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2018-12-17 10:21:15 +03:00
return &Client{
2019-10-30 13:57:26 +03:00
exit: false,
rtt: 0,
2019-10-22 16:27:21 +03:00
id: r.Intn(math.MaxInt16),
ipaddr: ipaddr,
tcpaddr: tcpaddr,
addr: addr,
ipaddrServer: ipaddrServer,
addrServer: server,
targetAddr: target,
timeout: timeout,
key: key,
tcpmode: tcpmode,
tcpmode_buffersize: tcpmode_buffersize,
tcpmode_maxwin: tcpmode_maxwin,
tcpmode_resend_timems: tcpmode_resend_timems,
2019-10-27 14:06:55 +03:00
tcpmode_compress: tcpmode_compress,
2019-10-28 06:58:01 +03:00
tcpmode_stat: tcpmode_stat,
2019-10-28 11:33:44 +03:00
open_sock5: open_sock5,
2019-10-31 16:16:14 +03:00
maxconn: maxconn,
2018-12-17 10:21:15 +03:00
}, nil
}
type Client struct {
2019-10-30 14:45:19 +03:00
exit bool
rtt time.Duration
interval *time.Ticker
workResultLock sync.WaitGroup
2019-10-31 16:16:14 +03:00
maxconn int
2019-10-30 13:57:26 +03:00
2018-12-21 11:09:54 +03:00
id int
sequence int
2018-12-21 10:52:34 +03:00
2019-10-19 17:36:17 +03:00
timeout int
sproto int
rproto int
key int
2019-10-20 11:27:03 +03:00
tcpmode int
2019-10-19 17:36:17 +03:00
tcpmode_buffersize int
tcpmode_maxwin int
tcpmode_resend_timems int
2019-10-27 14:06:55 +03:00
tcpmode_compress int
2019-10-28 06:58:01 +03:00
tcpmode_stat int
2019-10-28 11:33:44 +03:00
open_sock5 int
2019-10-18 16:30:54 +03:00
2019-10-16 16:09:21 +03:00
ipaddr *net.UDPAddr
tcpaddr *net.TCPAddr
addr string
2018-12-17 10:21:15 +03:00
2018-12-18 06:39:16 +03:00
ipaddrServer *net.IPAddr
addrServer string
2018-12-18 10:36:59 +03:00
targetAddr string
2018-12-17 10:21:15 +03:00
2019-10-16 16:09:21 +03:00
conn *icmp.PacketConn
listenConn *net.UDPConn
tcplistenConn *net.TCPListener
2018-12-17 14:06:46 +03:00
2019-10-31 16:05:48 +03:00
localAddrToConnMap sync.Map
localIdToConnMap sync.Map
sendPacket uint64
recvPacket uint64
sendPacketSize uint64
recvPacketSize uint64
localAddrToConnMapSize int
localIdToConnMapSize int
2018-12-18 10:36:59 +03:00
}
type ClientConn struct {
2019-10-31 16:48:32 +03:00
exit bool
2019-10-25 16:57:38 +03:00
ipaddr *net.UDPAddr
tcpaddr *net.TCPAddr
id string
activeRecvTime time.Time
activeSendTime time.Time
close bool
2019-10-16 16:09:21 +03:00
2019-10-19 17:36:17 +03:00
fm *FrameMgr
2018-12-17 10:21:15 +03:00
}
func (p *Client) Addr() string {
return p.addr
}
2018-12-17 14:06:46 +03:00
func (p *Client) IPAddr() *net.UDPAddr {
2018-12-17 10:21:15 +03:00
return p.ipaddr
}
2018-12-18 10:36:59 +03:00
func (p *Client) TargetAddr() string {
return p.targetAddr
2018-12-18 06:39:16 +03:00
}
func (p *Client) ServerIPAddr() *net.IPAddr {
return p.ipaddrServer
2018-12-17 10:21:15 +03:00
}
2018-12-18 06:39:16 +03:00
func (p *Client) ServerAddr() string {
return p.addrServer
2018-12-17 10:21:15 +03:00
}
2019-10-30 13:57:26 +03:00
func (p *Client) RTT() time.Duration {
return p.rtt
}
2019-10-30 15:15:43 +03:00
func (p *Client) RecvPacketSize() uint64 {
return p.recvPacketSize
}
func (p *Client) SendPacketSize() uint64 {
return p.sendPacketSize
}
func (p *Client) RecvPacket() uint64 {
return p.recvPacket
}
func (p *Client) SendPacket() uint64 {
return p.sendPacket
}
2019-10-31 16:05:48 +03:00
func (p *Client) LocalIdToConnMapSize() int {
return p.localIdToConnMapSize
}
func (p *Client) LocalAddrToConnMapSize() int {
return p.localAddrToConnMapSize
}
2019-10-30 14:14:27 +03:00
func (p *Client) Run() error {
2018-12-17 10:21:15 +03:00
conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
2019-10-16 16:09:21 +03:00
loggo.Error("Error listening for ICMP packets: %s", err.Error())
2019-10-30 14:14:27 +03:00
return err
2018-12-17 10:21:15 +03:00
}
p.conn = conn
2019-10-20 11:27:03 +03:00
if p.tcpmode > 0 {
2019-10-16 16:09:21 +03:00
tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr)
if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error())
2019-10-30 14:14:27 +03:00
return err
2019-10-16 16:09:21 +03:00
}
p.tcplistenConn = tcplistenConn
} else {
listener, err := net.ListenUDP("udp", p.ipaddr)
if err != nil {
loggo.Error("Error listening for udp packets: %s", err.Error())
2019-10-30 14:14:27 +03:00
return err
2019-10-16 16:09:21 +03:00
}
p.listenConn = listener
2018-12-17 10:21:15 +03:00
}
2019-10-20 11:27:03 +03:00
if p.tcpmode > 0 {
2019-10-16 16:09:21 +03:00
go p.AcceptTcp()
} else {
go p.Accept()
}
2018-12-17 10:21:15 +03:00
2018-12-19 09:42:53 +03:00
recv := make(chan *Packet, 10000)
2019-10-30 14:45:19 +03:00
go recvICMP(&p.workResultLock, &p.exit, *p.conn, recv)
2018-12-17 10:21:15 +03:00
2019-10-30 14:28:00 +03:00
p.interval = time.NewTicker(time.Second)
2018-12-18 10:36:59 +03:00
2019-10-30 14:14:27 +03:00
go func() {
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-30 14:14:27 +03:00
for !p.exit {
select {
2019-10-30 14:28:00 +03:00
case <-p.interval.C:
2019-10-30 14:14:27 +03:00
p.checkTimeoutConn()
p.ping()
p.showNet()
case r := <-recv:
p.processPacket(r)
}
2018-12-17 10:21:15 +03:00
}
2019-10-30 14:14:27 +03:00
}()
return nil
2018-12-17 10:21:15 +03:00
}
2019-10-30 13:57:26 +03:00
func (p *Client) Stop() {
p.exit = true
2019-10-30 14:45:19 +03:00
p.workResultLock.Wait()
2019-10-30 14:28:00 +03:00
p.conn.Close()
if p.tcplistenConn != nil {
p.tcplistenConn.Close()
}
if p.listenConn != nil {
p.listenConn.Close()
}
p.interval.Stop()
2019-10-30 13:57:26 +03:00
}
2019-10-16 16:09:21 +03:00
func (p *Client) AcceptTcp() error {
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-16 16:09:21 +03:00
loggo.Info("client waiting local accept tcp")
2019-10-30 13:57:26 +03:00
for !p.exit {
2019-10-25 16:48:10 +03:00
p.tcplistenConn.SetDeadline(time.Now().Add(time.Millisecond * 1000))
2019-10-16 16:09:21 +03:00
conn, err := p.tcplistenConn.AcceptTCP()
if err != nil {
2019-10-24 16:42:46 +03:00
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error accept tcp %s", err)
2019-10-24 16:42:46 +03:00
continue
2019-10-16 16:09:21 +03:00
}
}
2019-10-25 16:48:10 +03:00
if conn != nil {
2019-10-28 11:33:44 +03:00
if p.open_sock5 > 0 {
go p.AcceptSock5Conn(conn)
} else {
go p.AcceptTcpConn(conn, p.targetAddr)
}
2019-10-25 16:48:10 +03:00
}
2019-10-16 16:09:21 +03:00
}
2019-10-30 13:57:26 +03:00
return nil
2019-10-16 16:09:21 +03:00
}
2019-10-28 11:33:44 +03:00
func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
2019-10-16 16:09:21 +03:00
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-16 16:09:21 +03:00
tcpsrcaddr := conn.RemoteAddr().(*net.TCPAddr)
2019-10-31 16:18:42 +03:00
if p.maxconn > 0 && p.localIdToConnMapSize >= p.maxconn {
2019-10-31 16:16:14 +03:00
loggo.Info("too many connections %d, client accept new local tcp fail %s", p.localIdToConnMapSize, tcpsrcaddr.String())
return
}
uuid := UniqueId()
2019-10-28 06:58:01 +03:00
fm := NewFrameMgr(p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems, p.tcpmode_compress, p.tcpmode_stat)
2019-10-16 16:09:21 +03:00
2019-10-24 16:42:46 +03:00
now := time.Now()
2019-10-31 16:48:32 +03:00
clientConn := &ClientConn{exit: false, tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false,
2019-10-19 17:36:17 +03:00
fm: fm}
2019-10-31 16:05:48 +03:00
p.addClientConn(uuid, tcpsrcaddr.String(), clientConn)
2019-10-16 16:09:21 +03:00
loggo.Info("client accept new local tcp %s %s", uuid, tcpsrcaddr.String())
2019-10-27 12:17:47 +03:00
loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String())
2019-10-27 13:16:57 +03:00
clientConn.fm.Connect()
2019-10-27 12:17:47 +03:00
startConnectTime := time.Now()
2019-10-31 16:48:32 +03:00
for !p.exit && !clientConn.exit {
2019-10-27 13:16:57 +03:00
if clientConn.fm.IsConnected() {
2019-10-27 12:17:47 +03:00
break
}
clientConn.fm.Update()
sendlist := clientConn.fm.getSendList()
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, _ := proto.Marshal(f)
p.sequence++
2019-10-28 11:33:44 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
2019-10-27 12:17:47 +03:00
SEND_PROTO, RECV_PROTO, p.key,
2019-10-28 06:58:01 +03:00
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems, p.tcpmode_compress, p.tcpmode_stat,
2019-10-27 12:17:47 +03:00
p.timeout)
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
}
time.Sleep(time.Millisecond * 10)
now := time.Now()
diffclose := now.Sub(startConnectTime)
if diffclose > time.Second*(time.Duration(p.timeout)) {
loggo.Info("can not connect remote tcp %s %s", uuid, tcpsrcaddr.String())
2019-10-30 14:28:00 +03:00
p.close(clientConn)
2019-10-27 12:17:47 +03:00
return
}
}
loggo.Info("connected remote tcp %s %s", uuid, tcpsrcaddr.String())
2019-10-16 16:09:21 +03:00
bytes := make([]byte, 10240)
2019-10-26 07:01:30 +03:00
tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now()
2019-10-31 16:48:32 +03:00
for !p.exit && !clientConn.exit {
2019-10-26 07:01:30 +03:00
now := time.Now()
2019-10-27 07:55:40 +03:00
sleep := true
2019-10-26 07:01:30 +03:00
left := common.MinOfInt(clientConn.fm.GetSendBufferLeft(), len(bytes))
if left > 0 {
2019-10-27 07:32:31 +03:00
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 1))
2019-10-26 07:01:30 +03:00
n, err := conn.Read(bytes[0:left])
2019-10-16 16:09:21 +03:00
if err != nil {
2019-10-24 16:42:46 +03:00
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error read tcp %s %s %s", uuid, tcpsrcaddr.String(), err)
clientConn.fm.Close()
2019-10-24 16:42:46 +03:00
break
2019-10-16 16:09:21 +03:00
}
}
if n > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-19 17:36:17 +03:00
clientConn.fm.WriteSendBuffer(bytes[:n])
2019-10-26 12:05:08 +03:00
tcpActiveRecvTime = now
2019-10-16 16:09:21 +03:00
}
}
2019-10-19 17:36:17 +03:00
clientConn.fm.Update()
sendlist := clientConn.fm.getSendList()
2019-10-26 07:01:30 +03:00
if sendlist.Len() > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-26 07:01:30 +03:00
clientConn.activeSendTime = now
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, err := proto.Marshal(f)
if err != nil {
loggo.Error("Error tcp Marshal %s %s %s", uuid, tcpsrcaddr.String(), err)
continue
}
p.sequence++
2019-10-28 11:33:44 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
2019-10-26 07:01:30 +03:00
SEND_PROTO, RECV_PROTO, p.key,
2019-10-28 06:58:01 +03:00
p.tcpmode, 0, 0, 0, 0, 0,
2019-10-27 13:35:00 +03:00
0)
2019-10-26 07:01:30 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
2019-10-19 17:36:17 +03:00
}
}
2019-10-24 16:57:03 +03:00
if clientConn.fm.GetRecvBufferSize() > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-24 16:57:03 +03:00
rr := clientConn.fm.GetRecvReadLineBuffer()
2019-10-27 07:32:31 +03:00
conn.SetWriteDeadline(time.Now().Add(time.Millisecond * 1))
2019-10-24 16:57:03 +03:00
n, err := conn.Write(rr)
if err != nil {
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error write tcp %s %s %s", uuid, tcpsrcaddr.String(), err)
clientConn.fm.Close()
2019-10-24 16:57:03 +03:00
break
}
}
if n > 0 {
clientConn.fm.SkipRecvBuffer(n)
2019-10-26 12:05:08 +03:00
tcpActiveSendTime = now
2019-10-24 16:57:03 +03:00
}
}
2019-10-25 16:57:38 +03:00
2019-10-27 07:55:40 +03:00
if sleep {
time.Sleep(time.Millisecond * 10)
}
2019-10-25 16:57:38 +03:00
diffrecv := now.Sub(clientConn.activeRecvTime)
diffsend := now.Sub(clientConn.activeSendTime)
2019-10-26 07:01:30 +03:00
tcpdiffrecv := now.Sub(tcpActiveRecvTime)
tcpdiffsend := now.Sub(tcpActiveSendTime)
if diffrecv > time.Second*(time.Duration(p.timeout)) || diffsend > time.Second*(time.Duration(p.timeout)) ||
tcpdiffrecv > time.Second*(time.Duration(p.timeout)) || tcpdiffsend > time.Second*(time.Duration(p.timeout)) {
2019-10-25 16:57:38 +03:00
loggo.Info("close inactive conn %s %s", clientConn.id, clientConn.tcpaddr.String())
2019-10-26 07:01:30 +03:00
clientConn.fm.Close()
2019-10-25 16:57:38 +03:00
break
}
2019-10-26 07:01:30 +03:00
if clientConn.fm.IsRemoteClosed() {
loggo.Info("closed by remote conn %s %s", clientConn.id, clientConn.tcpaddr.String())
clientConn.fm.Close()
break
}
}
startCloseTime := time.Now()
2019-10-31 16:48:32 +03:00
for !p.exit && !clientConn.exit {
2019-10-26 07:01:30 +03:00
now := time.Now()
clientConn.fm.Update()
sendlist := clientConn.fm.getSendList()
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, _ := proto.Marshal(f)
p.sequence++
2019-10-28 11:33:44 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
2019-10-26 07:01:30 +03:00
SEND_PROTO, RECV_PROTO, p.key,
2019-10-28 06:58:01 +03:00
p.tcpmode, 0, 0, 0, 0, 0,
2019-10-27 13:35:00 +03:00
0)
2019-10-26 07:01:30 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
}
nodatarecv := true
if clientConn.fm.GetRecvBufferSize() > 0 {
rr := clientConn.fm.GetRecvReadLineBuffer()
conn.SetWriteDeadline(time.Now().Add(time.Millisecond * 100))
n, _ := conn.Write(rr)
if n > 0 {
clientConn.fm.SkipRecvBuffer(n)
nodatarecv = false
}
}
diffclose := now.Sub(startCloseTime)
timeout := diffclose > time.Second*(time.Duration(p.timeout))
remoteclosed := clientConn.fm.IsRemoteClosed()
if timeout {
loggo.Info("close conn had timeout %s %s", clientConn.id, clientConn.tcpaddr.String())
break
}
if remoteclosed && nodatarecv {
loggo.Info("remote conn had closed %s %s", clientConn.id, clientConn.tcpaddr.String())
break
}
time.Sleep(time.Millisecond * 100)
2019-10-16 16:09:21 +03:00
}
2019-10-19 17:36:17 +03:00
2019-10-25 16:57:38 +03:00
loggo.Info("close tcp conn %s %s", clientConn.id, clientConn.tcpaddr.String())
2019-10-19 17:36:17 +03:00
conn.Close()
2019-10-30 14:28:00 +03:00
p.close(clientConn)
2019-10-16 16:09:21 +03:00
}
2018-12-17 14:06:46 +03:00
func (p *Client) Accept() error {
2018-12-17 10:21:15 +03:00
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-16 16:09:21 +03:00
loggo.Info("client waiting local accept udp")
2018-12-17 10:21:15 +03:00
2018-12-17 14:06:46 +03:00
bytes := make([]byte, 10240)
2018-12-17 10:21:15 +03:00
2019-10-30 13:57:26 +03:00
for !p.exit {
2018-12-18 06:39:16 +03:00
p.listenConn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
2018-12-17 14:06:46 +03:00
n, srcaddr, err := p.listenConn.ReadFromUDP(bytes)
if err != nil {
2019-10-24 16:42:46 +03:00
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error read udp %s", err)
2019-10-24 16:42:46 +03:00
continue
2018-12-17 14:06:46 +03:00
}
}
2019-10-24 16:42:46 +03:00
if n <= 0 {
continue
}
2018-12-17 14:06:46 +03:00
2018-12-18 10:36:59 +03:00
now := time.Now()
2019-10-31 16:05:48 +03:00
clientConn := p.getClientConnByAddr(srcaddr.String())
2018-12-18 10:36:59 +03:00
if clientConn == nil {
2019-10-31 16:18:42 +03:00
if p.maxconn > 0 && p.localIdToConnMapSize >= p.maxconn {
2019-10-31 16:16:14 +03:00
loggo.Info("too many connections %d, client accept new local udp fail %s", p.localIdToConnMapSize, srcaddr.String())
continue
}
2018-12-18 10:36:59 +03:00
uuid := UniqueId()
2019-10-31 16:48:32 +03:00
clientConn = &ClientConn{exit: false, ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false}
2019-10-31 16:05:48 +03:00
p.addClientConn(uuid, srcaddr.String(), clientConn)
2019-10-16 16:09:21 +03:00
loggo.Info("client accept new local udp %s %s", uuid, srcaddr.String())
2018-12-17 14:06:46 +03:00
}
2019-10-25 16:57:38 +03:00
clientConn.activeSendTime = now
2019-10-22 16:04:25 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), bytes[:n],
2019-10-22 16:27:21 +03:00
SEND_PROTO, RECV_PROTO, p.key,
2019-10-28 06:58:01 +03:00
p.tcpmode, 0, 0, 0, 0, 0,
2019-10-26 07:07:20 +03:00
p.timeout)
2018-12-21 11:09:54 +03:00
p.sequence++
2018-12-19 10:50:21 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(n)
2018-12-17 10:21:15 +03:00
}
2019-10-30 13:57:26 +03:00
return nil
2018-12-17 10:21:15 +03:00
}
2018-12-17 14:06:46 +03:00
func (p *Client) processPacket(packet *Packet) {
2018-12-17 10:21:15 +03:00
2019-10-22 16:04:25 +03:00
if packet.my.Rproto >= 0 {
2018-12-20 09:18:08 +03:00
return
}
2019-10-22 16:04:25 +03:00
if packet.my.Key != (int32)(p.key) {
2019-01-08 05:31:52 +03:00
return
}
2019-01-08 06:13:41 +03:00
if packet.echoId != p.id {
return
}
2019-10-22 16:04:25 +03:00
if packet.my.Type == (int32)(MyMsg_PING) {
2018-12-19 10:05:22 +03:00
t := time.Time{}
2019-10-22 16:04:25 +03:00
t.UnmarshalBinary(packet.my.Data)
2018-12-23 02:02:57 +03:00
d := time.Now().Sub(t)
2019-10-16 16:09:21 +03:00
loggo.Info("pong from %s %s", packet.src.String(), d.String())
2019-10-30 13:57:26 +03:00
p.rtt = d
2018-12-19 10:05:22 +03:00
return
}
2019-10-31 16:48:32 +03:00
if packet.my.Type == (int32)(MyMsg_KICK) {
clientConn := p.getClientConnById(packet.my.Id)
if clientConn != nil {
p.close(clientConn)
loggo.Info("remote kick local %s", packet.my.Id)
}
return
}
2019-10-22 16:04:25 +03:00
loggo.Debug("processPacket %s %s %d", packet.my.Id, packet.src.String(), len(packet.my.Data))
2018-12-17 10:21:15 +03:00
2019-10-31 16:05:48 +03:00
clientConn := p.getClientConnById(packet.my.Id)
2018-12-18 10:36:59 +03:00
if clientConn == nil {
2019-10-22 16:04:25 +03:00
loggo.Debug("processPacket no conn %s ", packet.my.Id)
2018-12-17 14:06:46 +03:00
return
2018-12-17 10:21:15 +03:00
}
2018-12-18 10:36:59 +03:00
now := time.Now()
2019-10-25 16:57:38 +03:00
clientConn.activeRecvTime = now
2018-12-18 10:36:59 +03:00
2019-10-24 16:57:03 +03:00
if p.tcpmode > 0 {
f := &Frame{}
err := proto.Unmarshal(packet.my.Data, f)
if err != nil {
loggo.Error("Unmarshal tcp Error %s", err)
return
}
clientConn.fm.OnRecvFrame(f)
} else {
2019-10-31 16:05:48 +03:00
addr := clientConn.ipaddr
2019-10-24 16:57:03 +03:00
_, err := p.listenConn.WriteToUDP(packet.my.Data, addr)
if err != nil {
2019-10-26 07:01:30 +03:00
loggo.Info("WriteToUDP Error read udp %s", err)
2019-10-24 16:57:03 +03:00
clientConn.close = true
return
}
2018-12-17 10:21:15 +03:00
}
2018-12-19 10:50:21 +03:00
2018-12-23 02:07:25 +03:00
p.recvPacket++
2019-10-22 16:04:25 +03:00
p.recvPacketSize += (uint64)(len(packet.my.Data))
2018-12-17 10:21:15 +03:00
}
2018-12-18 10:36:59 +03:00
2019-10-30 14:28:00 +03:00
func (p *Client) close(clientConn *ClientConn) {
2019-10-31 16:48:32 +03:00
clientConn.exit = true
2019-10-31 16:05:48 +03:00
p.deleteClientConn(clientConn.id, clientConn.ipaddr.String())
p.deleteClientConn(clientConn.id, clientConn.tcpaddr.String())
2018-12-18 10:36:59 +03:00
}
func (p *Client) checkTimeoutConn() {
2019-10-19 17:36:17 +03:00
2019-10-20 11:27:03 +03:00
if p.tcpmode > 0 {
2019-10-19 17:36:17 +03:00
return
}
2019-10-31 16:05:48 +03:00
tmp := make(map[string]*ClientConn)
p.localIdToConnMap.Range(func(key, value interface{}) bool {
id := key.(string)
clientConn := value.(*ClientConn)
tmp[id] = clientConn
return true
})
2018-12-18 10:36:59 +03:00
now := time.Now()
2019-10-31 16:05:48 +03:00
for _, conn := range tmp {
2019-10-25 16:57:38 +03:00
diffrecv := now.Sub(conn.activeRecvTime)
diffsend := now.Sub(conn.activeSendTime)
if diffrecv > time.Second*(time.Duration(p.timeout)) || diffsend > time.Second*(time.Duration(p.timeout)) {
2018-12-18 11:56:40 +03:00
conn.close = true
}
}
2019-10-31 16:05:48 +03:00
for id, conn := range tmp {
2018-12-18 11:56:40 +03:00
if conn.close {
2019-10-16 16:09:21 +03:00
loggo.Info("close inactive conn %s %s", id, conn.ipaddr.String())
2019-10-30 14:28:00 +03:00
p.close(conn)
2018-12-18 10:36:59 +03:00
}
}
}
2018-12-19 10:00:30 +03:00
func (p *Client) ping() {
2019-10-31 16:05:48 +03:00
now := time.Now()
b, _ := now.MarshalBinary()
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, "", "", (uint32)(MyMsg_PING), b,
SEND_PROTO, RECV_PROTO, p.key,
0, 0, 0, 0, 0, 0,
0)
loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
p.sequence++
2018-12-19 10:00:30 +03:00
}
2018-12-19 10:50:21 +03:00
func (p *Client) showNet() {
2019-10-31 16:05:48 +03:00
p.localAddrToConnMapSize = 0
p.localIdToConnMap.Range(func(key, value interface{}) bool {
p.localAddrToConnMapSize++
return true
})
p.localIdToConnMapSize = 0
p.localIdToConnMap.Range(func(key, value interface{}) bool {
p.localIdToConnMapSize++
return true
})
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s %d/%dConnections",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.localAddrToConnMapSize, p.localIdToConnMapSize)
2018-12-19 10:50:21 +03:00
p.sendPacket = 0
p.recvPacket = 0
p.sendPacketSize = 0
p.recvPacketSize = 0
}
2019-10-28 11:33:44 +03:00
func (p *Client) AcceptSock5Conn(conn *net.TCPConn) {
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-28 11:33:44 +03:00
var err error = nil
if err = sock5Handshake(conn); err != nil {
loggo.Error("socks handshake: %s", err)
conn.Close()
return
}
_, addr, err := sock5GetRequest(conn)
if err != nil {
loggo.Error("error getting request: %s", err)
conn.Close()
return
}
// Sending connection established message immediately to client.
// This some round trip time for creating socks connection with the client.
// But if connection failed, the client will get connection reset error.
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
loggo.Error("send connection confirmation:", err)
conn.Close()
return
}
loggo.Info("accept new sock5 conn: %s", addr)
p.AcceptTcpConn(conn, addr)
}
2019-10-31 16:05:48 +03:00
func (p *Client) addClientConn(uuid string, addr string, clientConn *ClientConn) {
p.localAddrToConnMap.Store(addr, clientConn)
p.localIdToConnMap.Store(uuid, clientConn)
}
func (p *Client) getClientConnByAddr(addr string) *ClientConn {
ret, ok := p.localAddrToConnMap.Load(addr)
if !ok {
return nil
}
return ret.(*ClientConn)
}
func (p *Client) getClientConnById(uuid string) *ClientConn {
ret, ok := p.localIdToConnMap.Load(uuid)
if !ok {
return nil
}
return ret.(*ClientConn)
}
func (p *Client) deleteClientConn(uuid string, addr string) {
p.localIdToConnMap.Delete(uuid)
p.localAddrToConnMap.Delete(addr)
}