traffic

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 5 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 traffic counts the bytes mimicry's sockets send and receive (Config.Traffic, decision 0070). Each socket keeps its own Counter, which a Meter sums when read, so counting is one uncontended atomic add per read or write. A socket's protocol and peer come from the Tag its dial carried in its context, set once per connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProxyName

func ProxyName(p *url.URL) string

ProxyName is p without its credentials, path or query: scheme, host and port. "" for nil.

func With

func With(ctx context.Context, t *Tag) context.Context

With returns ctx carrying t for the sockets dialed under it; ctx itself when t is nil.

func Wrap

func Wrap(conn net.Conn, c *Counter) net.Conn

Wrap returns conn counted by c; conn itself when c is nil. A UDP socket stays a net.PacketConn (UDPConn, PacketConn), which a DNS client tells datagrams from a stream by.

func WrapUDP

func WrapUDP(uc *net.UDPConn, c *Counter) net.PacketConn

WrapUDP returns uc counted by c; uc itself when c is nil.

Types

type Bytes

type Bytes struct {
	Sent, Received int64
}

Bytes is what was sent and received.

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn is a TCP (or DNS) connection whose bytes c counts.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection and folds its counts in.

func (*Conn) Read

func (c *Conn) Read(p []byte) (int, error)

func (*Conn) Write

func (c *Conn) Write(p []byte) (int, error)

type Counter

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

A Counter counts one socket's bytes. Methods are safe on nil.

func (*Counter) Close

func (c *Counter) Close()

Close folds the counts into the meter's totals. A second Close does nothing.

func (*Counter) Received

func (c *Counter) Received(n int)

Received counts n bytes read.

func (*Counter) Sent

func (c *Counter) Sent(n int)

Sent counts n bytes written.

type Meter

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

A Meter sums the counts of the sockets it opened. Its methods are safe on nil, which counts nothing.

func New

func New(byHost bool) *Meter

New returns a meter; byHost keeps a total per Peer as well.

func (*Meter) Open

func (m *Meter) Open(ctx context.Context) *Counter

Open returns a counter for a socket dialed under ctx, registered with m; nil when m is nil. Close it with the socket.

func (*Meter) Snapshot

func (m *Meter) Snapshot(reset bool) Snapshot

Snapshot returns the counts so far: the closed sockets' and the live ones'. With reset, the meter starts again from zero.

type PacketConn

type PacketConn struct {
	Conn
	// contains filtered or unexported fields
}

PacketConn is a connected datagram socket other than a *net.UDPConn (a Resolver.Dial's) whose bytes c counts.

func (*PacketConn) ReadFrom

func (p *PacketConn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom reads a datagram and counts it.

func (*PacketConn) WriteTo

func (p *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo writes a datagram and counts it.

type Peer

type Peer struct {
	Host, Proxy string
}

Peer is where a socket went: the host it was dialed for and the proxy it went through, as ProxyName gives it ("" for none).

type Protocol

type Protocol uint32

Protocol is what a socket carried.

const (
	Other Protocol = iota
	HTTP1
	HTTP2
	HTTP3
	WebSocket
	DNS
)

The protocols counted apart. Other is a socket whose protocol was never learnt (a connection that ended before its TLS handshake did).

type Snapshot

type Snapshot struct {
	Protocols [numProtocols]Bytes
	// Hosts is nil unless the meter keeps them.
	Hosts map[Peer]Bytes
}

Snapshot is a meter's counts.

type Tag

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

A Tag says what the sockets a dial opens carry and where they went. A dial passes it in its context (With); the socket reads it when its counts are summed, so the protocol may be set after the dial, once the handshake has picked it. Methods are safe on nil.

func NewTag

func NewTag(p Protocol, peer Peer) *Tag

NewTag returns a tag for sockets to peer carrying p.

func TagOf

func TagOf(ctx context.Context) *Tag

TagOf is the tag ctx carries, or nil.

func (*Tag) Follow

func (t *Tag) Follow(o *Tag)

Follow makes t's sockets count as o's: an https proxy connection that carries one tunnel counts as that tunnel.

func (*Tag) SetProtocol

func (t *Tag) SetProtocol(p Protocol)

SetProtocol sets what t's sockets carry.

type UDPConn

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

UDPConn is a UDP socket whose bytes c counts. QUIC reads and writes the *net.UDPConn itself (UDP), in batches, counting through Sent and Received; its own Read, Write, ReadFrom and WriteTo count too.

func (*UDPConn) Close

func (u *UDPConn) Close() error

Close closes the socket and folds its counts in.

func (*UDPConn) Read

func (u *UDPConn) Read(p []byte) (int, error)

func (*UDPConn) ReadFrom

func (u *UDPConn) ReadFrom(p []byte) (int, net.Addr, error)

ReadFrom reads a datagram and counts it.

func (*UDPConn) Received

func (u *UDPConn) Received(n int)

Received counts n bytes read from the socket.

func (*UDPConn) Sent

func (u *UDPConn) Sent(n int)

Sent counts n bytes written on the socket.

func (*UDPConn) UDP

func (u *UDPConn) UDP() *net.UDPConn

UDP is the socket, for a reader that counts with Sent and Received.

func (*UDPConn) Write

func (u *UDPConn) Write(p []byte) (int, error)

func (*UDPConn) WriteTo

func (u *UDPConn) WriteTo(p []byte, addr net.Addr) (int, error)

WriteTo writes a datagram and counts it.

Jump to

Keyboard shortcuts

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