@@ -17,31 +17,31 @@ import (
17
17
)
18
18
19
19
// BuildTunnel creates a new instance of SSH tunnel.
20
- func BuildTunnel (cliCtx * cli.Context , remoteHost string ) (* portfwd.SSHTunnel , error ) {
21
- remoteURL , err := url .Parse (cliCtx .String (URLKey ))
20
+ func BuildTunnel (cliCtx * cli.Context , remoteHost * url.URL ) (* portfwd.SSHTunnel , error ) {
21
+ localEndpoint := forwardingLocalEndpoint (remoteHost , cliCtx .String (FwLocalPortKey ))
22
+
23
+ serverURL , err := url .Parse (cliCtx .String (FwServerURLKey ))
22
24
if err != nil {
23
25
return nil , err
24
26
}
25
27
26
- localEndpoint := forwardingLocalEndpoint (remoteURL , cliCtx .String (FwLocalPortKey ))
27
-
28
- serverURL , err := url .Parse (cliCtx .String (FwServerURLKey ))
28
+ authMethod , err := getAuthMethod (cliCtx )
29
29
if err != nil {
30
30
return nil , err
31
31
}
32
32
33
33
sshConfig := & ssh.ClientConfig {
34
34
User : serverURL .User .Username (),
35
35
Auth : []ssh.AuthMethod {
36
- portfwd . SSHAgent () ,
36
+ authMethod ,
37
37
},
38
38
HostKeyCallback : func (hostname string , remote net.Addr , key ssh.PublicKey ) error {
39
39
// Always accept key.
40
40
return nil
41
41
},
42
42
}
43
43
44
- tunnel := portfwd .NewTunnel (localEndpoint , serverURL .Host , remoteHost , sshConfig )
44
+ tunnel := portfwd .NewTunnel (localEndpoint , serverURL .Host , remoteHost . Host , sshConfig )
45
45
46
46
return tunnel , nil
47
47
}
@@ -54,6 +54,19 @@ func forwardingLocalEndpoint(remoteURL *url.URL, localPort string) string {
54
54
return fmt .Sprintf ("%s:%s" , "127.0.0.1" , localPort )
55
55
}
56
56
57
+ func getAuthMethod (cliCtx * cli.Context ) (ssh.AuthMethod , error ) {
58
+ if cliCtx .String (IdentityFileKey ) != "" {
59
+ authMethod , err := portfwd .ReadAuthFromIdentityFile (cliCtx .String (IdentityFileKey ))
60
+ if err != nil {
61
+ return nil , err
62
+ }
63
+
64
+ return authMethod , nil
65
+ }
66
+
67
+ return portfwd .SSHAgent (), nil
68
+ }
69
+
57
70
// BuildHostname builds a hostname string.
58
71
func BuildHostname (host , port string ) string {
59
72
return fmt .Sprintf ("%s:%s" , host , port )
0 commit comments