From 75edf4c3c143a6f9203aac71744ba61517a3d7cb Mon Sep 17 00:00:00 2001 From: esrrhs Date: Fri, 21 Dec 2018 15:52:34 +0800 Subject: [PATCH] add --- client.go | 12 ++++++++++-- pingtunnel.go | 14 +++++++++++--- server.go | 12 ++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index f8f0629..3cadb31 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,8 @@ package pingtunnel import ( "fmt" "golang.org/x/net/icmp" + "math" + "math/rand" "net" "time" ) @@ -19,7 +21,10 @@ func NewClient(addr string, server string, target string, timeout int, sproto in return nil, err } + r := rand.New(rand.NewSource(time.Now().UnixNano())) return &Client{ + id: r.Intn(math.MaxInt16), + r: r, ipaddr: ipaddr, addr: addr, ipaddrServer: ipaddrServer, @@ -32,6 +37,9 @@ func NewClient(addr string, server string, target string, timeout int, sproto in } type Client struct { + id int + r *rand.Rand + timeout int sproto int rproto int @@ -156,7 +164,7 @@ func (p *Client) Accept() error { } clientConn.activeTime = now - sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto) + sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto) p.sendPacket++ p.sendPacketSize += (uint64)(n) @@ -229,7 +237,7 @@ func (p *Client) ping() { if p.sendPacket == 0 && p.recvPacket == 0 { now := time.Now() b, _ := now.MarshalBinary() - sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) + sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) fmt.Printf("ping %s %s %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto) } } diff --git a/pingtunnel.go b/pingtunnel.go index 2a3b1dd..71ee706 100644 --- a/pingtunnel.go +++ b/pingtunnel.go @@ -125,7 +125,7 @@ func (p *MyMsg) UnmarshalData(b []byte) []byte { return data } -func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, sproto int, rproto int) { +func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, sproto int, rproto int) { m := &MyMsg{ ID: connId, @@ -136,10 +136,18 @@ func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId st ENDTYPE: END, } + mb, err := m.Marshal(0) + + body := &icmp.Echo{ + ID: id, + Seq: sequence, + Data: mb, + } + msg := &icmp.Message{ Type: (ipv4.ICMPType)(sproto), Code: 0, - Body: m, + Body: body, } bytes, err := msg.Marshal(nil) @@ -184,7 +192,7 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) { my := &MyMsg{ } - my.Unmarshal(bytes[4:n]) + my.Unmarshal(bytes[8:n]) 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) diff --git a/server.go b/server.go index ab17829..f0179af 100644 --- a/server.go +++ b/server.go @@ -3,17 +3,25 @@ package pingtunnel import ( "fmt" "golang.org/x/net/icmp" + "math" + "math/rand" "net" "time" ) func NewServer(timeout int) (*Server, error) { + r := rand.New(rand.NewSource(time.Now().UnixNano())) return &Server{ + id: r.Intn(math.MaxInt16), + r: r, timeout: timeout, }, nil } type Server struct { + id int + r *rand.Rand + timeout int conn *icmp.PacketConn @@ -69,7 +77,7 @@ func (p *Server) processPacket(packet *Packet) { t := time.Time{} t.UnmarshalBinary(packet.data) fmt.Printf("ping from %s %s %d\n", packet.src.String(), t.String(), packet.rproto) - sendICMP(*p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1) + sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, packet.src, "", "", (uint32)(PING), packet.data, packet.rproto, -1) return } @@ -136,7 +144,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) { now := time.Now() conn.activeTime = now - sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1) + sendICMP(p.id, p.r.Intn(math.MaxInt16), *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1) p.sendPacket++ p.sendPacketSize += (uint64)(n)