-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
162 lines (135 loc) · 4.17 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
package ssh
import (
"context"
"fmt"
"io"
"net"
"os"
"strings"
"github.com/pkg/errors"
gossh "golang.org/x/crypto/ssh"
"github.com/hckops/hckctl/pkg/client/terminal"
)
func NewSshClient(config *SshClientConfig) (*SshClient, error) {
sshConfig := sshClientConfig(config)
client, err := gossh.Dial("tcp", config.Address, sshConfig)
if err != nil {
return nil, errors.Wrap(err, "error ssh client")
}
return &SshClient{
ctx: context.Background(),
ssh: client,
}, nil
}
// TODO ssh agent auth
func sshClientConfig(config *SshClientConfig) *gossh.ClientConfig {
sshConfig := &gossh.ClientConfig{
User: config.Username,
Auth: []gossh.AuthMethod{
gossh.Password(config.Token),
},
HostKeyCallback: gossh.InsecureIgnoreHostKey(), // TODO remove
}
return sshConfig
}
func (client *SshClient) Close() error {
return client.ssh.Close()
}
func (client *SshClient) SendRequest(protocol string, payload string) (string, error) {
// "wantReply" must be true to get a response
ok, response, err := client.ssh.SendRequest(protocol, true, []byte(payload))
if !ok {
if err != nil {
return "", errors.Wrapf(err, "error ssh send request")
} else if strings.TrimSpace(string(response)) != "" {
return "", fmt.Errorf("error ssh server response %s", response)
} else {
return "", errors.New("error ssh invalid request")
}
}
return string(response), nil
}
func (client *SshClient) Exec(opts *SshExecOpts) error {
session, err := client.ssh.NewSession()
if err != nil {
return errors.Wrapf(err, "error ssh new session")
}
defer session.Close()
if err := handleStreams(session, opts.OnStreamErrorCallback); err != nil {
return errors.Wrapf(err, "error ssh stream")
}
rawTerminal, err := terminal.NewRawTerminal(os.Stdin)
if err != nil {
return errors.Wrap(err, "error ssh terminal")
}
defer rawTerminal.Restore()
opts.OnStreamStartCallback()
if err := session.Run(opts.Payload); err != nil && err != io.EOF {
return errors.Wrapf(err, "error ssh exec session")
}
return nil
}
func handleStreams(session *gossh.Session, onStreamErrorCallback func(error)) error {
stdin, err := session.StdinPipe()
if err != nil {
return errors.Wrap(err, "error opening stdin pipe")
}
go func() {
if _, err := io.Copy(stdin, os.Stdin); err != nil {
onStreamErrorCallback(errors.Wrap(err, "error copy stdin local->remote"))
}
}()
stdout, err := session.StdoutPipe()
if err != nil {
return errors.Wrap(err, "error opening stdout pipe")
}
go func() {
if _, err := io.Copy(os.Stdout, stdout); err != nil {
onStreamErrorCallback(errors.Wrap(err, "error copy stdout remote->local"))
}
}()
stderr, err := session.StderrPipe()
if err != nil {
return errors.Wrap(err, "error opening stderr pipe")
}
go func() {
if _, err := io.Copy(os.Stderr, stderr); err != nil {
onStreamErrorCallback(errors.Wrap(err, "error copy stderr remote->local"))
}
}()
return nil
}
func (client *SshClient) Tunnel(opts *SshTunnelOpts) {
// starts a local server and forwards traffic to a remote connection
listener, err := net.Listen(opts.Network(), opts.LocalAddress())
if err != nil {
opts.OnTunnelErrorCallback(errors.Wrapf(err, "error ssh creating local tunnel: address=%s", opts.LocalAddress()))
}
defer listener.Close()
copyStream := func(writer, reader net.Conn, label string) {
defer writer.Close()
defer reader.Close()
opts.OnTunnelStartCallback(label)
_, err := io.Copy(writer, reader)
if err != nil {
opts.OnTunnelErrorCallback(errors.Wrapf(err, "error ssh copying stream: %s", label))
}
opts.OnTunnelStopCallback(label)
}
for {
// blocks until a connection is opened
localConnection, err := listener.Accept()
if err != nil {
opts.OnTunnelErrorCallback(errors.Wrapf(err, "error ssh opening local tunnel: address=%s", opts.LocalAddress()))
}
// forward connections
go func() {
remoteConnection, err := client.ssh.Dial(opts.Network(), opts.RemoteAddress())
if err != nil {
opts.OnTunnelErrorCallback(errors.Wrapf(err, "error ssh opening remote tunnel: address=%s", opts.RemoteAddress()))
}
go copyStream(localConnection, remoteConnection, "remote->local")
go copyStream(remoteConnection, localConnection, "local->remote")
}()
}
}