pingtunnel/client.go

508 lines
12 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"
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,
tcpmode int, tcpmode_buffersize int, tcpmode_maxwin int, tcpmode_resend_timems 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-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,
2018-12-17 10:21:15 +03:00
}, nil
}
type Client struct {
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-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
2018-12-18 10:36:59 +03:00
localAddrToConnMap map[string]*ClientConn
localIdToConnMap map[string]*ClientConn
2018-12-19 10:50:21 +03:00
sendPacket uint64
recvPacket uint64
sendPacketSize uint64
recvPacketSize uint64
2018-12-18 10:36:59 +03:00
}
type ClientConn struct {
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
}
func (p *Client) Run() {
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())
2018-12-17 10:21:15 +03:00
return
}
defer conn.Close()
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())
return
}
defer tcplistenConn.Close()
p.tcplistenConn = tcplistenConn
} else {
listener, err := net.ListenUDP("udp", p.ipaddr)
if err != nil {
loggo.Error("Error listening for udp packets: %s", err.Error())
return
}
defer listener.Close()
p.listenConn = listener
2018-12-17 10:21:15 +03:00
}
2018-12-18 10:36:59 +03:00
p.localAddrToConnMap = make(map[string]*ClientConn)
p.localIdToConnMap = make(map[string]*ClientConn)
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)
2018-12-17 14:06:46 +03:00
go recvICMP(*p.conn, recv)
2018-12-17 10:21:15 +03:00
2018-12-18 10:36:59 +03:00
interval := time.NewTicker(time.Second)
defer interval.Stop()
2018-12-17 10:21:15 +03:00
for {
2018-12-17 14:06:46 +03:00
select {
2018-12-18 10:36:59 +03:00
case <-interval.C:
p.checkTimeoutConn()
2018-12-23 01:08:26 +03:00
p.ping()
2018-12-23 02:02:57 +03:00
p.showNet()
2018-12-17 14:06:46 +03:00
case r := <-recv:
p.processPacket(r)
2018-12-17 10:21:15 +03:00
}
}
}
2019-10-16 16:09:21 +03:00
func (p *Client) AcceptTcp() error {
loggo.Info("client waiting local accept tcp")
for {
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 {
go p.AcceptTcpConn(conn)
}
2019-10-16 16:09:21 +03:00
}
}
2019-10-19 17:36:17 +03:00
func (p *Client) AcceptTcpConn(conn *net.TCPConn) {
2019-10-16 16:09:21 +03:00
uuid := UniqueId()
tcpsrcaddr := conn.RemoteAddr().(*net.TCPAddr)
2019-10-19 17:36:17 +03:00
fm := NewFrameMgr(p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
2019-10-16 16:09:21 +03:00
2019-10-24 16:42:46 +03:00
now := time.Now()
2019-10-25 16:57:38 +03:00
clientConn := &ClientConn{tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false,
2019-10-19 17:36:17 +03:00
fm: fm}
2019-10-16 16:09:21 +03:00
p.localAddrToConnMap[tcpsrcaddr.String()] = clientConn
p.localIdToConnMap[uuid] = clientConn
loggo.Info("client accept new local tcp %s %s", uuid, tcpsrcaddr.String())
bytes := make([]byte, 10240)
2019-10-26 07:01:30 +03:00
tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now()
2019-10-16 16:09:21 +03:00
for {
2019-10-26 07:01:30 +03:00
now := time.Now()
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-19 17:36:17 +03:00
clientConn.fm.WriteSendBuffer(bytes[:n])
2019-10-26 12:05:08 +03:00
tcpActiveRecvTime = now
2019-10-27 07:52:02 +03:00
} else {
time.Sleep(time.Millisecond * 10)
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 {
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++
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
SEND_PROTO, RECV_PROTO, p.key,
2019-10-26 07:07:20 +03:00
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems,
p.timeout)
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 {
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
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()
for {
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++
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
SEND_PROTO, RECV_PROTO, p.key,
2019-10-26 07:07:20 +03:00
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems,
p.timeout)
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()
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-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
for {
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()
clientConn := p.localAddrToConnMap[srcaddr.String()]
if clientConn == nil {
uuid := UniqueId()
2019-10-25 16:57:38 +03:00
clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false}
2018-12-18 10:36:59 +03:00
p.localAddrToConnMap[srcaddr.String()] = clientConn
p.localIdToConnMap[uuid] = 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-26 07:07:20 +03:00
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems,
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
}
}
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())
2018-12-19 10:05:22 +03:00
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-22 16:04:25 +03:00
clientConn := p.localIdToConnMap[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
addr := clientConn.ipaddr
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 {
_, 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
func (p *Client) Close(clientConn *ClientConn) {
if p.localIdToConnMap[clientConn.id] != nil {
delete(p.localIdToConnMap, clientConn.id)
delete(p.localAddrToConnMap, clientConn.ipaddr.String())
}
}
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
}
2018-12-18 10:36:59 +03:00
now := time.Now()
2018-12-18 11:56:40 +03:00
for _, conn := range p.localIdToConnMap {
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
}
}
for id, conn := range p.localIdToConnMap {
if conn.close {
2019-10-16 16:09:21 +03:00
loggo.Info("close inactive conn %s %s", id, conn.ipaddr.String())
2018-12-18 10:36:59 +03:00
p.Close(conn)
}
}
}
2018-12-19 10:00:30 +03:00
func (p *Client) ping() {
2018-12-22 13:42:41 +03:00
if p.sendPacket == 0 {
now := time.Now()
b, _ := now.MarshalBinary()
2019-10-22 16:04:25 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(MyMsg_PING), b,
2019-10-22 16:27:21 +03:00
SEND_PROTO, RECV_PROTO, p.key,
2019-10-26 07:07:20 +03:00
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems,
p.timeout)
2019-10-16 16:09:21 +03:00
loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
2018-12-22 13:42:41 +03:00
p.sequence++
}
2018-12-19 10:00:30 +03:00
}
2018-12-19 10:50:21 +03:00
func (p *Client) showNet() {
2019-10-22 16:27:21 +03:00
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024)
2018-12-19 10:50:21 +03:00
p.sendPacket = 0
p.recvPacket = 0
p.sendPacketSize = 0
p.recvPacketSize = 0
}