pingtunnel/client.go

252 lines
5.3 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
"fmt"
"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
)
2018-12-19 09:38:44 +03:00
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int) (*Client, error) {
2018-12-17 10:21:15 +03:00
2018-12-17 14:06:46 +03:00
ipaddr, err := net.ResolveUDPAddr("udp", addr)
2018-12-17 10:21:15 +03:00
if err != nil {
return nil, err
}
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),
r: r,
2018-12-17 10:21:15 +03:00
ipaddr: ipaddr,
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-17 10:21:15 +03:00
}, nil
}
type Client struct {
2018-12-21 10:52:34 +03:00
id int
r *rand.Rand
2018-12-18 10:36:59 +03:00
timeout int
2018-12-19 09:38:44 +03:00
sproto int
rproto int
2018-12-18 10:36:59 +03:00
2018-12-17 14:06:46 +03:00
ipaddr *net.UDPAddr
2018-12-17 10:21:15 +03:00
addr string
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
conn *icmp.PacketConn
2018-12-17 14:06:46 +03:00
listenConn *net.UDPConn
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 {
ipaddr *net.UDPAddr
id string
activeTime time.Time
2018-12-18 11:56:40 +03:00
close bool
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 {
fmt.Printf("Error listening for ICMP packets: %s\n", err.Error())
return
}
defer conn.Close()
p.conn = conn
2018-12-17 14:06:46 +03:00
listener, err := net.ListenUDP("udp", p.ipaddr)
2018-12-17 10:21:15 +03:00
if err != nil {
2018-12-17 14:06:46 +03:00
fmt.Printf("Error listening for udp packets: %s\n", err.Error())
2018-12-17 10:21:15 +03:00
return
}
2018-12-17 14:06:46 +03:00
defer listener.Close()
2018-12-17 10:21:15 +03:00
p.listenConn = listener
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
2018-12-17 14:06:46 +03:00
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-19 10:58:58 +03:00
p.ping()
2018-12-20 09:15:36 +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
}
}
}
2018-12-17 14:06:46 +03:00
func (p *Client) Accept() error {
2018-12-17 10:21:15 +03:00
2018-12-17 14:06:46 +03:00
fmt.Println("client waiting local accept")
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 {
fmt.Printf("Error read udp %s\n", err)
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
fmt.Printf("client accept new local %s %s\n", uuid, srcaddr.String())
2018-12-17 14:06:46 +03:00
}
2018-12-18 10:36:59 +03:00
clientConn.activeTime = now
2018-12-21 10:52:34 +03:00
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto)
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
}
2018-12-19 10:05:22 +03:00
if packet.msgType == PING {
t := time.Time{}
t.UnmarshalBinary(packet.data)
d := time.Now().Sub(t)
2018-12-19 10:37:04 +03:00
fmt.Printf("pong from %s %s\n", packet.src.String(), d.String())
2018-12-19 10:05:22 +03:00
return
}
2018-12-19 10:54:54 +03:00
//fmt.Printf("processPacket %s %s %d\n", 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 {
fmt.Printf("processPacket no conn %s \n", 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-17 14:06:46 +03:00
_, err := p.listenConn.WriteToUDP(packet.data, addr)
2018-12-17 10:21:15 +03:00
if err != nil {
2018-12-17 14:06:46 +03:00
fmt.Printf("WriteToUDP Error read udp %s\n", 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
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 {
2018-12-18 10:36:59 +03:00
fmt.Printf("close inactive conn %s %s\n", id, conn.ipaddr.String())
p.Close(conn)
}
}
}
2018-12-19 10:00:30 +03:00
func (p *Client) ping() {
2018-12-20 09:15:36 +03:00
if p.sendPacket == 0 && p.recvPacket == 0 {
now := time.Now()
b, _ := now.MarshalBinary()
2018-12-21 10:52:34 +03:00
sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto)
2018-12-20 09:15:36 +03:00
fmt.Printf("ping %s %s %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto)
}
2018-12-19 10:00:30 +03:00
}
2018-12-19 10:50:21 +03:00
func (p *Client) showNet() {
2018-12-19 10:58:58 +03:00
fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s\n", 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
}