@@ -26,22 +26,28 @@ import (
2626 "errors"
2727 "io"
2828 "net"
29+ "os"
30+ "strings"
2931 "sync"
3032
3133 "github.com/Microsoft/go-winio"
3234 "golang.org/x/crypto/ssh/agent"
3335)
3436
3537const (
36- sshAgentPipe = `\\.\pipe\openssh-ssh-agent`
38+ pipe = `\\.\pipe\`
39+ openSSHAgentPipe = pipe + "openssh-ssh-agent"
3740)
3841
39- // Available returns true if Pageant is running
42+ // Available returns true if Pageant is running.
4043func Available () bool {
4144 if pageantWindow () != 0 {
4245 return true
4346 }
44- conn , err := winio .DialPipe (sshAgentPipe , nil )
47+ if sshAuthSock := os .Getenv ("SSH_AUTH_SOCK" ); sshAuthSock != "" {
48+ return true
49+ }
50+ conn , err := winio .DialPipe (openSSHAgentPipe , nil )
4551 if err != nil {
4652 return false
4753 }
@@ -50,18 +56,34 @@ func Available() bool {
5056}
5157
5258// New returns a new agent.Agent and the (custom) connection it uses
53- // to communicate with a running pagent.exe instance (see README.md)
59+ // to communicate with a running pagent.exe instance (see README.md).
5460func New () (agent.Agent , net.Conn , error ) {
5561 if pageantWindow () != 0 {
5662 return agent .NewClient (& conn {}), nil , nil
5763 }
64+
65+ sshAgentPipe := openSSHAgentPipe
66+ if sshAuthSock := os .Getenv ("SSH_AUTH_SOCK" ); sshAuthSock != "" {
67+ conn , err := net .Dial ("unix" , sshAuthSock )
68+ if err == nil {
69+ return agent .NewClient (conn ), conn , nil
70+ }
71+
72+ if ! strings .HasPrefix (sshAuthSock , pipe ) {
73+ sshAuthSock = pipe + sshAuthSock
74+ }
75+
76+ sshAgentPipe = sshAuthSock
77+ }
78+
5879 conn , err := winio .DialPipe (sshAgentPipe , nil )
5980 if err != nil {
6081 return nil , nil , errors .New (
6182 "SSH agent requested, but could not detect Pageant or Windows native SSH agent" ,
6283 )
6384 }
64- return agent .NewClient (conn ), nil , nil
85+
86+ return agent .NewClient (conn ), conn , nil
6587}
6688
6789type conn struct {
0 commit comments