-
Notifications
You must be signed in to change notification settings - Fork 19
/
direct.go
76 lines (64 loc) · 1.97 KB
/
direct.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
package sshego
import (
"fmt"
"log"
"net"
"golang.org/x/crypto/ssh"
)
// see also dev.justinjudd.org/justin/easyssh for examples
// of multiplexing ssh channels.
// channelOpenDirectMsg is the structure for RFC 4254 7.2. It can be
// used for "forwarded-tcpip" and "direct-tcpip"
type channelOpenDirectMsg struct {
Rhost string
Rport uint32
Lhost string
Lport uint32
}
const minus2_uint32 uint32 = 0xFFFFFFFE
// server side: handle channel type "direct-tcpip" - RFC 4254 7.2
func handleDirectTcp(newChannel ssh.NewChannel) {
//pp("handleDirectTcp called!")
p := &channelOpenDirectMsg{}
ssh.Unmarshal(newChannel.ExtraData(), p)
targetAddr := fmt.Sprintf("%s:%d", p.Rhost, p.Rport)
log.Printf("direct-tcpip got channelOpenDirectMsg request to destination %s",
targetAddr)
channel, req, err := newChannel.Accept() // (Channel, <-chan *Request, error)
panicOn(err)
go ssh.DiscardRequests(req)
go func(ch ssh.Channel, host string, port uint32) {
var targetConn net.Conn
var err error
addr := fmt.Sprintf("%s:%d", p.Rhost, p.Rport)
if port == minus2_uint32 {
// unix domain request
pp("direct.go has unix domain forwarding request")
targetConn, err = net.Dial("unix", host)
} else {
targetConn, err = net.Dial("tcp", targetAddr)
}
if err != nil {
log.Printf("sshd direct.go could not forward connection to addr: '%s'", addr)
return
}
log.Printf("sshd direct.go forwarding direct connection to addr: '%s'", addr)
sp := newShovelPair(false)
sp.Start(targetConn, ch, "targetBehindSshd<-fromDirectClient", "fromDirectClient<-targetBehindSshd")
}(channel, p.Rhost, p.Rport)
}
// client side
func dialDirect(c *ssh.Client, laddr string, lport int, raddr string, rport int) (ssh.Channel, error) {
msg := channelOpenDirectMsg{
Rhost: raddr,
Rport: uint32(rport),
Lhost: laddr,
Lport: uint32(lport),
}
ch, in, err := c.OpenChannel("direct-tcpip", ssh.Marshal(&msg))
if err != nil {
return nil, err
}
go ssh.DiscardRequests(in)
return ch, err
}