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
|
|
|
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 {
|
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
|
|
|
|
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 {
|
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-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)
|
2019-10-20 11:27:03 +03:00
|
|
|
loggo.Info("ping from %s %s %d %d %d", 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,
|
2019-10-20 11:27:03 +03:00
|
|
|
packet.rproto, -1, 0, p.key, packet.tcpmode)
|
2018-12-19 10:00:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:27:03 +03:00
|
|
|
loggo.Debug("processPacket %s %s %d", 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
|
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-20 11:27:03 +03:00
|
|
|
if packet.tcpmode > 0 {
|
2018-12-18 06:39:16 +03:00
|
|
|
|
2019-10-20 11:27:03 +03:00
|
|
|
addr := packet.target
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
catchQueue := make(chan *CatchMsg, packet.catch)
|
2018-12-23 02:02:57 +03:00
|
|
|
|
2019-10-20 11:27:03 +03:00
|
|
|
localConn = &ServerConn{tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
|
|
|
rproto: packet.rproto, catchQueue: catchQueue}
|
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 {
|
|
|
|
|
|
|
|
addr := packet.target
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
catchQueue := make(chan *CatchMsg, packet.catch)
|
|
|
|
|
|
|
|
localConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
|
|
|
rproto: packet.rproto, catchQueue: catchQueue}
|
|
|
|
|
|
|
|
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
|
|
|
|
localConn.catch = packet.catch
|
2018-12-18 10:36:59 +03:00
|
|
|
|
2018-12-23 02:02:57 +03:00
|
|
|
if packet.msgType == CATCH {
|
|
|
|
select {
|
2019-10-20 11:27:03 +03:00
|
|
|
case re := <-localConn.catchQueue:
|
2019-01-08 05:31:52 +03:00
|
|
|
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data,
|
2019-10-20 11:27:03 +03:00
|
|
|
re.conn.rproto, -1, 0, p.key, packet.tcpmode)
|
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 {
|
|
|
|
|
2019-10-20 11:27:03 +03:00
|
|
|
_, err := localConn.conn.Write(packet.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++
|
|
|
|
p.recvPacketSize += (uint64)(len(packet.data))
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
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 {
|
|
|
|
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n],
|
|
|
|
conn.rproto, -1, 0, p.key, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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],
|
2019-10-22 04:39:20 +03:00
|
|
|
conn.rproto, -1, 0, p.key, 0)
|
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 {
|
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-20 11:27:03 +03:00
|
|
|
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s",
|
2018-12-23 02:02:57 +03:00
|
|
|
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
|
|
|
}
|