-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
143 lines (118 loc) · 2.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"github.com/urfave/cli"
"io"
"log"
"net"
"os"
"sync"
"time"
)
var (
xmitBuf sync.Pool
)
func checkError(err error) {
if err != nil {
log.Printf("%+v\n", err)
os.Exit(-1)
}
}
func main() {
// add more log flags for debugging
log.SetFlags(log.LstdFlags | log.Lshortfile)
xmitBuf.New = func() interface{} {
return make([]byte, 32768)
}
myApp := cli.NewApp()
myApp.Name = "tcptun"
myApp.Usage = "tcptun -l :2022 -t 192.168.1.200:80"
myApp.Flags = []cli.Flag{
cli.StringFlag{
Name: "targettcp, t",
Value: "192.168.1.1:80",
Usage: "target server address",
},
cli.StringFlag{
Name: "listentcp,l",
Value: "127.0.0.1:2022",
Usage: "local listen address",
},
cli.StringFlag{
Name: "c",
Value: "", // when the value is not empty, the config path must exists
Usage: "config from json file, which will override the command from shell",
},
}
myApp.Action = func(c *cli.Context) error {
xmitBuf.New = func() interface{} {
return make([]byte, 32768)
}
config := Config{}
config.ListenTcp = c.String("listentcp")
config.TargetTcp = c.String("targettcp")
//config.TargetTcp = "127.0.0.1:22"
if c.String("c") != "" {
err := parseJSONConfig(&config, c.String("c"))
checkError(err)
}
chTCPConn := make(chan *net.TCPConn, 16)
go tcpListener(chTCPConn, &config)
tickerCheck := time.NewTicker(10*time.Second)
defer tickerCheck.Stop()
for {
select {
case p1 := <-chTCPConn:
go handleLocalTcp(p1, &config)
case <-tickerCheck.C:
//log.Println("working")
}
}
return nil
}
myApp.Run(os.Args)
}
func tcpListener(chTCPConn chan *net.TCPConn, config *Config){
listenTcpAddr, err := net.ResolveTCPAddr("tcp4", config.ListenTcp)
checkError(err)
listener, err := net.ListenTCP("tcp4", listenTcpAddr)
checkError(err)
log.Println("tcp listening on:", listener.Addr())
for{
p1, err := listener.AcceptTCP()
if err != nil {
log.Fatalln(err)
}
chTCPConn <- p1
}
}
func handleLocalTcp(p1 io.ReadWriteCloser, config *Config) {
p2, err := net.DialTimeout("tcp", config.TargetTcp, 5*time.Second)
if err != nil {
p1.Close()
log.Println(err)
return
}
go func() {
if true {
log.Println("tcp client opened")
defer log.Println("tcp client closed")
}
defer p1.Close()
defer p2.Close()
streamCopy := func(dst io.Writer, src io.ReadCloser) chan struct{} {
die := make(chan struct{})
go func() {
buf := xmitBuf.Get().([]byte)
CopyBuffer(dst, src, buf)
//generic.Copy(dst, src)
xmitBuf.Put(buf)
close(die)
}()
return die
}
select {
case <-streamCopy(p1, p2):
case <-streamCopy(p2, p1):
}
}()
}