-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
338 lines (297 loc) · 8.85 KB
/
server.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
// A small SSH daemon providing bash sessions
//
// Server:
// cd my/new/dir/
// #generate server keypair
// ssh-keygen -t rsa
// go get -v .
// go run sshd.go
//
// Client:
// ssh foo@localhost -p 2200 #pass=bar
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os/exec"
"os"
"sync"
"strings"
"strconv"
"github.com/kr/pty"
"golang.org/x/crypto/ssh"
)
type rsshtKey struct {
machID string
sshPort uint16
}
type rsshtSession struct {
machID string
sshConn *ssh.ServerConn
sshListener net.Listener
httpListener net.Listener
quit chan bool
}
var authorizedKeys map[string]rsshtKey
func acceptAndForward(listener net.Listener, session *rsshtSession, sshReq *forwardedTcpIpRequest) {
for {
tcpConn, err := listener.Accept()
if err != nil {
select {
case <-session.quit:
return
default:
log.Printf("Failed to accept incoming connection (%s)", err)
continue
}
}
sshc, reqs, err := session.sshConn.OpenChannel("forwarded-tcpip", ssh.Marshal(sshReq))
if err != nil {
log.Fatal("Failed to open ssh channel:", err, "for:", sshReq)
}
go ssh.DiscardRequests(reqs)
close := func() {
sshc.Close()
tcpConn.Close()
log.Printf("Session closed")
}
var once sync.Once
go func() {
io.Copy(tcpConn, sshc)
once.Do(close)
}()
go func() {
io.Copy(sshc, tcpConn)
once.Do(close)
}()
}
}
func createSession(sshConn *ssh.ServerConn) (s *rsshtSession) {
opts := authorizedKeys[sshConn.Permissions.Extensions["key"]]
session := rsshtSession{ machID: opts.machID, sshConn: sshConn, quit: make(chan bool) }
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", opts.sshPort))
if err != nil {
log.Fatalln("Failed to listen on tcp port:", opts.sshPort, "for machid:", opts.machID, "error:", err)
}
session.sshListener = listener
sockname := "http-sockets/"+opts.machID
os.Remove(sockname)
listener, err = net.Listen("unix", sockname)
if err != nil {
log.Fatal(err)
}
session.httpListener = listener
go acceptAndForward(session.sshListener, &session, &forwardedTcpIpRequest{ "localhost", 22, "proxy", 0 })
go acceptAndForward(session.httpListener, &session, &forwardedTcpIpRequest{ "localhost", 80, "proxy", 0 })
return &session
}
func (s *rsshtSession) Close() {
close(s.quit)
s.sshListener.Close()
s.httpListener.Close()
s.sshConn.Close()
}
func main() {
authorizedKeys = loadAuthorizedKeys("./authorized_keys")
hostKey := loadHostKey("./ssh_host_rsa_key")
os.MkdirAll("./http-sockets", 0700)
rsshtSessions := map[string]*rsshtSession{}
config := &ssh.ServerConfig{
PublicKeyCallback: func (c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
log.Println(c.RemoteAddr(), "authenticate with", key.Type(), strconv.Quote(string(key.Marshal())))
keyString := string(key.Marshal())
if _, found := authorizedKeys[keyString]; found {
return &ssh.Permissions{
Extensions: map[string]string{
"key": keyString,
},
}, nil
}
return nil, fmt.Errorf("key rejected for %q", c.User())
},
}
config.AddHostKey(hostKey)
listener, err := net.Listen("tcp", "0.0.0.0:2200")
if err != nil {
log.Fatalf("Failed to listen on 2200 (%s)", err)
}
// Accept all connections
log.Print("Listening on 2200...")
for {
tcpConn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept incoming connection (%s)", err)
continue
}
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, config)
if err != nil {
log.Printf("Failed to handshake (%s)", err)
continue
}
key := sshConn.Permissions.Extensions["key"]
oldSession, found := rsshtSessions[key]
if found {
log.Printf("Cleaning up old session for key: %s (%s, %s)",
strconv.Quote(key), oldSession.sshConn.RemoteAddr(), oldSession.sshConn.ClientVersion())
oldSession.Close()
}
rsshtSessions[key] = createSession(sshConn)
log.Printf("New SSH connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
go func(in <-chan *ssh.Request) {
for req := range in {
log.Println("OOB Request:", req.Type, "wants reply:", req.WantReply, "payload:", strconv.Quote(string(req.Payload)))
switch req.Type {
case "tcpip-forward":
req.Reply(true, nil)
opts := tcpIpForwardRequest{}
ssh.Unmarshal(req.Payload, &opts)
log.Println("forward:", opts)
case "[email protected]":
req.Reply(true, nil)
default:
req.Reply(false, []byte("unknown request"))
}
}
}(reqs)
// Accept all channels
go handleChannels(chans)
}
}
func loadAuthorizedKeys(fname string) (map[string]rsshtKey) {
keys := map[string]rsshtKey{}
bytes, err := ioutil.ReadFile(fname)
if err != nil {
log.Fatal("Failed to load authorized keys from:", fname)
}
for len(bytes) > 0 {
key, comment, options, rest, err := ssh.ParseAuthorizedKey(bytes)
if err != nil {
log.Fatal("failed to parse authorized keys at: ", strconv.Quote(string(bytes[:30])))
}
log.Println("found authorized key:", string(ssh.MarshalAuthorizedKey(key)), "with comment:", comment, "and options:", options)
keyString := string(key.Marshal())
rsshtkey := rsshtKey{}
for _, option := range options {
if val, found := withoutPrefix(option, "machid="); found {
rsshtkey.machID = val
}
if val, found := withoutPrefix(option, "sshport="); found {
port, err := strconv.ParseUint(val, 10, 16)
if err != nil {
log.Fatalf("invalid sshport: %v for key: ", val, ssh.MarshalAuthorizedKey(key))
}
rsshtkey.sshPort = uint16(port)
}
}
keys[keyString] = rsshtkey
bytes = rest
}
return keys
}
func loadHostKey(fname string) (ssh.Signer) {
privateBytes, err := ioutil.ReadFile(fname)
if err != nil {
log.Fatal("Failed to load private key (./ssh_host_rsa_key)")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("Failed to parse private key")
}
return private
}
func withoutPrefix(str string, prefix string) (string, bool) {
if strings.HasPrefix(str, prefix) {
return str[len(prefix):], true
}
return "", false
}
func handleChannels(chans <-chan ssh.NewChannel) {
// Service the incoming Channel channel in go routine
for newChannel := range chans {
go handleChannel(newChannel)
}
}
func handleChannel(newChannel ssh.NewChannel) {
// Since we're handling a shell, we expect a
// channel type of "session". The also describes
// "x11", "direct-tcpip" and "forwarded-tcpip"
// channel types.
if t := newChannel.ChannelType(); t != "session" && t != "direct-tcpip" {
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
return
}
log.Println("accepting channel:", newChannel.ChannelType(), "with payload:", strconv.Quote(string(newChannel.ExtraData())))
// 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
}
// Fire up bash for this session
bash := exec.Command("bash")
// Prepare teardown function
close := func() {
connection.Close()
_, err := bash.Process.Wait()
if err != nil {
log.Printf("Failed to exit bash (%s)", err)
}
log.Printf("Session closed")
}
// Allocate a terminal for this channel
log.Print("Creating pty...")
bashf, err := pty.Start(bash)
if err != nil {
log.Printf("Could not start pty (%s)", err)
close()
return
}
//pipe session to bash and visa-versa
var once sync.Once
go func() {
io.Copy(connection, bashf)
once.Do(close)
}()
go func() {
io.Copy(bashf, connection)
once.Do(close)
}()
// 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)
}
// case "pty-req":
// termLen := req.Payload[3]
// w, h := parseDims(req.Payload[termLen+4:])
// SetWinsize(bashf.Fd(), w, h)
// // Responding true (OK) here will let the client
// // know we have a pty ready for input
// req.Reply(true, nil)
// case "window-change":
// w, h := parseDims(req.Payload)
// SetWinsize(bashf.Fd(), w, h)
}
}
}()
}
// SSH protocol frames
type tcpIpForwardRequest struct {
Address string
Port uint32
}
type forwardedTcpIpRequest struct {
Address string
Port uint32
SrcAddress string
SrcPort uint32
}