From: Sick Yoon Date: Sun, 22 Dec 2019 08:19:16 +0000 (-0500) Subject: disable listeners if proxy is configured X-Git-Tag: v1.11.0~4 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=32579e72c79c7d6897dbbf00363ef59c465a70ee;p=btrtrc.git disable listeners if proxy is configured --- diff --git a/client.go b/client.go index 866aa235..b2c8c78d 100644 --- a/client.go +++ b/client.go @@ -438,7 +438,7 @@ func (cl *Client) acceptConnections(l net.Listener) { return } if err != nil { - cl.logger.Printf("error accepting connection: %s", err) + log.Fmsg("error accepting connection: %s", err).AddValue(debugLogValue).Log(cl.logger) continue } go func() { diff --git a/socket.go b/socket.go index 6c16e588..56b3eba5 100644 --- a/socket.go +++ b/socket.go @@ -2,6 +2,7 @@ package torrent import ( "context" + "fmt" "net" "net/url" "strconv" @@ -55,11 +56,14 @@ func listenTcp(network, address, proxyURL string) (s socket, err error) { // If we don't need the proxy - then we should return default net.Dialer, // otherwise, let's try to parse the proxyURL and return proxy.Dialer if len(proxyURL) != 0 { + + dl := disabledListener{l} + // TODO: The error should be propagated, as proxy may be in use for // security or privacy reasons. Also just pass proxy.Dialer in from // the Config. if dialer, err := getProxyDialer(proxyURL); err == nil { - return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) { + return tcpSocket{dl, func(ctx context.Context, addr string) (conn net.Conn, err error) { defer perf.ScopeTimerErr(&err)() return dialer.Dial(network, addr) }}, nil @@ -72,6 +76,14 @@ func listenTcp(network, address, proxyURL string) (s socket, err error) { }}, nil } +type disabledListener struct { + net.Listener +} + +func (dl disabledListener) Accept() (net.Conn, error) { + return nil, fmt.Errorf("tcp listener disabled due to proxy") +} + type tcpSocket struct { net.Listener d func(ctx context.Context, addr string) (net.Conn, error) @@ -141,14 +153,23 @@ func listenUtp(network, addr, proxyURL string, fc firewallCallback) (s socket, e // If we don't need the proxy - then we should return default net.Dialer, // otherwise, let's try to parse the proxyURL and return proxy.Dialer if len(proxyURL) != 0 { + ds := disabledUtpSocket{us} if dialer, err := getProxyDialer(proxyURL); err == nil { - return utpSocketSocket{us, network, dialer}, nil + return utpSocketSocket{ds, network, dialer}, nil } } return utpSocketSocket{us, network, nil}, nil } +type disabledUtpSocket struct { + utpSocket +} + +func (ds disabledUtpSocket) Accept() (net.Conn, error) { + return nil, fmt.Errorf("utp listener disabled due to proxy") +} + type utpSocketSocket struct { utpSocket network string