From e063c103b541ecf2425b383664d2be1b507a3fe2 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Fri, 9 Aug 2019 18:58:25 -0700 Subject: [PATCH] Add CmdCreaor to process and tor, allowing fully custom cmds to be inserted --- process/process.go | 20 ++++++++++++++------ tor/tor.go | 13 +++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/process/process.go b/process/process.go index a9b4d33..d6f5f31 100644 --- a/process/process.go +++ b/process/process.go @@ -40,14 +40,24 @@ type Creator interface { New(ctx context.Context, args ...string) (Process, error) } +type CmdCreatorFunc func(ctx context.Context, exePath string, args ...string) (*exec.Cmd, error) + type exeProcessCreator struct { exePath string + cmdCreatorFunc CmdCreatorFunc +} + +func NewCmd(ctx context.Context, exePath string, args ...string) (*exec.Cmd, error) { + cmd := exec.CommandContext(ctx, exePath, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd, nil } // NewCreator creates a Creator for external Tor process execution based on the // given exe path. -func NewCreator(exePath string) Creator { - return &exeProcessCreator{exePath} +func NewCreator(exePath string, newCmd CmdCreatorFunc) Creator { + return &exeProcessCreator{exePath, newCmd} } type exeProcess struct { @@ -55,10 +65,8 @@ type exeProcess struct { } func (e *exeProcessCreator) New(ctx context.Context, args ...string) (Process, error) { - cmd := exec.CommandContext(ctx, e.exePath, args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return &exeProcess{cmd}, nil + cmd, err := e.cmdCreatorFunc(ctx, e.exePath, args...) + return &exeProcess{cmd}, err } // ErrControlConnUnsupported is returned by Process.EmbeddedControlConn when diff --git a/tor/tor.go b/tor/tor.go index 0edd241..e5a1339 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -65,8 +65,12 @@ type StartConf struct { // set. ExePath string + // CmdCreatorFunc is the override to use a specific exec.Cmd. This is + // ignored if ProcessCreator is set + CmdCreator process.CmdCreatorFunc + // ProcessCreator is the override to use a specific process creator. If set, - // ExePath is ignored. + // ExePath and CmdCreator are ignored. ProcessCreator process.Creator // UseEmbeddedControlConn can be set to true to use @@ -229,13 +233,18 @@ func createFile(to string, from io.ReadCloser) error { func (t *Tor) startProcess(ctx context.Context, conf *StartConf) error { // Get the creator + cmdCreator := conf.CmdCreator + if cmdCreator == nil { + cmdCreator = process.NewCmd + } + creator := conf.ProcessCreator if creator == nil { torPath := conf.ExePath if torPath == "" { torPath = "tor" } - creator = process.NewCreator(torPath) + creator = process.NewCreator(torPath, cmdCreator) } // Build the args args := []string{"--DataDirectory", t.DataDir}