]> Sergey Matveev's repositories - btrtrc.git/commitdiff
disable listeners if proxy is configured
authorSick Yoon <sick.yoon@gmail.com>
Sun, 22 Dec 2019 08:19:16 +0000 (03:19 -0500)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 1 Jan 2020 23:18:14 +0000 (09:18 +1000)
client.go
socket.go

index 866aa2358578243fd4ab422c3a4dc34022b0ca2e..b2c8c78dd25652df3d71ff68ec3a9f72c41f57a8 100644 (file)
--- 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() {
index 6c16e588a874c334979e05da0f09592b0da1b6b5..56b3eba5f162e6ead772707910887bce774a8a97 100644 (file)
--- 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