|
@@ -35,12 +35,12 @@ func (rw *stdnet) RemoteAddr() net.Addr {
|
35
|
35
|
}
|
36
|
36
|
|
37
|
37
|
// not all of net.Conn, just RWC and RemoteAddr()
|
38
|
|
-type Rwc interface {
|
|
38
|
+type netReadWriteCloser interface {
|
39
|
39
|
io.ReadWriteCloser
|
40
|
40
|
RemoteAddr() net.Addr
|
41
|
41
|
}
|
42
|
42
|
|
43
|
|
-type PipeOpts struct {
|
|
43
|
+type Tunnel struct {
|
44
|
44
|
RemoteAddress string
|
45
|
45
|
RemotePort int
|
46
|
46
|
LocalAddress string
|
|
@@ -49,9 +49,7 @@ type PipeOpts struct {
|
49
|
49
|
ServerName string
|
50
|
50
|
}
|
51
|
51
|
|
52
|
|
-type Tun struct{}
|
53
|
|
-
|
54
|
|
-func pipe(r Rwc, w Rwc, t string) {
|
|
52
|
+func pipe(r netReadWriteCloser, w netReadWriteCloser, t string) {
|
55
|
53
|
buffer := make([]byte, 2048)
|
56
|
54
|
for {
|
57
|
55
|
done := false
|
|
@@ -87,11 +85,11 @@ func pipe(r Rwc, w Rwc, t string) {
|
87
|
85
|
}
|
88
|
86
|
}
|
89
|
87
|
|
90
|
|
-func handleConnection(remote string, conn Rwc, opts *PipeOpts) {
|
|
88
|
+func (t *Tunnel) handleConnection(remote string, conn netReadWriteCloser) {
|
91
|
89
|
sclient, err := tls.Dial("tcp", remote,
|
92
|
90
|
&tls.Config{
|
93
|
|
- ServerName: opts.ServerName,
|
94
|
|
- InsecureSkipVerify: opts.InsecureSkipVerify,
|
|
91
|
+ ServerName: t.ServerName,
|
|
92
|
+ InsecureSkipVerify: t.InsecureSkipVerify,
|
95
|
93
|
})
|
96
|
94
|
|
97
|
95
|
if err != nil {
|
|
@@ -102,22 +100,22 @@ func handleConnection(remote string, conn Rwc, opts *PipeOpts) {
|
102
|
100
|
|
103
|
101
|
if "stdio" == conn.RemoteAddr().Network() {
|
104
|
102
|
fmt.Fprintf(os.Stdout, "(connected to %s:%d and reading from %s)\n",
|
105
|
|
- opts.RemoteAddress, opts.RemotePort, conn.RemoteAddr().String())
|
|
103
|
+ t.RemoteAddress, t.RemotePort, conn.RemoteAddr().String())
|
106
|
104
|
} else {
|
107
|
105
|
fmt.Fprintf(os.Stdout, "[connect] %s => %s:%d\n",
|
108
|
|
- strings.Replace(conn.RemoteAddr().String(), "[::1]:", "localhost:", 1), opts.RemoteAddress, opts.RemotePort)
|
|
106
|
+ strings.Replace(conn.RemoteAddr().String(), "[::1]:", "localhost:", 1), t.RemoteAddress, t.RemotePort)
|
109
|
107
|
}
|
110
|
108
|
|
111
|
109
|
go pipe(conn, sclient, "local")
|
112
|
110
|
pipe(sclient, conn, "remote")
|
113
|
111
|
}
|
114
|
112
|
|
115
|
|
-func (*Tun) DialAndListen(opts *PipeOpts) error {
|
116
|
|
- remote := opts.RemoteAddress + ":" + strconv.Itoa(opts.RemotePort)
|
|
113
|
+func (t *Tunnel) DialAndListen() error {
|
|
114
|
+ remote := t.RemoteAddress + ":" + strconv.Itoa(t.RemotePort)
|
117
|
115
|
conn, err := tls.Dial("tcp", remote,
|
118
|
116
|
&tls.Config{
|
119
|
|
- ServerName: opts.ServerName,
|
120
|
|
- InsecureSkipVerify: opts.InsecureSkipVerify,
|
|
117
|
+ ServerName: t.ServerName,
|
|
118
|
+ InsecureSkipVerify: t.InsecureSkipVerify,
|
121
|
119
|
})
|
122
|
120
|
|
123
|
121
|
if err != nil {
|
|
@@ -127,28 +125,28 @@ func (*Tun) DialAndListen(opts *PipeOpts) error {
|
127
|
125
|
}
|
128
|
126
|
|
129
|
127
|
// use stdin/stdout
|
130
|
|
- if "-" == opts.LocalAddress || "|" == opts.LocalAddress {
|
|
128
|
+ if "-" == t.LocalAddress || "|" == t.LocalAddress {
|
131
|
129
|
var name string
|
132
|
130
|
network := "stdio"
|
133
|
|
- if "|" == opts.LocalAddress {
|
|
131
|
+ if "|" == t.LocalAddress {
|
134
|
132
|
name = "pipe"
|
135
|
133
|
} else {
|
136
|
134
|
name = "stdin"
|
137
|
135
|
}
|
138
|
136
|
conn := &stdnet{os.Stdin, os.Stdout, &stdaddr{net.UnixAddr{name, network}}}
|
139
|
|
- handleConnection(remote, conn, opts)
|
|
137
|
+ t.handleConnection(remote, conn)
|
140
|
138
|
return nil
|
141
|
139
|
}
|
142
|
140
|
|
143
|
141
|
// use net.Conn
|
144
|
|
- local := opts.LocalAddress + ":" + strconv.Itoa(opts.LocalPort)
|
|
142
|
+ local := t.LocalAddress + ":" + strconv.Itoa(t.LocalPort)
|
145
|
143
|
ln, err := net.Listen("tcp", local)
|
146
|
144
|
if err != nil {
|
147
|
145
|
return err
|
148
|
146
|
}
|
149
|
147
|
|
150
|
148
|
fmt.Fprintf(os.Stdout, "[listening] %s:%d <= %s:%d\n",
|
151
|
|
- opts.RemoteAddress, opts.RemotePort, opts.LocalAddress, opts.LocalPort)
|
|
149
|
+ t.RemoteAddress, t.RemotePort, t.LocalAddress, t.LocalPort)
|
152
|
150
|
|
153
|
151
|
for {
|
154
|
152
|
conn, err := ln.Accept()
|
|
@@ -156,6 +154,6 @@ func (*Tun) DialAndListen(opts *PipeOpts) error {
|
156
|
154
|
fmt.Fprintf(os.Stderr, "[error] %s\n", err)
|
157
|
155
|
continue
|
158
|
156
|
}
|
159
|
|
- go handleConnection(remote, conn, opts)
|
|
157
|
+ go t.handleConnection(remote, conn)
|
160
|
158
|
}
|
161
|
159
|
}
|