@@ -45,6 +45,7 @@ import (
4545
4646 "github.com/dsnet/golib/jsonfmt"
4747 "golang.org/x/crypto/ssh"
48+ "golang.org/x/crypto/ssh/agent"
4849 "golang.org/x/crypto/ssh/knownhosts"
4950)
5051
@@ -116,6 +117,23 @@ type KeepAliveConfig struct {
116117 CountMax uint
117118}
118119
120+ func setupSshAgent () ssh.AuthMethod {
121+ // ssh-agent(1) provides a UNIX socket at $SSH_AUTH_SOCK.
122+ socket := os .Getenv ("SSH_AUTH_SOCK" )
123+ if len (socket ) == 0 {
124+ return nil
125+ }
126+
127+ conn , err := net .Dial ("unix" , socket )
128+ if err != nil {
129+ log .Printf ("Failed to open SSH_AUTH_SOCK %s: %v\n " , socket , err )
130+ return nil
131+ }
132+
133+ agentClient := agent .NewClient (conn )
134+ return ssh .PublicKeysCallback (agentClient .Signers )
135+ }
136+
119137func loadConfig (conf string ) (tunns []tunnel , logger * log.Logger , closer func () error ) {
120138 var logBuf bytes.Buffer
121139 logger = log .New (io .MultiWriter (os .Stderr , & logBuf ), "" , log .Ldate | log .Ltime | log .Lshortfile )
@@ -171,11 +189,10 @@ func loadConfig(conf string) (tunns []tunnel, logger *log.Logger, closer func()
171189 closer = f .Close
172190 }
173191
192+ var auth []ssh.AuthMethod
193+
174194 // Parse all of the private keys.
175195 var keys []ssh.Signer
176- if len (config .KeyFiles ) == 0 {
177- logger .Fatal ("no private keys specified" )
178- }
179196 for _ , kf := range config .KeyFiles {
180197 b , err := ioutil .ReadFile (kf )
181198 if err != nil {
@@ -187,7 +204,19 @@ func loadConfig(conf string) (tunns []tunnel, logger *log.Logger, closer func()
187204 }
188205 keys = append (keys , k )
189206 }
190- auth := []ssh.AuthMethod {ssh .PublicKeys (keys ... )}
207+ if len (keys ) > 0 {
208+ auth = append (auth , ssh .PublicKeys (keys ... ))
209+ }
210+
211+ // Setup ssh-agent(1)
212+ agent := setupSshAgent ()
213+ if agent != nil {
214+ auth = append (auth , agent )
215+ }
216+
217+ if len (auth ) == 0 {
218+ logger .Panic ("no private keys and ssh-agent usable" )
219+ }
191220
192221 // Parse all of the host public keys.
193222 if len (config .KnownHostFiles ) == 0 {
0 commit comments