-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathagent_windows.go
More file actions
57 lines (51 loc) · 1.33 KB
/
agent_windows.go
File metadata and controls
57 lines (51 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package sshutil
import (
"context"
"net"
"os"
"bufio"
"regexp"
"github.com/Microsoft/go-winio"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/agent"
)
// dialAgent returns an ssh.Agent client. It uses the SSH_AUTH_SOCK to connect
// to the agent.
func dialAgent() (*Agent, error) {
// Attempt unix sockets for environments like cygwin.
if socket := os.Getenv("SSH_AUTH_SOCK"); socket != "" {
if conn, err := net.Dial("unix", socket); err == nil {
return &Agent{
ExtendedAgent: agent.NewClient(conn),
Conn: conn,
}, nil
}
}
homepath := os.Getenv("HOMEPATH")
sshagentfile := string(homepath) + "\\.ssh\\config"
pipename := "\\\\.\\pipe\\ssh-agent"
file, err := os.Open(sshagentfile);
if err == nil {
sc := bufio.NewScanner(file);
for sc.Scan() {
var line = sc.Text();
if len(line) > 15 {
compare := line[0:13]
if compare == "IdentityAgent" {
temp := line[14:len(line)]
re := regexp.MustCompile(`/`)
re2 := regexp.MustCompile(`[\s\"]*`)
pipename = re2.ReplaceAllString(re.ReplaceAllString(temp,"\\"),"")
}
}
}
}
if conn, err := winio.DialPipeContext(context.Background(), pipename); err == nil {
return &Agent{
ExtendedAgent: agent.NewClient(conn),
Conn: conn,
},nil
} else {
return nil, errors.Wrap(err, "error connecting with ssh-agent")
}
}