This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
/
client.go
85 lines (73 loc) · 1.83 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
package libscp
import (
"net"
"golang.org/x/crypto/ssh"
)
// Client is the remote machine that should be connected to for the transfer.
// Specifically, what hostname and port.
type Client struct {
addr string
port string
useAgent bool
key *Key
credentials *Credentials
client *ssh.Client
}
// NewAgentClient creates a new host object that will connect using the SSH
// Agent signers
func NewAgentClient(addr, port string, creds *Credentials) *Client {
return &Client{
addr: addr,
port: port,
useAgent: true,
credentials: creds,
}
}
// NewKeyClient creates a new host object that will connect using the SSH
// Agent signers
func NewKeyClient(addr, port string, creds *Credentials, key *Key) *Client {
return &Client{
addr: addr,
port: port,
useAgent: false,
credentials: creds,
key: key,
}
}
// Connect takes the host object and connects, creating an SSH Client connection
func (c *Client) Connect() error {
// An SSH client is represented with a ClientConn.
//
// To authenticate with the remote server you must pass at least one
// implementation of AuthMethod via the Auth field in ClientConfig.
var co *ssh.ClientConfig
var err error
if c.useAgent {
co, err = AgentConfig(c.credentials.Username)
if err != nil {
return err
}
} else {
co, err = KeyConfig(c.credentials.Username, c.key)
if err != nil {
return err
}
}
client, err := ssh.Dial("tcp", net.JoinHostPort(c.addr, c.port), co)
if err != nil {
return err
}
c.client = client
return nil
}
// VerifyClient checks if we have a client, and if not attempts to connect
func (c *Client) VerifyClient() error {
// If we don't have a client yet, we should try to create one
if c.client == nil {
err := c.Connect()
if err != nil {
return err
}
}
return nil
}