-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsssh.go
382 lines (307 loc) · 7.92 KB
/
sssh.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
// Package sssh (simple ssh) provides a wrapper that simplifies creating
// an ssh session (using golang.org/x/crypto/ssh).
//
// The package supports creating a session, authenticated via username/password
// or username/private key, with optional socks5 proxy or jump proxy.
//
// This is roughly equivalent to an ssh connection with the following options:
//
// Host hostname
// ProxyCommand /usr/bin/nc -x proxyserver:port %h %p
// User username
// IdentityFile privatekey-file
//
package sssh
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/proxy"
)
type sshConfig struct {
clientConfig *ssh.ClientConfig
proxyAddress string
jumpConfig *sshConfig
}
// ConnectOption is the common type for NewSession options
type ConnectOption func(c *sshConfig) error
// User sets the user name for authentication
func User(user string) ConnectOption {
return func(c *sshConfig) error {
c.clientConfig.User = user
return nil
}
}
// Password sets the user password for authentication
func Password(password string) ConnectOption {
return func(c *sshConfig) error {
c.clientConfig.Auth = append(c.clientConfig.Auth, ssh.Password(password))
return nil
}
}
// Timeout sets the connection timeout
func Timeout(t time.Duration) ConnectOption {
return func(c *sshConfig) error {
c.clientConfig.Timeout = t
return nil
}
}
// PrivateKeyFile sets the private key file for authentication
func PrivateKeyFile(keyFile string) ConnectOption {
return func(c *sshConfig) error {
key, err := ioutil.ReadFile(keyFile)
if err != nil {
return err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return err
}
c.clientConfig.Auth = append(c.clientConfig.Auth, ssh.PublicKeys(signer))
return nil
}
}
// PrivateKey sets the private key for authentication
func PrivateKey(key []byte) ConnectOption {
return func(c *sshConfig) error {
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return err
}
c.clientConfig.Auth = append(c.clientConfig.Auth, ssh.PublicKeys(signer))
return nil
}
}
//type KeyboardInteractiveChallenge func(user, instruction string, questions []string, echos []bool) (answers []string, err error)
type KeyboardInteractiveChallenge = ssh.KeyboardInteractiveChallenge
// KeyboardInteractive sets the authentication mode to keyboar/interactive
func KeyboardInteractive(cb KeyboardInteractiveChallenge) ConnectOption {
return func(c *sshConfig) error {
c.clientConfig.Auth = append(c.clientConfig.Auth, ssh.KeyboardInteractive(cb))
return nil
}
}
// Banner sets the banner callback, called when the remote host sends the banner
func Banner(callback ssh.BannerCallback) ConnectOption {
return func(c *sshConfig) error {
c.clientConfig.BannerCallback = callback
return nil
}
}
// SocksProxy sets the (socks5) proxy address (host:port)
func SocksProxy(proxy string) ConnectOption {
if !strings.Contains(proxy, ":") {
proxy = proxy + ":22"
}
return func(c *sshConfig) error {
c.proxyAddress = proxy
c.jumpConfig = nil
return nil
}
}
func makeConfig(options ...ConnectOption) (*sshConfig, error) {
config := &sshConfig{
clientConfig: &ssh.ClientConfig{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
}
for _, opt := range options {
if err := opt(config); err != nil {
return nil, err
}
}
return config, nil
}
// JumpProxy configures the session to jump through one proxy server
func JumpProxy(proxy string, options ...ConnectOption) ConnectOption {
if !strings.Contains(proxy, ":") {
proxy = proxy + ":22"
}
return func(c *sshConfig) (err error) {
c.proxyAddress = ""
c.jumpConfig, err = makeConfig(options...)
if c.jumpConfig != nil {
c.jumpConfig.proxyAddress = proxy
}
return
}
}
// NewClient creates a new ssh client/connection to host (host:port) with the specified options
func NewClient(host string, options ...ConnectOption) (*ssh.Client, error) {
if !strings.Contains(host, ":") {
host = host + ":22"
}
config, err := makeConfig(options...)
if err != nil {
return nil, err
}
client, err := sshClient(host, config)
if err != nil {
return nil, err
}
return client, nil
}
// NewSession creates a new ssh session/connection to host (host:port) with the specified options
func NewSession(host string, options ...ConnectOption) (*ssh.Session, error) {
client, err := NewClient(host, options...)
if err != nil {
return nil, err
}
return client.NewSession()
}
func sshClient(address string, config *sshConfig) (*ssh.Client, error) {
if config.proxyAddress != "" {
dialer, err := proxy.SOCKS5("tcp", config.proxyAddress, nil, proxy.Direct)
if err != nil {
return nil, err
}
conn, err := dialer.Dial("tcp", address)
if err != nil {
return nil, err
}
c, chans, reqs, err := ssh.NewClientConn(conn, address, config.clientConfig)
if err != nil {
return nil, err
}
return ssh.NewClient(c, chans, reqs), nil
} else if config.jumpConfig != nil {
proxy, err := ssh.Dial("tcp", config.jumpConfig.proxyAddress, config.jumpConfig.clientConfig)
if err != nil {
return nil, err
}
conn, err := proxy.Dial("tcp", address)
if err != nil {
return nil, err
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, address, config.clientConfig)
if err != nil {
return nil, err
}
return ssh.NewClient(ncc, chans, reqs), nil
}
return ssh.Dial("tcp", address, config.clientConfig)
}
var (
ErrDir = fmt.Errorf("source is a directory")
ErrRemote = fmt.Errorf("remote error")
ErrRemoteFatal = fmt.Errorf("remote fatal error")
ErrRemoteUnknown = fmt.Errorf("remote unknown error")
)
func CopyFile(client *ssh.Client, dest, src string) error {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
if stat.IsDir() {
return ErrDir
}
return Copy(client, dest, stat.Name(), stat.Size(), stat.Mode().Perm()&os.ModePerm, f)
}
func Copy(client *ssh.Client, dest, src string, size int64, perm os.FileMode, reader io.Reader) error {
session, err := client.NewSession()
if err != nil {
return err
}
defer session.Close()
rin, err := session.StdinPipe()
if err != nil {
return err
}
defer rin.Close()
rout, err := session.StdoutPipe()
if err != nil {
return err
}
checkStatus := func() (err error) {
var b [256]byte
if _, err := rout.Read(b[:1]); err != nil {
return err
}
switch b[0] {
case 0:
err = nil
case 1, 2:
err = ErrRemote
if b[0] == 2 {
err = ErrRemoteFatal
}
n, _ := rout.Read(b[:])
if n > 0 {
fmt.Printf("Error %v: %v", err, string(b[:n]))
}
default:
err = ErrRemoteUnknown
}
return
}
if err = session.Start("scp -t " + dest); err != nil {
return err
}
cmd := fmt.Sprintf("C%04o %d %s\n", perm, size, src)
_, err = io.WriteString(rin, cmd)
if err != nil {
return err
}
if err = checkStatus(); err != nil {
return err
}
if _, err = io.Copy(rin, reader); err != nil {
return err
}
var z [1]byte
rin.Write(z[:]) // send \0 to finish
if err = checkStatus(); err != nil {
return err
}
rin.Close()
return session.Wait()
}
func Shell(client *ssh.Client) error {
session, err := client.NewSession()
if err != nil {
return err
}
defer session.Close()
session.Stdin = os.Stdin
session.Stdout = os.Stdout
session.Stderr = os.Stderr
stdin := int(os.Stdin.Fd())
if terminal.IsTerminal(stdin) {
state, err := terminal.MakeRaw(stdin)
if err != nil {
return err
}
defer terminal.Restore(stdin, state)
modes := ssh.TerminalModes{
ssh.ECHO: 1,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
w, h, err := terminal.GetSize(stdin)
if err != nil {
return err
}
term := os.Getenv("TERM")
if term == "" {
term = "vt100"
}
if err := session.RequestPty(term, h, w, modes); err != nil {
return err
}
}
// Start remote shell
if err := session.Shell(); err != nil {
return err
}
return session.Wait()
}