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.
This commit is contained in:
yyforyongyu 2021-09-23 15:31:53 +08:00
parent 64211da40d
commit 1ebc3b82c8
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 30 additions and 25 deletions

View File

@ -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)
}
}

View File

@ -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

View File

@ -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,
)