pingtunnel/server.go

229 lines
4.9 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"
)
2019-01-08 05:31:52 +03:00
func NewServer(timeout int, key int) (*Server, error) {
2018-12-17 10:21:15 +03:00
return &Server{
2018-12-18 10:36:59 +03:00
timeout: timeout,
2019-01-08 05:31:52 +03:00
key: key,
2018-12-17 10:21:15 +03:00
}, nil
}
type Server struct {
2018-12-18 10:36:59 +03:00
timeout int
2019-01-08 05:31:52 +03:00
key 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-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
echoId int
echoSeq int
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-19 09:38:44 +03:00
rproto int
2018-12-23 02:02:57 +03:00
catch int
catchQueue chan *CatchMsg
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
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-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-19 10:50:21 +03:00
p.showNet()
2018-12-17 14:06:46 +03:00
case r := <-recv:
p.processPacket(r)
}
}
}
func (p *Server) processPacket(packet *Packet) {
2019-01-08 05:31:52 +03:00
if packet.key != p.key {
return
}
2018-12-23 01:08:26 +03:00
p.echoId = packet.echoId
p.echoSeq = packet.echoSeq
2018-12-19 10:00:30 +03:00
if packet.msgType == PING {
2018-12-23 02:02:57 +03:00
t := time.Time{}
t.UnmarshalBinary(packet.data)
fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
2019-01-08 05:31:52 +03:00
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data,
packet.rproto, -1, 0, p.key)
2018-12-19 10:00:30 +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-18 10:36:59 +03:00
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-23 02:02:57 +03:00
catchQueue := make(chan *CatchMsg, 1000)
udpConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
rproto: packet.rproto, catchQueue: catchQueue}
2018-12-17 14:06:46 +03:00
p.localConnMap[id] = udpConn
2018-12-23 02:02:57 +03:00
2018-12-17 14:06:46 +03:00
go p.Recv(udpConn, id, packet.src)
}
2018-12-18 10:36:59 +03:00
udpConn.activeTime = now
2018-12-23 02:02:57 +03:00
udpConn.catch = packet.catch
2018-12-18 10:36:59 +03:00
2018-12-23 02:02:57 +03:00
if packet.msgType == CATCH {
select {
case re := <-udpConn.catchQueue:
2019-01-08 05:31:52 +03:00
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data,
re.conn.rproto, -1, 0, p.key)
2018-12-23 02:02:57 +03:00
p.sendCatchPacket++
case <-time.After(time.Duration(1) * time.Millisecond):
}
p.recvCatchPacket++
2018-12-17 14:06:46 +03:00
return
}
2018-12-19 10:50:21 +03:00
2018-12-23 02:02:57 +03:00
if packet.msgType == DATA {
_, err := udpConn.conn.Write(packet.data)
if err != nil {
fmt.Printf("WriteToUDP Error %s\n", err)
udpConn.close = true
return
}
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 *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
2018-12-17 10:21:15 +03:00
for {
2018-12-23 02:02:57 +03:00
bytes := make([]byte, 2000)
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-23 02:02:57 +03:00
if conn.catch > 0 {
select {
case conn.catchQueue <- &CatchMsg{conn: conn, id: id, src: src, data: bytes[:n]}:
case <-time.After(time.Duration(10) * time.Millisecond):
}
} else {
2019-01-08 05:31:52 +03:00
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n],
conn.rproto, -1, 0, p.key)
2018-12-23 02:02:57 +03:00
}
2018-12-19 10:50:21 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(n)
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)
}
}
}
2018-12-19 10:50:21 +03:00
func (p *Server) showNet() {
2018-12-23 02:02:57 +03:00
fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s\n",
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
2018-12-19 10:50:21 +03:00
}