core: Remove hardcoded tor control address
The default address will be set from the environment, or fall back to trying 127.0.0.1:9051. There is now an API to allow this to be overriden as well.
This commit is contained in:
		
							parent
							
								
									9c0110dca2
								
							
						
					
					
						commit
						1be58828fe
					
				|  | @ -6,7 +6,6 @@ import ( | ||||||
| 	"github.com/ricochet-im/ricochet-go/core/utils" | 	"github.com/ricochet-im/ricochet-go/core/utils" | ||||||
| 	"github.com/ricochet-im/ricochet-go/rpc" | 	"github.com/ricochet-im/ricochet-go/rpc" | ||||||
| 	"github.com/yawning/bulb" | 	"github.com/yawning/bulb" | ||||||
| 	bulbutils "github.com/yawning/bulb/utils" |  | ||||||
| 	"golang.org/x/net/context" | 	"golang.org/x/net/context" | ||||||
| 	"golang.org/x/net/proxy" | 	"golang.org/x/net/proxy" | ||||||
| 	"log" | 	"log" | ||||||
|  | @ -65,23 +64,45 @@ func CreateNetwork() *Network { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Start connection to the tor control port at 'address', with the optional
 | func (n *Network) SetControlAddress(address string) error { | ||||||
| // control password 'password'. This function blocks until the first connection
 | 	n.controlMutex.Lock() | ||||||
| // attempt is finished. The first return value says whether the connection has
 | 	defer n.controlMutex.Unlock() | ||||||
| // been started; if true, the connection is up even if the first attempt failed.
 | 	if n.stoppedSignal != nil { | ||||||
| // The second return value is the connection attempt error, or nil on success.
 | 		return errors.New("Network is already started") | ||||||
| func (n *Network) Start(address, password string) (bool, error) { | 	} | ||||||
|  | 
 | ||||||
|  | 	n.controlAddress = address | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (n *Network) SetControlPassword(password string) error { | ||||||
|  | 	n.controlMutex.Lock() | ||||||
|  | 	defer n.controlMutex.Unlock() | ||||||
|  | 	if n.stoppedSignal != nil { | ||||||
|  | 		return errors.New("Network is already started") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	n.controlPassword = password | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Start connection to the tor control port. This function blocks until the first
 | ||||||
|  | // connection attempt is finished. The first return value says whether the
 | ||||||
|  | // connection has been started; if true, the connection is up even if the first
 | ||||||
|  | // attempt failed. The second return value is the connection attempt error, or
 | ||||||
|  | // nil on success.
 | ||||||
|  | func (n *Network) Start() (bool, error) { | ||||||
| 	n.controlMutex.Lock() | 	n.controlMutex.Lock() | ||||||
| 	if n.stoppedSignal != nil { | 	if n.stoppedSignal != nil { | ||||||
| 		// This is an error, because address/password might not be the same
 |  | ||||||
| 		n.controlMutex.Unlock() | 		n.controlMutex.Unlock() | ||||||
| 		return false, errors.New("Network is already started") | 		return false, errors.New("Network is already started") | ||||||
| 	} | 	} | ||||||
| 
 | 	if n.controlAddress == "" { | ||||||
|  | 		n.controlMutex.Unlock() | ||||||
|  | 		return false, errors.New("Control address not configured") | ||||||
|  | 	} | ||||||
| 	n.stopSignal = make(chan struct{}) | 	n.stopSignal = make(chan struct{}) | ||||||
| 	n.stoppedSignal = make(chan struct{}) | 	n.stoppedSignal = make(chan struct{}) | ||||||
| 	n.controlAddress = address |  | ||||||
| 	n.controlPassword = password |  | ||||||
| 	n.controlMutex.Unlock() | 	n.controlMutex.Unlock() | ||||||
| 
 | 
 | ||||||
| 	connectChannel := make(chan error) | 	connectChannel := make(chan error) | ||||||
|  | @ -176,10 +197,8 @@ func chooseSocksAddress(addresses []string, controlAddress string) (socksAddress | ||||||
| 		return selected, errors.New("No SOCKS port configured") | 		return selected, errors.New("No SOCKS port configured") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// controlAddress is in the form of tcp:// or unix://
 | 	if !strings.HasPrefix(controlAddress, "unix:") { | ||||||
| 	if strings.HasPrefix(controlAddress, "tcp:") { | 		addr, _, _ := net.SplitHostPort(controlAddress) | ||||||
| 		_, addrport, _ := bulbutils.ParseControlPortString(controlAddress) |  | ||||||
| 		addr, _, _ := net.SplitHostPort(addrport) |  | ||||||
| 		preferredIP = net.ParseIP(addr) | 		preferredIP = net.ParseIP(addr) | ||||||
| 		torOnLocalhost = preferredIP.IsLoopback() | 		torOnLocalhost = preferredIP.IsLoopback() | ||||||
| 	} | 	} | ||||||
|  | @ -467,8 +486,6 @@ func (n *Network) run(connectChannel chan<- error) { | ||||||
| 				n.conn.Close() | 				n.conn.Close() | ||||||
| 				n.conn = nil | 				n.conn = nil | ||||||
| 			} | 			} | ||||||
| 			n.controlAddress = "" |  | ||||||
| 			n.controlPassword = "" |  | ||||||
| 			n.stoppedSignal = nil | 			n.stoppedSignal = nil | ||||||
| 			n.status = ricochet.NetworkStatus{} | 			n.status = ricochet.NetworkStatus{} | ||||||
| 			n.controlMutex.Unlock() | 			n.controlMutex.Unlock() | ||||||
|  | @ -569,10 +586,13 @@ func (n *Network) connectControl() error { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func createConnection(address, password string) (*bulb.Conn, error) { | func createConnection(address, password string) (*bulb.Conn, error) { | ||||||
| 	net, addr, err := bulbutils.ParseControlPortString(address) | 	var net, addr string | ||||||
| 	if err != nil { | 	if strings.HasPrefix(address, "unix:") { | ||||||
| 		log.Printf("Parsing control network address '%s' failed: %v", address, err) | 		net = "unix" | ||||||
| 		return nil, err | 		addr = address[5:] | ||||||
|  | 	} else { | ||||||
|  | 		net = "tcp" | ||||||
|  | 		addr = address | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	conn, err := bulb.Dial(net, addr) | 	conn, err := bulb.Dial(net, addr) | ||||||
|  | @ -590,7 +610,7 @@ func createConnection(address, password string) (*bulb.Conn, error) { | ||||||
| 
 | 
 | ||||||
| 	conn.StartAsyncReader() | 	conn.StartAsyncReader() | ||||||
| 
 | 
 | ||||||
| 	log.Print("Control connected!") | 	log.Printf("Control connection to %s successful", address) | ||||||
| 	return conn, nil | 	return conn, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,6 +6,8 @@ import ( | ||||||
| 	"math" | 	"math" | ||||||
| 	"math/big" | 	"math/big" | ||||||
| 	"math/rand" | 	"math/rand" | ||||||
|  | 	"net" | ||||||
|  | 	"os" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Ricochet struct { | type Ricochet struct { | ||||||
|  | @ -14,18 +16,15 @@ type Ricochet struct { | ||||||
| 	Identity *Identity | 	Identity *Identity | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (core *Ricochet) Init(conf *Config) error { | func (core *Ricochet) Init(conf *Config) (err error) { | ||||||
| 	initRand() | 	initRand() | ||||||
| 
 | 
 | ||||||
| 	var err error |  | ||||||
| 	core.Config = conf | 	core.Config = conf | ||||||
| 	core.Network = CreateNetwork() |  | ||||||
| 	core.Identity, err = CreateIdentity(core) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	return nil | 	core.Network = CreateNetwork() | ||||||
|  | 	core.setupNetwork() | ||||||
|  | 	core.Identity, err = CreateIdentity(core) | ||||||
|  | 	return | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func initRand() { | func initRand() { | ||||||
|  | @ -36,3 +35,25 @@ func initRand() { | ||||||
| 
 | 
 | ||||||
| 	rand.Seed(n.Int64()) | 	rand.Seed(n.Int64()) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (core *Ricochet) setupNetwork() { | ||||||
|  | 	socket := os.Getenv("TOR_CONTROL_SOCKET") | ||||||
|  | 	host := os.Getenv("TOR_CONTROL_HOST") | ||||||
|  | 	port := os.Getenv("TOR_CONTROL_PORT") | ||||||
|  | 	passwd := os.Getenv("TOR_CONTROL_PASSWD") | ||||||
|  | 
 | ||||||
|  | 	if socket != "" { | ||||||
|  | 		core.Network.SetControlAddress("unix:" + socket) | ||||||
|  | 	} else if host != "" { | ||||||
|  | 		if port == "" { | ||||||
|  | 			port = "9051" | ||||||
|  | 		} | ||||||
|  | 		core.Network.SetControlAddress(net.JoinHostPort(host, port)) | ||||||
|  | 	} else { | ||||||
|  | 		core.Network.SetControlAddress("127.0.0.1:9051") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if passwd != "" { | ||||||
|  | 		core.Network.SetControlPassword(passwd) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -54,7 +54,7 @@ func (s *RpcServer) MonitorNetwork(req *ricochet.MonitorNetworkRequest, stream r | ||||||
| func (s *RpcServer) StartNetwork(ctx context.Context, req *ricochet.StartNetworkRequest) (*ricochet.NetworkStatus, error) { | func (s *RpcServer) StartNetwork(ctx context.Context, req *ricochet.StartNetworkRequest) (*ricochet.NetworkStatus, error) { | ||||||
| 	// err represents the result of the first connection attempt, but as long
 | 	// err represents the result of the first connection attempt, but as long
 | ||||||
| 	// as 'ok' is true, the network has started and this call was successful.
 | 	// as 'ok' is true, the network has started and this call was successful.
 | ||||||
| 	ok, err := s.Core.Network.Start("tcp://127.0.0.1:9051", "") | 	ok, err := s.Core.Network.Start() | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue