-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathssh.go
268 lines (233 loc) · 6.3 KB
/
ssh.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
package main
import (
"bufio"
"errors"
"io"
"log"
"net"
"net/netip"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
"golang.org/x/crypto/ssh"
)
type SSHConnection struct {
Client *ssh.Client
Connected *atomic.Bool
ControlTerminal *ssh.Session
ControlTerminalStdin *io.WriteCloser
ControlTerminalActive *atomic.Bool
Host string
Port string
Username string
Password string
UseSSHKey bool
SSHKeyPath string
ClientIpAddress string
Secret string
ForwardedConnections map[uint16]*net.Conn
Timeout time.Duration
}
func (sshConnection *SSHConnection) StartSSHConnection() error {
// get host public key
hostKey := getHostKey(sshConnection.Host)
// ssh client config
config := &ssh.ClientConfig{
User: sshConnection.Username,
Auth: []ssh.AuthMethod{
ssh.Password(sshConnection.Password),
},
// HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyCallback: ssh.FixedHostKey(hostKey),
HostKeyAlgorithms: []string{
ssh.KeyAlgoRSA,
ssh.KeyAlgoDSA,
ssh.KeyAlgoECDSA256,
ssh.KeyAlgoECDSA384,
ssh.KeyAlgoECDSA521,
ssh.KeyAlgoED25519,
},
Timeout: sshConnection.Timeout,
}
if sshConnection.UseSSHKey {
authMethod, merr := GetSSHPrivateKeyAuthMethod(sshConnection.SSHKeyPath)
if merr == nil {
config.Auth = append(config.Auth, authMethod)
} else {
log.Println("couldn't enable SSH key auth ", merr.Error())
}
}
// connect
client, err := ssh.Dial("tcp", sshConnection.Host+":"+sshConnection.Port, config)
if err != nil {
log.Println(err)
return err
}
sshConnection.Client = client
controlTerminal, cterr := client.NewSession()
if cterr != nil {
client.Close()
return cterr
}
controlTerminal.Setenv("SECRET", sshConnection.Secret)
controlTerminal.Setenv("REMOTE_ADDR", sshConnection.ClientIpAddress)
sshConnection.ControlTerminal = controlTerminal
sshConnection.Connected.Store(true)
go sshConnection.SetupControlTerminal()
return nil
}
func (sshConnection *SSHConnection) StartTerminal() (Session *ssh.Session,
StdIn *io.WriteCloser, StdOut *io.Reader, StdErr *io.Reader, Error error) {
if sshConnection.Connected.Load() {
sess, err := sshConnection.Client.NewSession()
if err != nil {
return nil, nil, nil, nil, err
}
sess.RequestPty("xterm-256color", 80, 80, ssh.TerminalModes{
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
ssh.ECHO: 0,
ssh.ECHOCTL: 0,
})
st1err := sess.Setenv("SECRET", sshConnection.Secret)
if st1err != nil {
log.Println(st1err, "SECRET")
}
st2err := sess.Setenv("REMOTE_ADDR", sshConnection.ClientIpAddress)
if st2err != nil {
log.Println(st2err, "REMOTE_ADDR")
}
stdin, sierr := sess.StdinPipe()
if sierr != nil {
return nil, nil, nil, nil, sierr
}
stdout, soerr := sess.StdoutPipe()
if soerr != nil {
return nil, nil, nil, nil, soerr
}
stderr, seerr := sess.StderrPipe()
if seerr != nil {
return nil, nil, nil, nil, seerr
}
serr := sess.Shell()
if serr != nil {
sess.Close()
return nil, nil, nil, nil, serr
}
return sess, &stdin, &stdout, &stderr, nil
}
return nil, nil, nil, nil, errors.New("connection is not active yet")
}
func (sshConnection *SSHConnection) StopSSHConnection() error {
if sshConnection.Connected.Load() {
for _, forwardedConn := range sshConnection.ForwardedConnections {
(*forwardedConn).Close()
}
sshConnection.Client.Close()
return sshConnection.Client.Wait()
}
return errors.New("connection is not active")
}
func (sshConnection *SSHConnection) WaitForConnection(tries int, checkDelay time.Duration) error {
for tries > 0 {
if sshConnection.Connected.Load() {
return nil
}
tries -= 1
time.Sleep(checkDelay)
}
return errors.New("SSH Connection timeout")
}
func getHostKey(host string) ssh.PublicKey {
// parse OpenSSH known_hosts file
// ssh or use ssh-keyscan to get initial key
file, err := os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts"))
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var hostKey ssh.PublicKey
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) != 3 {
continue
}
if strings.Contains(fields[0], host) {
var err error
hostKey, _, _, _, err = ssh.ParseAuthorizedKey(scanner.Bytes())
if err != nil {
log.Fatalf("error parsing %q: %v", fields[2], err)
}
break
}
}
if hostKey == nil {
log.Fatalf("no hostkey found for %s", host)
}
return hostKey
}
func (sshConnection *SSHConnection) SetupControlTerminal() {
err := sshConnection.ControlTerminal.RequestPty("xterm-256color", 80, 80, ssh.TerminalModes{
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
ssh.ECHO: 0,
ssh.ECHOCTL: 0,
})
if err != nil {
log.Println(err)
return
}
stdin, err := sshConnection.ControlTerminal.StdinPipe()
if err != nil {
log.Println(err)
return
}
err = sshConnection.ControlTerminal.Shell()
if err != nil {
log.Println(err)
return
}
sshConnection.ControlTerminalStdin = &stdin
sshConnection.ControlTerminalActive.Store(true)
}
func (sshConnection *SSHConnection) RunControlCommand(command string) error {
if sshConnection.ControlTerminalActive.Load() {
stdin := *sshConnection.ControlTerminalStdin
n, err := stdin.Write(append([]byte(command), 10, 13)) // append /n/c to the end
if err != nil {
log.Println(err)
return err
}
if n < len(command) {
log.Println("short write, exec failed")
return errors.New("short write, exec failed")
}
return nil
}
return errors.New("control terminal not active")
}
func (sshConnection *SSHConnection) ForwardRemotePort(port uint16) (*net.Conn, error) {
conn, err := sshConnection.Client.DialTCP("tcp4", nil, net.TCPAddrFromAddrPort(
netip.AddrPortFrom(
netip.AddrFrom4(
[4]byte{127, 0, 0, 1},
),
port,
),
))
return &conn, err
}
func GetSSHPrivateKeyAuthMethod(keyFilePath string) (ssh.AuthMethod, error) {
filebytes, ferr := os.ReadFile(keyFilePath)
if ferr != nil {
return nil, ferr
}
signer, serr := ssh.ParsePrivateKey(filebytes)
if serr != nil {
return nil, serr
}
return ssh.PublicKeys(signer), nil
}