brontide+tor:add timeout value for network connections

This commit is contained in:
yyforyongyu
2020-08-25 12:48:32 +08:00
parent 9dcb522ebc
commit fb67b58d3f
4 changed files with 68 additions and 25 deletions

View File

@@ -1,19 +1,31 @@
package tor
import (
"context"
"errors"
"net"
"time"
)
// TODO: this interface and its implementations should ideally be moved
// elsewhere as they are not Tor-specific.
const (
// DefaultConnTimeout is the maximum amount of time a dial will wait for
// a connect to complete.
DefaultConnTimeout time.Duration = time.Second * 120
)
// DialFunc is a type defines the signature of a dialer used by our Net
// interface.
type DialFunc func(net, addr string, timeout time.Duration) (net.Conn, error)
// Net is an interface housing a Dial function and several DNS functions that
// allows us to abstract the implementations of these functions over different
// networks, e.g. clearnet, Tor net, etc.
type Net interface {
// Dial connects to the address on the named network.
Dial(network, address string) (net.Conn, error)
Dial(network, address string, timeout time.Duration) (net.Conn, error)
// LookupHost performs DNS resolution on a given host and returns its
// addresses.
@@ -21,7 +33,8 @@ type Net interface {
// LookupSRV tries to resolve an SRV query of the given service,
// protocol, and domain name.
LookupSRV(service, proto, name string) (string, []*net.SRV, error)
LookupSRV(service, proto, name string,
timeout time.Duration) (string, []*net.SRV, error)
// ResolveTCPAddr resolves TCP addresses.
ResolveTCPAddr(network, address string) (*net.TCPAddr, error)
@@ -32,8 +45,10 @@ type Net interface {
type ClearNet struct{}
// Dial on the regular network uses net.Dial
func (r *ClearNet) Dial(network, address string) (net.Conn, error) {
return net.Dial(network, address)
func (r *ClearNet) Dial(
network, address string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout(network, address, timeout)
}
// LookupHost for regular network uses the net.LookupHost function
@@ -42,8 +57,14 @@ func (r *ClearNet) LookupHost(host string) ([]string, error) {
}
// LookupSRV for regular network uses net.LookupSRV function
func (r *ClearNet) LookupSRV(service, proto, name string) (string, []*net.SRV, error) {
return net.LookupSRV(service, proto, name)
func (r *ClearNet) LookupSRV(service, proto, name string,
timeout time.Duration) (string, []*net.SRV, error) {
// Create a context with a timeout value.
ctxt, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return net.DefaultResolver.LookupSRV(ctxt, service, proto, name)
}
// ResolveTCPAddr for regular network uses net.ResolveTCPAddr function
@@ -71,13 +92,15 @@ type ProxyNet struct {
// Dial uses the Tor Dial function in order to establish connections through
// Tor. Since Tor only supports TCP connections, only TCP networks are allowed.
func (p *ProxyNet) Dial(network, address string) (net.Conn, error) {
func (p *ProxyNet) Dial(network, address string,
timeout time.Duration) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return nil, errors.New("cannot dial non-tcp network via Tor")
}
return Dial(address, p.SOCKS, p.StreamIsolation)
return Dial(address, p.SOCKS, p.StreamIsolation, timeout)
}
// LookupHost uses the Tor LookupHost function in order to resolve hosts over
@@ -88,8 +111,13 @@ func (p *ProxyNet) LookupHost(host string) ([]string, error) {
// LookupSRV uses the Tor LookupSRV function in order to resolve SRV DNS queries
// over Tor.
func (p *ProxyNet) LookupSRV(service, proto, name string) (string, []*net.SRV, error) {
return LookupSRV(service, proto, name, p.SOCKS, p.DNS, p.StreamIsolation)
func (p *ProxyNet) LookupSRV(service, proto,
name string, timeout time.Duration) (string, []*net.SRV, error) {
return LookupSRV(
service, proto, name, p.SOCKS, p.DNS,
p.StreamIsolation, timeout,
)
}
// ResolveTCPAddr uses the Tor ResolveTCPAddr function in order to resolve TCP