net

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 18 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyKeepalive added in v0.28.1

func ApplyKeepalive(conn net.Conn, cfg net.KeepAliveConfig)

ApplyKeepalive applies cfg to conn if it is a *net.TCPConn.

func ApplyNoDelay added in v0.28.1

func ApplyNoDelay(conn net.Conn)

ApplyNoDelay disables Nagle's algorithm on conn if it is a *net.TCPConn.

tnet tunnels carry small, latency-sensitive datagrams (e.g. WireGuard UDP over relay+ws/wss). Without this, Nagle coalesces each frame until a peer ACK returns, adding up to one RTT per datagram — the root cause of the latency regression reported in dolfly/tnet#858. Match wstunnel, which sets TCP_NODELAY on its tunnel socket by default.

func CopyBuffer

func CopyBuffer(dst io.Writer, src io.Reader, bufSize int) error

CopyBuffer copies from src to dst using a buffer of bufSize obtained from the buffer pool, reducing allocation overhead for repeated copies.

func IsIPv4

func IsIPv4(address string) bool

IsIPv4 reports whether address is an IPv4 address by checking whether it starts with ':' or '[' (IPv6) vs a digit (IPv4).

func NewReadWriteConn added in v0.0.3

func NewReadWriteConn(r io.Reader, w io.Writer, c net.Conn) net.Conn

NewReadWriteConn returns a net.Conn that reads from r and writes to w, delegating all other operations to c. If c supports CloseRead or CloseWrite, those are forwarded; otherwise they return ErrUnsupported.

func ParseInterfaceAddr added in v0.0.3

func ParseInterfaceAddr(ifceName, network string) (ifce string, addr []net.Addr, isIP bool, err error)

ParseInterfaceAddr resolves ifceName to a network interface name and its addresses. If ifceName is an IP address, it finds the corresponding interface and returns a single address with port 0. If ifceName is an interface name, it returns all addresses assigned to that interface. The network parameter determines the address type (TCPAddr for "tcp"/"tcp4"/"tcp6", UDPAddr for "udp"/"udp4"/"udp6", IPAddr otherwise).

isIP reports whether ifceName was parsed as an IP address (true) rather than an interface name (false). When isIP is true, callers should skip SO_BINDTODEVICE — the user's intent is source IP binding for policy routing, not device binding.

func Pipe added in v0.0.3

func Pipe(ctx context.Context, rw1, rw2 io.ReadWriteCloser, opts ...PipeOption) error

Pipe establishes a bidirectional data channel between two connections. Data read from rw1 is written to rw2, and data read from rw2 is written to rw1. Each direction runs in its own goroutine. Pipe returns the first non-EOF error encountered, or nil if both directions complete cleanly. When ctx is cancelled, both connections are forcefully closed.

func Resolve added in v0.0.3

func Resolve(ctx context.Context, network, addr string, r resolver.Resolver, hosts hosts.HostMapper, log logger.Logger) (string, error)

Resolve resolves addr to a single concrete IP address. It is a thin wrapper around ResolveAll that keeps the first candidate, see ResolveAll for the lookup order.

func ResolveAll added in v0.28.1

func ResolveAll(ctx context.Context, network, addr string, r resolver.Resolver, hosts hosts.HostMapper, log logger.Logger) ([]string, error)

ResolveAll resolves addr to all candidate IP:port addresses, preserving the order returned by the hosts mapper and the resolver. If hosts is non-nil, it is consulted first. If r is non-nil and no host mapping matched, the resolver is used. If neither is available, the address is returned unchanged so that the original hostname is preserved for downstream connectors (e.g., SOCKS5 ATYP=domain). DNS resolution is deferred to the final TCP dialer, which uses a pure-Go resolver to avoid cgo thread exhaustion.

func Transport

func Transport(rw1, rw2 io.ReadWriter) error

Transport copies data bidirectionally between rw1 and rw2 using two goroutines. It waits for both directions to finish and returns the first non-EOF error encountered.

It must not return as soon as the first direction is done: the direction that finishes first is often the one without an error, and returning early would swallow the error of the other direction.

func WrapKeepaliveListener added in v0.28.1

func WrapKeepaliveListener(ln net.Listener, cfg net.KeepAliveConfig) net.Listener

WrapKeepaliveListener wraps ln so that TCP keepalive config is applied to every accepted *net.TCPConn before it enters the middleware wrapper chain.

func WrapNoDelayListener added in v0.28.1

func WrapNoDelayListener(ln net.Listener) net.Listener

WrapNoDelayListener wraps ln so that TCP_NODELAY is applied to every accepted *net.TCPConn before it enters the middleware wrapper chain.

Types

type AddrPortRange

type AddrPortRange string

AddrPortRange is the network address with port range supported. e.g. 192.168.1.1:0-65535

func (AddrPortRange) Addrs

func (p AddrPortRange) Addrs() (addrs []string)

Addrs expands the port range into individual "host:port" strings.

type IPRange added in v0.0.3

type IPRange struct {
	Min netip.Addr
	Max netip.Addr
}

IPRange represents a range of IP addresses.

func (*IPRange) Contains added in v0.0.3

func (r *IPRange) Contains(addr netip.Addr) bool

Contains reports whether addr falls within the IP range.

func (*IPRange) Parse added in v0.0.3

func (r *IPRange) Parse(s string) error

Parse parses s as a single IP or IP range "min-max" into r.

type ListenConfig added in v0.0.3

type ListenConfig struct {
	Netns string
	net.ListenConfig
}

ListenConfig extends net.ListenConfig with network namespace support.

func (*ListenConfig) Listen added in v0.0.3

func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (net.Listener, error)

Listen announces on the local network address, switching into the configured network namespace first if one is set.

func (*ListenConfig) ListenPacket added in v0.0.3

func (lc *ListenConfig) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error)

ListenPacket announces on the local network address for packet connections, switching into the configured network namespace first if one is set.

type PipeOption added in v0.28.1

type PipeOption func(*pipeOptions)

PipeOption configures the behaviour of Pipe.

func WithReadTimeout added in v0.28.1

func WithReadTimeout(d time.Duration) PipeOption

WithReadTimeout sets an idle read timeout on each pipe half. When a read blocks for longer than d the connection is closed. A value of 0 disables the timeout entirely, relying on TCP keepalives or context cancellation to detect dead connections.

type PortRange

type PortRange struct {
	Min int
	Max int
}

Port range is a range of port list.

func (*PortRange) Contains

func (pr *PortRange) Contains(port int) bool

Contains reports whether port falls within the range.

func (*PortRange) Parse

func (pr *PortRange) Parse(s string) error

Parse parses the s to PortRange. The s can be a single port number and will be converted to port range port-port.

type RemoteAddr

type RemoteAddr interface {
	RemoteAddr() net.Addr
}

RemoteAddr is a connection that exposes its remote address.

type SetBuffer

type SetBuffer interface {
	SetReadBuffer(bytes int) error
	SetWriteBuffer(bytes int) error
}

SetBuffer is a connection that supports setting send and receive buffer sizes.

type SetDSCP

type SetDSCP interface {
	SetDSCP(int) error
}

SetDSCP is a connection that supports setting the DSCP field.

type SyscallConn

type SyscallConn interface {
	SyscallConn() (syscall.RawConn, error)
}

SyscallConn is a connection that exposes its raw system file descriptor.

Directories

Path Synopsis
Package httptunnel provides the pieces shared by the HTTP tunnel dialers and connectors: a replayable tunnel body and the redirect policy a tunnel client must use.
Package httptunnel provides the pieces shared by the HTTP tunnel dialers and connectors: a replayable tunnel body and the redirect policy a tunnel client must use.

Jump to

Keyboard shortcuts

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