Debian Codegraph @

tor/dialer.go

raw symbols SCIP
1 package tor
2
3 import (
4 "context"
5 "fmt"
6 "net"
7 "strings"
8
9 "golang.org/x/net/proxy"
10 )
11
12 // Dialer is a wrapper around a proxy.Dialer for dialing connections.
13 type Dialer struct {
14 proxy.Dialer
15 }
16
17 // DialConf is the configuration used for Dialer.
18 type DialConf struct {
19 // ProxyAddress is the address for the SOCKS5 proxy. If empty, it is looked
20 // up.
21 ProxyAddress string
22
23 // ProxyNetwork is the network for the SOCKS5 proxy. If ProxyAddress is
24 // empty, this value is ignored and overridden by what is looked up. If this
25 // is empty and ProxyAddress is not empty, it defaults to "tcp".
26 ProxyNetwork string
27
28 // ProxyAuth is the auth for the proxy. Since Tor's SOCKS5 proxy is
29 // unauthenticated, this is rarely needed. It can be used when
30 // IsolateSOCKSAuth is set to ensure separate circuits.
31 //
32 // This should not be confused with downstream SOCKS proxy authentication
33 // which is set via Tor values for Socks5ProxyUsername and
34 // Socks5ProxyPassword when Socks5Proxy is set.
35 ProxyAuth *proxy.Auth
36
37 // SkipEnableNetwork, if true, will skip the enable network step in Dialer.
38 SkipEnableNetwork bool
39
40 // Forward is the dialer to forward to. If nil, just uses normal net dialer.
41 Forward proxy.Dialer
42 }
43
44 // Dialer creates a new Dialer for the given configuration. Context can be nil.
45 // If conf is nil, a default is used.
46 func (t *Tor) Dialer(ctx context.Context, conf *DialConf) (*Dialer, error) {
47 if ctx == nil {
48 ctx = context.Background()
49 }
50 if conf == nil {
51 conf = &DialConf{}
52 }
53 // Enable the network if requested
54 if !conf.SkipEnableNetwork {
55 if err := t.EnableNetwork(ctx, true); err != nil {
56 return nil, err
57 }
58 }
59 // Lookup proxy address as needed
60 proxyNetwork := conf.ProxyNetwork
61 proxyAddress := conf.ProxyAddress
62 if proxyAddress == "" {
63 info, err := t.Control.GetInfo("net/listeners/socks")
64 if err != nil {
65 return nil, err
66 }
67 if len(info) != 1 || info[0].Key != "net/listeners/socks" {
68 return nil, fmt.Errorf("Unable to get socks proxy address")
69 }
70 proxyAddress = info[0].Val
71 if strings.HasPrefix(proxyAddress, "unix:") {
72 proxyAddress = proxyAddress[5:]
73 proxyNetwork = "unix"
74 } else {
75 proxyNetwork = "tcp"
76 }
77 } else if proxyNetwork == "" {
78 proxyNetwork = "tcp"
79 }
80
81 dialer, err := proxy.SOCKS5(proxyNetwork, proxyAddress, conf.ProxyAuth, conf.Forward)
82 if err != nil {
83 return nil, err
84 }
85 return &Dialer{dialer}, nil
86 }
87
88 // DialContext is the equivalent of net.DialContext.
89 //
90 // TODO: Remove when https://github.com/golang/go/issues/17759 is released.
91 func (d *Dialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
92 errCh := make(chan error, 1)
93 connCh := make(chan net.Conn, 1)
94 go func() {
95 if conn, err := d.Dial(network, addr); err != nil {
96 errCh <- err
97 } else if ctx.Err() != nil {
98 conn.Close()
99 } else {
100 connCh <- conn
101 }
102 }()
103 select {
104 case err := <-errCh:
105 return nil, err
106 case conn := <-connCh:
107 return conn, nil
108 case <-ctx.Done():
109 return nil, ctx.Err()
110 }
111 }