pod.go raw

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