netreport

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package netreport probes the local network environment to build a Report describing relay latencies and (where available) the public reflexive address of the host.

It is a port of iroh's net_report module (iroh/src/net_report). A Client runs three kinds of probes against the configured relays:

  • HTTPS probes fetch the relay's "/ping" path and time the round trip.
  • QAD probes open a QUIC Address Discovery connection (ALPN "/iroh-qad/0", QUIC port 7842) and read the connection RTT.
  • A captive-portal check fetches "/generate_204" with an X-Iroh-Challenge header and verifies the X-Iroh-Response echo, but only on full reports.

From those probes the client picks a preferred relay, applying hysteresis so a responsive relay is not abandoned unless a new one is meaningfully faster.

Reflexive address discovery

A QAD connection also learns the host's public address from the relay via the QUIC Address Discovery observed-address extension (draft-seemann-quic-address-discovery), implemented in the forked quic-go github.com/tmc/go-iroh/internal/qng (slice X3). The QAD client advertises the receive-only address-discovery role, and a QAD relay reports the client's reflexive address with OBSERVED_ADDRESS frames; the latest report is surfaced as Report.GlobalV4/Report.GlobalV6 (and the matching UDP fields).

Reports arrive after the handshake, so [qadConn.observedAddr] waits briefly for the first one; an immediate read would always miss. Without a report (not negotiated, or timed out) the probe stays latency-only.

Which socket a probe rides decides what the observed address means: a private per-probe socket learns a mapping that dies with it. Client.WithQADDialer routes probes through the caller's transport instead, so the observed address is the caller's real public mapping.

Example

Example shows running a net_report against an empty relay map, which performs no probes and yields an empty report. With real relays configured, the report would carry per-relay latencies and a preferred relay.

package main

import (
	"context"
	"fmt"

	"github.com/tmc/go-iroh/internal/netreport"
	"github.com/tmc/go-iroh/relay"
)

func main() {
	client := netreport.NewClient(relay.NewMap())

	report, err := client.GetReport(context.Background(), netreport.IfStateDetails{HaveV4: true}, true)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println("has udp:", report.HasUDP())
	fmt.Println("preferred relay set:", !report.PreferredRelay.IsZero())
}
Output:
has udp: false
preferred relay set: false

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrExtensionNotNegotiated = errors.New("netreport: quic observed-address extension not negotiated")

ErrExtensionNotNegotiated is returned by [qadConn.observedAddr] when no reflexive address is available: either QUIC Address Discovery was not negotiated on the connection, or no OBSERVED_ADDRESS report arrived within [qadObservedAddrWait]. The forked quic-go implements the extension (slice X3); see the package doc.

Functions

This section is empty.

Types

type Client

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

Client runs net_report probes against a set of relays and tracks report history so it can apply preferred-relay hysteresis across runs. It is a port of net_report::Client (iroh/src/net_report.rs:90).

A Client is safe for concurrent use; GetReport serializes internally.

The zero value is not usable; construct a Client with NewClient.

func NewClient

func NewClient(relayMap *relay.Map) *Client

NewClient returns a Client that probes the relays in relayMap.

func (*Client) GetReport

func (c *Client) GetReport(ctx context.Context, ifState IfStateDetails, doFull bool) (*Report, error)

GetReport runs a single net_report. doFull forces a full report (captive portal check and reset probe history); otherwise the report is full only if no report has been generated within [fullReportInterval]. ifState informs the sufficiency check.

The whole call is bounded by [overallReportTimeout]; the probes within it are bounded by [probesTimeout].

If the relay map is empty the report is empty: no probes run and PreferredRelay is the zero value.

func (*Client) WithDNSResolver

func (c *Client) WithDNSResolver(r *net.Resolver) *Client

WithDNSResolver sets the resolver used to look up relay hostnames.

func (*Client) WithQADDialer

func (c *Client) WithQADDialer(d QADDialer) *Client

WithQADDialer routes QAD probes through d instead of a private per-probe UDP socket, so the observed address is the mapping of the dialer's own socket. A per-probe socket's mapping dies with it and its port is nobody's dial candidate; per-probe mappings also differ between relays, which makes MappingVariesByDest misreport symmetric NAT.

func (*Client) WithQADTLSConfig

func (c *Client) WithQADTLSConfig(cfg *itls.Config) *Client

WithQADTLSConfig sets the TLS verification policy for QAD QUIC connections. It is used in tests to trust a self-signed relay certificate.

func (*Client) WithQUICConfig

func (c *Client) WithQUICConfig(cfg *quic.Config) *Client

WithQUICConfig sets the QAD QUIC transport configuration.

func (*Client) WithTLSConfig

func (c *Client) WithTLSConfig(cfg *tls.Config) *Client

WithTLSConfig sets the TLS configuration used for HTTPS probes. It is used in tests to trust self-signed relay certificates.

type IfStateDetails

type IfStateDetails struct {
	// HaveV4 reports whether the host has IPv4 connectivity.
	HaveV4 bool
	// HaveV6 reports whether the host has IPv6 connectivity.
	HaveV6 bool
}

IfStateDetails describes the host's interface capabilities, used to decide when enough probes have completed. It is a port of net_report's IfStateDetails (iroh/src/net_report/reportgen.rs:74).

type Probe

type Probe int

Probe identifies how a relay's latency was measured. The order matches the Rust enum (iroh/src/net_report/probes.rs:22) so a Probe sorts and prints consistently across the port.

const (
	// ProbeHTTPS times an HTTPS GET of the relay's probe path.
	ProbeHTTPS Probe = iota
	// ProbeQADv4 times a QUIC Address Discovery connection over IPv4.
	ProbeQADv4
	// ProbeQADv6 times a QUIC Address Discovery connection over IPv6.
	ProbeQADv6
)

func (Probe) String

func (p Probe) String() string

String returns the probe name, matching the Rust Display impl.

Example

ExampleProbe_String prints the wire-stable probe names.

package main

import (
	"fmt"

	"github.com/tmc/go-iroh/internal/netreport"
)

func main() {
	fmt.Println(netreport.ProbeHTTPS)
	fmt.Println(netreport.ProbeQADv4)
	fmt.Println(netreport.ProbeQADv6)
}
Output:
Https
QadIpv4
QadIpv6

type QADDialer

type QADDialer func(ctx context.Context, addr netip.AddrPort, tlsConf *itls.Config, cfg *quic.Config) (*quic.Conn, error)

QADDialer opens the QUIC connection for one QAD probe to addr; tlsConf already carries the QAD ALPN and server name.

type RelayLatencies

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

RelayLatencies records the lowest latency seen per relay for each probe type. It is a port of net_report::RelayLatencies (report.rs:130). The zero value is empty and ready to use.

func (*RelayLatencies) Snapshot

func (rl *RelayLatencies) Snapshot() map[netaddr.RelayURL]time.Duration

Snapshot returns the lowest latency recorded for each relay.

type Report

type Report struct {
	// Full reports whether this run used the full probe plan.
	Full bool

	// UDPv4 reports whether a QAD IPv4 round trip completed and reported an
	// observed IPv4 address.
	UDPv4 bool
	// UDPv6 reports whether a QAD IPv6 round trip completed and reported an
	// observed IPv6 address.
	UDPv6 bool

	// MappingVariesByDestV4 reports whether the observed public IPv4 address
	// differs across relays, when known.
	MappingVariesByDestV4 *bool
	// MappingVariesByDestV6 reports whether the observed public IPv6 address
	// differs across relays, when known.
	MappingVariesByDestV6 *bool

	// PreferredRelay is the relay with the best recent latency, chosen with
	// hysteresis. It is the zero RelayURL when no relay responded.
	PreferredRelay netaddr.RelayURL

	// RelayLatency holds per-relay, per-probe latencies.
	RelayLatency RelayLatencies

	// GlobalV4 is the host's public IPv4 address as seen by a relay. It is
	// absent when no QAD IPv4 observed-address report arrived.
	GlobalV4 netip.AddrPort
	// GlobalV6 is the host's public IPv6 address as seen by a relay. It is
	// absent when no QAD IPv6 observed-address report arrived.
	GlobalV6 netip.AddrPort

	// CaptivePortal reports whether a captive portal is intercepting HTTP, when
	// the check ran (full reports only).
	CaptivePortal *bool
}

Report describes the network environment as measured by a single Client.GetReport run. It is a port of iroh's net_report::Report (iroh/src/net_report/report.rs:14).

The zero Report is a valid empty report.

func (*Report) HasUDP

func (r *Report) HasUDP() bool

HasUDP reports whether any QAD round trip succeeded.

Jump to

Keyboard shortcuts

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