dialer

package
v3.14.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package dialer allows you to create a net.Dialer-compatible DialContext-enabled dialer with error wrapping, optional logging, optional network-events saving, and optional proxying.

Example
package main

import (
	"context"
	"net"

	"github.com/apex/log"
	"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
	"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
)

func main() {
	saver := &trace.Saver{}

	dlr := dialer.New(&dialer.Config{
		DialSaver:      saver,
		Logger:         log.Log,
		ReadWriteSaver: saver,
	}, &net.Resolver{})

	ctx := context.Background()
	conn, err := dlr.DialContext(ctx, "tcp", "8.8.8.8:53")
	if err != nil {
		log.WithError(err).Fatal("DialContext failed")
	}

	// ... use the connection ...

	conn.Close()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrProxyUnsupportedScheme = errors.New("proxy: unsupported scheme")

ErrProxyUnsupportedScheme indicates we don't support a protocol scheme.

Functions

func WithExperimentByteCounter

func WithExperimentByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context

WithExperimentByteCounter assigns the experiment byte counter to the context.

func WithSessionByteCounter

func WithSessionByteCounter(ctx context.Context, counter *bytecounter.Counter) context.Context

WithSessionByteCounter assigns the session byte counter to the context.

Types

type Config added in v3.13.0

type Config struct {
	// ContextByteCounting optionally configures context-based
	// byte counting. By default we don't do that.
	//
	// Use WithExperimentByteCounter and WithSessionByteCounter
	// to assign byte counters to a context. The code will use
	// corresponding, private functions to access the configured
	// byte counters and will notify them about I/O events.
	//
	// Bug
	//
	// This implementation cannot properly account for the bytes that are sent by
	// persistent connections, because they stick to the counters set when the
	// connection was established. This typically means we miss the bytes sent and
	// received when submitting a measurement. Such bytes are specifically not
	// seen by the experiment specific byte counter.
	//
	// For this reason, this implementation may be heavily changed/removed.
	ContextByteCounting bool

	// DialSaver is the optional saver for dialing events. If not
	// set, we will not save any dialing event.
	DialSaver *trace.Saver

	// Logger is the optional logger. If not set, there
	// will be no logging from the new dialer.
	Logger Logger

	// ProxyURL is the optional proxy URL.
	ProxyURL *url.URL

	// ReadWriteSaver is like DialSaver but for I/O events.
	ReadWriteSaver *trace.Saver
}

Config contains the settings for New.

type Dialer

type Dialer interface {
	// DialContext behaves like net.Dialer.DialContext.
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer establishes network connections.

func New added in v3.13.0

func New(config *Config, resolver Resolver) Dialer

New creates a new Dialer from the specified config and resolver.

type Logger

type Logger interface {
	// Debugf formats and emits a debug message.
	Debugf(format string, v ...interface{})

	// Debug emits a debug message.
	Debug(msg string)
}

Logger is the interface we expect from a logger.

type Resolver

type Resolver interface {
	// LookupHost behaves like net.Resolver.LookupHost.
	LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
}

Resolver is the interface we expect from a DNS resolver.

Jump to

Keyboard shortcuts

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