/
client.go
285 lines (248 loc) · 7.62 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
package ssh2docker
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"github.com/moul/ssh2docker/vendor/github.com/Sirupsen/logrus"
"github.com/moul/ssh2docker/vendor/github.com/kr/pty"
"github.com/moul/ssh2docker/vendor/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`
Allowed bool `json:"allowed",omitempty`
Env Environment `json:"env",omitempty`
IsLocal bool `json:"is_local",omitempty`
Command []string `json:"command",omitempty`
User string `json:"user",omitempty`
Keys []string `json:"keys",omitempty`
EntryPoint string `json:"entrypoint",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",
Env: 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
}
clientCounter++
logrus.Infof("NewClient (%d): User=%q, ClientVersion=%q", client.Idx, conn.User(), fmt.Sprintf("%x", conn.ClientVersion()))
return &client
}
// HandleRequests handles SSH requests
func (c *Client) HandleRequests() error {
go func(in <-chan *ssh.Request) {
for req := range in {
logrus.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" {
logrus.Debugf("Unknown channel type: %s", newChannel.ChannelType())
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
return nil
}
channel, requests, err := newChannel.Accept()
if err != nil {
logrus.Errorf("newChannel.Accept failed: %v", err)
return err
}
c.ChannelIdx++
logrus.Debugf("HandleChannel.channel (client=%d channel=%d): %v", c.Idx, c.ChannelIdx, channel)
logrus.Debug("Creating pty...")
f, tty, err := pty.Open()
if err != nil {
logrus.Errorf("pty.Open failed: %v", err)
return nil
}
c.Tty = tty
c.Pty = f
c.HandleChannelRequests(channel, requests)
return nil
}
// 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":
logrus.Debugf("HandleChannelRequests.req shell")
if len(req.Payload) != 0 {
break
}
ok = true
var cmd *exec.Cmd
var err error
if c.Config.IsLocal {
cmd = exec.Command("/bin/bash")
} 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 {
logrus.Warnf("docker ps ... failed: %v", err)
continue
}
existingContainer = strings.TrimSpace(string(buf))
}
// Opening Docker process
if existingContainer != "" {
// Attaching to an existing container
shell := c.Server.DefaultShell
if c.Config.EntryPoint != "" {
shell = c.Config.EntryPoint
}
args := []string{"exec", "-it", existingContainer, shell}
logrus.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"}
args = append(args, c.Server.DockerRunArgs...)
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 c.Config.EntryPoint != "" {
args = append(args, "--entrypoint", c.Config.EntryPoint)
}
args = append(args, c.Config.ImageName)
if c.Config.Command != nil {
args = append(args, c.Config.Command...)
} else {
args = append(args, c.Server.DefaultShell)
}
logrus.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 = c.Tty
cmd.Stdin = c.Tty
cmd.Stderr = c.Tty
cmd.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
Setsid: true,
}
err = cmd.Start()
if err != nil {
logrus.Warnf("cmd.Start failed: %v", err)
continue
}
var once sync.Once
close := func() {
channel.Close()
logrus.Debug("session closed")
}
go func() {
io.Copy(channel, c.Pty)
once.Do(close)
}()
go func() {
io.Copy(c.Pty, channel)
once.Do(close)
}()
go func() {
if err := cmd.Wait(); err != nil {
logrus.Warnf("cmd.Wait failed: %v", err)
}
once.Do(close)
}()
case "exec":
command := string(req.Payload)
logrus.Debugf("HandleChannelRequests.req exec: %q", command)
ok = false
fmt.Fprintln(channel, "⚠️ ssh2docker: exec is not yet implemented. https://github.com/moul/ssh2docker/issues/51.")
time.Sleep(3 * time.Second)
case "pty-req":
ok = true
termLen := req.Payload[3]
c.Config.Env["TERM"] = string(req.Payload[4 : termLen+4])
w, h := parseDims(req.Payload[termLen+4:])
SetWinsize(c.Pty.Fd(), w, h)
logrus.Debugf("HandleChannelRequests.req pty-req: TERM=%q w=%q h=%q", c.Config.Env["TERM"], int(w), int(h))
case "window-change":
w, h := parseDims(req.Payload)
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])
logrus.Debugf("HandleChannelRequets.req 'env': %s=%q", key, value)
c.Config.Env[key] = value
default:
logrus.Debugf("Unhandled request type: %q: %v", req.Type, req)
}
if req.WantReply {
if !ok {
logrus.Debugf("Declining %s request...", req.Type)
}
req.Reply(ok, nil)
}
}
}(requests)
}