-
Notifications
You must be signed in to change notification settings - Fork 132
/
client.go
215 lines (173 loc) · 4.34 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
// Copyright 2020 Mohammed El Bahja. All rights reserved.
// Use of this source code is governed by a MIT license.
package goph
import (
"context"
"fmt"
"io"
"net"
"os"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// Client represents Goph client.
type Client struct {
*ssh.Client
Config *Config
}
// Config for Client.
type Config struct {
Auth Auth
User string
Addr string
Port uint
Timeout time.Duration
Callback ssh.HostKeyCallback
BannerCallback ssh.BannerCallback
}
// DefaultTimeout is the timeout of ssh client connection.
var DefaultTimeout = 20 * time.Second
// New starts a new ssh connection, the host public key must be in known hosts.
func New(user string, addr string, auth Auth) (c *Client, err error) {
callback, err := DefaultKnownHosts()
if err != nil {
return
}
c, err = NewConn(&Config{
User: user,
Addr: addr,
Port: 22,
Auth: auth,
Timeout: DefaultTimeout,
Callback: callback,
})
return
}
// NewUnknown starts a ssh connection get client without cheking knownhosts.
// PLEASE AVOID USING THIS, UNLESS YOU KNOW WHAT ARE YOU DOING!
// if there a "man in the middle proxy", this can harm you!
// You can add the key to know hosts and use New() func instead!
func NewUnknown(user string, addr string, auth Auth) (*Client, error) {
return NewConn(&Config{
User: user,
Addr: addr,
Port: 22,
Auth: auth,
Timeout: DefaultTimeout,
Callback: ssh.InsecureIgnoreHostKey(),
})
}
// NewConn returns new client and error if any.
func NewConn(config *Config) (c *Client, err error) {
c = &Client{
Config: config,
}
c.Client, err = Dial("tcp", config)
return
}
// Dial starts a client connection to SSH server based on config.
func Dial(proto string, c *Config) (*ssh.Client, error) {
return ssh.Dial(proto, net.JoinHostPort(c.Addr, fmt.Sprint(c.Port)), &ssh.ClientConfig{
User: c.User,
Auth: c.Auth,
Timeout: c.Timeout,
HostKeyCallback: c.Callback,
BannerCallback: c.BannerCallback,
})
}
// Run starts a new SSH session and runs the cmd, it returns CombinedOutput and err if any.
func (c Client) Run(cmd string) ([]byte, error) {
var (
err error
sess *ssh.Session
)
if sess, err = c.NewSession(); err != nil {
return nil, err
}
defer sess.Close()
return sess.CombinedOutput(cmd)
}
// Run starts a new SSH session with context and runs the cmd. It returns CombinedOutput and err if any.
func (c Client) RunContext(ctx context.Context, name string) ([]byte, error) {
cmd, err := c.CommandContext(ctx, name)
if err != nil {
return nil, err
}
return cmd.CombinedOutput()
}
// Command returns new Cmd and error if any.
func (c Client) Command(name string, args ...string) (*Cmd, error) {
var (
sess *ssh.Session
err error
)
if sess, err = c.NewSession(); err != nil {
return nil, err
}
return &Cmd{
Path: name,
Args: args,
Session: sess,
Context: context.Background(),
}, nil
}
// Command returns new Cmd with context and error, if any.
func (c Client) CommandContext(ctx context.Context, name string, args ...string) (*Cmd, error) {
cmd, err := c.Command(name, args...)
if err != nil {
return cmd, err
}
cmd.Context = ctx
return cmd, nil
}
// NewSftp returns new sftp client and error if any.
func (c Client) NewSftp(opts ...sftp.ClientOption) (*sftp.Client, error) {
return sftp.NewClient(c.Client, opts...)
}
// Close client net connection.
func (c Client) Close() error {
return c.Client.Close()
}
// Upload a local file to remote server!
func (c Client) Upload(localPath string, remotePath string) (err error) {
local, err := os.Open(localPath)
if err != nil {
return
}
defer local.Close()
ftp, err := c.NewSftp()
if err != nil {
return
}
defer ftp.Close()
remote, err := ftp.Create(remotePath)
if err != nil {
return
}
defer remote.Close()
_, err = io.Copy(remote, local)
return
}
// Download file from remote server!
func (c Client) Download(remotePath string, localPath string) (err error) {
local, err := os.Create(localPath)
if err != nil {
return
}
defer local.Close()
ftp, err := c.NewSftp()
if err != nil {
return
}
defer ftp.Close()
remote, err := ftp.Open(remotePath)
if err != nil {
return
}
defer remote.Close()
if _, err = io.Copy(local, remote); err != nil {
return
}
return local.Sync()
}