commit 0f40b62cba0aa8581432b37f867d197ba2c32fba Author: David Fifield david@bamsoftware.com Date: Thu Apr 10 08:41:12 2014 -0700
Move a couple programs to be named after themselves. --- meek-client-torbrowser/main.go | 181 -------------------- meek-client-torbrowser/meek-client-torbrowser.go | 181 ++++++++++++++++++++ terminateprocess-buffer/main.go | 38 ---- terminateprocess-buffer/terminateprocess-buffer.go | 38 ++++ 4 files changed, 219 insertions(+), 219 deletions(-)
diff --git a/meek-client-torbrowser/main.go b/meek-client-torbrowser/main.go deleted file mode 100644 index 1d55eff..0000000 --- a/meek-client-torbrowser/main.go +++ /dev/null @@ -1,181 +0,0 @@ -// The meek-client-torbrowser program starts a copy of Tor Browser running -// meek-http-helper in a special profile, and then starts meek-client set up to -// use the browser helper. -// -// Arguments to this program are passed unmodified to meek-client, with the -// addition of a --helper option pointing to the browser helper. -package main - -import ( - "bufio" - "io" - "io/ioutil" - "log" - "os" - "os/exec" - "os/signal" - "path/filepath" - "regexp" - "syscall" -) - -var helperAddrPattern *regexp.Regexp - -// Log a call to os.Process.Kill. -func logKill(p *os.Process) error { - log.Printf("killing PID %d", p.Pid) - err := p.Kill() - if err != nil { - log.Print(err) - } - return err -} - -// Log a call to os.Process.Signal. -func logSignal(p *os.Process, sig os.Signal) error { - log.Printf("sending signal %s to PID %d", sig, p.Pid) - err := p.Signal(sig) - if err != nil { - log.Print(err) - } - return err -} - -// Run firefox and return its exec.Cmd and stdout pipe. -func runFirefox() (cmd *exec.Cmd, stdout io.Reader, err error) { - var profilePath string - // Mac OS X needs an absolute profile path. - profilePath, err = filepath.Abs(firefoxProfilePath) - if err != nil { - return - } - cmd = exec.Command(firefoxPath, "-no-remote", "-profile", profilePath) - cmd.Stderr = os.Stderr - stdout, err = cmd.StdoutPipe() - if err != nil { - return - } - log.Printf("running firefox command %q", cmd.Args) - err = cmd.Start() - if err != nil { - return - } - log.Printf("firefox started with pid %d", cmd.Process.Pid) - return cmd, stdout, nil -} - -// Look for the magic meek-http-helper address string in the Reader, and return -// the address it contains. Start a goroutine to continue reading and discarding -// output of the Reader before returning. -func grepHelperAddr(r io.Reader) (string, error) { - var helperAddr string - scanner := bufio.NewScanner(r) - for scanner.Scan() { - line := scanner.Text() - if m := helperAddrPattern.FindStringSubmatch(line); m != nil { - helperAddr = m[1] - break - } - } - err := scanner.Err() - if err != nil { - return "", err - } - // Ran out of input before finding the pattern. - if helperAddr == "" { - return "", io.EOF - } - // Keep reading from the browser to avoid its output buffer filling. - go io.Copy(ioutil.Discard, r) - return helperAddr, nil -} - -// Run meek-client and return its exec.Cmd. -func runMeekClient(helperAddr string) (cmd *exec.Cmd, err error) { - args := os.Args[1:] - args = append(args, []string{"--helper", helperAddr}...) - cmd = exec.Command(meekClientPath, args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - log.Printf("running meek-client command %q", cmd.Args) - err = cmd.Start() - if err != nil { - return - } - log.Printf("meek-client started with pid %d", cmd.Process.Pid) - return cmd, nil -} - -func main() { - var err error - - f, err := os.OpenFile("meek-client-torbrowser.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) - if err != nil { - log.Fatal(err) - } - defer f.Close() - log.SetOutput(f) - - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - - // This magic string is emitted by meek-http-helper. - helperAddrPattern, err = regexp.Compile(`^meek-http-helper: listen (127.0.0.1:\d+)$`) - if err != nil { - log.Fatal(err) - } - - // Start firefox. - firefoxCmd, stdout, err := runFirefox() - if err != nil { - log.Print(err) - return - } - defer logKill(firefoxCmd.Process) - - // Find out the helper's listening address. - helperAddr, err := grepHelperAddr(stdout) - if err != nil { - log.Print(err) - return - } - - // Start meek-client with the helper address. - meekClientCmd, err := runMeekClient(helperAddr) - if err != nil { - log.Print(err) - return - } - defer logKill(meekClientCmd.Process) - - if exitOnStdinEOF { - // On Windows, we don't get a SIGINT or SIGTERM, rather we are killed - // without a chance to clean up our subprocesses. When run inside - // terminateprocess-buffer, it is instead terminateprocess-buffer that - // is killed, and we can detect that event by that our stdin gets - // closed. - // https://trac.torproject.org/projects/tor/ticket/9330 - go func() { - io.Copy(ioutil.Discard, os.Stdin) - log.Printf("synthesizing SIGTERM because of stdin close") - sigChan <- syscall.SIGTERM - }() - } - - sig := <-sigChan - log.Printf("sig %s", sig) - err = logSignal(meekClientCmd.Process, sig) - if err != nil { - log.Print(err) - } - - // If SIGINT, wait for a second SIGINT. - if sig == syscall.SIGINT { - sig := <-sigChan - log.Printf("sig %s", sig) - err = logSignal(meekClientCmd.Process, sig) - if err != nil { - log.Print(err) - } - } -} diff --git a/meek-client-torbrowser/meek-client-torbrowser.go b/meek-client-torbrowser/meek-client-torbrowser.go new file mode 100644 index 0000000..1d55eff --- /dev/null +++ b/meek-client-torbrowser/meek-client-torbrowser.go @@ -0,0 +1,181 @@ +// The meek-client-torbrowser program starts a copy of Tor Browser running +// meek-http-helper in a special profile, and then starts meek-client set up to +// use the browser helper. +// +// Arguments to this program are passed unmodified to meek-client, with the +// addition of a --helper option pointing to the browser helper. +package main + +import ( + "bufio" + "io" + "io/ioutil" + "log" + "os" + "os/exec" + "os/signal" + "path/filepath" + "regexp" + "syscall" +) + +var helperAddrPattern *regexp.Regexp + +// Log a call to os.Process.Kill. +func logKill(p *os.Process) error { + log.Printf("killing PID %d", p.Pid) + err := p.Kill() + if err != nil { + log.Print(err) + } + return err +} + +// Log a call to os.Process.Signal. +func logSignal(p *os.Process, sig os.Signal) error { + log.Printf("sending signal %s to PID %d", sig, p.Pid) + err := p.Signal(sig) + if err != nil { + log.Print(err) + } + return err +} + +// Run firefox and return its exec.Cmd and stdout pipe. +func runFirefox() (cmd *exec.Cmd, stdout io.Reader, err error) { + var profilePath string + // Mac OS X needs an absolute profile path. + profilePath, err = filepath.Abs(firefoxProfilePath) + if err != nil { + return + } + cmd = exec.Command(firefoxPath, "-no-remote", "-profile", profilePath) + cmd.Stderr = os.Stderr + stdout, err = cmd.StdoutPipe() + if err != nil { + return + } + log.Printf("running firefox command %q", cmd.Args) + err = cmd.Start() + if err != nil { + return + } + log.Printf("firefox started with pid %d", cmd.Process.Pid) + return cmd, stdout, nil +} + +// Look for the magic meek-http-helper address string in the Reader, and return +// the address it contains. Start a goroutine to continue reading and discarding +// output of the Reader before returning. +func grepHelperAddr(r io.Reader) (string, error) { + var helperAddr string + scanner := bufio.NewScanner(r) + for scanner.Scan() { + line := scanner.Text() + if m := helperAddrPattern.FindStringSubmatch(line); m != nil { + helperAddr = m[1] + break + } + } + err := scanner.Err() + if err != nil { + return "", err + } + // Ran out of input before finding the pattern. + if helperAddr == "" { + return "", io.EOF + } + // Keep reading from the browser to avoid its output buffer filling. + go io.Copy(ioutil.Discard, r) + return helperAddr, nil +} + +// Run meek-client and return its exec.Cmd. +func runMeekClient(helperAddr string) (cmd *exec.Cmd, err error) { + args := os.Args[1:] + args = append(args, []string{"--helper", helperAddr}...) + cmd = exec.Command(meekClientPath, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + log.Printf("running meek-client command %q", cmd.Args) + err = cmd.Start() + if err != nil { + return + } + log.Printf("meek-client started with pid %d", cmd.Process.Pid) + return cmd, nil +} + +func main() { + var err error + + f, err := os.OpenFile("meek-client-torbrowser.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) + if err != nil { + log.Fatal(err) + } + defer f.Close() + log.SetOutput(f) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + + // This magic string is emitted by meek-http-helper. + helperAddrPattern, err = regexp.Compile(`^meek-http-helper: listen (127.0.0.1:\d+)$`) + if err != nil { + log.Fatal(err) + } + + // Start firefox. + firefoxCmd, stdout, err := runFirefox() + if err != nil { + log.Print(err) + return + } + defer logKill(firefoxCmd.Process) + + // Find out the helper's listening address. + helperAddr, err := grepHelperAddr(stdout) + if err != nil { + log.Print(err) + return + } + + // Start meek-client with the helper address. + meekClientCmd, err := runMeekClient(helperAddr) + if err != nil { + log.Print(err) + return + } + defer logKill(meekClientCmd.Process) + + if exitOnStdinEOF { + // On Windows, we don't get a SIGINT or SIGTERM, rather we are killed + // without a chance to clean up our subprocesses. When run inside + // terminateprocess-buffer, it is instead terminateprocess-buffer that + // is killed, and we can detect that event by that our stdin gets + // closed. + // https://trac.torproject.org/projects/tor/ticket/9330 + go func() { + io.Copy(ioutil.Discard, os.Stdin) + log.Printf("synthesizing SIGTERM because of stdin close") + sigChan <- syscall.SIGTERM + }() + } + + sig := <-sigChan + log.Printf("sig %s", sig) + err = logSignal(meekClientCmd.Process, sig) + if err != nil { + log.Print(err) + } + + // If SIGINT, wait for a second SIGINT. + if sig == syscall.SIGINT { + sig := <-sigChan + log.Printf("sig %s", sig) + err = logSignal(meekClientCmd.Process, sig) + if err != nil { + log.Print(err) + } + } +} diff --git a/terminateprocess-buffer/main.go b/terminateprocess-buffer/main.go deleted file mode 100644 index 16a7297..0000000 --- a/terminateprocess-buffer/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// This program is designed to sit between tor and a transport plugin on -// Windows. On Windows, transport plugins are killed with a TerminateProcess, -// which doesn't give them a chance to clean up before exiting. -// https://trac.torproject.org/projects/tor/ticket/9330 -// The idea of this program is that the transport plugin can read from its -// standard input, which will be closed when this program is terminated. The -// transport plugin can then treat the stdin-closed event like a SIGTERM. -package main - -import ( - "io" - "log" - "os" - "os/exec" -) - -func main() { - args := os.Args[1:] - if len(args) < 1 { - log.Fatalf("%s needs a command to run", os.Args[0]) - } - cmd := exec.Command(args[0], args[1:]...) - stdin, err := cmd.StdinPipe() - if err != nil { - log.Fatal(err) - } - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Start() - if err != nil { - log.Fatal(err) - } - io.Copy(stdin, os.Stdin) - err = cmd.Wait() - if err != nil { - log.Fatal(err) - } -} diff --git a/terminateprocess-buffer/terminateprocess-buffer.go b/terminateprocess-buffer/terminateprocess-buffer.go new file mode 100644 index 0000000..16a7297 --- /dev/null +++ b/terminateprocess-buffer/terminateprocess-buffer.go @@ -0,0 +1,38 @@ +// This program is designed to sit between tor and a transport plugin on +// Windows. On Windows, transport plugins are killed with a TerminateProcess, +// which doesn't give them a chance to clean up before exiting. +// https://trac.torproject.org/projects/tor/ticket/9330 +// The idea of this program is that the transport plugin can read from its +// standard input, which will be closed when this program is terminated. The +// transport plugin can then treat the stdin-closed event like a SIGTERM. +package main + +import ( + "io" + "log" + "os" + "os/exec" +) + +func main() { + args := os.Args[1:] + if len(args) < 1 { + log.Fatalf("%s needs a command to run", os.Args[0]) + } + cmd := exec.Command(args[0], args[1:]...) + stdin, err := cmd.StdinPipe() + if err != nil { + log.Fatal(err) + } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Start() + if err != nil { + log.Fatal(err) + } + io.Copy(stdin, os.Stdin) + err = cmd.Wait() + if err != nil { + log.Fatal(err) + } +}
tor-commits@lists.torproject.org