/
sshcon.go
238 lines (211 loc) · 6 KB
/
sshcon.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
/*
Copyright (c) 2021 Platform9, Inc.
All rights reserved.
*/
package tunnel
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"time"
"go.uber.org/zap"
"golang.org/x/crypto/ssh"
"github.com/pkg/sftp"
)
type SSHConn struct {
NetworkType string
ServerAddr string
LocalAddr string
RemoteAddr string
Username string
SSHPrivateKeyFile string
exit chan bool
localListener net.Listener
sshClient *ssh.Client
sftpClient *sftp.Client
}
func New(netType string, serverAddr string, localAddr string, remoteAddr string, username string, sshPrivateKeyFile string) *SSHConn {
cfg := SSHConn{
NetworkType: netType,
ServerAddr: serverAddr,
LocalAddr: localAddr,
RemoteAddr: remoteAddr,
Username: username,
SSHPrivateKeyFile: sshPrivateKeyFile,
}
cfg.exit = make(chan bool)
return &cfg
}
func (sc *SSHConn) Open() error {
privateKey, err := ioutil.ReadFile(sc.SSHPrivateKeyFile)
if err != nil {
return fmt.Errorf("error reading private key file %s %s", sc.SSHPrivateKeyFile, err)
}
authMethods := make([]ssh.AuthMethod, 1)
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return fmt.Errorf("error parsing private key: %s", err)
}
authMethods[0] = ssh.PublicKeys(signer)
sshConfig := &ssh.ClientConfig{
User: string(sc.Username),
Auth: authMethods,
// by default ignore host key checks
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Setup localListener (type net.Listener)
sc.localListener, err = net.Listen(sc.NetworkType, sc.LocalAddr)
if err != nil {
return fmt.Errorf("failed to listen on %s: %v", sc.LocalAddr, err)
}
// Setup sshClientConn (type *ssh.ClientConn)
sc.sshClient, err = ssh.Dial(sc.NetworkType, sc.ServerAddr, sshConfig)
if err != nil {
return fmt.Errorf("failed to dial serverAddr %s: %s", sc.ServerAddr, err)
}
sc.sftpClient, err = sftp.NewClient(sc.sshClient)
if err != nil {
return fmt.Errorf("failed to create sftpClient %s: %s", sc.ServerAddr, err)
}
go sc.startListening(sshConfig)
return nil
}
func (sc *SSHConn) startListening(sshConfig *ssh.ClientConfig) {
defer sc.localListener.Close()
defer sc.sshClient.Close()
for {
select {
case <-sc.exit:
return
default:
// Setup localConn (type net.Conn)
localConn, err := sc.localListener.Accept()
if err != nil {
zap.S().Fatalf("failed to accept connections: %v", err)
}
go sc.forward(localConn, sshConfig)
}
}
}
func (sc *SSHConn) Close() {
zap.S().Infof("closing tunnel")
select {
case sc.exit <- true:
default:
// HACK - the other goroutine will still be blocked on localListener.Accept()
// sending a dummy request to return from it
client := http.Client{
Timeout: 1 * time.Second,
}
_, _ = client.Get(fmt.Sprintf("http://%s/v2/_catalog", sc.LocalAddr))
}
}
func (sc *SSHConn) forward(localConn net.Conn, config *ssh.ClientConfig) {
done := make(chan bool)
// Setup sshConn (type net.Conn)
sshConn, err := sc.sshClient.Dial(sc.NetworkType, sc.RemoteAddr)
if err != nil {
zap.S().Errorf("failed to dial remoteAddr %s: %s", sc.RemoteAddr, err)
sc.sshClient.Close()
return
}
// Copy localConn.Reader to sshConn.Writer
go func() {
_, err = io.Copy(sshConn, localConn)
if err != nil {
zap.S().Errorf("tunnel: local to remote copy failed: %v", err)
done <- true
return
}
done <- true
}()
// Copy sshConn.Reader to localConn.Writer
go func() {
_, err = io.Copy(localConn, sshConn)
if err != nil {
zap.S().Errorf("tunnel: remote to local copy failed: %v", err)
done <- true
}
done <- true
}()
<-done
<-done
sshConn.Close()
localConn.Close()
}
// RunCommand runs a command on the machine and returns stdout and stderr
// separately
func (sc *SSHConn) RunCommand(cmd string) ([]byte, []byte, error) {
session, err := sc.sshClient.NewSession()
if err != nil {
return nil, nil, fmt.Errorf("unable to create session: %s", err)
}
stdOutPipe, err := session.StdoutPipe()
if err != nil {
return nil, nil, fmt.Errorf("unable to pipe stdout: %s", err)
}
stdErrPipe, err := session.StderrPipe()
if err != nil {
return nil, nil, fmt.Errorf("unable to pipe stderr: %s", err)
}
err = session.Start(cmd)
if err != nil {
return nil, nil, fmt.Errorf("unable to run command: %s", err)
}
stdOut, err := ioutil.ReadAll(stdOutPipe)
stdErr, err := ioutil.ReadAll(stdErrPipe)
err = session.Wait()
if err != nil {
retError := err
switch err.(type) {
case *ssh.ExitError:
retError = fmt.Errorf("command %s failed: %s", cmd, err)
case *ssh.ExitMissingError:
retError = fmt.Errorf("command %s failed (no exit status): %s", cmd, err)
default:
retError = fmt.Errorf("command %s failed: %s", cmd, err)
}
zap.L().Debug("Error ", zap.String("stdout", string(stdOut)), zap.String("stderr", string(stdErr)))
return stdOut, stdErr, retError
}
return stdOut, stdErr, nil
}
// Upload writes a file to the machine
func (sc *SSHConn) UploadFile(localFile string, remoteFilePath string, mode os.FileMode) error {
// first check if the local file exists or not
localFp, err := os.Open(localFile)
if err != nil {
return fmt.Errorf("unable to read localFile: %s", err)
}
defer localFp.Close()
_, err = localFp.Stat()
if err != nil {
return fmt.Errorf("Unable to find size of the file %s", localFile)
}
localFileReader := bufio.NewReader(localFp)
remoteFile, err := sc.sftpClient.Create(remoteFilePath)
if err != nil {
return fmt.Errorf("unable to create file: %s", err)
}
defer remoteFile.Close()
// IMHO this function is misnomer, it actually writes to the remoteFile
_, err = remoteFile.ReadFrom(localFileReader)
if err != nil {
// rmove the remote file since write failed and ignore the errors
// we can't do much about it anyways.
sc.sftpClient.Remove(remoteFilePath)
return fmt.Errorf("write failed: %s, ", err)
}
err = remoteFile.Chmod(mode)
if err != nil {
return fmt.Errorf("chmod failed: %s", err)
}
return nil
}
func (sc *SSHConn) GetLocalAddress() string {
return sc.LocalAddr
}