Documentation
¶
Index ¶
- func ConnWithCallbacks(c net.Conn, cb *Callbacks) net.Conn
- func FalseFilter(_, _ string) bool
- func GetWrapped(obj any) any
- func ListenerWithCallbacks(l net.Listener, cb *Callbacks) net.Listener
- func LookupPortOffline(network, service string) (port int, err error)
- func LoopbackFilter(_, address string) bool
- func NetPacketConnWithCallbacks(c net.PacketConn, cb *Callbacks) net.PacketConn
- func TrueFilter(_, _ string) bool
- func UnfuckGoDns()
- type CallbackConn
- type CallbackListener
- type CallbackNetPacketConn
- type CallbackPacketConn
- type CallbackTCPConn
- type CallbackTCPListener
- type CallbackUDPConn
- type Callbacks
- type CustomFilter
- type Dial
- type DialTCP
- type DialUDP
- type DnsServer
- type Filter
- type HostFilterEntry
- type InterfaceNetwork
- type IpFilterEntry
- type Listen
- type ListenConfig
- type ListenConfigNetwork
- type ListenTCP
- type ListenUDP
- type LiteralInterface
- func (i *LiteralInterface) Addrs() ([]net.Addr, error)
- func (i *LiteralInterface) Flags() net.Flags
- func (i *LiteralInterface) HardwareAddr() net.HardwareAddr
- func (i *LiteralInterface) ID() string
- func (i *LiteralInterface) Index() int
- func (i *LiteralInterface) MTU() int
- func (i *LiteralInterface) MulticastAddrs() ([]net.Addr, error)
- func (i *LiteralInterface) Name() string
- type LookupAddr
- type LookupCNAME
- type LookupHost
- type LookupIP
- type LookupIPAddr
- type LookupMX
- type LookupNS
- type LookupNetIP
- type LookupPort
- type LookupSRV
- type LookupTXT
- type NativeInterface
- func (i *NativeInterface) Addrs() ([]net.Addr, error)
- func (i *NativeInterface) Flags() net.Flags
- func (i *NativeInterface) HardwareAddr() net.HardwareAddr
- func (i *NativeInterface) ID() string
- func (i *NativeInterface) Index() int
- func (i *NativeInterface) MTU() int
- func (i *NativeInterface) MulticastAddrs() ([]net.Addr, error)
- func (i *NativeInterface) Name() string
- type NetTCPListener
- type Network
- type NetworkInterface
- type PacketConn
- type PacketDial
- type PacketListen
- type Resolver
- type ResolverCfg
- type TCPConn
- type TCPListener
- type UDPConn
- type UpDown
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnWithCallbacks ¶
ConnWithCallbacks wraps a net.Conn with callbacks, using the most specific wrapper type based on the underlying connection type.
func FalseFilter ¶
FalseFilter is a Filter that always returns false.
func GetWrapped ¶
GetWrapped extracts the wrapped value from an object. If obj is nil or does not implement Wrapper, returns nil.
func ListenerWithCallbacks ¶
ListenerWithCallbacks wraps a net.Listener with callbacks, using the most specific wrapper type based on the underlying listener type.
func LookupPortOffline ¶
LookupPortOffline resolve port name to port (e.g. http to 80) using only local data.
func LoopbackFilter ¶
LoopbackFilter returns true for localhost and loopback addresses.
func NetPacketConnWithCallbacks ¶
func NetPacketConnWithCallbacks( c net.PacketConn, cb *Callbacks, ) net.PacketConn
NetPacketConnWithCallbacks wraps a net.PacketConn with callbacks, using the most specific wrapper type based on the underlying connection type.
func TrueFilter ¶
TrueFilter is a Filter that always returns true.
func UnfuckGoDns ¶
func UnfuckGoDns()
UnfuckGoDns sets GODEBUG=netedns0=0 to disable EDNS0 extension unsupported by a lot of consumer routers but still used by go resolver by default for some fucking reason. And there is no ways to disable it with Resolver methods/fields or something.
Some related links:
- https://pkg.go.dev/net#hdr-Name_Resolution
- https://github.com/golang/go/issues/67925
- https://github.com/moby/moby/issues/47923
It is safe to use UnfuckGoDns with already set GODEBUG var. UnfuckGoDns only appends netedns0=0 if netedns0 is not set. It doesn't alter other fields and do nothing if netedns0 is already set.
Types ¶
type CallbackConn ¶
CallbackConn wraps a net.Conn and invokes callbacks on events.
func (*CallbackConn) Close ¶
func (c *CallbackConn) Close() error
Close calls the BeforeClose callback, then closes the underlying connection.
func (*CallbackConn) GetWrapped ¶
func (c *CallbackConn) GetWrapped() any
GetWrapped returns the underlying wrapped connection.
type CallbackListener ¶
CallbackListener wraps a net.Listener and invokes callbacks on events.
func (*CallbackListener) Accept ¶
func (c *CallbackListener) Accept() (net.Conn, error)
Accept accepts a connection and invokes OnAccept if the callback is set.
func (*CallbackListener) Close ¶
func (c *CallbackListener) Close() error
Close calls the BeforeClose callback, then closes the underlying listener.
func (*CallbackListener) GetWrapped ¶
func (c *CallbackListener) GetWrapped() any
GetWrapped returns the underlying wrapped listener.
type CallbackNetPacketConn ¶
type CallbackNetPacketConn struct {
net.PacketConn
CB *Callbacks
}
CallbackNetPacketConn wraps a net.PacketConn and invokes callbacks on events.
func (*CallbackNetPacketConn) Close ¶
func (c *CallbackNetPacketConn) Close() error
Close calls the BeforeClose callback, then closes the underlying connection.
func (*CallbackNetPacketConn) GetWrapped ¶
func (c *CallbackNetPacketConn) GetWrapped() any
GetWrapped returns the underlying wrapped connection.
type CallbackPacketConn ¶
type CallbackPacketConn struct {
PacketConn
CB *Callbacks
}
CallbackPacketConn wraps a PacketConn and invokes callbacks on events.
func (*CallbackPacketConn) Close ¶
func (c *CallbackPacketConn) Close() error
Close calls the BeforeClose callback, then closes the underlying connection.
func (*CallbackPacketConn) GetWrapped ¶
func (c *CallbackPacketConn) GetWrapped() any
GetWrapped returns the underlying wrapped connection.
type CallbackTCPConn ¶
CallbackTCPConn wraps a net.TCPConn and invokes callbacks on events.
func (*CallbackTCPConn) Close ¶
func (c *CallbackTCPConn) Close() error
Close calls the BeforeClose callback, then closes the underlying connection.
func (*CallbackTCPConn) GetWrapped ¶
func (c *CallbackTCPConn) GetWrapped() any
GetWrapped returns the underlying wrapped connection.
type CallbackTCPListener ¶
type CallbackTCPListener struct {
TCPListener
CB *Callbacks
}
CallbackTCPListener wraps a net.TCPListener and invokes callbacks on events.
func (*CallbackTCPListener) Accept ¶
func (c *CallbackTCPListener) Accept() (net.Conn, error)
Accept accepts a connection and invokes OnAccept if the callback is set.
func (*CallbackTCPListener) AcceptTCP ¶
func (c *CallbackTCPListener) AcceptTCP() (TCPConn, error)
AcceptTCP accepts a TCP connection and invokes OnAcceptTCP if the callback is set.
func (*CallbackTCPListener) Close ¶
func (c *CallbackTCPListener) Close() error
Close calls the BeforeClose callback, then closes the underlying listener.
func (*CallbackTCPListener) GetWrapped ¶
func (c *CallbackTCPListener) GetWrapped() any
GetWrapped returns the underlying wrapped listener.
type CallbackUDPConn ¶
CallbackUDPConn wraps a net.UDPConn and invokes callbacks on events.
func (*CallbackUDPConn) Close ¶
func (c *CallbackUDPConn) Close() error
Close calls the BeforeClose callback, then closes the underlying connection.
func (*CallbackUDPConn) GetWrapped ¶
func (c *CallbackUDPConn) GetWrapped() any
GetWrapped returns the underlying wrapped connection.
type Callbacks ¶
type Callbacks struct {
// BeforeClose is called before the connection or listener is closed.
BeforeClose func()
// OnAccept is called when a listener accepts a new connection.
// The callback can return a different connection or an error to reject it.
OnAccept func(net.Conn) (net.Conn, error)
// OnAcceptTCP is called when a TCP listener accepts a new TCP connection.
// The callback can return a different TCP connection or an error to reject it.
OnAcceptTCP func(TCPConn) (TCPConn, error)
}
Callbacks holds callback functions invoked on various network events.
func (*Callbacks) RunBeforeClose ¶
func (c *Callbacks) RunBeforeClose()
type CustomFilter ¶
type CustomFilter struct {
Hosts []HostFilterEntry
IPs []IpFilterEntry
CIDRs []*net.IPNet
}
CustomFilter is a Filter that matches addresses based on a set of patterns. Patterns are similar to the NO_PROXY environment variable format.
func FilterFromString ¶
func FilterFromString(str string) CustomFilter
FilterFromString creates a CustomFilter from a comma-separated string.
Each entry can be:
- host:port - matches this host and port combination
- host - matches this host on any port
- ip - matches this exact IP address
- ip/subnet - matches any IP in this CIDR subnet
- Wildcards (*, ?) are supported in host patterns using shell glob matching
Examples:
- "localhost,127.0.0.1" - true for localhost and IPv4 loopback
- "*.example.com" - true for all subdomains of example.com
- "192.168.0.0/16" - true for entire 192.168.x.x subnet
- "internal.corp:8080" - true for specific host:port
The filter is case-insensitive and handles both bracketed IPv6 addresses (e.g., [::1]:8080) and trailing dots in hostnames.
func (CustomFilter) Filter ¶
func (f CustomFilter) Filter(network, address string) bool
Filter implements the Filter function for CustomFilter.
type Dial ¶
Dial is a function type for establishing TCP-like connections. It matches the signature of net.Dialer.DialContext.
type DnsServer ¶
type DnsServer struct {
// Net is the network type to use (e.g., "udp", "tcp").
// If empty, defaults to "udp".
Net string
// Addr is the address of the DNS server (e.g., "8.8.8.8:53" or "1.1.1.1").
// By default port is 53 so it can be omitted.
Addr string
}
DnsServer specifies a custom DNS server to use for resolution.
type Filter ¶
Filter functions are used whenever different handling should be applied to connections/requests depending on their network and address. For example, a proxy can use a filter to decide whether a connection should be passed directly (true) or proxied (false), or to block/allow, etc.
Network may be "" if unknown.
func InvertFilter ¶
InvertFilter is a Filter that returns iverted result of filter.
type HostFilterEntry ¶
type InterfaceNetwork ¶
type InterfaceNetwork interface {
// Interfaces returns the list of network interfaces known to this Network.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable. Multiple
// entries with the same Index or Name are allowed.
Interfaces() ([]NetworkInterface, error)
// InterfaceAddrs returns the addresses associated with interfaces known
// to this Network.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Duplicates are allowed.
InterfaceAddrs() ([]net.Addr, error)
// InterfacesByIndex returns all interfaces that match the provided index.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Multiple interfaces with the same index are allowed.
InterfacesByIndex(index int) ([]NetworkInterface, error)
// InterfacesByName returns all interfaces that match the provided name.
// Implementations may return an empty slice when the network is virtual,
// even though Dial/Listen/ListenPacket may still be usable.
// Multiple interfaces with the same name are allowed.
InterfacesByName(name string) ([]NetworkInterface, error)
}
Network defines an abstraction over network providers with multiple interfaces support.
type ListenConfig ¶ added in v0.8.0
type ListenConfigNetwork ¶ added in v0.8.0
type ListenConfigNetwork interface {
ListenPacketConfig(
ctx context.Context,
lc *ListenConfig,
network, address string,
) (PacketConn, error)
ListenUDPConfig(
ctx context.Context,
lc *ListenConfig,
network, laddr string,
) (UDPConn, error)
}
type ListenTCP ¶
type ListenTCP = func( ctx context.Context, network, laddr string, ) (TCPListener, error)
ListenTCP announces and returns a listener for TCP connections.
type LiteralInterface ¶
type LiteralInterface struct {
IDVal string
IndexVal int
NameVal string
MTUVal int
HardwareAddrVal net.HardwareAddr
FlagsVal net.Flags
AddrsVal []net.Addr
MulticastAddrsVal []net.Addr
}
func (*LiteralInterface) Flags ¶
func (i *LiteralInterface) Flags() net.Flags
func (*LiteralInterface) HardwareAddr ¶
func (i *LiteralInterface) HardwareAddr() net.HardwareAddr
func (*LiteralInterface) ID ¶
func (i *LiteralInterface) ID() string
func (*LiteralInterface) Index ¶
func (i *LiteralInterface) Index() int
func (*LiteralInterface) MTU ¶
func (i *LiteralInterface) MTU() int
func (*LiteralInterface) MulticastAddrs ¶
func (i *LiteralInterface) MulticastAddrs() ([]net.Addr, error)
func (*LiteralInterface) Name ¶
func (i *LiteralInterface) Name() string
type LookupAddr ¶
LookupAddr performs a reverse lookup for the given address, returning a list of names mapping to that address.
type LookupCNAME ¶
LookupCNAME returns the canonical name for the given host.
type LookupHost ¶
LookupHost looks up host just like LookupIP but unlike it returns slice of IP strings.
type LookupIPAddr ¶
LookupIPAddr looks up host just like LookupIP but unlike it returns slice of net.IPAddr.
type LookupNetIP ¶
LookupNetIP looks up host just like LookupIP but unlike it returns slice of netip.Addr.
type LookupPort ¶
LookupPort looks up the port for the given network and service.
type LookupSRV ¶
LookupSRV tries to resolve an SRV query of the given service, protocol, and domain name. The proto is "tcp" or "udp".
type LookupTXT ¶
LookupTXT returns the DNS TXT records for the given domain name. If a DNS TXT record holds multiple strings, they are concatenated as a single string.
type NativeInterface ¶
func (*NativeInterface) Flags ¶
func (i *NativeInterface) Flags() net.Flags
func (*NativeInterface) HardwareAddr ¶
func (i *NativeInterface) HardwareAddr() net.HardwareAddr
func (*NativeInterface) ID ¶
func (i *NativeInterface) ID() string
func (*NativeInterface) Index ¶
func (i *NativeInterface) Index() int
func (*NativeInterface) MTU ¶
func (i *NativeInterface) MTU() int
func (*NativeInterface) MulticastAddrs ¶
func (i *NativeInterface) MulticastAddrs() ([]net.Addr, error)
func (*NativeInterface) Name ¶
func (i *NativeInterface) Name() string
type NetTCPListener ¶
type NetTCPListener struct {
*net.TCPListener
}
func (*NetTCPListener) AcceptTCP ¶
func (l *NetTCPListener) AcceptTCP() (TCPConn, error)
type Network ¶
type Network interface {
// IsNative reports whether Network instance provides direct access to
// OS network stack or emulates it/adding mutation wrappers.
// Simple Network middlewares like logging ones should preserve IsNative status
// while complex ones (e.g. encrypting/proxyng/etc) should reports false.
IsNative() bool
Dial(
ctx context.Context,
network, address string,
) (net.Conn, error)
Listen(
ctx context.Context,
network, address string,
) (net.Listener, error)
PacketDial(ctx context.Context, network, address string) (PacketConn, error)
ListenPacket(
ctx context.Context,
network, address string,
) (PacketConn, error)
DialTCP(
ctx context.Context,
network, laddr, raddr string,
) (TCPConn, error)
ListenTCP(
ctx context.Context,
network, laddr string,
) (TCPListener, error)
DialUDP(
ctx context.Context,
network, laddr, raddr string,
) (UDPConn, error)
ListenUDP(
ctx context.Context,
network, laddr string,
) (UDPConn, error)
}
Network defines an abstraction over network providers.
type NetworkInterface ¶
type NetworkInterface interface {
ID() string
Index() int
Name() string
MTU() int
HardwareAddr() net.HardwareAddr
Flags() net.Flags
Addrs() ([]net.Addr, error)
MulticastAddrs() ([]net.Addr, error)
}
func WrapNativeInterfaces ¶
func WrapNativeInterfaces(in []net.Interface) []NetworkInterface
type PacketConn ¶
type PacketConn interface {
net.PacketConn
net.Conn
}
PacketConn is an interface for UDP-like packet connections.
func PacketConnWithCallbacks ¶
func PacketConnWithCallbacks(c PacketConn, cb *Callbacks) PacketConn
PacketConnWithCallbacks wraps a PacketConn with callbacks, using the most specific wrapper type based on the underlying connection type.
type PacketDial ¶
type PacketDial = func(ctx context.Context, network, address string) (PacketConn, error)
PacketDial is a function type for establishing UDP-like packet connections.
type PacketListen ¶
type PacketListen = func(ctx context.Context, network, address string) (PacketConn, error)
PacketListen is a function type for creating UDP-like packet listeners.
type Resolver ¶
type Resolver interface {
LookupIP(ctx context.Context, network, address string) ([]net.IP, error)
LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
LookupHost(ctx context.Context, host string) (addrs []string, err error)
LookupAddr(ctx context.Context, addr string) (names []string, err error)
LookupCNAME(ctx context.Context, host string) (cname string, err error)
LookupPort(
ctx context.Context,
network, service string,
) (port int, err error)
LookupNS(ctx context.Context, name string) ([]*net.NS, error)
LookupMX(
ctx context.Context,
name string,
) ([]*net.MX, error)
LookupSRV(
ctx context.Context,
service, proto, name string,
) (string, []*net.SRV, error)
LookupTXT(
ctx context.Context,
name string,
) ([]string, error)
}
type ResolverCfg ¶
type ResolverCfg struct {
// DontPreferGo disables the use of Go's pure Go DNS resolver.
// When false (default), PreferGo is enabled in the built resolver.
DontPreferGo bool
// StrictErrors controls whether errors from the DNS server are fatal.
// When true, errors stop resolution immediately.
StrictErrors bool
// Dial is an optional custom dial function for establishing connections.
// If nil, uses net.Dialer.DialContext.
Dial Dial
// Server is an optional custom DNS server to use for resolution.
// If nil, uses the system default DNS servers.
Server *DnsServer
}
ResolverCfg configures a DNS resolver.
func (ResolverCfg) Build ¶
func (cfg ResolverCfg) Build() net.Resolver
Build creates and returns a configured net.Resolver instance. If Server is set, all DNS queries are routed through that server. If Dial is set, it is used for establishing connections instead of the default dialer.
type TCPConn ¶
type TCPConn interface {
// Read(b []byte) (n int, err error)
// Write(b []byte) (n int, err error)
// Close() error
// LocalAddr() Addr
// RemoteAddr() Addr
// SetDeadline(t time.Time) error
// SetReadDeadline(t time.Time) error
// SetWriteDeadline(t time.Time) error
net.Conn
// ReadFrom(r io.Reader) (int64, error)
io.ReaderFrom
// WriteTo(w io.Writer) (int64, error)
io.WriterTo
SetKeepAlive(keepalive bool) error
SetKeepAliveConfig(config net.KeepAliveConfig) error
SetKeepAlivePeriod(d time.Duration) error
SetLinger(sec int) error
SetNoDelay(noDelay bool) error
CloseRead() error
CloseWrite() error
}
type TCPListener ¶
type UDPConn ¶
type UDPConn interface {
// Read(b []byte) (n int, err error)
// Write(b []byte) (n int, err error)
// Close() error
// LocalAddr() Addr
// RemoteAddr() Addr
// SetDeadline(t time.Time) error
// SetReadDeadline(t time.Time) error
// SetWriteDeadline(t time.Time) error
net.Conn
// ReadFrom(p []byte) (n int, addr Addr, err error)
// WriteTo(p []byte, addr Addr) (n int, err error)
net.PacketConn
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error)
ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
ReadMsgUDPAddrPort(
b, oob []byte,
) (n, oobn, flags int, addr netip.AddrPort, err error)
WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
WriteMsgUDPAddrPort(
b, oob []byte,
addr netip.AddrPort,
) (n, oobn int, err error)
}
func UDPConnWithCallbacks ¶
UDPConnWithCallbacks wraps a UDPConn with callbacks, using the most specific wrapper type based on the underlying connection type.
type UpDown ¶
type UpDown interface {
// Up activates or brings the resource online.
Up() error
// Down deactivates or brings the resource offline.
// All active connections/listeners/etc must be closed on down.
Down() error
IsUp() (bool, error)
}
UpDown defines an interface for managing the operational state of a resource.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package defnet provides a default network implementation that uses native networking on platforms that support it, and falls back to a loopback (in-memory) network on platforms like wasm.
|
Package defnet provides a default network implementation that uses native networking on platforms that support it, and falls back to a loopback (in-memory) network on platforms like wasm. |
|
Package errors provides canonical generators for frequently used network errors.
|
Package errors provides canonical generators for frequently used network errors. |
|
Package helpers provides utility functions for network operations.
|
Package helpers provides utility functions for network operations. |
|
Package loopback provides an in-memory loopback network implementation that simulates network operations without using actual network sockets.
|
Package loopback provides an in-memory loopback network implementation that simulates network operations without using actual network sockets. |
|
Package native provides a native implementation of the gonnect network interfaces based Go's standard net package.
|
Package native provides a native implementation of the gonnect network interfaces based Go's standard net package. |
|
Package reject provides a network implementation that rejects all operations with canonical errors.
|
Package reject provides a network implementation that rejects all operations with canonical errors. |
|
Package sockopt provides platform-specific socket option manipulation.
|
Package sockopt provides platform-specific socket option manipulation. |
|
Package subnet provides utilities for working with IP subnets, including well-known CIDR ranges, IP address arithmetic, and network manipulation.
|
Package subnet provides utilities for working with IP subnets, including well-known CIDR ranges, IP address arithmetic, and network manipulation. |
|
Package testing implements helpers for gonnect interfaces implementations testing
|
Package testing implements helpers for gonnect interfaces implementations testing |
|
Package tun provides a TUN (network tunnel) interface for handling virtual network devices.
|
Package tun provides a TUN (network tunnel) interface for handling virtual network devices. |