This commit is contained in:
esrrhs 2018-12-19 11:48:48 +08:00
parent b83e6e7d45
commit 743b82dda4
4 changed files with 18 additions and 10 deletions

View File

@ -7,7 +7,7 @@ import (
"time" "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) ipaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil { if err != nil {
@ -26,11 +26,13 @@ func NewClient(addr string, server string, target string, timeout int) (*Client,
addrServer: server, addrServer: server,
targetAddr: target, targetAddr: target,
timeout: timeout, timeout: timeout,
proto: proto,
}, nil }, nil
} }
type Client struct { type Client struct {
timeout int timeout int
proto int
ipaddr *net.UDPAddr ipaddr *net.UDPAddr
addr string addr string
@ -145,7 +147,7 @@ func (p *Client) Accept() error {
} }
clientConn.activeTime = now 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)
} }
} }

View File

@ -28,8 +28,11 @@ Usage:
-t 远端服务器转发的目的地址流量将转发到这个地址 -t 远端服务器转发的目的地址流量将转发到这个地址
Destination address forwarded by the remote server, traffic will be forwarded to this address Destination address forwarded by the remote server, traffic will be forwarded to this address
-timeout 本地记录连接超时的时间单位是秒 -timeout 本地记录连接超时的时间单位是秒默认60s
The time when the local record connection timed out, in seconds 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() { func main() {
@ -39,6 +42,7 @@ func main() {
target := flag.String("t", "", "target addr") target := flag.String("t", "", "target addr")
server := flag.String("s", "", "server addr") server := flag.String("s", "", "server addr")
timeout := flag.Int("timeout", 60, "conn timeout") timeout := flag.Int("timeout", 60, "conn timeout")
proto := flag.Int("proto", 2, "ping proto")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf(usage) fmt.Printf(usage)
} }
@ -53,7 +57,7 @@ func main() {
fmt.Println("start...") fmt.Println("start...")
if *t == "server" { if *t == "server" {
s, err := pingtunnel.NewServer(*timeout) s, err := pingtunnel.NewServer(*timeout, *proto)
if err != nil { if err != nil {
fmt.Printf("ERROR: %s\n", err.Error()) fmt.Printf("ERROR: %s\n", err.Error())
return return
@ -68,7 +72,7 @@ func main() {
fmt.Printf("server %s\n", *server) fmt.Printf("server %s\n", *server)
fmt.Printf("target %s\n", *target) 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 { if err != nil {
fmt.Printf("ERROR: %s\n", err.Error()) fmt.Printf("ERROR: %s\n", err.Error())
return return

View File

@ -89,7 +89,7 @@ func (p *MyMsg) UnmarshalString(b []byte) string {
return string(data) 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{ m := &MyMsg{
ID: connId, ID: connId,
@ -99,7 +99,7 @@ func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId st
} }
msg := &icmp.Message{ msg := &icmp.Message{
Type: ipv4.ICMPTypeExtendedEchoRequest, Type: (ipv4.ICMPType)(proto),
Code: 0, Code: 0,
Body: m, Body: m,
} }

View File

@ -7,14 +7,16 @@ import (
"time" "time"
) )
func NewServer(timeout int) (*Server, error) { func NewServer(timeout int, proto int) (*Server, error) {
return &Server{ return &Server{
timeout: timeout, timeout: timeout,
proto: proto,
}, nil }, nil
} }
type Server struct { type Server struct {
timeout int timeout int
proto int
conn *icmp.PacketConn conn *icmp.PacketConn
@ -118,7 +120,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
now := time.Now() now := time.Now()
conn.activeTime = now conn.activeTime = now
sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n]) sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], p.proto)
} }
} }