/
client.go
265 lines (251 loc) · 5.57 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
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
package main
import (
"context"
"errors"
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/balibuild/tunnelssh/cli"
"github.com/balibuild/tunnelssh/pty"
"github.com/balibuild/tunnelssh/tunnel"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
// SSHClient client
type SSHClient struct {
ssh *ssh.Client
config *ssh.ClientConfig
sess *ssh.Session
home string
IdentityFile string
ka *KeyAgent
argv []string // unresolved command argv
env map[string]string
host string
port int
mode TerminalMode
v4 bool
v6 bool
insecure bool
serverAliveInterval int
connectTimeout int
sys *sysInfo
wg sync.WaitGroup
}
// error
var (
ErrGotSignal = errors.New("got signal")
)
func (sc *SSHClient) onFinal(err error) {
if err == nil {
DebugPrint("ssh connetion to %s successfully", sc.host)
return
}
if err == ErrGotSignal {
DebugPrint("received signal, terminated")
return
}
switch e := err.(type) {
case *ssh.ExitMissingError:
DebugPrint("ssh connetion to %s but remote didn't send exit status: %s", sc.host, e)
case *ssh.ExitError:
DebugPrint("ssh connetion to %s with error: %s", sc.host, err)
default:
DebugPrint("ssh connetion to %s with unknown error: %s", sc.host, err)
}
}
// SendEnv todo
func (sc *SSHClient) SendEnv() error {
if len(sc.env) == 0 {
return nil
}
for k, v := range sc.env {
DebugPrint("SetEnv %s=%s", k, v)
if err := sc.sess.Setenv(k, v); err != nil {
return err
}
}
return nil
}
// WatchSignals todo
func (sc *SSHClient) WatchSignals() chan os.Signal {
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT)
return sigC
}
type windowChangeReq struct {
W, H, Wpx, Hpx uint32
}
func (sc *SSHClient) invokeResizeTerminal(ctx context.Context) {
ch := sc.watchTerminalResize(ctx)
sc.wg.Add(1)
go func() {
defer sc.wg.Done()
w, h, err := pty.GetWinSize()
if err != nil {
DebugPrint("failed get windows size %v", err)
}
for {
select {
case <-ctx.Done():
return
case _, ok := <-ch:
if !ok {
return
}
}
nw, nh, err := pty.GetWinSize()
if err != nil {
continue
}
if nw == w && nh == h {
continue
}
_, err = sc.sess.SendRequest("window-change", false, ssh.Marshal(
windowChangeReq{W: uint32(nw), H: uint32(nh)},
))
if err != nil {
continue
}
w = nw
h = nh
}
}()
}
// RunInteractive to open a shell
func (sc *SSHClient) RunInteractive() error {
DebugPrint("ssh shell mode. host: %s", sc.host)
sc.sess.Stdout = os.Stdout
sc.sess.Stderr = os.Stderr
sc.sess.Stdin = os.Stdin
sc.sys = &sysInfo{}
if sc.mode == TerminalModeForce {
x, y, err := pty.GetWinSize()
if err != nil {
return err
}
if termstate, err := term.MakeRaw(int(os.Stdout.Fd())); err == nil {
defer term.Restore(int(os.Stdout.Fd()), termstate)
}
if termstate, err := term.MakeRaw(int(os.Stderr.Fd())); err == nil {
defer term.Restore(int(os.Stderr.Fd()), termstate)
}
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.IGNCR: 1,
ssh.TTY_OP_ISPEED: 115200, // baud in
ssh.TTY_OP_OSPEED: 115200, // baud out
}
if err := sc.sess.RequestPty("xterm", y, x, modes); err != nil {
return err
}
}
if err := sc.sess.Shell(); err != nil {
return err
}
if err := sc.changeLocalTerminalMode(); err != nil {
return err
}
sigC := sc.WatchSignals()
ctx, cancel := context.WithCancel(context.Background())
defer func() {
signal.Stop(sigC)
cancel()
sc.wg.Wait()
}()
sc.invokeResizeTerminal(ctx)
sessC := make(chan error)
go func() {
sessC <- sc.sess.Wait()
}()
select {
case <-sigC:
return ErrGotSignal
case err := <-sessC:
return err
}
}
// Loop todo
func (sc *SSHClient) Loop() error {
_ = sc.SendEnv()
if len(sc.argv) == 0 {
return sc.RunInteractive()
}
sc.sess.Stdout = os.Stdout
sc.sess.Stderr = os.Stderr
sc.sess.Stdin = os.Stdin
// git escape argv done
args := strings.Join(sc.argv, " ")
DebugPrint("Exec cmd: %s", args)
sigC := sc.WatchSignals()
defer func() {
signal.Stop(sigC)
}()
sessC := make(chan error)
go func() {
sessC <- sc.sess.Run(args)
}()
select {
case <-sigC:
return ErrGotSignal
case err := <-sessC:
return err
}
}
// DialTunnel todo
func DialTunnel(network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
var bm tunnel.BoringMachine
if IsDebugMode {
bm.Debug = func(msg string) {
_, _ = os.Stderr.WriteString(cli.StrCat("debug3: \x1b[33m", msg, "\x1b[0m\n"))
}
}
_ = bm.Initialize()
conn, err := bm.DialTimeout(network, addr, config.Timeout)
if err != nil {
return nil, err
}
c, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
return nil, err
}
return ssh.NewClient(c, chans, reqs), nil
}
// Dial todo
func (sc *SSHClient) Dial() error {
if sc.connectTimeout != 0 {
sc.config.Timeout = time.Duration(sc.connectTimeout) * time.Second
} else {
sc.config.Timeout = 5 * time.Second
}
addr := net.JoinHostPort(sc.host, strconv.Itoa(sc.port))
conn, err := DialTunnel("tcp", addr, sc.config)
if err != nil {
return err
}
sc.ssh = conn
sess, err := sc.ssh.NewSession()
if err != nil {
return err
}
sc.sess = sess
return nil
}
// Close client
func (sc *SSHClient) Close() error {
if sc.sess != nil {
sc.sess.Close()
}
if sc.ka != nil {
sc.ka.Close()
}
if sc.ssh != nil {
return sc.ssh.Close()
}
return nil
}