-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
217 lines (177 loc) · 5.41 KB
/
ssh.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
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"syscall"
"time"
"unsafe"
"github.com/creack/pty"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
)
// has someone connected yet?
var hasConnection bool = false
func setWinsize(f *os.File, w, h int) {
// best effort set the window size
//nolint:errcheck
syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
uintptr(syscall.TIOCSWINSZ),
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
}
//nolint:errcheck
func dismissSessionHandler(s ssh.Session) {
io.WriteString(s, "This session has already been connected to. You cannot have multiple connections to the same session.\n")
s.Exit(1)
}
func sessionHandler(options *RemoteShellOptions, notify chan bool, s ssh.Session) {
log.Printf("Session User (requested): %s\n", s.User())
log.Printf("Session RemoteIP: %s\n", s.RemoteAddr().String())
log.Printf("Session ClientVersion: %s\n", s.Context().ClientVersion())
log.Printf("Session ServerVersion: %s\n", s.Context().ServerVersion())
log.Printf("Session SessionID: %s\n", s.Context().SessionID())
hasConnection = true
cmd := exec.Command(options.shellCommand)
ptyReq, winCh, isPty := s.Pty()
if isPty {
log.Println("Starting PTY Session")
cmd.Env = append(filteredEnvironmentVars(),
fmt.Sprintf("TERM=%s", ptyReq.Term),
fmt.Sprintf("HOME=%s", options.homeDir),
fmt.Sprintf("USER=%s", options.currentUserName),
fmt.Sprintf("LOGNAME=%s", options.currentUserName),
fmt.Sprintf("SHELL=%s", options.shellCommand),
fmt.Sprintf("C87RS_SESSIONID=%s", s.Context().SessionID()),
)
f, err := pty.Start(cmd)
check(err)
go func() {
for win := range winCh {
setWinsize(f, win.Width, win.Height)
}
}()
// if these error, then it will abort the session.
go func() {
io.Copy(f, s) // stdin
}()
io.Copy(s, f) // stdout
log.Println("Shell command output has ended. Waiting for command to end.")
err = cmd.Wait()
if err != nil {
log.Println("The requested shell errored out. Are you sure it was correct?")
s.Exit(1)
} else {
log.Println("Shell command terminated successfully")
s.Exit(0)
}
} else {
log.Println("NoPTY requested")
io.WriteString(s, "No PTY requested.\n")
s.Exit(1)
}
log.Println("Session ended")
notify <- true
}
func openSSHSocket(options *RemoteShellOptions) net.Listener {
connAddr := fmt.Sprintf(":%d", options.port)
// lc := net.ListenConfig{
// C
// }
sock, err := net.Listen("tcp", connAddr)
check(err)
return sock
}
func startSSHService() {
publicKeys := exportAuthorizedKeys()
if len(publicKeys) == 0 {
panic("No SSH keys were provided")
}
log.Println("Starting SSH Service")
notificationChannel := make(chan bool)
ssh.Handle(func(s ssh.Session) {
// log.Println("Handler invoked")
if hasConnection {
dismissSessionHandler(s)
} else {
sessionHandler(&globalOptions, notificationChannel, s)
}
})
// Auth Request
pubKeyAuthHandle := func(ctx ssh.Context, key ssh.PublicKey) bool {
for _, pubKey := range publicKeys {
if ssh.KeysEqual(key, pubKey) {
return true
}
}
return false
}
serverConfCallback := func(ctx ssh.Context) *gossh.ServerConfig {
return &gossh.ServerConfig{
NoClientAuth: false,
BannerCallback: func(conn gossh.ConnMetadata) string {
return "You are now connected to Cloud87 Remote Shell instance.\n\n"
},
AuthLogCallback: func(conn gossh.ConnMetadata, method string, err error) {
log.Printf("Auth Attempt: %s method=%s err=%s\n", conn.RemoteAddr().String(), method, err)
},
}
}
sessionRequestCallback := func(sess ssh.Session, requestType string) bool {
log.Printf("Session Requested by %s for type=%s\n", sess.Context().SessionID(), requestType)
return true
}
server := &ssh.Server{
Version: fmt.Sprintf("Cloud87-%s", buildVersion),
PublicKeyHandler: pubKeyAuthHandle,
PasswordHandler: nil,
KeyboardInteractiveHandler: nil,
LocalPortForwardingCallback: nil,
ReversePortForwardingCallback: nil,
SessionRequestCallback: sessionRequestCallback,
ServerConfigCallback: serverConfCallback,
MaxTimeout: globalOptions.timeLimit,
IdleTimeout: globalOptions.idleTimeout,
}
log.Println("Creating socket")
sock := openSSHSocket(&globalOptions)
// boot the server socket
go func() {
log.Println("Booting SSH Listener")
err := server.Serve(sock)
// if err != nil && err != ssh.ErrServerClosed {
if err != nil && !errors.Is(err, ssh.ErrServerClosed) {
panic(err)
}
log.Println("Serve goroutine ending")
notificationChannel <- true
}()
go func() {
log.Printf("Starting connection grace timer: %s\n", globalOptions.connectionGrace)
time.Sleep(globalOptions.connectionGrace)
if hasConnection {
log.Println("Server has received a connection within grace period. Not killing self.")
return
}
log.Println("Triggering socket shutdown. No longer accepting connections")
err := server.Shutdown(context.Background())
check(err)
notificationChannel <- true
}()
select {
case <-time.After(globalOptions.timeLimit):
log.Println("Reached deadline for service. Dying")
checkNoPanic(server.Close())
case <-notificationChannel:
log.Println("Death has been requested!")
}
// <-notificationChannel
log.Println("End of startSSHService")
}
// https://gist.github.com/protosam/53cf7970e17e06135f1622fa9955415f