Skip to content

Commit

Permalink
Add CmdCreaor to process and tor, allowing fully custom cmds to be
Browse files Browse the repository at this point in the history
inserted
  • Loading branch information
dballard authored and Dan Ballard committed Jan 22, 2020
1 parent 1c71414 commit e063c10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 14 additions & 6 deletions process/process.go
Expand Up @@ -40,25 +40,33 @@ 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 {
*exec.Cmd
}

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
Expand Down
13 changes: 11 additions & 2 deletions tor/tor.go
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit e063c10

Please sign in to comment.