mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-03 18:22:25 +02:00
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:
@@ -52,7 +52,9 @@ var _ OnionStore = (*OnionFile)(nil)
|
|||||||
|
|
||||||
// NewOnionFile creates a file-based implementation of the OnionStore interface
|
// NewOnionFile creates a file-based implementation of the OnionStore interface
|
||||||
// to store an onion service's private key.
|
// 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{
|
return &OnionFile{
|
||||||
privateKeyPath: privateKeyPath,
|
privateKeyPath: privateKeyPath,
|
||||||
privateKeyPerm: privateKeyPerm,
|
privateKeyPerm: privateKeyPerm,
|
||||||
@@ -64,8 +66,8 @@ func (f *OnionFile) StorePrivateKey(_ OnionType, privateKey []byte) error {
|
|||||||
return ioutil.WriteFile(f.privateKeyPath, privateKey, f.privateKeyPerm)
|
return ioutil.WriteFile(f.privateKeyPath, privateKey, f.privateKeyPerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrivateKey retrieves the private key from its expected path. If the file does
|
// PrivateKey retrieves the private key from its expected path. If the file
|
||||||
// not exist, then ErrNoPrivateKey is returned.
|
// does not exist, then ErrNoPrivateKey is returned.
|
||||||
func (f *OnionFile) PrivateKey(_ OnionType) ([]byte, error) {
|
func (f *OnionFile) PrivateKey(_ OnionType) ([]byte, error) {
|
||||||
if _, err := os.Stat(f.privateKeyPath); os.IsNotExist(err) {
|
if _, err := os.Stat(f.privateKeyPath); os.IsNotExist(err) {
|
||||||
return nil, ErrNoPrivateKey
|
return nil, ErrNoPrivateKey
|
||||||
@@ -78,8 +80,8 @@ func (f *OnionFile) DeletePrivateKey(_ OnionType) error {
|
|||||||
return os.Remove(f.privateKeyPath)
|
return os.Remove(f.privateKeyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddOnionConfig houses all of the required parameters in order to successfully
|
// AddOnionConfig houses all of the required parameters in order to
|
||||||
// create a new onion service or restore an existing one.
|
// successfully create a new onion service or restore an existing one.
|
||||||
type AddOnionConfig struct {
|
type AddOnionConfig struct {
|
||||||
// Type denotes the type of the onion service that should be created.
|
// Type denotes the type of the onion service that should be created.
|
||||||
Type OnionType
|
Type OnionType
|
||||||
@@ -87,9 +89,9 @@ type AddOnionConfig struct {
|
|||||||
// VirtualPort is the externally reachable port of the onion address.
|
// VirtualPort is the externally reachable port of the onion address.
|
||||||
VirtualPort int
|
VirtualPort int
|
||||||
|
|
||||||
// TargetPorts is the set of ports that the service will be listening on
|
// TargetPorts is the set of ports that the service will be listening
|
||||||
// locally. The Tor server will use choose a random port from this set
|
// on locally. The Tor server will use choose a random port from this
|
||||||
// to forward the traffic from the virtual port.
|
// set to forward the traffic from the virtual port.
|
||||||
//
|
//
|
||||||
// NOTE: If nil/empty, the virtual port will be used as the only target
|
// NOTE: If nil/empty, the virtual port will be used as the only target
|
||||||
// port.
|
// 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
|
// We'll start off by checking if the store contains an existing
|
||||||
// key. If it does not, then we should request the server to create a
|
// private key. If it does not, then we should request the server to
|
||||||
// new onion service and return its private key. Otherwise, we'll
|
// create a new onion service and return its private key. Otherwise,
|
||||||
// request the server to recreate the onion server from our private key.
|
// we'll request the server to recreate the onion server from our
|
||||||
|
// private key.
|
||||||
var keyParam string
|
var keyParam string
|
||||||
switch cfg.Type {
|
switch cfg.Type {
|
||||||
case V2:
|
case V2:
|
||||||
@@ -155,8 +158,8 @@ func (c *Controller) AddOnion(cfg AddOnionConfig) (*OnionAddr, error) {
|
|||||||
portParam += fmt.Sprintf("Port=%d,%d ", cfg.VirtualPort,
|
portParam += fmt.Sprintf("Port=%d,%d ", cfg.VirtualPort,
|
||||||
targetPort)
|
targetPort)
|
||||||
} else {
|
} else {
|
||||||
portParam += fmt.Sprintf("Port=%d,%s:%d ", cfg.VirtualPort,
|
portParam += fmt.Sprintf("Port=%d,%s:%d ",
|
||||||
c.targetIPAddress, targetPort)
|
cfg.VirtualPort, c.targetIPAddress, targetPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -114,9 +114,9 @@ func NewController(controlAddr string, targetIPAddress string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start establishes and authenticates the connection between the controller and
|
// Start establishes and authenticates the connection between the controller
|
||||||
// a Tor server. Once done, the controller will be able to send commands and
|
// and a Tor server. Once done, the controller will be able to send commands
|
||||||
// expect responses.
|
// and expect responses.
|
||||||
func (c *Controller) Start() error {
|
func (c *Controller) Start() error {
|
||||||
if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
|
if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
|
||||||
return nil
|
return nil
|
||||||
|
18
tor/tor.go
18
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,
|
// around net.Conn in order to expose the actual remote address we're dialing,
|
||||||
// rather than the proxy's address.
|
// rather than the proxy's address.
|
||||||
func Dial(address, socksAddr string, streamIsolation bool,
|
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,
|
address, socksAddr, streamIsolation,
|
||||||
skipProxyForClearNetTargets, timeout,
|
skipProxyForClearNetTargets, timeout,
|
||||||
)
|
)
|
||||||
if err != nil {
|
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
|
// Now that the connection is established, we'll create our internal
|
||||||
@@ -90,7 +91,7 @@ func Dial(address, socksAddr string, streamIsolation bool,
|
|||||||
}, nil
|
}, 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.
|
// proxy. Only TCP traffic may be routed via Tor.
|
||||||
//
|
//
|
||||||
// streamIsolation determines if we should force stream isolation for this new
|
// 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
|
// skipProxyForClearNetTargets argument allows the dialer to directly connect
|
||||||
// to the provided address if it does not represent an union service, skipping
|
// to the provided address if it does not represent an union service, skipping
|
||||||
// the SOCKS proxy.
|
// the SOCKS proxy.
|
||||||
func dial(address, socksAddr string, streamIsolation bool,
|
func dialProxy(address, socksAddr string, streamIsolation bool,
|
||||||
skipProxyForClearNetTargets bool, timeout time.Duration) (net.Conn, error) {
|
skipProxyForClearNetTargets bool,
|
||||||
|
timeout time.Duration) (net.Conn, error) {
|
||||||
|
|
||||||
// If we were requested to force stream isolation for this connection,
|
// If we were requested to force stream isolation for this connection,
|
||||||
// we'll populate the authentication credentials with random data as
|
// 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.
|
// Establish the connection through Tor's SOCKS proxy.
|
||||||
dialer, err := proxy.SOCKS5("tcp", socksAddr, auth, clearDialer)
|
dialer, err := proxy.SOCKS5("tcp", socksAddr, auth, clearDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("establish sock proxy: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dialer.Dial("tcp", address)
|
return dialer.Dial("tcp", address)
|
||||||
@@ -163,7 +165,7 @@ func LookupSRV(service, proto, name, socksAddr,
|
|||||||
timeout time.Duration) (string, []*net.SRV, error) {
|
timeout time.Duration) (string, []*net.SRV, error) {
|
||||||
|
|
||||||
// Connect to the DNS server we'll be using to query SRV records.
|
// Connect to the DNS server we'll be using to query SRV records.
|
||||||
conn, err := dial(
|
conn, err := dialProxy(
|
||||||
dnsServer, socksAddr, streamIsolation,
|
dnsServer, socksAddr, streamIsolation,
|
||||||
skipProxyForClearNetTargets, timeout,
|
skipProxyForClearNetTargets, timeout,
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user