pingtunnel/client.go

414 lines
8.8 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
2019-10-16 16:09:21 +03:00
"github.com/esrrhs/go-engine/src/loggo"
"github.com/esrrhs/go-engine/src/rbuffergo"
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-16 16:09:21 +03:00
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, catch int, key int, tcpmode bool) (*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
if tcpmode {
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{
2018-12-21 10:52:34 +03:00
id: r.Intn(math.MaxInt16),
2018-12-17 10:21:15 +03:00
ipaddr: ipaddr,
2019-10-16 16:09:21 +03:00
tcpaddr: tcpaddr,
2018-12-17 10:21:15 +03:00
addr: addr,
2018-12-18 06:39:16 +03:00
ipaddrServer: ipaddrServer,
addrServer: server,
2018-12-18 10:36:59 +03:00
targetAddr: target,
timeout: timeout,
2018-12-19 09:38:44 +03:00
sproto: sproto,
rproto: rproto,
2018-12-23 02:02:57 +03:00
catch: catch,
2019-01-08 05:31:52 +03:00
key: key,
2019-10-16 16:09:21 +03:00
tcpmode: tcpmode,
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
2018-12-18 10:36:59 +03:00
timeout int
2018-12-19 09:38:44 +03:00
sproto int
rproto int
2018-12-23 02:02:57 +03:00
catch int
2019-01-08 05:31:52 +03:00
key int
2019-10-16 16:09:21 +03:00
tcpmode bool
2018-12-18 10:36:59 +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-23 01:08:26 +03:00
2018-12-23 02:02:57 +03:00
sendCatchPacket uint64
recvCatchPacket uint64
2018-12-18 10:36:59 +03:00
}
type ClientConn struct {
2018-12-22 12:58:07 +03:00
ipaddr *net.UDPAddr
2019-10-16 16:09:21 +03:00
tcpaddr *net.TCPAddr
2018-12-22 12:58:07 +03:00
id string
activeTime time.Time
close bool
2019-10-16 16:09:21 +03:00
sendb *rbuffergo.RBuffergo
recvb *rbuffergo.RBuffergo
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-16 16:09:21 +03:00
if p.tcpmode {
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-16 16:09:21 +03:00
if p.tcpmode {
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-23 02:02:57 +03:00
inter := 1000
if p.catch > 0 {
inter = 1000 / p.catch
if inter <= 0 {
inter = 1
}
}
intervalCatch := time.NewTicker(time.Millisecond * time.Duration(inter))
defer intervalCatch.Stop()
2018-12-23 01:08:26 +03:00
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()
case <-intervalCatch.C:
p.sendCatch()
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 {
p.tcplistenConn.SetDeadline(time.Now().Add(time.Millisecond * 100))
conn, err := p.tcplistenConn.AcceptTCP()
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
loggo.Error("Error accept tcp %s", err)
continue
}
}
}
go p.AcceptTcpConn(conn)
}
}
func (p *Client) AcceptTcpConn(conn *net.TCPConn) error {
now := time.Now()
uuid := UniqueId()
tcpsrcaddr := conn.RemoteAddr().(*net.TCPAddr)
sendb := rbuffergo.New(1024*1024, false)
recvb := rbuffergo.New(1024*1024, false)
cutsize := 800
sendwin := sendb.Capacity() / cutsize
clientConn := &ClientConn{tcpaddr: tcpsrcaddr, id: uuid, activeTime: now, close: false, sendb: sendb, recvb: recvb}
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)
sendwinmap := make(map[string]*ClientConn)
for {
left := sendb.Capacity() - sendb.Size()
if left >= len(bytes) {
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, srcaddr, err := p.conn.ReadFrom(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
loggo.Error("Error read tcp %s %s", srcaddr.String(), err)
break
}
}
}
if n > 0 {
sendb.Write(bytes[:n])
}
}
clientConn.activeTime = now
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n],
p.sproto, p.rproto, p.catch, p.key)
p.sequence++
p.sendPacket++
p.sendPacketSize += (uint64)(n)
}
}
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 {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
2019-10-16 16:09:21 +03:00
loggo.Error("Error read udp %s", err)
2018-12-17 14:06:46 +03:00
continue
}
}
}
2018-12-18 10:36:59 +03:00
now := time.Now()
clientConn := p.localAddrToConnMap[srcaddr.String()]
if clientConn == nil {
uuid := UniqueId()
2018-12-18 11:56:40 +03:00
clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeTime: 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
}
2018-12-18 10:36:59 +03:00
clientConn.activeTime = now
2019-01-08 05:31:52 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n],
p.sproto, p.rproto, p.catch, p.key)
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
2018-12-20 09:25:02 +03:00
if packet.rproto >= 0 {
2018-12-20 09:18:08 +03:00
return
}
2019-01-08 05:31:52 +03:00
if packet.key != p.key {
return
}
2019-01-08 06:13:41 +03:00
if packet.echoId != p.id {
return
}
2018-12-19 10:05:22 +03:00
if packet.msgType == PING {
t := time.Time{}
t.UnmarshalBinary(packet.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-16 16:09:21 +03:00
//loggo.Debug("processPacket %s %s %d", packet.id, packet.src.String(), len(packet.data))
2018-12-17 10:21:15 +03:00
2018-12-18 10:36:59 +03:00
clientConn := p.localIdToConnMap[packet.id]
if clientConn == nil {
2019-10-16 16:09:21 +03:00
//loggo.Debug("processPacket no conn %s ", packet.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()
clientConn.activeTime = now
2018-12-23 02:02:57 +03:00
if packet.msgType == CATCH {
p.recvCatchPacket++
}
2018-12-17 14:06:46 +03:00
_, err := p.listenConn.WriteToUDP(packet.data, addr)
2018-12-17 10:21:15 +03:00
if err != nil {
2019-10-16 16:09:21 +03:00
loggo.Error("WriteToUDP Error read udp %s", err)
2018-12-18 11:56:40 +03:00
clientConn.close = true
2018-12-17 14:06:46 +03:00
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++
p.recvPacketSize += (uint64)(len(packet.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() {
now := time.Now()
2018-12-18 11:56:40 +03:00
for _, conn := range p.localIdToConnMap {
2018-12-18 10:36:59 +03:00
diff := now.Sub(conn.activeTime)
if diff > 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-01-08 05:31:52 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b,
p.sproto, p.rproto, p.catch, p.key)
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-16 16:09:21 +03:00
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s",
2018-12-23 02:02:57 +03:00
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.sendCatchPacket, p.recvCatchPacket)
2018-12-19 10:50:21 +03:00
p.sendPacket = 0
p.recvPacket = 0
p.sendPacketSize = 0
p.recvPacketSize = 0
2018-12-23 02:02:57 +03:00
p.sendCatchPacket = 0
p.recvCatchPacket = 0
}
func (p *Client) sendCatch() {
if p.catch > 0 {
for _, conn := range p.localIdToConnMap {
2019-01-08 05:31:52 +03:00
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(CATCH), make([]byte, 0),
p.sproto, p.rproto, p.catch, p.key)
2018-12-23 02:02:57 +03:00
p.sequence++
p.sendCatchPacket++
}
}
2018-12-19 10:50:21 +03:00
}