forked from moul/assh
/
proxy.go
318 lines (268 loc) · 7.51 KB
/
proxy.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package commands
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"path"
"strconv"
"strings"
"sync"
"syscall"
"golang.org/x/net/context"
"github.com/codegangsta/cli"
shlex "github.com/flynn/go-shlex"
"github.com/moul/advanced-ssh-config/pkg/config"
. "github.com/moul/advanced-ssh-config/pkg/logger"
)
func cmdProxy(c *cli.Context) error {
Logger.Debugf("assh args: %s", c.Args())
if len(c.Args()) < 1 {
Logger.Fatalf("assh: \"connect\" requires 1 argument. See 'assh connect --help'.")
}
// dry-run option
// Setting the 'ASSH_DRYRUN=1' environment variable,
// so 'assh' can use gateways using sub-SSH commands.
if c.Bool("dry-run") {
os.Setenv("ASSH_DRYRUN", "1")
}
dryRun := os.Getenv("ASSH_DRYRUN") == "1"
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
Logger.Fatalf("Cannot open configuration file: %v", err)
}
if err = conf.LoadKnownHosts(); err != nil {
Logger.Debugf("Failed to load assh known_hosts: %v", err)
}
target := c.Args()[0]
isOutdated, err := conf.IsConfigOutdated(target)
if err != nil {
Logger.Warnf("Cannot check if ~/.ssh/config is outdated.")
}
if isOutdated {
Logger.Debugf("The configuration file is outdated, rebuilding it before calling ssh")
Logger.Warnf("'~/.ssh/config' has been rewritten. SSH needs to be restarted. See https://github.com/moul/advanced-ssh-config/issues/122 for more information.")
Logger.Debugf("Saving SSH config")
err = conf.SaveSSHConfig()
if err != nil {
Logger.Fatalf("Cannot save SSH config file: %v", err)
}
}
// FIXME: handle complete host with json
host, err := computeHost(target, c.Int("port"), conf)
if err != nil {
Logger.Fatalf("Cannot get host '%s': %v", target, err)
}
w := Logger.Writer()
host.WriteSSHConfigTo(w)
w.Close()
hostJson, err := json.Marshal(host)
if err != nil {
Logger.Warnf("Failed to marshal host: %v", err)
}
Logger.Debugf("Host: %s", hostJson)
Logger.Debugf("Proxying")
err = proxy(host, conf, dryRun)
if err != nil {
Logger.Fatalf("Proxy error: %v", err)
}
return nil
}
func computeHost(dest string, portOverride int, conf *config.Config) (*config.Host, error) {
host := conf.GetHostSafe(dest)
if portOverride > 0 {
host.Port = strconv.Itoa(portOverride)
}
return host, nil
}
func prepareHostControlPath(host, gateway *config.Host) error {
controlPathDir := path.Dir(os.ExpandEnv(strings.Replace(host.ControlPath, "~", "$HOME", -1)))
gatewayControlPath := path.Join(controlPathDir, gateway.Name())
if config.BoolVal(host.NoControlMasterMkdir) {
return nil
}
return os.MkdirAll(gatewayControlPath, 0700)
}
func proxy(host *config.Host, conf *config.Config, dryRun bool) error {
if len(host.Gateways) > 0 {
Logger.Debugf("Trying gateways: %s", host.Gateways)
for _, gateway := range host.Gateways {
if gateway == "direct" {
err := proxyDirect(host, dryRun)
if err != nil {
Logger.Errorf("Failed to use 'direct' connection: %v", err)
}
} else {
hostCopy := host.Clone()
gatewayHost := conf.GetGatewaySafe(gateway)
err := prepareHostControlPath(hostCopy, gatewayHost)
if err != nil {
return err
}
// FIXME: dynamically add "-v" flags
var command string
// FIXME: detect ssh client version and use netcat if too old
// for now, the workaround is to configure the ProxyCommand of the host to "nc %h %p"
if err = hostPrepare(hostCopy); err != nil {
return err
}
if hostCopy.ProxyCommand != "" {
command = "ssh %name -- " + hostCopy.ExpandString(hostCopy.ProxyCommand)
} else {
command = hostCopy.ExpandString("ssh -W %h:%p ") + "%name"
}
Logger.Debugf("Using gateway '%s': %s", gateway, command)
err = proxyCommand(gatewayHost, command, dryRun)
if err == nil {
return nil
}
Logger.Errorf("Cannot use gateway '%s': %v", gateway, err)
}
}
return fmt.Errorf("No such available gateway")
}
Logger.Debugf("Connecting without gateway")
return proxyDirect(host, dryRun)
}
func proxyDirect(host *config.Host, dryRun bool) error {
if host.ProxyCommand != "" {
return proxyCommand(host, host.ProxyCommand, dryRun)
}
return proxyGo(host, dryRun)
}
func proxyCommand(host *config.Host, command string, dryRun bool) error {
command = host.ExpandString(command)
Logger.Debugf("ProxyCommand: %s", command)
args, err := shlex.Split(command)
if err != nil {
return err
}
if dryRun {
return fmt.Errorf("dry-run: Execute %s", args)
}
spawn := exec.Command(args[0], args[1:]...)
spawn.Stdout = os.Stdout
spawn.Stdin = os.Stdin
spawn.Stderr = os.Stderr
return spawn.Run()
}
func hostPrepare(host *config.Host) error {
if host.HostName == "" {
host.HostName = host.Name()
}
if len(host.ResolveNameservers) > 0 {
Logger.Debugf("Resolving host: '%s' using nameservers %s", host.HostName, host.ResolveNameservers)
// FIXME: resolve using custom dns server
results, err := net.LookupAddr(host.HostName)
if err != nil {
return err
}
if len(results) > 0 {
host.HostName = results[0]
}
Logger.Debugf("Resolved host is: %s", host.HostName)
}
if host.ResolveCommand != "" {
command := host.ExpandString(host.ResolveCommand)
Logger.Debugf("Resolving host: %q using command: %q", host.HostName, command)
args, err := shlex.Split(command)
if err != nil {
return err
}
cmd := exec.Command(args[0], args[1:]...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
Logger.Errorf("ResolveCommand failed: %s", stderr.String())
return err
}
host.HostName = strings.TrimSpace(fmt.Sprintf("%s", stdout.String()))
Logger.Debugf("Resolved host is: %s", host.HostName)
}
return nil
}
type exportReadWrite struct {
written uint64
err error
}
func proxyGo(host *config.Host, dryRun bool) error {
Logger.Debugf("Preparing host object")
if err := hostPrepare(host); err != nil {
return err
}
if dryRun {
return fmt.Errorf("dry-run: Golang native TCP connection to '%s:%s'", host.HostName, host.Port)
}
Logger.Debugf("Connecting to %s:%s", host.HostName, host.Port)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", host.HostName, host.Port))
if err != nil {
return err
}
Logger.Debugf("Connected to %s:%s", host.HostName, host.Port)
// Ignore SIGHUP
signal.Ignore(syscall.SIGHUP)
waitGroup := sync.WaitGroup{}
result := exportReadWrite{}
ctx, cancel := context.WithCancel(context.Background())
ctx = context.WithValue(ctx, "sync", &waitGroup)
waitGroup.Add(2)
c1 := readAndWrite(ctx, conn, os.Stdout)
c2 := readAndWrite(ctx, os.Stdin, conn)
select {
case result = <-c1:
case result = <-c2:
}
if result.err != nil && result.err == io.EOF {
result.err = nil
}
select {
case res := <-c2:
Logger.Debugf("Byte written %v", res.written)
default:
Logger.Debugf("Byte written %v", result.written)
}
conn.Close()
cancel()
waitGroup.Wait()
return result.err
}
func readAndWrite(ctx context.Context, r io.Reader, w io.Writer) <-chan exportReadWrite {
buff := make([]byte, 1024)
c := make(chan exportReadWrite, 1)
go func() {
defer ctx.Value("sync").(*sync.WaitGroup).Done()
export := exportReadWrite{}
for {
select {
case <-ctx.Done():
c <- export
return
default:
nr, err := r.Read(buff)
if err != nil {
export.err = err
c <- export
return
}
if nr > 0 {
wr, err := w.Write(buff[:nr])
if err != nil {
export.err = err
c <- export
return
}
if wr > 0 {
export.written += uint64(wr)
}
}
}
}
}()
return c
}