From 5ae2514b5993381bd0729d556fb736b20a06ea8e Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Sat, 9 Dec 2023 19:11:32 +0100 Subject: [PATCH] Improve SSH_AUTH_SOCK support on Windows --- sshagent_windows.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/sshagent_windows.go b/sshagent_windows.go index 175d161..a363d43 100644 --- a/sshagent_windows.go +++ b/sshagent_windows.go @@ -26,6 +26,8 @@ import ( "errors" "io" "net" + "os" + "strings" "sync" "github.com/Microsoft/go-winio" @@ -33,7 +35,8 @@ import ( ) const ( - sshAgentPipe = `\\.\pipe\openssh-ssh-agent` + pipe = `\\.\pipe\` + openSSHAgentPipe = pipe + "openssh-ssh-agent" ) // Available returns true if Pageant is running @@ -41,7 +44,10 @@ func Available() bool { if pageantWindow() != 0 { return true } - conn, err := winio.DialPipe(sshAgentPipe, nil) + if sshAuthSock := os.Getenv("SSH_AUTH_SOCK"); sshAuthSock != "" { + return true + } + conn, err := winio.DialPipe(openSSHAgentPipe, nil) if err != nil { return false } @@ -55,13 +61,29 @@ func New() (agent.Agent, net.Conn, error) { if pageantWindow() != 0 { return agent.NewClient(&conn{}), nil, nil } + + sshAgentPipe := openSSHAgentPipe + if sshAuthSock := os.Getenv("SSH_AUTH_SOCK"); sshAuthSock != "" { + conn, err := net.Dial("unix", sshAuthSock) + if err == nil { + return agent.NewClient(conn), conn, nil + } + + if !strings.HasPrefix(sshAuthSock, pipe) { + sshAuthSock = pipe + sshAuthSock + } + + sshAgentPipe = sshAuthSock + } + conn, err := winio.DialPipe(sshAgentPipe, nil) if err != nil { return nil, nil, errors.New( "SSH agent requested, but could not detect Pageant or Windows native SSH agent", ) } - return agent.NewClient(conn), nil, nil + + return agent.NewClient(conn), conn, nil } type conn struct {