-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
111 lines (97 loc) · 2.75 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
package scp
import (
"errors"
"fmt"
"net"
"strings"
"golang.org/x/crypto/ssh"
)
var (
// ErrNoClientOption indicates a non-nil ClientOption should be provided
ErrNoClientOption = errors.New("scp: ClientOption is not provided")
)
// Client has the "golang.org/x/crypto/ssh/Client" embedded,
// so it can be used as normal SSH client with additional SCP features.
type Client struct {
// The underlying ssh client
*ssh.Client
// Option for the scp client
scpOpt *ClientOption
// a flag to indicate whether it's root user
rootUser *bool
}
// ClientOption contains several configurable options for SCP client.
type ClientOption struct {
// Use sudo to run remote scp server.
// Default: false.
Sudo bool
// The scp remote server executable file.
// Default: "scp".
//
// If your scp command is not in the default path,
// specify it as "/path/to/scp".
RemoteBinary string
}
// applies the default values if not set
func (o *ClientOption) applyDefault() {
if len(o.RemoteBinary) == 0 {
o.RemoteBinary = "scp"
}
}
// NewClient returns a SSH client with SCP capability.
//
// The serverAddr should be in "host" or "host:port" format.
// If no port is supplied, the default port 22 will be used.
//
// IPv6 serverAddr must be enclosed in square brackets, as in "[::1]" or "[::1]:22"
func NewClient(serverAddr string, sshCfg *ssh.ClientConfig, scpOpt *ClientOption) (*Client, error) {
c, err := dialServer(serverAddr, sshCfg)
if err != nil {
return nil, err
}
return newClient(c, scpOpt)
}
// NewClientFromExistingSSH returns a SSH client with SCP capability.
// It reuse the existing SSH connection without dialing
func NewClientFromExistingSSH(existing *ssh.Client, scpOpt *ClientOption) (*Client, error) {
return newClient(existing, scpOpt)
}
func newClient(ssh *ssh.Client, opt *ClientOption) (*Client, error) {
if opt == nil {
return nil, ErrNoClientOption
}
opt.applyDefault()
return &Client{Client: ssh, scpOpt: opt}, nil
}
// dial SSH to given server addr
func dialServer(addr string, cfg *ssh.ClientConfig) (*ssh.Client, error) {
ep, err := addDefaultPort(addr)
if err != nil {
return nil, err
}
return ssh.Dial("tcp", ep, cfg)
}
// add default port 22 if needed
func addDefaultPort(addr string) (string, error) {
_, _, err := net.SplitHostPort(addr)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
newAddr := net.JoinHostPort(strings.Trim(addr, "[]"), "22")
// Test the addr again
if _, _, err = net.SplitHostPort(newAddr); err == nil {
return newAddr, nil
}
}
return "", fmt.Errorf("error parsing serverAddr: %s", err)
}
return addr, nil
}
func (c *Client) isRootUser() bool {
if c.rootUser != nil {
// short path
return *c.rootUser
}
result := c.User() == "root"
c.rootUser = &result
return *c.rootUser
}