|
|
|
@ -4,9 +4,14 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/yawning/bulb"
|
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"os/user"
|
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
@ -15,6 +20,7 @@ import (
|
|
|
|
|
type Manager struct {
|
|
|
|
|
socksPort int
|
|
|
|
|
controlPort int
|
|
|
|
|
process *exec.Cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewTorManager Instantiates a new connection manager, returns non-nil error if it fails to connect to a tor daemon on the given ports.
|
|
|
|
@ -23,6 +29,39 @@ func NewTorManager(socksPort int, controlPort int) (*Manager, error) {
|
|
|
|
|
torManager.socksPort = socksPort
|
|
|
|
|
torManager.controlPort = controlPort
|
|
|
|
|
err := torManager.TestConnection()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrc := path.Join(usr.HomeDir, ".cwtch", "torrc")
|
|
|
|
|
if _, err := os.Stat(torrc); os.IsNotExist(err) {
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(path.Join(usr.HomeDir, ".cwtch"), 0700)
|
|
|
|
|
|
|
|
|
|
file, err := os.Create(torrc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(file, "SOCKSPort %d\nControlPort %d\n", socksPort, controlPort)
|
|
|
|
|
file.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command("tor", "-f", torrc)
|
|
|
|
|
err = cmd.Start()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("\nWaiting to connect to Tor Proxy...\n")
|
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
|
torManager.process = cmd
|
|
|
|
|
err = torManager.TestConnection()
|
|
|
|
|
return torManager, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return torManager, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -35,6 +74,15 @@ const (
|
|
|
|
|
proxyStatusTimeout
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Shutdown kills the managed Tor Process
|
|
|
|
|
func (tm *Manager) Shutdown() {
|
|
|
|
|
if tm.process != nil {
|
|
|
|
|
if err := tm.process.Process.Kill(); err != nil {
|
|
|
|
|
log.Fatal("failed to kill process: ", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detect whether a proxy is connectable and is a Tor proxy
|
|
|
|
|
func checkTorProxy(proxyAddress string) proxyStatus {
|
|
|
|
|
// A trick to do this without making an outward connection is,
|
|
|
|
|