pingtunnel/server.go

183 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"
"time"
)
2018-12-19 09:38:44 +03:00
func NewServer(timeout 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-17 10:21:15 +03:00
}, nil
}
type Server struct {
2018-12-18 10:36:59 +03:00
timeout int
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-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-21 11:09:54 +03:00
echoId int
echoSeq int
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) {
2018-12-19 10:00:30 +03:00
if packet.msgType == PING {
2018-12-19 10:02:37 +03:00
t := time.Time{}
t.UnmarshalBinary(packet.data)
2018-12-21 11:09:54 +03:00
fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1)
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-19 09:38:44 +03:00
udpConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false, rproto: packet.rproto}
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-21 11:09:54 +03:00
udpConn.echoId = packet.echoId
udpConn.echoSeq = packet.echoSeq
2018-12-18 10:36:59 +03:00
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-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 *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-21 11:09:54 +03:00
sendICMP(conn.echoId, conn.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1)
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-22 12:58:07 +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
}