Skip to content

Commit

Permalink
Improve SSH_AUTH_SOCK support on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Dec 9, 2023
1 parent 2d55483 commit 5ae2514
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions sshagent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,28 @@ import (
"errors"
"io"
"net"
"os"
"strings"
"sync"

"github.com/Microsoft/go-winio"
"golang.org/x/crypto/ssh/agent"
)

const (
sshAgentPipe = `\\.\pipe\openssh-ssh-agent`
pipe = `\\.\pipe\`
openSSHAgentPipe = pipe + "openssh-ssh-agent"
)

// Available returns true if Pageant is running
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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 5ae2514

Please sign in to comment.