From 1ebc3b82c8fa316e6c1f975ab0524eeeb4256148 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 23 Sep 2021 15:31:53 +0800 Subject: [PATCH] tor: rename dial to dialProxy This commit renames dial to be dialProxy to make the connections clearer. It also cleans the column width and provides more verbose error messages. --- tor/add_onion.go | 31 +++++++++++++++++-------------- tor/controller.go | 6 +++--- tor/tor.go | 18 ++++++++++-------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/tor/add_onion.go b/tor/add_onion.go index aca433ad4..17b83dba0 100644 --- a/tor/add_onion.go +++ b/tor/add_onion.go @@ -52,7 +52,9 @@ var _ OnionStore = (*OnionFile)(nil) // NewOnionFile creates a file-based implementation of the OnionStore interface // to store an onion service's private key. -func NewOnionFile(privateKeyPath string, privateKeyPerm os.FileMode) *OnionFile { +func NewOnionFile(privateKeyPath string, + privateKeyPerm os.FileMode) *OnionFile { + return &OnionFile{ privateKeyPath: privateKeyPath, privateKeyPerm: privateKeyPerm, @@ -64,8 +66,8 @@ func (f *OnionFile) StorePrivateKey(_ OnionType, privateKey []byte) error { return ioutil.WriteFile(f.privateKeyPath, privateKey, f.privateKeyPerm) } -// PrivateKey retrieves the private key from its expected path. If the file does -// not exist, then ErrNoPrivateKey is returned. +// PrivateKey retrieves the private key from its expected path. If the file +// does not exist, then ErrNoPrivateKey is returned. func (f *OnionFile) PrivateKey(_ OnionType) ([]byte, error) { if _, err := os.Stat(f.privateKeyPath); os.IsNotExist(err) { return nil, ErrNoPrivateKey @@ -78,8 +80,8 @@ func (f *OnionFile) DeletePrivateKey(_ OnionType) error { return os.Remove(f.privateKeyPath) } -// AddOnionConfig houses all of the required parameters in order to successfully -// create a new onion service or restore an existing one. +// AddOnionConfig houses all of the required parameters in order to +// successfully create a new onion service or restore an existing one. type AddOnionConfig struct { // Type denotes the type of the onion service that should be created. Type OnionType @@ -87,9 +89,9 @@ type AddOnionConfig struct { // VirtualPort is the externally reachable port of the onion address. VirtualPort int - // TargetPorts is the set of ports that the service will be listening on - // locally. The Tor server will use choose a random port from this set - // to forward the traffic from the virtual port. + // TargetPorts is the set of ports that the service will be listening + // on locally. The Tor server will use choose a random port from this + // set to forward the traffic from the virtual port. // // NOTE: If nil/empty, the virtual port will be used as the only target // port. @@ -116,10 +118,11 @@ func (c *Controller) AddOnion(cfg AddOnionConfig) (*OnionAddr, error) { } } - // We'll start off by checking if the store contains an existing private - // key. If it does not, then we should request the server to create a - // new onion service and return its private key. Otherwise, we'll - // request the server to recreate the onion server from our private key. + // We'll start off by checking if the store contains an existing + // private key. If it does not, then we should request the server to + // create a new onion service and return its private key. Otherwise, + // we'll request the server to recreate the onion server from our + // private key. var keyParam string switch cfg.Type { case V2: @@ -155,8 +158,8 @@ func (c *Controller) AddOnion(cfg AddOnionConfig) (*OnionAddr, error) { portParam += fmt.Sprintf("Port=%d,%d ", cfg.VirtualPort, targetPort) } else { - portParam += fmt.Sprintf("Port=%d,%s:%d ", cfg.VirtualPort, - c.targetIPAddress, targetPort) + portParam += fmt.Sprintf("Port=%d,%s:%d ", + cfg.VirtualPort, c.targetIPAddress, targetPort) } } diff --git a/tor/controller.go b/tor/controller.go index f828b6397..5e24ae20d 100644 --- a/tor/controller.go +++ b/tor/controller.go @@ -114,9 +114,9 @@ func NewController(controlAddr string, targetIPAddress string, } } -// Start establishes and authenticates the connection between the controller and -// a Tor server. Once done, the controller will be able to send commands and -// expect responses. +// Start establishes and authenticates the connection between the controller +// and a Tor server. Once done, the controller will be able to send commands +// and expect responses. func (c *Controller) Start() error { if !atomic.CompareAndSwapInt32(&c.started, 0, 1) { return nil diff --git a/tor/tor.go b/tor/tor.go index c373a250e..f2cb472e8 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -66,14 +66,15 @@ func (c *proxyConn) RemoteAddr() net.Addr { // around net.Conn in order to expose the actual remote address we're dialing, // rather than the proxy's address. func Dial(address, socksAddr string, streamIsolation bool, - skipProxyForClearNetTargets bool, timeout time.Duration) (net.Conn, error) { + skipProxyForClearNetTargets bool, + timeout time.Duration) (net.Conn, error) { - conn, err := dial( + conn, err := dialProxy( address, socksAddr, streamIsolation, skipProxyForClearNetTargets, timeout, ) if err != nil { - return nil, err + return nil, fmt.Errorf("dial proxy failed: %w", err) } // Now that the connection is established, we'll create our internal @@ -90,7 +91,7 @@ func Dial(address, socksAddr string, streamIsolation bool, }, nil } -// dial establishes a connection to the address via the provided TOR SOCKS +// dialProxy establishes a connection to the address via the provided TOR SOCKS // proxy. Only TCP traffic may be routed via Tor. // // streamIsolation determines if we should force stream isolation for this new @@ -100,8 +101,9 @@ func Dial(address, socksAddr string, streamIsolation bool, // skipProxyForClearNetTargets argument allows the dialer to directly connect // to the provided address if it does not represent an union service, skipping // the SOCKS proxy. -func dial(address, socksAddr string, streamIsolation bool, - skipProxyForClearNetTargets bool, timeout time.Duration) (net.Conn, error) { +func dialProxy(address, socksAddr string, streamIsolation bool, + skipProxyForClearNetTargets bool, + timeout time.Duration) (net.Conn, error) { // If we were requested to force stream isolation for this connection, // we'll populate the authentication credentials with random data as @@ -136,7 +138,7 @@ func dial(address, socksAddr string, streamIsolation bool, // Establish the connection through Tor's SOCKS proxy. dialer, err := proxy.SOCKS5("tcp", socksAddr, auth, clearDialer) if err != nil { - return nil, err + return nil, fmt.Errorf("establish sock proxy: %w", err) } return dialer.Dial("tcp", address) @@ -163,7 +165,7 @@ func LookupSRV(service, proto, name, socksAddr, timeout time.Duration) (string, []*net.SRV, error) { // Connect to the DNS server we'll be using to query SRV records. - conn, err := dial( + conn, err := dialProxy( dnsServer, socksAddr, streamIsolation, skipProxyForClearNetTargets, timeout, )