-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient.go
388 lines (341 loc) · 10.4 KB
/
client.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package ssh2docker
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"text/template"
"github.com/apex/log"
"github.com/flynn/go-shlex"
"github.com/kr/pty"
"github.com/moul/ssh2docker/pkg/envhelper"
"github.com/moul/ssh2docker/pkg/ttyhelper"
"golang.org/x/crypto/ssh"
)
var clientCounter = 0
// Client is one client connection
type Client struct {
Idx int
ChannelIdx int
Conn *ssh.ServerConn
Chans <-chan ssh.NewChannel
Reqs <-chan *ssh.Request
Server *Server
Pty, Tty *os.File
Config *ClientConfig
ClientID string
}
type ClientConfig struct {
ImageName string `json:"image-name,omitempty"`
RemoteUser string `json:"remote-user,omitempty"`
Env envhelper.Environment `json:"env,omitempty"`
Command []string `json:"command,omitempty"`
DockerRunArgs []string `json:"docker-run-args,omitempty"`
DockerExecArgs []string `json:"docker-exec-args,omitempty"`
User string `json:"user,omitempty"`
Keys []string `json:"keys,omitempty"`
AuthenticationMethod string `json:"authentication-method,omitempty"`
AuthenticationComment string `json:"authentication-coment,omitempty"`
EntryPoint string `json:"entrypoint,omitempty"`
AuthenticationAttempts int `json:"authentication-attempts,omitempty"`
Allowed bool `json:"allowed,omitempty"`
IsLocal bool `json:"is-local,omitempty"`
UseTTY bool `json:"use-tty,omitempty"`
}
// NewClient initializes a new client
func NewClient(conn *ssh.ServerConn, chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request, server *Server) *Client {
client := Client{
Idx: clientCounter,
ClientID: conn.RemoteAddr().String(),
ChannelIdx: 0,
Conn: conn,
Chans: chans,
Reqs: reqs,
Server: server,
// Default ClientConfig, will be overwritten if a hook is used
Config: &ClientConfig{
ImageName: strings.Replace(conn.User(), "_", "/", -1),
RemoteUser: "anonymous",
AuthenticationMethod: "noauth",
AuthenticationComment: "",
AuthenticationAttempts: 0,
Env: envhelper.Environment{},
Command: make([]string, 0),
},
}
if server.LocalUser != "" {
client.Config.IsLocal = client.Config.ImageName == server.LocalUser
}
if _, found := server.ClientConfigs[client.ClientID]; !found {
server.ClientConfigs[client.ClientID] = client.Config
}
client.Config = server.ClientConfigs[conn.RemoteAddr().String()]
client.Config.Env.ApplyDefaults()
clientCounter++
remoteAddr := strings.Split(client.ClientID, ":")
log.Infof("Accepted %s for %s from %s port %s ssh2: %s", client.Config.AuthenticationMethod, conn.User(), remoteAddr[0], remoteAddr[1], client.Config.AuthenticationComment)
return &client
}
// HandleRequests handles SSH requests
func (c *Client) HandleRequests() error {
go func(in <-chan *ssh.Request) {
for req := range in {
log.Debugf("HandleRequest: %v", req)
if req.WantReply {
req.Reply(false, nil)
}
}
}(c.Reqs)
return nil
}
// HandleChannels handles SSH channels
func (c *Client) HandleChannels() error {
for newChannel := range c.Chans {
if err := c.HandleChannel(newChannel); err != nil {
return err
}
}
return nil
}
// HandleChannel handles one SSH channel
func (c *Client) HandleChannel(newChannel ssh.NewChannel) error {
if newChannel.ChannelType() != "session" {
log.Debugf("Unknown channel type: %s", newChannel.ChannelType())
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
return nil
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Errorf("newChannel.Accept failed: %v", err)
return err
}
c.ChannelIdx++
log.Debugf("HandleChannel.channel (client=%d channel=%d)", c.Idx, c.ChannelIdx)
log.Debug("Creating pty...")
c.Pty, c.Tty, err = pty.Open()
if err != nil {
log.Errorf("pty.Open failed: %v", err)
return nil
}
c.HandleChannelRequests(channel, requests)
return nil
}
func (c *Client) alterArg(arg string) (string, error) {
tmpl, err := template.New("run-args").Parse(arg)
if err != nil {
return "", err
}
var buff bytes.Buffer
if err := tmpl.Execute(&buff, c.Config); err != nil {
return "", err
}
return buff.String(), nil
}
func (c *Client) alterArgs(args []string) error {
for idx, arg := range args {
newArg, err := c.alterArg(arg)
if err != nil {
return err
}
args[idx] = newArg
}
return nil
}
func (c *Client) runCommand(channel ssh.Channel, entrypoint string, command []string) {
var cmd *exec.Cmd
var err error
if c.Config.IsLocal {
cmd = exec.Command(entrypoint, command...)
} else {
// checking if a container already exists for this user
existingContainer := ""
if !c.Server.NoJoin {
cmd = exec.Command("docker", "ps", "--filter=label=ssh2docker", fmt.Sprintf("--filter=label=image=%s", c.Config.ImageName), fmt.Sprintf("--filter=label=user=%s", c.Config.RemoteUser), "--quiet", "--no-trunc")
cmd.Env = c.Config.Env.List()
buf, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("docker ps ... failed: %v", err)
channel.Close()
return
}
existingContainer = strings.TrimSpace(string(buf))
}
// Opening Docker process
if existingContainer != "" {
// Attaching to an existing container
args := []string{"exec"}
if len(c.Config.DockerExecArgs) > 0 {
args = append(args, c.Config.DockerExecArgs...)
if err := c.alterArgs(args); err != nil {
log.Errorf("Failed to execute template on args: %v", err)
return
}
} else {
inlineExec, err := c.alterArg(c.Server.DockerExecArgsInline)
if err != nil {
log.Errorf("Failed to execute template on arg: %v", err)
return
}
execArgs, err := shlex.Split(inlineExec)
if err != nil {
log.Errorf("Failed to split arg %q: %v", inlineExec, err)
return
}
args = append(args, execArgs...)
}
args = append(args, existingContainer)
if entrypoint != "" {
args = append(args, entrypoint)
}
args = append(args, command...)
log.Debugf("Executing 'docker %s'", strings.Join(args, " "))
cmd = exec.Command("docker", args...)
cmd.Env = c.Config.Env.List()
} else {
// Creating and attaching to a new container
args := []string{"run"}
if len(c.Config.DockerRunArgs) > 0 {
args = append(args, c.Config.DockerRunArgs...)
if err := c.alterArgs(args); err != nil {
log.Errorf("Failed to execute template on args: %v", err)
return
}
} else {
inlineRun, err := c.alterArg(c.Server.DockerRunArgsInline)
if err != nil {
log.Errorf("Failed to execute template on arg: %v", err)
return
}
runArgs, err := shlex.Split(inlineRun)
if err != nil {
log.Errorf("Failed to split arg %q: %v", inlineRun, err)
return
}
args = append(args, runArgs...)
}
args = append(args, "--label=ssh2docker", fmt.Sprintf("--label=user=%s", c.Config.RemoteUser), fmt.Sprintf("--label=image=%s", c.Config.ImageName))
if c.Config.User != "" {
args = append(args, "-u", c.Config.User)
}
if entrypoint != "" {
args = append(args, "--entrypoint", entrypoint)
}
args = append(args, c.Config.ImageName)
args = append(args, command...)
log.Debugf("Executing 'docker %s'", strings.Join(args, " "))
cmd = exec.Command("docker", args...)
cmd.Env = c.Config.Env.List()
}
}
if c.Server.Banner != "" {
banner := c.Server.Banner
banner = strings.Replace(banner, "\r", "", -1)
banner = strings.Replace(banner, "\n", "\n\r", -1)
fmt.Fprintf(channel, "%s\n\r", banner)
}
cmd.Stdout = channel
cmd.Stdin = channel
cmd.Stderr = channel
var wg sync.WaitGroup
if c.Config.UseTTY {
cmd.Stdout = c.Tty
cmd.Stdin = c.Tty
cmd.Stderr = c.Tty
wg.Add(1)
go func() {
io.Copy(channel, c.Pty)
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(c.Pty, channel)
wg.Done()
}()
defer wg.Wait()
}
cmd.SysProcAttr = &syscall.SysProcAttr{
Setctty: c.Config.UseTTY,
Setsid: true,
}
err = cmd.Start()
if err != nil {
log.Warnf("cmd.Start failed: %v", err)
channel.Close()
return
}
if err := cmd.Wait(); err != nil {
log.Warnf("cmd.Wait failed: %v", err)
}
channel.Close()
log.Debugf("cmd.Wait done")
}
// HandleChannelRequests handles channel requests
func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh.Request) {
go func(in <-chan *ssh.Request) {
defer c.Tty.Close()
for req := range in {
ok := false
switch req.Type {
case "shell":
log.Debugf("HandleChannelRequests.req shell")
if len(req.Payload) != 0 {
break
}
ok = true
entrypoint := ""
if c.Config.EntryPoint != "" {
entrypoint = c.Config.EntryPoint
}
var args []string
if c.Config.Command != nil {
args = c.Config.Command
}
if entrypoint == "" && len(args) == 0 {
args = []string{c.Server.DefaultShell}
}
c.runCommand(channel, entrypoint, args)
case "exec":
command := string(req.Payload[4:])
log.Debugf("HandleChannelRequests.req exec: %q", command)
ok = true
args, err := shlex.Split(command)
if err != nil {
log.Errorf("Failed to parse command %q: %v", command, args)
}
c.runCommand(channel, c.Config.EntryPoint, args)
case "pty-req":
ok = true
c.Config.UseTTY = true
termLen := req.Payload[3]
c.Config.Env["TERM"] = string(req.Payload[4 : termLen+4])
c.Config.Env["USE_TTY"] = "1"
w, h := ttyhelper.ParseDims(req.Payload[termLen+4:])
ttyhelper.SetWinsize(c.Pty.Fd(), w, h)
log.Debugf("HandleChannelRequests.req pty-req: TERM=%q w=%q h=%q", c.Config.Env["TERM"], int(w), int(h))
case "window-change":
w, h := ttyhelper.ParseDims(req.Payload)
ttyhelper.SetWinsize(c.Pty.Fd(), w, h)
continue
case "env":
keyLen := req.Payload[3]
key := string(req.Payload[4 : keyLen+4])
valueLen := req.Payload[keyLen+7]
value := string(req.Payload[keyLen+8 : keyLen+8+valueLen])
log.Debugf("HandleChannelRequets.req 'env': %s=%q", key, value)
c.Config.Env[key] = value
default:
log.Debugf("Unhandled request type: %q: %v", req.Type, req)
}
if req.WantReply {
if !ok {
log.Debugf("Declining %s request...", req.Type)
}
req.Reply(ok, nil)
}
}
}(requests)
}