-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcpip.go
172 lines (136 loc) · 4.13 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
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
package easyssh
import (
"encoding/binary"
"fmt"
"net"
"strconv"
"golang.org/x/crypto/ssh"
)
// Applicaple SSH Request types for Port Forwarding - RFC 4254 7.X
const (
DirectForwardRequest = "direct-tcpip" // RFC 4254 7.2
RemoteForwardRequest = "tcpip-forward" // RFC 4254 7.1
ForwardedTCPReturnRequest = "forwarded-tcpip" // RFC 4254 7.2
CancelRemoteForwardRequest = "cancel-tcpip-forward" // RFC 4254 7.1
)
// tcpipForward is structure for RFC 4254 7.1 "tcpip-forward" request
type tcpipForward struct {
Host string
Port uint32
}
// directForward is struxture for RFC 4254 7.2 - can be used for "forwarded-tcpip" and "direct-tcpip"
type directForward struct {
Host1 string
Port1 uint32
Host2 string
Port2 uint32
}
func (p directForward) String() string {
return fmt.Sprintf("CONNECT: %s:%d FROM: %s:%d", p.Host1, p.Port1, p.Host2, p.Port2)
}
// TCPIPForwardRequestHandler returns a GlobalRequestHandler that implements remote port forwarding - ssh -R
func TCPIPForwardRequestHandler() GlobalRequestHandler {
return GlobalRequestHandlerFunc(TCPIPForwardRequest)
}
// TCPIPForwardRequest fulfills RFC 4254 7.1 "tcpip-forward" request
//
// TODO: Need to add state to handle "cancel-tcpip-forward"
func TCPIPForwardRequest(req *ssh.Request, sshConn ssh.Conn) {
t := tcpipForward{}
reply := (t.Port == 0) && req.WantReply
ssh.Unmarshal(req.Payload, &t)
addr := fmt.Sprintf("%s:%d", t.Host, t.Port)
ln, err := net.Listen("tcp", addr) //tie to the client connection
if err != nil {
logger.Println("Unable to listen on address: ", addr)
return
}
logger.Println("Listening on address: ", ln.Addr().String())
quit := make(chan bool)
if reply { // Client sent port 0. let them know which port is actually being used
_, port, err := getHostPortFromAddr(ln.Addr())
if err != nil {
return
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(port))
t.Port = uint32(port)
req.Reply(true, b)
} else {
req.Reply(true, nil)
}
go func() { // Handle incoming connections on this new listener
for {
select {
case <-quit:
return
default:
conn, err := ln.Accept()
if err != nil { // Unable to accept new connection - listener likely closed
continue
}
go func(conn net.Conn) {
p := directForward{}
var err error
var portnum int
p.Host1 = t.Host
p.Port1 = t.Port
p.Host2, portnum, err = getHostPortFromAddr(conn.RemoteAddr())
if err != nil {
return
}
p.Port2 = uint32(portnum)
ch, reqs, err := sshConn.OpenChannel(ForwardedTCPReturnRequest, ssh.Marshal(p))
if err != nil {
logger.Println("Open forwarded Channel: ", err.Error())
return
}
ssh.DiscardRequests(reqs)
go func(ch ssh.Channel, conn net.Conn) {
close := func() {
ch.Close()
conn.Close()
// logger.Printf("forwarding closed")
}
go CopyReadWriters(conn, ch, close)
}(ch, conn)
}(conn)
}
}
}()
sshConn.Wait()
logger.Println("Stop forwarding/listening on ", ln.Addr())
ln.Close()
quit <- true
}
func getHostPortFromAddr(addr net.Addr) (host string, port int, err error) {
host, portString, err := net.SplitHostPort(addr.String())
if err != nil {
return
}
port, err = strconv.Atoi(portString)
return
}
// DirectPortForwardHandler returns a ChannelHandler that implements standard SSH direct portforwarding
func DirectPortForwardHandler() ChannelHandler { return ChannelHandlerFunc(DirectPortForwardChannel) }
// DirectPortForwardChannel acts as an SSH Direct Port Forwarder - ssh -L
//
// Should be to channel type - "direct-tcpip" - RFC 4254 7.2
func DirectPortForwardChannel(newChannel ssh.NewChannel, channel ssh.Channel, reqs <-chan *ssh.Request, sshConn ssh.Conn) {
p := directForward{}
ssh.Unmarshal(newChannel.ExtraData(), &p)
logger.Println(p)
go func(ch ssh.Channel, sshConn ssh.Conn) {
addr := fmt.Sprintf("%s:%d", p.Host1, p.Port1)
conn, err := net.Dial("tcp", addr)
if err != nil {
return
}
close := func() {
ch.Close()
conn.Close()
//logger.Printf("forwarding closed")
}
go CopyReadWriters(conn, ch, close)
}(channel, sshConn)
}