From da0c32af20d51d13a3cffc88dc3942331c56334e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=B4=B2=E6=B4=8B?= Date: Sun, 17 Jan 2021 10:37:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BD=91=E5=85=B3demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- demo/gateway/main.go | 42 ++++++++++++++++++++++++++ demo/{ => tunnel}/client/socks/main.go | 0 demo/{ => tunnel}/client/tcp/main.go | 0 demo/{ => tunnel}/server/main.go | 0 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 demo/gateway/main.go rename demo/{ => tunnel}/client/socks/main.go (100%) rename demo/{ => tunnel}/client/tcp/main.go (100%) rename demo/{ => tunnel}/server/main.go (100%) diff --git a/README.md b/README.md index 2115fcb2..81bf1df0 100755 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ if flags.TCPTun != "" { 接着我们看tcp.go中的**tcpTun**函数: -``` +```golang func tcpTun(addr, server, target string, shadow func(net.Conn) net.Conn) { tgt := socks.ParseAddr(target) if tgt == nil { @@ -185,7 +185,7 @@ func tcpTun(addr, server, target string, shadow func(net.Conn) net.Conn) { **在golang中可以把函数作为一种类型,并且可以把函数作为参数进行传递**。 -``` +```golang func(net.Conn) (socks.Addr, error) { return tgt, nil } diff --git a/demo/gateway/main.go b/demo/gateway/main.go new file mode 100644 index 00000000..72af98b5 --- /dev/null +++ b/demo/gateway/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "flag" + "fmt" + "io" + "log" + "net" + "os" +) + +var local = flag.String("local", "", "please enter monitor ip, example: 192.168.0.104:8805") +var target = flag.String("target", "", "please enter target ip, example : 192.168.1.105:8806") + +func main() { + + l, err := net.Listen("tcp", *local) + if err != nil { + fmt.Println(err, err.Error()) + os.Exit(0) + } + + for { + s_conn, err := l.Accept() + log.Println("接收到 " + s_conn.RemoteAddr().String() + " 发送请求") + if err != nil { + continue + } + + d_tcpAddr, _ := net.ResolveTCPAddr("tcp4", *target) + d_conn, err := net.DialTCP("tcp", nil, d_tcpAddr) + log.Println("向 " + *target + " 发送请求") + if err != nil { + fmt.Println(err) + s_conn.Write([]byte("can't connect " + *target)) + s_conn.Close() + continue + } + go io.Copy(s_conn, d_conn) + go io.Copy(d_conn, s_conn) + } +} diff --git a/demo/client/socks/main.go b/demo/tunnel/client/socks/main.go similarity index 100% rename from demo/client/socks/main.go rename to demo/tunnel/client/socks/main.go diff --git a/demo/client/tcp/main.go b/demo/tunnel/client/tcp/main.go similarity index 100% rename from demo/client/tcp/main.go rename to demo/tunnel/client/tcp/main.go diff --git a/demo/server/main.go b/demo/tunnel/server/main.go similarity index 100% rename from demo/server/main.go rename to demo/tunnel/server/main.go