This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sshserver.go
360 lines (318 loc) · 8.52 KB
/
sshserver.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"sync"
"golang.org/x/crypto/ssh"
)
const (
forwardedTCPChannelType = "forwarded-tcpip"
)
type SSHServer struct {
config *ssh.ServerConfig
vhosts map[net.Addr]*VHost
users []string
forwards map[string]net.Listener
sync.RWMutex
}
type RemoteForwardRequest struct {
BindAddr string
BindPort uint32
}
type RemoteForwardSuccess struct {
BindPort uint32
}
type RemoteForwardCancelRequest struct {
BindAddr string
BindPort uint32
}
type RemoteForwardChannelData struct {
DestAddr string
DestPort uint32
OriginAddr string
OriginPort uint32
}
// Starts the SSH Server
func (server *SSHServer) Start(mux *ProxyMux) {
log.Println("Starting SSH server...")
// You may also explicitly allow anonymous client authentication
// server.config.NoClientAuth = true
server.config = &ssh.ServerConfig{
PublicKeyCallback: publicKeyAuthStrategy,
}
server.vhosts = make(map[net.Addr]*VHost)
// You can generate a keypair with 'ssh-keygen -t rsa'
privateBytes, err := ioutil.ReadFile(sshServerKeyPath)
if err != nil {
log.Fatalf("Failed to load private key (./%s)", sshServerKeyPath)
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("Failed to parse private key")
}
server.config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be accepted.
listener, err := net.Listen("tcp", sshListenPort)
if err != nil {
log.Fatalf("Failed to listen on %s (%s)", sshListenPort, err)
}
// Accept all connections
log.Printf("Listening on %s...", sshListenPort)
for {
tcpConn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection (%s)", err)
continue
}
log.Printf("Beginning SSH handshake for %s", tcpConn.RemoteAddr())
// Before use, a handshake must be performed on the incoming net.Conn.
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, server.config)
if err != nil {
log.Printf("Failed to handshake (%s)", err)
continue
}
log.Printf("New SSH connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
// Handle requests
go server.handleRequests(reqs, sshConn, mux)
// Accept all channels
go server.handleChannels(chans, sshConn, mux)
}
}
func publicKeyAuthStrategy(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
convertPublicKeyToString := func(key ssh.PublicKey) string {
return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))
}
publicKey := convertPublicKeyToString(key)
user := conn.User()
if whitelist.users != nil {
if !contains(whitelist.users, user) {
return nil, errors.New("User not in whitelist")
}
}
githubKeys := getPublicKeys(user)
if githubKeys != nil {
for _, key := range githubKeys {
if publicKey == key {
log.Printf("Successfully authenticated %s@%s", conn.User(), conn.RemoteAddr())
return &ssh.Permissions{}, nil
}
}
log.Printf("Unauthorized access from %s@%s", conn.User(), conn.RemoteAddr())
return nil, errors.New("Unauthorized access")
}
log.Printf("Unauthorized access from %s@%s", conn.User(), conn.RemoteAddr())
return nil, errors.New("Unauthorized access")
}
func (server *SSHServer) handleChannels(chans <-chan ssh.NewChannel, conn *ssh.ServerConn, mux *ProxyMux) {
// Service the incoming Channel channel in go routine
for newChannel := range chans {
go server.handleChannel(newChannel, conn, mux)
}
}
func (server *SSHServer) handleRequests(reqs <-chan *ssh.Request, conn *ssh.ServerConn, mux *ProxyMux) {
server.Lock()
if server.forwards == nil {
server.forwards = make(map[string]net.Listener)
}
server.Unlock()
port, err := getFreePort()
if err != nil {
log.Fatal(err)
}
for req := range reqs {
switch req.Type {
case "tcpip-forward":
var reqPayload RemoteForwardRequest
if err := ssh.Unmarshal(req.Payload, &reqPayload); err != nil {
// TODO: log parse failure
req.Reply(false, []byte{})
}
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(port))
ln, err := net.Listen("tcp", addr)
if err != nil {
// TODO: log listen failure
req.Reply(false, []byte{})
}
server.Lock()
server.forwards[addr] = ln
server.Unlock()
subdomain := randStringBytesMaskImprSrc(5)
for {
var found = false
for _, vhost := range server.vhosts {
if vhost.Subdomain == subdomain {
subdomain = randStringBytesMaskImprSrc(5)
found = true
break
}
}
if found {
continue
}
break
}
// Setup vhost
vhost, err := url.Parse("http://" + addr)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(vhost)
mux.HandleFunc(subdomain+mainDomain, proxyHandler(proxy))
// Add and save the vhost in our server object
server.Lock()
server.vhosts[conn.RemoteAddr()] = &VHost{
User: conn.User(),
Subdomain: subdomain,
Addr: addr,
}
server.Unlock()
go func() {
for {
c, err := ln.Accept()
if err != nil {
// TODO: log accept failure
log.Println("accept:", err)
break
}
originAddr, orignPortStr, _ := net.SplitHostPort(c.RemoteAddr().String())
originPort, _ := strconv.Atoi(orignPortStr)
payload := ssh.Marshal(&RemoteForwardChannelData{
DestAddr: reqPayload.BindAddr,
DestPort: reqPayload.BindPort,
OriginAddr: originAddr,
OriginPort: uint32(originPort),
})
go func() {
ch, reqs, err := conn.OpenChannel(forwardedTCPChannelType, payload)
if err != nil {
// TODO: log failure to open channel
log.Println(err)
c.Close()
return
}
go ssh.DiscardRequests(reqs)
go func() {
defer ch.Close()
defer c.Close()
io.Copy(ch, c)
}()
go func() {
defer ch.Close()
defer c.Close()
io.Copy(c, ch)
}()
}()
}
server.Lock()
delete(server.forwards, addr)
server.Unlock()
}()
req.Reply(true, ssh.Marshal(&RemoteForwardSuccess{reqPayload.BindPort}))
case "cancel-tcpip-forward":
var reqPayload RemoteForwardCancelRequest
if err := ssh.Unmarshal(req.Payload, &reqPayload); err != nil {
// TODO: log parse failure
req.Reply(false, []byte{})
}
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(port))
server.Lock()
ln, ok := server.forwards[addr]
server.Unlock()
if ok {
ln.Close()
}
req.Reply(true, []byte{})
default:
req.Reply(false, []byte{})
}
}
}
// Handle the session SSH channel
func (server *SSHServer) handleChannel(newChannel ssh.NewChannel, conn *ssh.ServerConn, mux *ProxyMux) {
if t := newChannel.ChannelType(); t != "session" {
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
return
}
// At this point, we have the opportunity to reject the client's
// request for another logical connection
connection, requests, err := newChannel.Accept()
if err != nil {
log.Printf("Could not accept channel (%s)", err)
return
}
// Wait and get subdomain for connection
// Then send information to user
for {
server.Lock()
vhost, ok := server.vhosts[conn.RemoteAddr()]
server.Unlock()
if ok {
var subdomainText bytes.Buffer
subdomainText.WriteString("\x1b[32mForwarding HTTP traffic from \x1b[4m")
if sslActive {
subdomainText.WriteString("https")
} else {
subdomainText.WriteString("http")
}
subdomainText.WriteString("://")
subdomainText.WriteString(vhost.Subdomain)
subdomainText.WriteString(mainDomain)
subdomainText.WriteString("\x1b[24m\x1b[39m\r\nPress ctrl-c to quit\r\n")
connection.Write(subdomainText.Bytes())
break
}
}
// Prepare teardown function
close := func() {
server.Lock()
vhost, ok := server.vhosts[conn.RemoteAddr()]
if ok {
ln, ok := server.forwards[vhost.Addr]
if ok {
ln.Close()
}
mux.Deregister(vhost.Subdomain + mainDomain)
}
server.Unlock()
connection.Close()
log.Printf("Session closed")
}
go func() {
for {
reader := bufio.NewReader(connection)
result, _, err := reader.ReadRune()
if err != nil {
log.Println(err)
return
}
switch result {
case 3: // ctrl-c
close()
return
}
}
}()
// Sessions have out-of-band requests such as "shell", "pty-req" and "env"
go func() {
for req := range requests {
switch req.Type {
case "shell":
// We only accept the default shell
// (i.e. no command in the Payload)
if len(req.Payload) == 0 {
req.Reply(true, nil)
}
}
}
}()
}