pingtunnel/server.go

151 lines
3.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"
"time"
)
2018-12-19 06:48:48 +03:00
func NewServer(timeout int, proto int) (*Server, error) {
2018-12-17 10:21:15 +03:00
return &Server{
2018-12-18 10:36:59 +03:00
timeout: timeout,
2018-12-19 06:48:48 +03:00
proto: proto,
2018-12-17 10:21:15 +03:00
}, nil
}
type Server struct {
2018-12-18 10:36:59 +03:00
timeout int
2018-12-19 06:48:48 +03:00
proto int
2018-12-18 10:36:59 +03:00
2018-12-17 14:06:46 +03:00
conn *icmp.PacketConn
2018-12-18 10:36:59 +03:00
localConnMap map[string]*ServerConn
2018-12-17 10:21:15 +03:00
}
2018-12-18 10:36:59 +03:00
type ServerConn struct {
2018-12-18 06:39:16 +03:00
ipaddrTarget *net.UDPAddr
conn *net.UDPConn
2018-12-18 10:36:59 +03:00
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 *Server) Run() {
conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
fmt.Printf("Error listening for ICMP packets: %s\n", err.Error())
return
}
p.conn = conn
2018-12-18 10:36:59 +03:00
p.localConnMap = make(map[string]*ServerConn)
2018-12-17 14:06:46 +03:00
recv := make(chan *Packet, 1000)
go recvICMP(*p.conn, recv)
2018-12-18 10:36:59 +03:00
interval := time.NewTicker(time.Second)
defer interval.Stop()
2018-12-17 14:06:46 +03:00
for {
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)
}
}
}
func (p *Server) processPacket(packet *Packet) {
2018-12-18 10:36:59 +03:00
fmt.Printf("processPacket %s %s %d\n", packet.id, packet.src.String(), len(packet.data))
now := time.Now()
2018-12-17 14:06:46 +03:00
id := packet.id
udpConn := p.localConnMap[id]
if udpConn == nil {
2018-12-18 06:39:16 +03:00
2018-12-18 10:36:59 +03:00
addr := packet.target
2018-12-18 06:39:16 +03:00
ipaddrTarget, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
fmt.Printf("Error ResolveUDPAddr for udp addr: %s %s\n", addr, err.Error())
return
}
targetConn, err := net.DialUDP("udp", nil, ipaddrTarget)
2018-12-17 14:06:46 +03:00
if err != nil {
fmt.Printf("Error listening for udp packets: %s\n", err.Error())
return
}
2018-12-18 11:56:40 +03:00
udpConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false}
2018-12-17 14:06:46 +03:00
p.localConnMap[id] = udpConn
go p.Recv(udpConn, id, packet.src)
}
2018-12-18 10:36:59 +03:00
udpConn.activeTime = now
2018-12-18 06:39:16 +03:00
_, err := udpConn.conn.Write(packet.data)
2018-12-17 14:06:46 +03:00
if err != nil {
2018-12-18 06:39:16 +03:00
fmt.Printf("WriteToUDP Error %s\n", err)
2018-12-18 11:56:40 +03:00
udpConn.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 *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
2018-12-17 14:06:46 +03:00
2018-12-18 10:36:59 +03:00
fmt.Printf("server waiting target response %s -> %s %s\n", conn.ipaddrTarget.String(), conn.id, conn.conn.LocalAddr().String())
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
conn.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, _, err := conn.conn.ReadFromUDP(bytes)
2018-12-17 10:21:15 +03:00
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
2018-12-17 14:06:46 +03:00
fmt.Printf("ReadFromUDP Error read udp %s\n", err)
2018-12-18 11:56:40 +03:00
conn.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
now := time.Now()
conn.activeTime = now
2018-12-19 06:48:48 +03:00
sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], p.proto)
2018-12-18 06:39:16 +03:00
}
}
2018-12-18 10:36:59 +03:00
func (p *Server) Close(conn *ServerConn) {
2018-12-18 06:39:16 +03:00
if p.localConnMap[conn.id] != nil {
conn.conn.Close()
2018-12-18 10:36:59 +03:00
delete(p.localConnMap, conn.id)
2018-12-17 10:21:15 +03:00
}
}
2018-12-18 10:36:59 +03:00
func (p *Server) checkTimeoutConn() {
now := time.Now()
2018-12-18 11:56:40 +03:00
for _, conn := range p.localConnMap {
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.localConnMap {
if conn.close {
2018-12-18 10:36:59 +03:00
fmt.Printf("close inactive conn %s %s\n", id, conn.ipaddrTarget.String())
p.Close(conn)
}
}
}