forked from p9c/pod-archive
/
pod.go
45 lines (40 loc) · 1.74 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package chainrpc
import (
"fmt"
"net"
"strings"
"time"
"github.com/l0k18/pod/cmd/node/state"
)
// DefaultConnectTimeout is a reasonable 30 seconds
var DefaultConnectTimeout = time.Second * 30
// Dial connects to the address on the named network using the appropriate dial function depending on the address and
// configuration options. For example .onion addresses will be dialed using the onion specific proxy if one was
// specified, but will otherwise use the normal dial function ( which could itself use a proxy or not).
var Dial = func(stateCfg *state.Config) func(addr net.Addr) (net.Conn, error) {
return func(addr net.Addr) (net.Conn, error) {
if strings.Contains(addr.String(), ".onion:") {
return stateCfg.Oniondial(addr.Network(), addr.String(),
DefaultConnectTimeout)
}
Trace("StateCfg.Dial", addr.Network(), addr.String(),
DefaultConnectTimeout)
conn, er := stateCfg.Dial(addr.Network(), addr.String(), DefaultConnectTimeout)
if er != nil {
Trace("connection error:", conn, er)
}
return conn, er
}
}
// Lookup resolves the IP of the given host using the correct DNS lookup function depending on the configuration
// options. For example, addresses will be resolved using tor when the --proxy flag was specified unless --noonion was
// also specified in which case the normal system DNS resolver will be used. Any attempt to resolve a tor address (.
// onion) will return an error since they are not intended to be resolved outside of the tor proxy.
var Lookup = func(stateCfg *state.Config) func(host string) ([]net.IP, error) {
return func(host string) ([]net.IP, error) {
if strings.HasSuffix(host, ".onion") {
return nil, fmt.Errorf("attempt to resolve tor address %s", host)
}
return stateCfg.Lookup(host)
}
}