-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcpip.go
75 lines (65 loc) · 1.76 KB
/
tcpip.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
package revssh
import (
"fmt"
"io"
"log"
"net"
"golang.org/x/crypto/ssh"
)
// direct-tcpip data struct as specified in RFC4254, Section 7.2
type forwardData struct {
DestinationHost string
DestinationPort uint32
OriginatorHost string
OriginatorPort uint32
}
func directTcpipChannelHandler(srv *Server, sshConn *ssh.ServerConn, newChan ssh.NewChannel) {
log.Printf("direct tcp-ip handler")
d := forwardData{}
var conn net.Conn
if err := ssh.Unmarshal(newChan.ExtraData(), &d); err != nil {
newChan.Reject(ssh.ConnectionFailed, "error parsing forward data: "+err.Error())
return
}
// TODO: callback to allow / deny specific forwarding
rc, err := srv.ReverseClientList.GetReverseClient(d.DestinationHost, sshConn.User())
if err != nil || rc == nil {
dest := fmt.Sprintf("%s:%d", d.DestinationHost, d.DestinationPort)
var dialer net.Dialer
conn, err = dialer.Dial("tcp", dest)
if err != nil {
newChan.Reject(ssh.ConnectionFailed, err.Error())
log.Printf("%s", err.Error())
return
}
} else {
rchannel, rreqs, err := rc.SSHConn.OpenChannel("reverse", newChan.ExtraData())
if err != nil {
newChan.Reject(ssh.ConnectionFailed, "Could not open forward channel")
log.Printf("Reverse Channel error: %+v", err)
return
}
go ssh.DiscardRequests(rreqs)
conn = NewSSHChannelConn(rchannel)
}
ch, reqs, err := newChan.Accept()
if err != nil {
conn.Close()
log.Printf("Error: %+v", conn)
return
}
go ssh.DiscardRequests(reqs)
go func() {
defer ch.Close()
defer conn.Close()
io.Copy(ch, conn)
}()
go func() {
defer ch.Close()
defer conn.Close()
io.Copy(conn, ch)
}()
log.Println("Got a reverse SSH channel")
// dest := fmt.Sprintf("%s:%d", d.DestinationHost, d.DestinationPort)
// log.Printf("direct-tcp-ip %+v", d)
}