pingtunnel/server.go

263 lines
5.7 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
2019-10-20 11:27:03 +03:00
"github.com/esrrhs/go-engine/src/loggo"
2018-12-17 10:21:15 +03:00
"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
echoId int
echoSeq int
2018-12-17 10:21:15 +03:00
}
2018-12-18 10:36:59 +03:00
type ServerConn struct {
2019-10-20 11:27:03 +03:00
ipaddrTarget *net.UDPAddr
conn *net.UDPConn
tcpaddrTarget *net.TCPAddr
tcpconn *net.TCPConn
id string
activeTime time.Time
close bool
rproto int
2018-12-17 10:21:15 +03:00
}
func (p *Server) Run() {
conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
2019-10-20 11:27:03 +03:00
loggo.Error("Error listening for ICMP packets: %s", err.Error())
2018-12-17 10:21:15 +03:00
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-10-22 16:04:25 +03:00
if packet.my.Key != (int32)(p.key) {
2019-01-08 05:31:52 +03:00
return
}
2018-12-23 01:08:26 +03:00
p.echoId = packet.echoId
p.echoSeq = packet.echoSeq
2019-10-22 16:04:25 +03:00
if packet.my.Type == (int32)(MyMsg_PING) {
2018-12-23 02:02:57 +03:00
t := time.Time{}
2019-10-22 16:04:25 +03:00
t.UnmarshalBinary(packet.my.Data)
loggo.Info("ping from %s %s %d %d %d", packet.src.String(), t.String(), packet.my.Rproto, packet.echoId, packet.echoSeq)
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(MyMsg_PING), packet.my.Data,
2019-10-22 16:27:21 +03:00
(int)(packet.my.Rproto), -1, p.key,
2019-10-22 16:04:25 +03:00
0, 0, 0, 0)
2018-12-19 10:00:30 +03:00
return
}
2019-10-22 16:04:25 +03:00
loggo.Debug("processPacket %s %s %d", packet.my.Id, packet.src.String(), len(packet.my.Data))
2018-12-18 10:36:59 +03:00
now := time.Now()
2018-12-17 14:06:46 +03:00
2019-10-22 16:04:25 +03:00
id := packet.my.Id
2019-10-20 11:27:03 +03:00
localConn := p.localConnMap[id]
if localConn == nil {
2018-12-18 06:39:16 +03:00
2019-10-22 16:04:25 +03:00
if packet.my.Tcpmode > 0 {
2018-12-18 06:39:16 +03:00
2019-10-22 16:04:25 +03:00
addr := packet.my.Target
2019-10-20 11:27:03 +03:00
ipaddrTarget, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
loggo.Error("Error ResolveUDPAddr for tcp addr: %s %s", addr, err.Error())
return
}
targetConn, err := net.DialTCP("tcp", nil, ipaddrTarget)
if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error())
return
}
localConn = &ServerConn{tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
2019-10-22 16:27:21 +03:00
rproto: (int)(packet.my.Rproto)}
2018-12-23 02:02:57 +03:00
2019-10-20 11:27:03 +03:00
p.localConnMap[id] = localConn
2018-12-23 02:02:57 +03:00
2019-10-20 11:27:03 +03:00
go p.RecvTCP(localConn, id, packet.src)
2018-12-23 02:02:57 +03:00
2019-10-20 11:27:03 +03:00
} else {
2019-10-22 16:04:25 +03:00
addr := packet.my.Target
2019-10-20 11:27:03 +03:00
ipaddrTarget, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
loggo.Error("Error ResolveUDPAddr for udp addr: %s %s", addr, err.Error())
return
}
targetConn, err := net.DialUDP("udp", nil, ipaddrTarget)
if err != nil {
loggo.Error("Error listening for udp packets: %s", err.Error())
return
}
localConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
2019-10-22 16:27:21 +03:00
rproto: (int)(packet.my.Rproto)}
2019-10-20 11:27:03 +03:00
p.localConnMap[id] = localConn
go p.Recv(localConn, id, packet.src)
}
2018-12-17 14:06:46 +03:00
}
2019-10-20 11:27:03 +03:00
localConn.activeTime = now
2018-12-19 10:50:21 +03:00
2019-10-22 16:04:25 +03:00
if packet.my.Type == (int32)(MyMsg_DATA) {
2018-12-23 02:02:57 +03:00
2019-10-22 16:04:25 +03:00
_, err := localConn.conn.Write(packet.my.Data)
2018-12-23 02:02:57 +03:00
if err != nil {
2019-10-20 11:27:03 +03:00
loggo.Error("WriteToUDP Error %s", err)
localConn.close = true
2018-12-23 02:02:57 +03:00
return
}
p.recvPacket++
2019-10-22 16:04:25 +03:00
p.recvPacketSize += (uint64)(len(packet.my.Data))
2018-12-23 02:02:57 +03:00
}
2018-12-17 10:21:15 +03:00
}
2019-10-22 04:39:20 +03:00
func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
loggo.Info("server waiting target response %s -> %s %s", conn.tcpaddrTarget.String(), conn.id, conn.tcpconn.LocalAddr().String())
for {
bytes := make([]byte, 2000)
conn.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, _, err := conn.conn.ReadFromUDP(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
loggo.Error("ReadFromUDP Error read udp %s", err)
conn.close = true
return
}
}
}
now := time.Now()
conn.activeTime = now
2019-10-22 16:27:21 +03:00
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
conn.rproto, -1, p.key,
0, 0, 0, 0)
2019-10-22 04:39:20 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(n)
}
}
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
2019-10-20 11:27:03 +03:00
loggo.Info("server waiting target response %s -> %s %s", 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 {
2019-10-20 11:27:03 +03:00
loggo.Error("ReadFromUDP Error read udp %s", 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
2019-10-22 16:27:21 +03:00
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
conn.rproto, -1, p.key,
0, 0, 0, 0)
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 {
2019-10-20 11:27:03 +03:00
loggo.Info("close inactive conn %s %s", id, conn.ipaddrTarget.String())
2018-12-18 10:36:59 +03:00
p.Close(conn)
}
}
}
2018-12-19 10:50:21 +03:00
func (p *Server) showNet() {
2019-10-22 16:27:21 +03:00
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s",
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
}