Skip to content

Commit

Permalink
添加网关demo
Browse files Browse the repository at this point in the history
  • Loading branch information
w910820618 committed Jan 17, 2021
1 parent aa246d1 commit da0c32a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
42 changes: 42 additions & 0 deletions demo/gateway/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit da0c32a

Please sign in to comment.