-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
forward.go
368 lines (310 loc) · 8.1 KB
/
forward.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) 2021 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sshlib
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/armon/go-socks5"
xauth "github.com/blacknon/go-x11auth"
"golang.org/x/crypto/ssh"
)
// x11 request data struct
type x11Request struct {
SingleConnection bool
AuthProtocol string
AuthCookie string
ScreenNumber uint32
}
// X11Forward send x11-req to ssh server and do x11 forwarding.
// Since the display number of the transfer destination and the PATH of the socket communication file
// are checked from the local environmsdent variable DISPLAY, this does not work if it is not set.
//
// Also, the value of COOKIE transfers the local value as it is. This will be addressed in the future.
func (c *Connect) X11Forward(session *ssh.Session) (err error) {
// get xauthority path
xauthorityPath := os.Getenv("XAUTHORITY")
if len(xauthorityPath) == 0 {
home := os.Getenv("HOME")
if len(home) == 0 {
err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set")
return err
}
xauthorityPath = home + "/.Xauthority"
}
xa := xauth.XAuth{}
xa.Display = os.Getenv("DISPLAY")
cookie, err := xa.GetXAuthCookie(xauthorityPath, c.ForwardX11Trusted)
// set x11-req Payload
payload := x11Request{
SingleConnection: false,
AuthProtocol: string("MIT-MAGIC-COOKIE-1"),
AuthCookie: string(cookie),
ScreenNumber: uint32(0),
}
// Send x11-req Request
ok, err := session.SendRequest("x11-req", true, ssh.Marshal(payload))
if err == nil && !ok {
return errors.New("ssh: x11-req failed")
} else {
// Open HandleChannel x11
x11channels := c.Client.HandleChannelOpen("x11")
go func() {
for ch := range x11channels {
channel, _, err := ch.Accept()
if err != nil {
continue
}
go x11forwarder(channel)
}
}()
}
return err
}
// x11Connect return net.Conn x11 socket.
func x11Connect(display string) (conn net.Conn, err error) {
var conDisplay string
protocol := "unix"
if display[0] == '/' { // PATH type socket
conDisplay = display
} else if display[0] != ':' { // Forwarded display
protocol = "tcp"
if b, _, ok := strings.Cut(display, ":"); ok {
conDisplay = fmt.Sprintf("%v:%v", b, getX11DisplayNumber(display)+6000)
} else {
conDisplay = display
}
} else { // /tmp/.X11-unix/X0
conDisplay = fmt.Sprintf("/tmp/.X11-unix/X%v", getX11DisplayNumber(display))
}
return net.Dial(protocol, conDisplay)
}
// x11forwarder forwarding socket x11 data.
func x11forwarder(channel ssh.Channel) {
conn, err := x11Connect(os.Getenv("DISPLAY"))
if err != nil {
return
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(conn, channel)
conn.Close()
wg.Done()
}()
go func() {
io.Copy(channel, conn)
channel.CloseWrite()
wg.Done()
}()
wg.Wait()
conn.Close()
channel.Close()
}
// getX11Display return X11 display number from env $DISPLAY
func getX11DisplayNumber(display string) int {
colonIdx := strings.LastIndex(display, ":")
dotIdx := strings.LastIndex(display, ".")
if colonIdx < 0 {
return 0
}
if dotIdx < 0 {
dotIdx = len(display)
}
i, err := strconv.Atoi(display[colonIdx+1 : dotIdx])
if err != nil {
return 0
}
return i
}
// TCPLocalForward forwarding tcp data. Like Local port forward (ssh -L).
// localAddr, remoteAddr is write as "address:port".
//
// example) "127.0.0.1:22", "abc.com:9977"
func (c *Connect) TCPLocalForward(localAddr, remoteAddr string) (err error) {
// create listener
listener, err := net.Listen("tcp", localAddr)
if err != nil {
return
}
// forwarding
go func() {
for {
// local (type net.Conn)
local, err := listener.Accept()
if err != nil {
return
}
// remote (type net.Conn)
remote, err := c.Client.Dial("tcp", remoteAddr)
// forward
go c.forwarder(local, remote)
}
}()
return
}
// TCPRemoteForward forwarding tcp data. Like Remote port forward (ssh -R).
// localAddr, remoteAddr is write as "address:port".
//
// example) "127.0.0.1:22", "abc.com:9977"
func (c *Connect) TCPRemoteForward(localAddr, remoteAddr string) (err error) {
// create listener
listener, err := c.Client.Listen("tcp", remoteAddr)
if err != nil {
return
}
// forwarding
go func() {
for {
// local (type net.Conn)
local, err := net.Dial("tcp", localAddr)
if err != nil {
return
}
// remote (type net.Conn)
remote, err := listener.Accept()
if err != nil {
return
}
go c.forwarder(local, remote)
}
}()
return
}
// forwarder tcp/udp port forward. dialType in `tcp` or `udp`.
// addr is remote port forward address (`localhost:80`, `192.168.10.100:443` etc...).
func (c *Connect) forwarder(local net.Conn, remote net.Conn) {
var wg sync.WaitGroup
wg.Add(2)
// Copy local to remote
go func() {
io.Copy(remote, local)
wg.Done()
}()
// Copy remote to local
go func() {
io.Copy(local, remote)
wg.Done()
}()
wg.Wait()
remote.Close()
local.Close()
}
// socks5Resolver prevents DNS from resolving on the local machine, rather than over the SSH connection.
type socks5Resolver struct{}
// Resolve
func (socks5Resolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
return ctx, nil, nil
}
func (c *Connect) getDynamicForwardLogger() *log.Logger {
if c.DynamicForwardLogger == nil {
return log.New(io.Discard, "", log.LstdFlags)
}
return c.DynamicForwardLogger
}
// TCPDynamicForward forwarding tcp data. Like Dynamic forward (`ssh -D <port>`).
// listen port Socks5 proxy server.
func (c *Connect) TCPDynamicForward(address, port string) (err error) {
// Create Socks5 config
conf := &socks5.Config{
Dial: func(ctx context.Context, n, addr string) (net.Conn, error) {
return c.Client.Dial(n, addr)
},
Resolver: socks5Resolver{},
Logger: c.getDynamicForwardLogger(),
}
// Create Socks5 server
s, err := socks5.New(conf)
if err != nil {
return
}
// Listen
err = s.ListenAndServe("tcp", net.JoinHostPort(address, port))
return
}
// TCPReverseDynamicForward reverse forwarding tcp data.
// Like Openssh Reverse Dynamic forward (`ssh -R <port>`).
func (c *Connect) TCPReverseDynamicForward(address, port string) (err error) {
// Create Socks5 config
conf := &socks5.Config{
Dial: func(ctx context.Context, n, addr string) (net.Conn, error) {
return net.Dial(n, addr)
},
Resolver: socks5Resolver{},
Logger: c.getDynamicForwardLogger(),
}
// create listener
listener, err := c.Client.Listen("tcp", net.JoinHostPort(address, port))
if err != nil {
return
}
// Create Socks5 server
s, err := socks5.New(conf)
if err != nil {
return
}
// Listen
err = s.Serve(listener)
return
}
// HTTPDynamicForward forwarding http data.
// Like Dynamic forward (`ssh -D <port>`). but use http proxy.
func (c *Connect) HTTPDynamicForward(address, port string) (err error) {
// create dial
dial := c.Client.Dial
// create listener
listener, err := net.Listen("tcp", net.JoinHostPort(address, port))
if err != nil {
return
}
defer listener.Close()
// create proxy server.
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
handleHTTPSProxy(dial, w, r)
} else {
handleHTTPProxy(dial, w, r)
}
}),
ErrorLog: c.getDynamicForwardLogger(),
}
// listen
err = server.Serve(listener)
return
}
// HTTPReverseDynamicForward reverse forwarding http data.
// Like Reverse Dynamic forward (`ssh -R <port>`). but use http proxy.
func (c *Connect) HTTPReverseDynamicForward(address, port string) (err error) {
// create dial
dial := net.Dial
// create listener
listener, err := c.Client.Listen("tcp", net.JoinHostPort(address, port))
if err != nil {
return
}
defer listener.Close()
// create proxy server.
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
handleHTTPSProxy(dial, w, r)
} else {
handleHTTPProxy(dial, w, r)
}
}),
ErrorLog: c.getDynamicForwardLogger(),
}
// listen
err = server.Serve(listener)
return
}