From 11b15f071e90e11ecaafe1dc4f926c0648bbaa07 Mon Sep 17 00:00:00 2001 From: esrrhs Date: Wed, 19 Dec 2018 15:00:30 +0800 Subject: [PATCH] fix --- client.go | 8 ++++++++ pingtunnel.go | 21 ++++++++++++--------- server.go | 6 ++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 641a30c..e338626 100644 --- a/client.go +++ b/client.go @@ -111,6 +111,7 @@ func (p *Client) Run() { select { case <-interval.C: p.checkTimeoutConn() + p.ping() case r := <-recv: p.processPacket(r) } @@ -199,3 +200,10 @@ func (p *Client) checkTimeoutConn() { } } } + +func (p *Client) ping() { + now := time.Now() + b, _ := now.MarshalBinary() + sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) + fmt.Printf("ping %s %s\n", p.addrServer, now.String()) +} diff --git a/pingtunnel.go b/pingtunnel.go index 6010d7d..b7ad25c 100644 --- a/pingtunnel.go +++ b/pingtunnel.go @@ -17,6 +17,8 @@ import ( const ( DATA uint32 = 0x01010101 + PING uint32 = 0x02020202 + END uint32 = 0xAAAABBBB ) // An Echo represents an ICMP echo request or reply message body. @@ -131,7 +133,7 @@ func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId st TARGET: target, Data: data, RPROTO: (uint16)(rproto), - ENDTYPE: msgType, + ENDTYPE: END, } msg := &icmp.Message{ @@ -184,26 +186,27 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) { } my.Unmarshal(bytes[4:n]) - if my.TYPE != (uint32)(DATA) || my.ENDTYPE != (uint32)(DATA) { + if (my.TYPE != (uint32)(DATA) && my.TYPE != (uint32)(PING)) || my.ENDTYPE != (uint32)(END) { //fmt.Printf("processPacket diff type %s %d %d \n", my.ID, my.TYPE, my.ENDTYPE) continue } if my.Data == nil { - //fmt.Printf("processPacket data nil %s\n", my.ID) + fmt.Printf("processPacket data nil %s\n", my.ID) return } - recv <- &Packet{data: my.Data, id: my.ID, target: my.TARGET, src: srcaddr.(*net.IPAddr), rproto: (int)(my.RPROTO)} + recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET, src: srcaddr.(*net.IPAddr), rproto: (int)(my.RPROTO)} } } type Packet struct { - data []byte - id string - target string - src *net.IPAddr - rproto int + msgType uint32 + data []byte + id string + target string + src *net.IPAddr + rproto int } func UniqueId() string { diff --git a/server.go b/server.go index 598468e..d145170 100644 --- a/server.go +++ b/server.go @@ -59,6 +59,12 @@ func (p *Server) Run() { func (p *Server) processPacket(packet *Packet) { + if packet.msgType == PING { + fmt.Printf("pong from %s\n", packet.src.String()) + sendICMP(*p.conn, packet.src, "", "", (uint32)(DATA), packet.data, packet.rproto, 0) + return + } + fmt.Printf("processPacket %s %s %d\n", packet.id, packet.src.String(), len(packet.data)) now := time.Now()