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"
)
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)
}
}

View File

@ -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

View File

@ -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,
}

View File

@ -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)
}
}