From 743b82dda4fc74df3a4217ad26abf1710e0dea50 Mon Sep 17 00:00:00 2001 From: esrrhs Date: Wed, 19 Dec 2018 11:48:48 +0800 Subject: [PATCH] add --- client.go | 6 ++++-- cmd/main.go | 12 ++++++++---- pingtunnel.go | 4 ++-- server.go | 6 ++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index e629010..439d27c 100644 --- a/client.go +++ b/client.go @@ -7,7 +7,7 @@ import ( "time" ) -func NewClient(addr string, server string, target string, timeout int) (*Client, error) { +func NewClient(addr string, server string, target string, timeout int, proto int) (*Client, error) { ipaddr, err := net.ResolveUDPAddr("udp", addr) if err != nil { @@ -26,11 +26,13 @@ func NewClient(addr string, server string, target string, timeout int) (*Client, addrServer: server, targetAddr: target, timeout: timeout, + proto: proto, }, nil } type Client struct { timeout int + proto int ipaddr *net.UDPAddr addr string @@ -145,7 +147,7 @@ func (p *Client) Accept() error { } clientConn.activeTime = now - sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n]) + sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.proto) } } diff --git a/cmd/main.go b/cmd/main.go index 0801647..007e56f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -28,8 +28,11 @@ Usage: -t 远端服务器转发的目的地址,流量将转发到这个地址 Destination address forwarded by the remote server, traffic will be forwarded to this address - -timeout 本地记录连接超时的时间,单位是秒 - The time when the local record connection timed out, in seconds + -timeout 本地记录连接超时的时间,单位是秒,默认60s + The time when the local record connection timed out, in seconds, 60 seconds by default + + -proto ping的协议,默认是42 + Ping protocol, the default is 42 ` func main() { @@ -39,6 +42,7 @@ func main() { target := flag.String("t", "", "target addr") server := flag.String("s", "", "server addr") timeout := flag.Int("timeout", 60, "conn timeout") + proto := flag.Int("proto", 2, "ping proto") flag.Usage = func() { fmt.Printf(usage) } @@ -53,7 +57,7 @@ func main() { fmt.Println("start...") if *t == "server" { - s, err := pingtunnel.NewServer(*timeout) + s, err := pingtunnel.NewServer(*timeout, *proto) if err != nil { fmt.Printf("ERROR: %s\n", err.Error()) return @@ -68,7 +72,7 @@ func main() { fmt.Printf("server %s\n", *server) fmt.Printf("target %s\n", *target) - c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout) + c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *proto) if err != nil { fmt.Printf("ERROR: %s\n", err.Error()) return diff --git a/pingtunnel.go b/pingtunnel.go index 090f6a0..f4f5061 100644 --- a/pingtunnel.go +++ b/pingtunnel.go @@ -89,7 +89,7 @@ func (p *MyMsg) UnmarshalString(b []byte) string { return string(data) } -func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte) { +func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, proto int) { m := &MyMsg{ ID: connId, @@ -99,7 +99,7 @@ func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId st } msg := &icmp.Message{ - Type: ipv4.ICMPTypeExtendedEchoRequest, + Type: (ipv4.ICMPType)(proto), Code: 0, Body: m, } diff --git a/server.go b/server.go index ee2ab94..d00e2fb 100644 --- a/server.go +++ b/server.go @@ -7,14 +7,16 @@ import ( "time" ) -func NewServer(timeout int) (*Server, error) { +func NewServer(timeout int, proto int) (*Server, error) { return &Server{ timeout: timeout, + proto: proto, }, nil } type Server struct { timeout int + proto int conn *icmp.PacketConn @@ -118,7 +120,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]) + sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], p.proto) } }