dialer

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const SocketBufferSize = 7 << 20

UDP socket read/write buffer size (7MB). The value of 7MB is chosen as it is the max supported by a default configuration of macOS. Some platforms will silently clamp the value to other maximums, such as linux clamping to net.core.{r,w}mem_max (see _linux.go for additional implementation that works around this limitation)

Variables

View Source
var (
	DefaultInterfaceName  = ""
	DefaultInterfaceIndex = 0
	DefaultRoutingMark    = 0 // maybe need root permission
	DefaultMarkSymbol     func(socket int32) bool
)
View Source
var DefaultHappyEyeballsv2Dialer = &HappyEyeballsv2Dialer[net.Conn]{
	DialContext: func(ctx context.Context, ip net.IP, port uint16) (net.Conn, error) {
		return DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), strconv.Itoa(int(port))), func(opts *Options) {
			if DefaultIPv6PreferUnicastLocalAddr && opts.InterfaceName != "" || opts.InterfaceIndex != 0 {
				if ip.IsGlobalUnicast() && !ip.IsPrivate() && ip.To4() == nil && ip.To16() != nil {
					opts.LocalAddr = GetUnicastAddr(true, "tcp", opts.InterfaceName, opts.InterfaceIndex)
					log.Info("happy eyeballs dialer prefer ipv6", slog.Any("localaddr", opts.LocalAddr))
				}
			}
		})
	},
	Cache: happyEyeballsCache,
	Avg:   NewAvg(),
}
View Source
var DefaultIPv6PreferUnicastLocalAddr = false
View Source
var InternetResolver netapi.Resolver = NewSystemResolver(
	netapi.ParseAddressPort("udp", "8.8.8.8", 53),
	netapi.ParseAddressPort("udp", "1.1.1.1", 53),
	netapi.ParseAddressPort("udp", "223.5.5.5", 53),
	netapi.ParseAddressPort("udp", "114.114.114.114", 53),
)
View Source
var KeepAliveConfig = net.KeepAliveConfig{
	Enable:   true,
	Idle:     time.Second * 300,
	Interval: time.Second * 20,
	Count:    9,
}

Functions

func DialContext

func DialContext(ctx context.Context, network, address string, opts ...func(*Options)) (net.Conn, error)

func DialContextWithOptions

func DialContextWithOptions(ctx context.Context, network, address string, opts *Options) (net.Conn, error)

func DialHappyEyeballs added in v0.3.5

func DialHappyEyeballs(ctx context.Context, ips []*net.TCPAddr) (net.Conn, error)

DialHappyEyeballs is a function that implements Happy Eyeballs algorithm for IPv4 and IPv6 addresses. It divides given TCP addresses into primaries and fallbacks and then calls DialParallel function.

It takes a context and a slice of TCP addresses as input and returns a net.Conn and an error.

https://www.rfc-editor.org/rfc/rfc8305

func DialHappyEyeballsv1 added in v0.3.7

func DialHappyEyeballsv1(ctx context.Context, addr netapi.Address) (net.Conn, error)

func DialParallel added in v0.3.5

func DialParallel(ctx context.Context, primaries []*net.TCPAddr, fallbacks []*net.TCPAddr) (net.Conn, error)

https://github.com/golang/go/blob/315b6ae682a2a4e7718924a45b8b311a0fe10043/src/net/dial.go#L534

dialParallel races two copies of dialSerial, giving the first a head start. It returns the first established connection and closes the others. Otherwise it returns an error from the first primary address.

func DialSerial added in v0.3.5

func DialSerial(ctx context.Context, ras []*net.TCPAddr) (net.Conn, error)

DialSerial connects to a list of addresses in sequence, returning either the first successful connection, or the first error.

func GetUnicastAddr added in v0.3.7

func GetUnicastAddr(ipv6 bool, network string, name string, index int) net.Addr

func Interleave added in v0.3.7

func Interleave[S ~[]T, T any](a, b S) S

Interleave combines two slices of the form [a, b, c] and [x, y, z] into a slice with elements interleaved; i.e. [a, x, b, y, c, z].

func LinuxMarkSymbol

func LinuxMarkSymbol(fd int32, mark int) error

func ListenContext

func ListenContext(ctx context.Context, network string, address string) (net.Listener, error)

func ListenContextWithOptions

func ListenContextWithOptions(ctx context.Context, network string, address string, opts *Options) (net.Listener, error)

func ListenPacket

func ListenPacket(network, address string, opts ...func(*Options)) (net.PacketConn, error)

func ListenPacketWithOptions

func ListenPacketWithOptions(network, address string, opts *Options) (net.PacketConn, error)

func LookupIP added in v0.3.7

func LookupIP(ctx context.Context, addr netapi.Address) ([]net.IP, error)

func MergeDnsError added in v0.3.7

func MergeDnsError(err1, err2 error) error

func PartialDeadline added in v0.3.5

func PartialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error)

PartialDeadline returns the deadline to use for a single address, when multiple addresses are pending.

func PartialDeadlineCtx added in v0.3.5

func PartialDeadlineCtx(ctx context.Context, addrsRemaining int) (context.Context, context.CancelFunc, error)

func ResolveTCPAddr added in v0.3.7

func ResolveTCPAddr(ctx context.Context, addr netapi.Address) (*net.TCPAddr, error)

func ResolveUDPAddr added in v0.3.7

func ResolveUDPAddr(ctx context.Context, addr netapi.Address) (*net.UDPAddr, error)

func ResolverAddrPort added in v0.3.7

func ResolverAddrPort(ctx context.Context, addr netapi.Address) (netip.AddrPort, error)

func ResolverIP added in v0.3.7

func ResolverIP(ctx context.Context, addr netapi.Address) (net.IP, error)

func WithListener added in v0.3.6

func WithListener() func(*Options)

func WithTryUpgradeToBatch added in v0.3.7

func WithTryUpgradeToBatch() func(*Options)

Types

type Avg added in v0.3.7

type Avg struct {
	// contains filtered or unexported fields
}

func NewAvg added in v0.3.7

func NewAvg() *Avg

func (*Avg) Avg added in v0.3.7

func (a *Avg) Avg() time.Duration

func (*Avg) Get added in v0.3.7

func (a *Avg) Get() time.Duration

func (*Avg) Push added in v0.3.7

func (a *Avg) Push(n time.Duration)

type BatchPacketConn added in v0.3.7

type BatchPacketConn struct {
	*net.UDPConn
	// contains filtered or unexported fields
}

func NewBatchPacketConn added in v0.3.7

func NewBatchPacketConn(pc *net.UDPConn) *BatchPacketConn

func (*BatchPacketConn) Close added in v0.3.7

func (bc *BatchPacketConn) Close() error

func (*BatchPacketConn) ReadFrom added in v0.3.7

func (bc *BatchPacketConn) ReadFrom(b []byte) (int, net.Addr, error)

type HappyEyeballsv2Cache added in v0.3.7

type HappyEyeballsv2Cache interface {
	Load(key unique.Handle[string]) (net.IP, bool)
	Add(key unique.Handle[string], value net.IP, opt ...lru.AddOption[unique.Handle[string], net.IP])
}

type HappyEyeballsv2Dialer added in v0.3.7

type HappyEyeballsv2Dialer[T net.Conn] struct {
	DialContext func(ctx context.Context, ip net.IP, port uint16) (T, error)
	Cache       HappyEyeballsv2Cache
	Avg         *Avg
}

func (*HappyEyeballsv2Dialer[T]) DialHappyEyeballsv2 added in v0.3.7

func (h *HappyEyeballsv2Dialer[T]) DialHappyEyeballsv2(ctx context.Context, addr netapi.Address) (t T, err error)

type Options

type Options struct {

	// RoutingMark is the mark for each packet sent through this
	// socket. Changing the mark can be used for mark-based routing
	// without netfilter or for packet filtering.
	MarkSymbol func(socket int32) bool

	// InterfaceName is the name of interface/device to bind.
	// If a socket is bound to an interface, only packets received
	// from that particular interface are processed by the socket.
	InterfaceName string

	// InterfaceIndex is the index of interface/device to bind.
	// It is almost the same as InterfaceName except it uses the
	// index of the interface instead of the name.
	InterfaceIndex int

	LocalAddr net.Addr
	// contains filtered or unexported fields
}

type SystemResolver added in v0.3.7

type SystemResolver struct {
	// contains filtered or unexported fields
}

func NewSystemResolver added in v0.3.7

func NewSystemResolver(host ...netapi.Address) *SystemResolver

func (*SystemResolver) Close added in v0.3.7

func (d *SystemResolver) Close() error

func (*SystemResolver) LookupIP added in v0.3.7

func (d *SystemResolver) LookupIP(ctx context.Context, domain string, opts ...func(*netapi.LookupIPOption)) ([]net.IP, error)

func (*SystemResolver) Raw added in v0.3.7

Jump to

Keyboard shortcuts

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