dialers

package
v2.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 19 Imported by: 2

Documentation

Overview

Implementation of different dialers.

You can think about dialers as implementation of net.Dialer on steroids. The goal of each dialer is to return an established net.Conn with a target netloc. If it required to use proxies, this is not a responsibility of the Transport or any other concepts, this is a responsibility of Dialer.

This might sound confusing for you but if you think that we have to support websockets and generic upgrades as well as HTTP requests, it is quite appealing to delegate these operation to dialer.

For example, if you work with http proxies, a goal of dialer is to make all these ceremonies like CONNECT tunneling, TLS upgrade whatsoever.

Another important consideration is that dialer has to do 3 different things:

1. To Dial, establish a TCP connection with a target netloc. A result is the socket which talks with a target.

2. To upgrade a socket from the step 1 to TLS. Sometimes it is trivial but in case of HTTP proxies, for example, we have to make an additional steps like CONNECT ceremony.

3. Patch HTTP request. Sometimes we want to process a request in a special way for this socket. This method prepares an HTTP request so it can be passed down to a socket.

Index

Constants

View Source
const (
	// TLSConfigCacheSize defines a size of LRU cache which is used by base
	// dialer.
	TLSConfigCacheSize = 512

	// TLSConfigTTL defines a TTL for each tls.Config we generate.
	TLSConfigTTL = 10 * time.Minute
)
View Source
const (
	// DefaultTimeout defines a timeout which should be used to
	// establish a TCP connections to target netlocs if user provides no
	// value.
	DefaultTimeout = 20 * time.Second
)

Variables

View Source
var ErrNoIPs = &errors.Error{
	Message: "no ips",
	Code:    "dns_no_ips",
}

ErrNoIPs is returned if we tried to resolve DNS name and got no IPs.

Functions

This section is empty.

Types

type Dialer

type Dialer interface {
	// Dial defines a method which main purpose is to establish a plain
	// TCP with a target netloc. It need to make all ceremonies under
	// the hood. If TLS connection is mandatory and UpgradeToTLS is not
	// optional, it also has to be done there.
	Dial(ctx context.Context, host, port string) (net.Conn, error)

	// UpgradeToTLS transforms a plain TCP connection to secured one.
	// Hostname is a hostname we connect to. Sometimes we can reuse cached
	// TLS sessions based on this parameter, for example.
	UpgradeToTLS(ctx context.Context, tcpConn net.Conn, host, port string) (net.Conn, error)

	// PatchHTTPRequest has to patch HTTP request so it can be passed
	// down to a socket. Sometimes you want to do something special with
	// this request.
	PatchHTTPRequest(*fasthttp.Request)
}

Dialer defines an interface for instances which can dial to the target.

func DialerFromURL

func DialerFromURL(opt Opts, proxyURL string) (Dialer, error)

DialerFromURL construct a dialer based on given URI. You can think about this function as a factory to make dialers.

Examples:

DialerFromURL(opt, "http://user:password@myhost:3561")
DialerFromURL(opt, "https://user:password@myhost:3561")

will make dialers which treat myhost as HTTP proxy.

DialerFromURL(opt, "socks5://user:password@myhost:441")

will make dialer which treat myhost as SOCKS5 proxy.

Please pay attention that host part HAS TO HAVE port. As with NewProxyAuth, there is no any implicit guessing.

func NewBase

func NewBase(opt Opts) Dialer

NewBase returns a base dialer which connects to a target website and does only those operations which are required:

1. Dial establishes a TCP connection to a target netloc

2. UpgradeToTLS upgrades this TCP connection to secured one.

3. PatchHTTPRequest does processing which makes sense only to adjust with fasthttp specific logic.

Apart from that, it sets timeouts, uses SO_REUSEADDR socket option, uses DNS cache and reuses tls.Config instances when possible.

func NewHTTPProxy

func NewHTTPProxy(opt Opts, proxyAuth ProxyAuth) Dialer

NewHTTPProxy returns a dialer which dials using HTTP proxies It uses. a base dialer under the hood so you get all its niceties there .

func NewSocks5

func NewSocks5(opt Opts, proxyAuth ProxyAuth) (Dialer, error)

NewSocks5 returns a dialer which can connect to SOCKS5 proxies. It uses a base dialer under the hood.

type Opts

type Opts struct {
	// Timeout defines a timeout which use to establish TCP connections to
	// target netlocs. It is also a timeout on UpgradeToTLS operations.
	Timeout time.Duration

	// TLSSkipVerify defines if we want to skip verification of
	// TLScertificates or not.
	//
	// People like to disable it so I just wanna make a yet another general
	// announcement that it can be a bad practice from securtity POV.
	//
	// You was warned.
	TLSSkipVerify bool
}

Opts define a set of common options for each dialer. This struct here is just to avoid a huge method signature and boilerplate code about default values for each and every dialer.

func (*Opts) GetTLSSkipVerify

func (o *Opts) GetTLSSkipVerify() bool

GetTLSSkipVerify return a value for skipping TLS verification or fallbacks to default one.

func (*Opts) GetTimeout

func (o *Opts) GetTimeout() time.Duration

GetTimeout returns a timeout value or fallbacks to default one.

type ProxyAuth

type ProxyAuth struct {
	// Address is address to a proxy in a form of host:port.
	Address string

	// Username is a name of user used for authentication.
	Username string

	// Password is a password of user used for authentication.
	Password string
}

ProxyAuth contains an information how to authenticate to a given proxy.

func NewProxyAuth

func NewProxyAuth(address, username, password string) (ProxyAuth, error)

NewProxyAuth return a new ProxyAuth instance according to given parameters.

func (*ProxyAuth) HasCredentials

func (p *ProxyAuth) HasCredentials() bool

HasCredentials checks if this proxy has authentication or not.

func (*ProxyAuth) Host

func (p *ProxyAuth) Host() string

Host returns a host part of Address.

func (*ProxyAuth) Port

func (p *ProxyAuth) Port() int

Port return a port part of Address.

func (*ProxyAuth) String

func (p *ProxyAuth) String() string

String conforms fmt.Stringer interface.

type StdDialerWrapper

type StdDialerWrapper struct {
	Dialer Dialer
}

StdDialerWrapper just wraps a Dialer of this package into net.Dialer interface.

func (StdDialerWrapper) Dial

func (s StdDialerWrapper) Dial(network, address string) (net.Conn, error)

Dial is to conform Dial method of net.Dialer.

func (StdDialerWrapper) DialContext

func (s StdDialerWrapper) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext is to conform DialContext method of net.Dialer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL