|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "flag" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "sync" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + Option_Local string |
| 14 | + Option_Remote string |
| 15 | + Option_Protocol string |
| 16 | + Option_Tls bool |
| 17 | + Option_Help bool |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + flag.BoolVar(&Option_Help, "help", false, "this help") |
| 22 | + flag.StringVar(&Option_Local, "local", "0.0.0.0:8080", "set local tcp protocol listen address.") |
| 23 | + flag.StringVar(&Option_Remote, "remote", "", "set remote tcp protocol proxy address.") |
| 24 | + flag.StringVar(&Option_Protocol, "protocol", "tcp", "set remote tcp protocol tcp or tcp6.") |
| 25 | + flag.BoolVar(&Option_Tls, "tls", false, "enable remote tcp with tls encryption.") |
| 26 | +} |
| 27 | + |
| 28 | +func IoCopy(c *sync.WaitGroup, up bool, in net.Conn, out net.Conn) { |
| 29 | + defer c.Done() |
| 30 | + size, _ := io.Copy(out, in) |
| 31 | + if size > 0 { |
| 32 | + if up { |
| 33 | + StatUpdate(size, 0) |
| 34 | + } else { |
| 35 | + StatUpdate(0, size) |
| 36 | + } |
| 37 | + } |
| 38 | + in.Close() |
| 39 | + out.Close() |
| 40 | +} |
| 41 | + |
| 42 | +func TcpProxy(local_conn net.Conn, remote_conn net.Conn) { |
| 43 | + log.Printf("start %s connect to %s\n", local_conn.RemoteAddr(), remote_conn.RemoteAddr()) |
| 44 | + |
| 45 | + syncSem := new(sync.WaitGroup) |
| 46 | + syncSem.Add(2) |
| 47 | + |
| 48 | + go IoCopy(syncSem, true, local_conn, remote_conn) |
| 49 | + go IoCopy(syncSem, false, remote_conn, local_conn) |
| 50 | + |
| 51 | + syncSem.Wait() |
| 52 | + |
| 53 | + log.Printf("close %s connect to %s\n", local_conn.RemoteAddr(), remote_conn.RemoteAddr()) |
| 54 | +} |
| 55 | + |
| 56 | +func main() { |
| 57 | + flag.Parse() |
| 58 | + if Option_Help { |
| 59 | + flag.Usage() |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + listen, err := net.Listen("tcp", Option_Local) |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("listen error: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + log.Printf("listen %s successfully\n", Option_Local) |
| 69 | + |
| 70 | + var tls_config *tls.Config |
| 71 | + |
| 72 | + if Option_Tls { |
| 73 | + tls_config, err = TlsConfigClient(Option_Remote) |
| 74 | + if err != nil { |
| 75 | + log.Println(err.Error()) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for { |
| 80 | + local_conn, err := listen.Accept() |
| 81 | + if err != nil { |
| 82 | + log.Println(err.Error()) |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + remote_conn, err := net.Dial(Option_Protocol, Option_Remote) |
| 87 | + if err != nil { |
| 88 | + log.Println(err.Error()) |
| 89 | + local_conn.Close() |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + if tls_config != nil { |
| 94 | + remote_conn = tls.Client(remote_conn, tls_config) |
| 95 | + } |
| 96 | + |
| 97 | + go TcpProxy(local_conn, remote_conn) |
| 98 | + } |
| 99 | +} |
0 commit comments