pingtunnel/client.go

202 lines
4.0 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
"fmt"
"golang.org/x/net/icmp"
"net"
2018-12-17 14:06:46 +03:00
"time"
2018-12-17 10:21:15 +03:00
)
2018-12-18 10:36:59 +03:00
func NewClient(addr string, server string, target string, timeout 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
}
return &Client{
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-17 10:21:15 +03:00
}, nil
}
type Client struct {
2018-12-18 10:36:59 +03:00
timeout int
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
}
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-17 14:06:46 +03:00
recv := make(chan *Packet, 1000)
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-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
sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[: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-18 13:11:39 +03:00
if packet.data == nil {
2018-12-18 13:05:08 +03:00
return
}
2018-12-18 10:36:59 +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-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)
}
}
}