pingtunnel/server.go

453 lines
11 KiB
Go
Raw Normal View History

2018-12-17 10:21:15 +03:00
package pingtunnel
import (
2019-10-26 07:01:30 +03:00
"github.com/esrrhs/go-engine/src/common"
2019-10-20 11:27:03 +03:00
"github.com/esrrhs/go-engine/src/loggo"
2019-10-23 15:36:13 +03:00
"github.com/golang/protobuf/proto"
2018-12-17 10:21:15 +03:00
"golang.org/x/net/icmp"
"net"
2019-10-30 14:45:19 +03:00
"sync"
2018-12-17 10:21:15 +03:00
"time"
)
2019-10-26 07:01:30 +03:00
func NewServer(key int) (*Server, error) {
2018-12-17 10:21:15 +03:00
return &Server{
2019-10-30 14:14:27 +03:00
exit: false,
key: key,
2018-12-17 10:21:15 +03:00
}, nil
}
type Server struct {
2019-10-30 14:45:19 +03:00
exit bool
key int
interval *time.Ticker
workResultLock sync.WaitGroup
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-26 07:01:30 +03:00
timeout int
2019-10-25 16:57:38 +03:00
ipaddrTarget *net.UDPAddr
conn *net.UDPConn
tcpaddrTarget *net.TCPAddr
tcpconn *net.TCPConn
id string
activeRecvTime time.Time
activeSendTime time.Time
close bool
rproto int
fm *FrameMgr
tcpmode int
2018-12-17 10:21:15 +03:00
}
2019-10-30 14:14:27 +03:00
func (p *Server) Run() error {
2018-12-17 10:21:15 +03:00
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())
2019-10-30 14:14:27 +03:00
return err
2018-12-17 10:21:15 +03:00
}
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)
2019-10-30 14:45:19 +03:00
go recvICMP(&p.workResultLock, &p.exit, *p.conn, recv)
2018-12-17 14:06:46 +03:00
2019-10-30 14:28:00 +03:00
p.interval = time.NewTicker(time.Second)
2018-12-18 10:36:59 +03:00
2019-10-30 14:14:27 +03:00
go func() {
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-30 14:14:27 +03:00
for !p.exit {
select {
2019-10-30 14:28:00 +03:00
case <-p.interval.C:
2019-10-30 14:14:27 +03:00
p.checkTimeoutConn()
p.showNet()
case r := <-recv:
p.processPacket(r)
}
2018-12-17 14:06:46 +03:00
}
2019-10-30 14:14:27 +03:00
}()
return nil
2018-12-17 14:06:46 +03:00
}
2019-10-30 14:28:00 +03:00
func (p *Server) Stop() {
p.exit = true
2019-10-30 14:45:19 +03:00
p.workResultLock.Wait()
2019-10-30 14:28:00 +03:00
p.conn.Close()
p.interval.Stop()
}
2018-12-17 14:06:46 +03:00
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-28 06:58:01 +03:00
0, 0, 0, 0, 0, 0,
2019-10-26 07:07:20 +03:00
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
2019-10-31 15:30:38 +03:00
c, err := net.DialTimeout("tcp", addr, time.Second)
2019-10-20 11:27:03 +03:00
if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error())
return
}
2019-10-31 15:30:38 +03:00
targetConn := c.(*net.TCPConn)
ipaddrTarget := targetConn.RemoteAddr().(*net.TCPAddr)
2019-10-20 11:27:03 +03:00
2019-10-28 06:58:01 +03:00
fm := NewFrameMgr((int)(packet.my.TcpmodeBuffersize), (int)(packet.my.TcpmodeMaxwin), (int)(packet.my.TcpmodeResendTimems), (int)(packet.my.TcpmodeCompress),
(int)(packet.my.TcpmodeStat))
2019-10-23 15:36:13 +03:00
2019-10-26 07:01:30 +03:00
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
2019-10-23 15:36:13 +03:00
rproto: (int)(packet.my.Rproto), fm: fm, tcpmode: (int)(packet.my.Tcpmode)}
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
2019-10-31 15:30:38 +03:00
c, err := net.DialTimeout("udp", addr, time.Second)
2019-10-20 11:27:03 +03:00
if err != nil {
2019-10-31 15:30:38 +03:00
loggo.Error("Error listening for tcp packets: %s", err.Error())
2019-10-20 11:27:03 +03:00
return
}
2019-10-31 15:30:38 +03:00
targetConn := c.(*net.UDPConn)
ipaddrTarget := targetConn.RemoteAddr().(*net.UDPAddr)
2019-10-20 11:27:03 +03:00
2019-10-26 07:01:30 +03:00
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
2019-10-23 15:36:13 +03:00
rproto: (int)(packet.my.Rproto), tcpmode: (int)(packet.my.Tcpmode)}
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-25 16:57:38 +03:00
localConn.activeRecvTime = 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-23 15:36:13 +03:00
if packet.my.Tcpmode > 0 {
f := &Frame{}
err := proto.Unmarshal(packet.my.Data, f)
if err != nil {
loggo.Error("Unmarshal tcp Error %s", err)
return
}
localConn.fm.OnRecvFrame(f)
} else {
_, err := localConn.conn.Write(packet.my.Data)
if err != nil {
2019-10-26 07:01:30 +03:00
loggo.Info("WriteToUDP Error %s", err)
2019-10-23 15:36:13 +03:00
localConn.close = true
return
}
2018-12-23 02:02:57 +03:00
}
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) {
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
2019-10-22 04:39:20 +03:00
loggo.Info("server waiting target response %s -> %s %s", conn.tcpaddrTarget.String(), conn.id, conn.tcpconn.LocalAddr().String())
2019-10-27 12:17:47 +03:00
loggo.Info("start wait remote connect tcp %s %s", conn.id, conn.tcpaddrTarget.String())
startConnectTime := time.Now()
2019-10-30 14:14:27 +03:00
for !p.exit {
2019-10-27 13:16:57 +03:00
if conn.fm.IsConnected() {
2019-10-27 12:17:47 +03:00
break
}
conn.fm.Update()
sendlist := conn.fm.getSendList()
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, _ := proto.Marshal(f)
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), mb,
conn.rproto, -1, p.key,
2019-10-28 06:58:01 +03:00
0, 0, 0, 0, 0, 0,
2019-10-27 12:17:47 +03:00
0)
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
}
time.Sleep(time.Millisecond * 10)
now := time.Now()
diffclose := now.Sub(startConnectTime)
if diffclose > time.Second*(time.Duration(conn.timeout)) {
loggo.Info("can not connect remote tcp %s %s", conn.id, conn.tcpaddrTarget.String())
2019-10-30 14:28:00 +03:00
p.close(conn)
2019-10-27 12:17:47 +03:00
return
}
}
loggo.Info("remote connected tcp %s %s", conn.id, conn.tcpaddrTarget.String())
2019-10-23 15:36:13 +03:00
bytes := make([]byte, 10240)
2019-10-22 04:39:20 +03:00
2019-10-26 07:01:30 +03:00
tcpActiveRecvTime := time.Now()
tcpActiveSendTime := time.Now()
2019-10-30 14:14:27 +03:00
for !p.exit {
2019-10-26 07:01:30 +03:00
now := time.Now()
2019-10-27 07:55:40 +03:00
sleep := true
2019-10-26 07:01:30 +03:00
left := common.MinOfInt(conn.fm.GetSendBufferLeft(), len(bytes))
if left > 0 {
2019-10-27 07:32:31 +03:00
conn.tcpconn.SetReadDeadline(time.Now().Add(time.Millisecond * 1))
2019-10-26 07:01:30 +03:00
n, err := conn.tcpconn.Read(bytes[0:left])
2019-10-23 15:36:13 +03:00
if err != nil {
2019-10-24 16:42:46 +03:00
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error read tcp %s %s %s", conn.id, conn.tcpaddrTarget.String(), err)
conn.fm.Close()
2019-10-24 16:42:46 +03:00
break
2019-10-22 04:39:20 +03:00
}
}
2019-10-23 15:36:13 +03:00
if n > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-23 15:36:13 +03:00
conn.fm.WriteSendBuffer(bytes[:n])
2019-10-26 07:01:30 +03:00
tcpActiveRecvTime = now
2019-10-23 15:36:13 +03:00
}
2019-10-22 04:39:20 +03:00
}
2019-10-23 15:36:13 +03:00
conn.fm.Update()
sendlist := conn.fm.getSendList()
2019-10-26 07:01:30 +03:00
if sendlist.Len() > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-26 07:01:30 +03:00
conn.activeSendTime = now
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, err := proto.Marshal(f)
if err != nil {
loggo.Error("Error tcp Marshal %s %s %s", conn.id, conn.tcpaddrTarget.String(), err)
continue
}
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), mb,
conn.rproto, -1, p.key,
2019-10-28 06:58:01 +03:00
0, 0, 0, 0, 0, 0,
2019-10-26 07:07:20 +03:00
0)
2019-10-26 07:01:30 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
2019-10-23 15:36:13 +03:00
}
}
2019-10-24 16:42:46 +03:00
if conn.fm.GetRecvBufferSize() > 0 {
2019-10-27 07:55:40 +03:00
sleep = false
2019-10-24 16:42:46 +03:00
rr := conn.fm.GetRecvReadLineBuffer()
2019-10-27 07:32:31 +03:00
conn.tcpconn.SetWriteDeadline(time.Now().Add(time.Millisecond * 1))
2019-10-24 16:42:46 +03:00
n, err := conn.tcpconn.Write(rr)
if err != nil {
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("Error write tcp %s %s %s", conn.id, conn.tcpaddrTarget.String(), err)
conn.fm.Close()
2019-10-24 16:42:46 +03:00
break
}
}
if n > 0 {
conn.fm.SkipRecvBuffer(n)
2019-10-26 07:01:30 +03:00
tcpActiveSendTime = now
2019-10-24 16:42:46 +03:00
}
}
2019-10-25 16:57:38 +03:00
2019-10-27 07:55:40 +03:00
if sleep {
time.Sleep(time.Millisecond * 10)
}
2019-10-25 16:57:38 +03:00
diffrecv := now.Sub(conn.activeRecvTime)
diffsend := now.Sub(conn.activeSendTime)
2019-10-26 07:01:30 +03:00
tcpdiffrecv := now.Sub(tcpActiveRecvTime)
tcpdiffsend := now.Sub(tcpActiveSendTime)
if diffrecv > time.Second*(time.Duration(conn.timeout)) || diffsend > time.Second*(time.Duration(conn.timeout)) ||
tcpdiffrecv > time.Second*(time.Duration(conn.timeout)) || tcpdiffsend > time.Second*(time.Duration(conn.timeout)) {
2019-10-25 16:57:38 +03:00
loggo.Info("close inactive conn %s %s", conn.id, conn.tcpaddrTarget.String())
2019-10-26 07:01:30 +03:00
conn.fm.Close()
break
}
if conn.fm.IsRemoteClosed() {
loggo.Info("closed by remote conn %s %s", conn.id, conn.tcpaddrTarget.String())
conn.fm.Close()
break
}
}
startCloseTime := time.Now()
2019-10-30 14:14:27 +03:00
for !p.exit {
2019-10-26 07:01:30 +03:00
now := time.Now()
conn.fm.Update()
sendlist := conn.fm.getSendList()
for e := sendlist.Front(); e != nil; e = e.Next() {
f := e.Value.(*Frame)
mb, _ := proto.Marshal(f)
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), mb,
conn.rproto, -1, p.key,
2019-10-28 06:58:01 +03:00
0, 0, 0, 0, 0, 0,
2019-10-26 07:07:20 +03:00
0)
2019-10-26 07:01:30 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(len(mb))
}
nodatarecv := true
if conn.fm.GetRecvBufferSize() > 0 {
rr := conn.fm.GetRecvReadLineBuffer()
conn.tcpconn.SetWriteDeadline(time.Now().Add(time.Millisecond * 100))
n, _ := conn.tcpconn.Write(rr)
if n > 0 {
conn.fm.SkipRecvBuffer(n)
nodatarecv = false
}
}
diffclose := now.Sub(startCloseTime)
timeout := diffclose > time.Second*(time.Duration(conn.timeout))
remoteclosed := conn.fm.IsRemoteClosed()
if timeout {
loggo.Info("close conn had timeout %s %s", conn.id, conn.tcpaddrTarget.String())
2019-10-25 16:57:38 +03:00
break
}
2019-10-26 07:01:30 +03:00
if remoteclosed && nodatarecv {
loggo.Info("remote conn had closed %s %s", conn.id, conn.tcpaddrTarget.String())
break
}
time.Sleep(time.Millisecond * 100)
2019-10-22 04:39:20 +03:00
}
2019-10-24 16:42:46 +03:00
2019-10-26 07:01:30 +03:00
time.Sleep(time.Second)
2019-10-24 16:42:46 +03:00
loggo.Info("close tcp conn %s %s", conn.id, conn.tcpaddrTarget.String())
2019-10-30 14:28:00 +03:00
p.close(conn)
2019-10-22 04:39:20 +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
2019-10-30 14:45:19 +03:00
p.workResultLock.Add(1)
defer p.workResultLock.Done()
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
2019-10-30 14:14:27 +03:00
for !p.exit {
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 {
2019-10-24 16:42:46 +03:00
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
2019-10-26 07:01:30 +03:00
loggo.Info("ReadFromUDP Error read udp %s", err)
2019-10-24 16:42:46 +03:00
conn.close = true
return
2018-12-17 10:21:15 +03:00
}
}
2018-12-18 10:36:59 +03:00
now := time.Now()
2019-10-25 16:57:38 +03:00
conn.activeSendTime = now
2018-12-18 10:36:59 +03:00
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,
2019-10-28 06:58:01 +03:00
0, 0, 0, 0, 0, 0,
2019-10-26 07:07:20 +03:00
0)
2018-12-19 10:50:21 +03:00
p.sendPacket++
p.sendPacketSize += (uint64)(n)
2018-12-18 06:39:16 +03:00
}
}
2019-10-30 14:28:00 +03:00
func (p *Server) close(conn *ServerConn) {
2018-12-18 06:39:16 +03:00
if p.localConnMap[conn.id] != nil {
2019-10-26 04:39:36 +03:00
if conn.conn != nil {
conn.conn.Close()
}
if conn.tcpconn != nil {
conn.tcpconn.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 {
2019-10-23 15:36:13 +03:00
if conn.tcpmode > 0 {
continue
}
2019-10-25 16:57:38 +03:00
diffrecv := now.Sub(conn.activeRecvTime)
diffsend := now.Sub(conn.activeSendTime)
2019-10-26 07:01:30 +03:00
if diffrecv > time.Second*(time.Duration(conn.timeout)) || diffsend > time.Second*(time.Duration(conn.timeout)) {
2018-12-18 11:56:40 +03:00
conn.close = true
}
}
for id, conn := range p.localConnMap {
2019-10-23 15:36:13 +03:00
if conn.tcpmode > 0 {
continue
}
2018-12-18 11:56:40 +03:00
if conn.close {
2019-10-20 11:27:03 +03:00
loggo.Info("close inactive conn %s %s", id, conn.ipaddrTarget.String())
2019-10-30 14:28:00 +03:00
p.close(conn)
2018-12-18 10:36:59 +03:00
}
}
}
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
}