Skip to content

Commit f0348d8

Browse files
authored
Merge pull request #14 from mame/make-pipename-configurable
Make the pipe name configurable
2 parents 46bfe77 + 2707a90 commit f0348d8

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
type config struct {
2020
socketPath string
2121
powershellPath string
22+
pipeName string
2223
format string
2324
foreground bool
2425
verbose bool
@@ -57,6 +58,7 @@ func newConfig() *config {
5758

5859
flag.StringVar(&c.socketPath, "socket", defaultSocketPath(), "a path of UNIX domain socket to listen")
5960
flag.StringVar(&c.powershellPath, "powershell-path", powershellPath(), "a path of Windows PowerShell")
61+
flag.StringVar(&c.pipeName, "pipename", "openssh-ssh-agent", "a name of pipe to connect")
6062
flag.BoolVar(&c.foreground, "foreground", false, "run in foreground mode")
6163
flag.BoolVar(&c.verbose, "verbose", false, "verbose mode")
6264
flag.StringVar(&c.logFile, "log", "", "a file path to write the log")

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ func main() {
55

66
ctx := c.start()
77

8-
s := newServer(c.socketPath, c.powershellPath)
8+
s := newServer(c.socketPath, c.powershellPath, c.pipeName)
99

1010
s.run(ctx)
1111
}

repeater.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var waitTimes = []time.Duration{
2626
}
2727

2828
// invoke PowerShell.exe and run
29-
func newRepeater(ctx context.Context, powershell string) (*repeater, error) {
29+
func newRepeater(ctx context.Context, powershell string, pipename string) (*repeater, error) {
3030
for i, limit := range waitTimes {
3131
log.Printf("invoking [W] in PowerShell.exe%s", trial(i))
3232

@@ -69,6 +69,19 @@ func newRepeater(ctx context.Context, powershell string) (*repeater, error) {
6969
case ok := <-done:
7070
if ok {
7171
log.Printf("[W] invoked successfully")
72+
73+
buf := make([]byte, 4)
74+
buf[0] = byte((len(pipename) >> 24) & 0xff)
75+
buf[1] = byte((len(pipename) >> 16) & 0xff)
76+
buf[2] = byte((len(pipename) >> 8) & 0xff)
77+
buf[3] = byte(len(pipename) & 0xff)
78+
_, err = io.WriteString(in, string(buf)+pipename)
79+
if err != nil {
80+
log.Printf("failed to give [W] the pipe name: %s", err)
81+
terminate(cmd)
82+
continue
83+
}
84+
7285
return &repeater{in, out, cmd}, nil
7386
}
7487
case <-time.After(limit):

repeater.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Function MainLoop {
4747
$ssh_client_out.WriteByte(0xff)
4848
Log "ready: PSVersion $ver"
4949

50+
$buf = ReadMessage $ssh_client_in
51+
$pipename = [System.Text.Encoding]::UTF8.GetString($buf[4..$buf.Length])
52+
Log "[W] named pipe: $pipename"
53+
5054
while ($true) {
5155
Try {
5256
$null = $ssh_client_in.Read((New-Object byte[] 1), 0, 0)
@@ -57,7 +61,7 @@ Function MainLoop {
5761
Log "[W] return dummy for OpenSSH ext."
5862
Continue
5963
}
60-
$ssh_agent = New-Object System.IO.Pipes.NamedPipeClientStream ".", "openssh-ssh-agent", InOut
64+
$ssh_agent = New-Object System.IO.Pipes.NamedPipeClientStream ".", $pipename, InOut
6165
$ssh_agent.Connect()
6266
Log "[W] named pipe: connected"
6367
$ssh_agent.Write($buf, 0, $buf.Length)

repeater_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func setupDummyEnv(t *testing.T) string {
3939
func TestRepeaterNoPowerShell(t *testing.T) {
4040
setupDummyEnv(t)
4141

42-
_, err := newRepeater(context.Background(), "/dummy/powershell.exe")
42+
_, err := newRepeater(context.Background(), "/dummy/powershell.exe", "dummy-pipe-name")
4343
if err == nil || err.Error() != "failed to invoke PowerShell.exe 3 times; give up" {
4444
t.Errorf("should fail")
4545
}
@@ -52,7 +52,7 @@ func TestRepeaterBrokenPowerShell(t *testing.T) {
5252
if err != nil {
5353
t.Fatal(err)
5454
}
55-
_, err = newRepeater(context.Background(), powershellPath())
55+
_, err = newRepeater(context.Background(), powershellPath(), "dummy-pipe-name")
5656
if err == nil || err.Error() != "failed to invoke PowerShell.exe 3 times; give up" {
5757
t.Errorf("should fail")
5858
}
@@ -66,7 +66,7 @@ func TestRepeaterNormal(t *testing.T) {
6666
t.Fatal(err)
6767
}
6868

69-
rep, err := newRepeater(context.Background(), powershellPath())
69+
rep, err := newRepeater(context.Background(), powershellPath(), "dummy-pipe-name")
7070
if err != nil {
7171
t.Errorf("failed: %s", err)
7272
}
@@ -77,6 +77,12 @@ func TestRepeaterNormal(t *testing.T) {
7777
t.Errorf("does not work")
7878
}
7979

80+
buf = make([]byte, 19)
81+
_, err = io.ReadFull(rep.out, buf)
82+
if err != nil || string(buf) != "\x00\x00\x00\x0fdummy-pipe-name" {
83+
t.Errorf("does not work: %s", string(buf))
84+
}
85+
8086
_, err = rep.in.Write([]byte("Hello"))
8187
if err != nil {
8288
t.Fatal(err)

server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ import (
1414
type server struct {
1515
listener net.Listener
1616
powershellPath string
17+
pipeName string
1718
}
1819

19-
func newServer(socketPath string, powershellPath string) *server {
20+
func newServer(socketPath string, powershellPath string, pipeName string) *server {
2021
listener, err := net.Listen("unix", socketPath)
2122
if err != nil {
2223
log.Fatal(err)
2324
}
2425
log.Printf("start listening on %s", socketPath)
2526

26-
return &server{listener, powershellPath}
27+
return &server{listener, powershellPath, pipeName}
2728
}
2829

2930
type request struct {
@@ -91,7 +92,7 @@ func (s *server) server(ctx context.Context, cancel func(), requestQueue chan re
9192

9293
for {
9394
// invoke PowerShell.exe
94-
rep, err := newRepeater(ctx, s.powershellPath)
95+
rep, err := newRepeater(ctx, s.powershellPath, s.pipeName)
9596
if err != nil {
9697
return
9798
}

server_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ File.write('` + tmpDir + `/pid', $$.to_s)
2222
$stdout.sync = true
2323
$stdout << "\xff"
2424
s = $stdin.read(` + fmt.Sprintf("%d", len(repeaterPs1)) + `)
25+
len = $stdin.read(4)
26+
pipename = $stdin.read(len.unpack1("N"))
2527
loop do
2628
# echo
2729
len = $stdin.read(4)
@@ -38,7 +40,7 @@ end
3840
}
3941

4042
path := filepath.Join(tmpDir, "tmp.sock")
41-
s := newServer(path, powershellPath())
43+
s := newServer(path, powershellPath(), "dummy-pipe-name")
4244

4345
done := make(chan struct{})
4446
ctx, cancel := context.WithCancel(context.Background())

0 commit comments

Comments
 (0)