| 1 | package torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.15 context/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/" |
| 6 | "netscip-go gomod github.com/golang/go/src go1.15 net/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.15 strings/" |
| 8 | |
| 9 | "golang.org/x/net/proxyscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/" |
| 10 | ) |
| 11 | |
| 12 | // Dialer is a wrapper around a proxy.Dialer for dialing connections. |
| 13 | type Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Dialer# struct { |
| 14 | proxyscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/.Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Dialer#Dialer. |
| 15 | } |
| 16 | |
| 17 | // DialConf is the configuration used for Dialer. |
| 18 | type DialConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf# struct { |
| 19 | // ProxyAddress is the address for the SOCKS5 proxy. If empty, it is looked |
| 20 | // up. |
| 21 | ProxyAddressscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#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 | ProxyNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#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 | ProxyAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#ProxyAuth. *proxyscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/.Authscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/Auth# |
| 36 | |
| 37 | // SkipEnableNetwork, if true, will skip the enable network step in Dialer. |
| 38 | SkipEnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#SkipEnableNetwork. bool |
| 39 | |
| 40 | // Forward is the dialer to forward to. If nil, just uses normal net dialer. |
| 41 | Forwardscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#Forward. proxyscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/.Dialerscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/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 *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Dialer().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, conf *DialConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#) (*Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Dialer#, error) { |
| 47 | if ctx == nil { |
| 48 | ctx = contextscip-go gomod github.com/golang/go/src go1.15 context/.Backgroundscip-go gomod github.com/golang/go/src go1.15 context/Background().() |
| 49 | } |
| 50 | if conf == nil { |
| 51 | conf = &DialConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#{} |
| 52 | } |
| 53 | // Enable the network if requested |
| 54 | if !conf.SkipEnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#SkipEnableNetwork. { |
| 55 | if err := t.EnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#EnableNetwork().(ctx, true); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | } |
| 59 | // Lookup proxy address as needed |
| 60 | proxyNetwork := conf.ProxyNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#ProxyNetwork. |
| 61 | proxyAddress := conf.ProxyAddressscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#ProxyAddress. |
| 62 | if proxyAddress == "" { |
| 63 | info, err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..GetInfoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#GetInfo().("net/listeners/socks") |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | if len(info) != 1 || info[0].Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Key. != "net/listeners/socks" { |
| 68 | return nil, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to get socks proxy address") |
| 69 | } |
| 70 | proxyAddress = info[0].Valscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Val. |
| 71 | if stringsscip-go gomod github.com/golang/go/src go1.15 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.15 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 := proxyscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/.SOCKS5scip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/SOCKS5().(proxyNetwork, proxyAddress, conf.ProxyAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#ProxyAuth., conf.Forwardscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/DialConf#Forward.) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | return &Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/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 *Dialerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Dialer#) DialContextscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Dialer#DialContext().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, network string, addr string) (netscip-go gomod github.com/golang/go/src go1.15 net/.Connscip-go gomod github.com/golang/go/src go1.15 net/Conn#, error) { |
| 92 | errCh := make(chan error, 1) |
| 93 | connCh := make(chan netscip-go gomod github.com/golang/go/src go1.15 net/.Connscip-go gomod github.com/golang/go/src go1.15 net/Conn#, 1) |
| 94 | go func() { |
| 95 | if conn, err := d.Dialscip-go gomod golang.org/x/net abc453219eb5 `golang.org/x/net/proxy`/Dialer#Dial().(network, addr); err != nil { |
| 96 | errCh <- err |
| 97 | } else if ctx.Errscip-go gomod github.com/golang/go/src go1.15 context/Context#Err().() != nil { |
| 98 | conn.Closescip-go gomod github.com/golang/go/src go1.15 net/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.Donescip-go gomod github.com/golang/go/src go1.15 context/Context#Done().(): |
| 109 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.15 context/Context#Err().() |
| 110 | } |
| 111 | } |