Skip to content

Commit

Permalink
Merge pull request #14 from mame/make-pipename-configurable
Browse files Browse the repository at this point in the history
Make the pipe name configurable
  • Loading branch information
mame authored Sep 20, 2024
2 parents 46bfe77 + 2707a90 commit f0348d8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type config struct {
socketPath string
powershellPath string
pipeName string
format string
foreground bool
verbose bool
Expand Down Expand Up @@ -57,6 +58,7 @@ func newConfig() *config {

flag.StringVar(&c.socketPath, "socket", defaultSocketPath(), "a path of UNIX domain socket to listen")
flag.StringVar(&c.powershellPath, "powershell-path", powershellPath(), "a path of Windows PowerShell")
flag.StringVar(&c.pipeName, "pipename", "openssh-ssh-agent", "a name of pipe to connect")
flag.BoolVar(&c.foreground, "foreground", false, "run in foreground mode")
flag.BoolVar(&c.verbose, "verbose", false, "verbose mode")
flag.StringVar(&c.logFile, "log", "", "a file path to write the log")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func main() {

ctx := c.start()

s := newServer(c.socketPath, c.powershellPath)
s := newServer(c.socketPath, c.powershellPath, c.pipeName)

s.run(ctx)
}
15 changes: 14 additions & 1 deletion repeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var waitTimes = []time.Duration{
}

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

Expand Down Expand Up @@ -69,6 +69,19 @@ func newRepeater(ctx context.Context, powershell string) (*repeater, error) {
case ok := <-done:
if ok {
log.Printf("[W] invoked successfully")

buf := make([]byte, 4)
buf[0] = byte((len(pipename) >> 24) & 0xff)
buf[1] = byte((len(pipename) >> 16) & 0xff)
buf[2] = byte((len(pipename) >> 8) & 0xff)
buf[3] = byte(len(pipename) & 0xff)
_, err = io.WriteString(in, string(buf)+pipename)
if err != nil {
log.Printf("failed to give [W] the pipe name: %s", err)
terminate(cmd)
continue
}

return &repeater{in, out, cmd}, nil
}
case <-time.After(limit):
Expand Down
6 changes: 5 additions & 1 deletion repeater.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Function MainLoop {
$ssh_client_out.WriteByte(0xff)
Log "ready: PSVersion $ver"

$buf = ReadMessage $ssh_client_in
$pipename = [System.Text.Encoding]::UTF8.GetString($buf[4..$buf.Length])
Log "[W] named pipe: $pipename"

while ($true) {
Try {
$null = $ssh_client_in.Read((New-Object byte[] 1), 0, 0)
Expand All @@ -57,7 +61,7 @@ Function MainLoop {
Log "[W] return dummy for OpenSSH ext."
Continue
}
$ssh_agent = New-Object System.IO.Pipes.NamedPipeClientStream ".", "openssh-ssh-agent", InOut
$ssh_agent = New-Object System.IO.Pipes.NamedPipeClientStream ".", $pipename, InOut
$ssh_agent.Connect()
Log "[W] named pipe: connected"
$ssh_agent.Write($buf, 0, $buf.Length)
Expand Down
12 changes: 9 additions & 3 deletions repeater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func setupDummyEnv(t *testing.T) string {
func TestRepeaterNoPowerShell(t *testing.T) {
setupDummyEnv(t)

_, err := newRepeater(context.Background(), "/dummy/powershell.exe")
_, err := newRepeater(context.Background(), "/dummy/powershell.exe", "dummy-pipe-name")
if err == nil || err.Error() != "failed to invoke PowerShell.exe 3 times; give up" {
t.Errorf("should fail")
}
Expand All @@ -52,7 +52,7 @@ func TestRepeaterBrokenPowerShell(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = newRepeater(context.Background(), powershellPath())
_, err = newRepeater(context.Background(), powershellPath(), "dummy-pipe-name")
if err == nil || err.Error() != "failed to invoke PowerShell.exe 3 times; give up" {
t.Errorf("should fail")
}
Expand All @@ -66,7 +66,7 @@ func TestRepeaterNormal(t *testing.T) {
t.Fatal(err)
}

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

buf = make([]byte, 19)
_, err = io.ReadFull(rep.out, buf)
if err != nil || string(buf) != "\x00\x00\x00\x0fdummy-pipe-name" {
t.Errorf("does not work: %s", string(buf))
}

_, err = rep.in.Write([]byte("Hello"))
if err != nil {
t.Fatal(err)
Expand Down
7 changes: 4 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import (
type server struct {
listener net.Listener
powershellPath string
pipeName string
}

func newServer(socketPath string, powershellPath string) *server {
func newServer(socketPath string, powershellPath string, pipeName string) *server {
listener, err := net.Listen("unix", socketPath)
if err != nil {
log.Fatal(err)
}
log.Printf("start listening on %s", socketPath)

return &server{listener, powershellPath}
return &server{listener, powershellPath, pipeName}
}

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

for {
// invoke PowerShell.exe
rep, err := newRepeater(ctx, s.powershellPath)
rep, err := newRepeater(ctx, s.powershellPath, s.pipeName)
if err != nil {
return
}
Expand Down
4 changes: 3 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ File.write('` + tmpDir + `/pid', $$.to_s)
$stdout.sync = true
$stdout << "\xff"
s = $stdin.read(` + fmt.Sprintf("%d", len(repeaterPs1)) + `)
len = $stdin.read(4)
pipename = $stdin.read(len.unpack1("N"))
loop do
# echo
len = $stdin.read(4)
Expand All @@ -38,7 +40,7 @@ end
}

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

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

0 comments on commit f0348d8

Please sign in to comment.