net

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AddressFamilyIPv4 represents address as IPv4
	AddressFamilyIPv4 = AddressFamily(0)

	// AddressFamilyIPv6 represents address as IPv6
	AddressFamilyIPv6 = AddressFamily(1)

	// AddressFamilyDomain represents address as Domain
	AddressFamilyDomain = AddressFamily(2)
)
View Source
const (
	IPv4len = net.IPv4len
	IPv6len = net.IPv6len
)

Variables

View Source
var (
	// LocalHostIP is a constant value for localhost IP in IPv4.
	LocalHostIP = IPAddress([]byte{127, 0, 0, 1})

	// AnyIP is a constant value for any IP in IPv4.
	AnyIP = IPAddress([]byte{0, 0, 0, 0})

	// LocalHostDomain is a constant value for localhost domain.
	LocalHostDomain = DomainAddress("localhost")

	// LocalHostIPv6 is a constant value for localhost IP in IPv6.
	LocalHostIPv6 = IPAddress([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})

	// AnyIPv6 is a constant value for any IP in IPv6.
	AnyIPv6 = IPAddress([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})

	AliyunDns4 = IPAddress([]byte{223, 5, 5, 5})

	CfDns4 = IPAddress([]byte{1, 1, 1, 1})

	GoogleDns4 = IPAddress([]byte{8, 8, 8, 8})
)
View Source
var (
	Network_name = map[int32]string{
		0: "Unknown",
		2: "TCP",
		3: "UDP",
		4: "UNIX",
	}
	Network_value = map[string]int32{
		"Unknown": 0,
		"TCP":     2,
		"UDP":     3,
		"UNIX":    4,
	}
)

Enum value maps for Network.

View Source
var (
	PacketAddrType_name = map[int32]string{
		0: "None",
		1: "Packet",
	}
	PacketAddrType_value = map[string]int32{
		"None":   0,
		"Packet": 1,
	}
)

Enum value maps for PacketAddrType.

View Source
var (
	CIDRMask        = net.CIDRMask
	Dial            = net.Dial
	DialTCP         = net.DialTCP
	DialUDP         = net.DialUDP
	DialUnix        = net.DialUnix
	FileConn        = net.FileConn
	Listen          = net.Listen
	ListenTCP       = net.ListenTCP
	ListenUDP       = net.ListenUDP
	ListenUnix      = net.ListenUnix
	LookupIP        = net.LookupIP
	ParseCIDR       = net.ParseCIDR
	ParseIP         = net.ParseIP
	ResolveUDPAddr  = net.ResolveUDPAddr
	ResolveUnixAddr = net.ResolveUnixAddr
	SplitHostPort   = net.SplitHostPort
	ResolveTCPAddr  = net.ResolveTCPAddr
)
View Source
var AnyUdpDest = Destination{
	Address: AnyIP,
	Network: Network_UDP,
}
View Source
var File_common_net_net_proto protoreflect.FileDescriptor
View Source
var (
	PrivateAddress = netip.PrefixFrom(netip.AddrFrom4([4]byte{172, 16, 0, 0}), 12)
)

Functions

func AddressestoNetIPs

func AddressestoNetIPs(addrs []Address) ([]net.IP, error)

func Checksum

func Checksum(sum uint32, b []byte) (answer [2]byte)

checksum for Internet Protocol family headers

func FetchHTTPContent

func FetchHTTPContent(target string) ([]byte, error)

FetchHTTPContent dials http(s) for remote content

func GetInnerMostConn

func GetInnerMostConn(conn net.Conn) net.Conn

func IPToNetipAddr

func IPToNetipAddr(ip net.IP) netip.Addr

func IntToIP

func IntToIP(ip int) net.IP

func IpToInt

func IpToInt(ip net.IP) int

func IsDirectedBoradcast

func IsDirectedBoradcast(ip net.IP) bool

func IsDomainTooLong

func IsDomainTooLong(domain string) bool

func NetIPFromGvisorTcpipAddress

func NetIPFromGvisorTcpipAddress(address tcpip.Address) net.IP

func NewConnection

func NewConnection(opts ...ConnectionOption) net.Conn

func PickUDPPort

func PickUDPPort() uint16

pickPort returns an unused UDP port of the system.

func PrefixStringFromIP

func PrefixStringFromIP(ip net.IP) string

func RandomIP

func RandomIP(startIP, endIP net.IP) net.IP

func StandardIp

func StandardIp(ip net.IP) net.IP

func Sum

func Sum(b []byte) uint32

Types

type Addr

type Addr = net.Addr

type AddrError

type AddrError = net.AddrError

type Address

type Address interface {
	IP() net.IP     // IP of this Address
	Domain() string // Domain of this Address
	Family() AddressFamily
	// IPv6 address is wrapped in "[]"
	String() string // String representation of this Address
}

Address represents a network address to be communicated with. It may be an IP address or domain address, not both. This interface doesn't resolve IP address for a given domain.

func AddressFromNetIpAddr

func AddressFromNetIpAddr(ip netip.Addr) Address

func DomainAddress

func DomainAddress(domain string) Address

DomainAddress creates an Address with given domain.

func IPAddress

func IPAddress(ip []byte) Address

IPAddress creates an Address with given IP.

func ParseAddress

func ParseAddress(addr string) Address

ParseAddress parses a string into an Address. The return value will be an IPAddress when the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.

func ParseIPFromString

func ParseIPFromString(addr string) Address

type AddressFamily

type AddressFamily byte

AddressFamily is the type of address.

func (AddressFamily) IsDomain

func (af AddressFamily) IsDomain() bool

IsDomain returns true if current AddressFamily is Domain.

func (AddressFamily) IsIP

func (af AddressFamily) IsIP() bool

IsIP returns true if current AddressFamily is IPv6 or IPv4.

func (AddressFamily) IsIPv4

func (af AddressFamily) IsIPv4() bool

IsIPv4 returns true if current AddressFamily is IPv4.

func (AddressFamily) IsIPv6

func (af AddressFamily) IsIPv6() bool

IsIPv6 returns true if current AddressFamily is IPv6.

type AddressPort

type AddressPort struct {
	Address Address
	Port    Port
}

func (AddressPort) String

func (ap AddressPort) String() string

type BytesConn

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

read will read from bytes first

func NewBytesConn

func NewBytesConn(conn net.Conn, b []byte) *BytesConn

func (*BytesConn) Read

func (c *BytesConn) Read(b []byte) (int, error)

type Conn

type Conn = net.Conn

type ConnWithClose

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

func NewConnWithClose

func NewConnWithClose(conn net.Conn, cb func()) *ConnWithClose

func (*ConnWithClose) Close

func (c *ConnWithClose) Close() error

type ConnectionOption

type ConnectionOption func(*connection)

func ConnectionInput

func ConnectionInput(writer io.Writer) ConnectionOption

func ConnectionInputMulti

func ConnectionInputMulti(writer buf.Writer) ConnectionOption

func ConnectionLocalAddr

func ConnectionLocalAddr(a net.Addr) ConnectionOption

func ConnectionOnClose

func ConnectionOnClose(n io.Closer) ConnectionOption

func ConnectionOutput

func ConnectionOutput(reader io.Reader) ConnectionOption

func ConnectionOutputMulti

func ConnectionOutputMulti(reader buf.Reader) ConnectionOption

func ConnectionOutputMultiUDP

func ConnectionOutputMultiUDP(reader buf.Reader) ConnectionOption

func ConnectionRemoteAddr

func ConnectionRemoteAddr(a net.Addr) ConnectionOption

type Destination

type Destination struct {
	Address Address
	Port    Port
	Network Network
}

Destination represents a network destination including address and protocol (tcp / udp).

func DestinationFromAddr

func DestinationFromAddr(addr net.Addr) Destination

DestinationFromAddr generates a Destination from a net address.

func ParseDestination

func ParseDestination(dest string) (Destination, error)

ParseDestination converts a destination from its string presentation.

func TCPDestination

func TCPDestination(address Address, port Port) Destination

TCPDestination creates a TCP destination with given address

func UDPDestination

func UDPDestination(address Address, port Port) Destination

UDPDestination creates a UDP destination with given address

func UnixDestination

func UnixDestination(address Address) Destination

UnixDestination creates a Unix destination with given address

func (*Destination) Addr

func (d *Destination) Addr() net.Addr

func (Destination) IsValid

func (d Destination) IsValid() bool

IsValid returns true if this Destination is valid, i.e. it is not unknown

func (Destination) NetAddr

func (d Destination) NetAddr() string

NetAddr returns the network address in this Destination in string form.

func (Destination) String

func (d Destination) String() string

String returns the strings form of this Destination.

func (Destination) ToAddressPort

func (t Destination) ToAddressPort() AddressPort

func (Destination) ToUdpNetwork

func (t Destination) ToUdpNetwork() string

type Dialer

type Dialer = net.Dialer

type Endpoint

type Endpoint struct {
	Network Network `protobuf:"varint,1,opt,name=network,proto3,enum=x.common.net.Network" json:"network,omitempty"`
	Address string  `protobuf:"bytes,2,opt,name=address,proto3" json:"address,omitempty"`
	Port    uint32  `protobuf:"varint,3,opt,name=port,proto3" json:"port,omitempty"`
	// contains filtered or unexported fields
}

func (*Endpoint) Descriptor deprecated

func (*Endpoint) Descriptor() ([]byte, []int)

Deprecated: Use Endpoint.ProtoReflect.Descriptor instead.

func (*Endpoint) GetAddress

func (x *Endpoint) GetAddress() string

func (*Endpoint) GetNetwork

func (x *Endpoint) GetNetwork() Network

func (*Endpoint) GetPort

func (x *Endpoint) GetPort() uint32

func (*Endpoint) ProtoMessage

func (*Endpoint) ProtoMessage()

func (*Endpoint) ProtoReflect

func (x *Endpoint) ProtoReflect() protoreflect.Message

func (*Endpoint) Reset

func (x *Endpoint) Reset()

func (*Endpoint) String

func (x *Endpoint) String() string

type Error

type Error = net.Error

type HasNetConn

type HasNetConn interface {
	NetConn() net.Conn
}

type IP

type IP = net.IP

type IPMask

type IPMask = net.IPMask

type IPNet

type IPNet = net.IPNet

type IPOrDomain

type IPOrDomain struct {

	// Types that are valid to be assigned to Address:
	//
	//	*IPOrDomain_Ip
	//	*IPOrDomain_Domain
	Address isIPOrDomain_Address `protobuf_oneof:"address"`
	// contains filtered or unexported fields
}

Address of a network host. It may be either an IP address or a domain address.

func NewIPOrDomain

func NewIPOrDomain(addr Address) *IPOrDomain

// NewIPOrDomain translates Address to IPOrDomain

func (*IPOrDomain) AsAddress

func (d *IPOrDomain) AsAddress() Address

// AsAddress translates IPOrDomain to Address.

func (*IPOrDomain) Descriptor deprecated

func (*IPOrDomain) Descriptor() ([]byte, []int)

Deprecated: Use IPOrDomain.ProtoReflect.Descriptor instead.

func (*IPOrDomain) GetAddress

func (x *IPOrDomain) GetAddress() isIPOrDomain_Address

func (*IPOrDomain) GetDomain

func (x *IPOrDomain) GetDomain() string

func (*IPOrDomain) GetIp

func (x *IPOrDomain) GetIp() []byte

func (*IPOrDomain) ProtoMessage

func (*IPOrDomain) ProtoMessage()

func (*IPOrDomain) ProtoReflect

func (x *IPOrDomain) ProtoReflect() protoreflect.Message

func (*IPOrDomain) Reset

func (x *IPOrDomain) Reset()

func (*IPOrDomain) String

func (x *IPOrDomain) String() string

type IPOrDomain_Domain

type IPOrDomain_Domain struct {
	// Domain address.
	Domain string `protobuf:"bytes,2,opt,name=domain,proto3,oneof"`
}

type IPOrDomain_Ip

type IPOrDomain_Ip struct {
	// IP address. Must by either 4 or 16 bytes.
	Ip []byte `protobuf:"bytes,1,opt,name=ip,proto3,oneof"`
}

type IPPort

type IPPort struct {
	Ip   string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
	Port uint32 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"`
	// contains filtered or unexported fields
}

func (*IPPort) Descriptor deprecated

func (*IPPort) Descriptor() ([]byte, []int)

Deprecated: Use IPPort.ProtoReflect.Descriptor instead.

func (*IPPort) GetIp

func (x *IPPort) GetIp() string

func (*IPPort) GetPort

func (x *IPPort) GetPort() uint32

func (*IPPort) ProtoMessage

func (*IPPort) ProtoMessage()

func (*IPPort) ProtoReflect

func (x *IPPort) ProtoReflect() protoreflect.Message

func (*IPPort) Reset

func (x *IPPort) Reset()

func (*IPPort) String

func (x *IPPort) String() string

type ListenConfig

type ListenConfig = net.ListenConfig

type Listener

type Listener = net.Listener

type MbConn

type MbConn struct {
	net.Conn
	Mb buf.MultiBuffer
}

read will read from Mb first

func NewMbConn

func NewMbConn(conn net.Conn, cache buf.MultiBuffer) *MbConn

func (*MbConn) CloseWrite

func (c *MbConn) CloseWrite() error

func (*MbConn) NetConn

func (c *MbConn) NetConn() net.Conn

func (*MbConn) OkayToUnwrapReader

func (c *MbConn) OkayToUnwrapReader() int

func (*MbConn) OkayToUnwrapWriter

func (c *MbConn) OkayToUnwrapWriter() int

func (*MbConn) Read

func (c *MbConn) Read(b []byte) (n int, err error)

func (*MbConn) UnwrapReader

func (c *MbConn) UnwrapReader() any

func (*MbConn) UnwrapWriter

func (c *MbConn) UnwrapWriter() any

type MemoryConn

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

all data read from Conn will be saved to history until stop

func NewMemoryRConn

func NewMemoryRConn(conn net.Conn) *MemoryConn

func (*MemoryConn) History

func (c *MemoryConn) History() buf.MultiBuffer

func (*MemoryConn) OkayToUnwrapReader

func (c *MemoryConn) OkayToUnwrapReader() int

func (*MemoryConn) OkayToUnwrapWriter

func (c *MemoryConn) OkayToUnwrapWriter() int

func (*MemoryConn) Read

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

func (*MemoryConn) StopMemorize

func (c *MemoryConn) StopMemorize()

func (*MemoryConn) UnwrapReader

func (c *MemoryConn) UnwrapReader() any

func (*MemoryConn) UnwrapWriter

func (c *MemoryConn) UnwrapWriter() any

type NetConnToPacketConn

type NetConnToPacketConn struct {
	net.Conn
}

func (*NetConnToPacketConn) ReadFrom

func (c *NetConnToPacketConn) ReadFrom(p []byte) (n int, addr Addr, err error)

func (*NetConnToPacketConn) SetReadBuffer added in v1.0.2

func (c *NetConnToPacketConn) SetReadBuffer(bytes int) error

suppress quic-go log

func (*NetConnToPacketConn) WriteTo

func (c *NetConnToPacketConn) WriteTo(p []byte, addr Addr) (n int, err error)

type NetDialer

type NetDialer struct {
	net.Dialer
}

func (*NetDialer) Dial

func (d *NetDialer) Dial(ctx context.Context, dst Destination) (net.Conn, error)

type NetPacketListener

type NetPacketListener struct {
	net.ListenConfig
}

func (*NetPacketListener) ListenPacket

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

type Network

type Network int32
const (
	Network_Unknown Network = 0
	Network_TCP     Network = 2
	Network_UDP     Network = 3
	Network_UNIX    Network = 4
)

func NetworkFromAddr

func NetworkFromAddr(addr net.Addr) Network

func (Network) Descriptor

func (Network) Descriptor() protoreflect.EnumDescriptor

func (Network) Enum

func (x Network) Enum() *Network

func (Network) EnumDescriptor deprecated

func (Network) EnumDescriptor() ([]byte, []int)

Deprecated: Use Network.Descriptor instead.

func (Network) Number

func (x Network) Number() protoreflect.EnumNumber

func (Network) String

func (x Network) String() string

func (Network) SystemString

func (n Network) SystemString() string

func (Network) Type

func (Network) Type() protoreflect.EnumType

func (*Network) UnmarshalJSON

func (n *Network) UnmarshalJSON(b []byte) error

type NetworkList

type NetworkList struct {
	Network []Network `protobuf:"varint,1,rep,packed,name=network,proto3,enum=x.common.net.Network" json:"network,omitempty"`
	// contains filtered or unexported fields
}

func (*NetworkList) Descriptor deprecated

func (*NetworkList) Descriptor() ([]byte, []int)

Deprecated: Use NetworkList.ProtoReflect.Descriptor instead.

func (*NetworkList) GetNetwork

func (x *NetworkList) GetNetwork() []Network

func (*NetworkList) ProtoMessage

func (*NetworkList) ProtoMessage()

func (*NetworkList) ProtoReflect

func (x *NetworkList) ProtoReflect() protoreflect.Message

func (*NetworkList) Reset

func (x *NetworkList) Reset()

func (*NetworkList) String

func (x *NetworkList) String() string

type PacketAddrType

type PacketAddrType int32
const (
	PacketAddrType_None   PacketAddrType = 0
	PacketAddrType_Packet PacketAddrType = 1
)

func (PacketAddrType) Descriptor

func (PacketAddrType) Enum

func (x PacketAddrType) Enum() *PacketAddrType

func (PacketAddrType) EnumDescriptor deprecated

func (PacketAddrType) EnumDescriptor() ([]byte, []int)

Deprecated: Use PacketAddrType.Descriptor instead.

func (PacketAddrType) Number

func (PacketAddrType) String

func (x PacketAddrType) String() string

func (PacketAddrType) Type

type PacketConn

type PacketConn = net.PacketConn

type Port

type Port uint16

Port represents a network port in TCP and UDP protocol.

func PickTCPPort

func PickTCPPort() Port

PickTCPPort returns an unused TCP port of the system.

func PortFromBytes

func PortFromBytes(port []byte) Port

PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order. @unsafe Caller must ensure that the byte array has at least 2 elements.

func PortFromInt

func PortFromInt(val uint32) (Port, error)

PortFromInt converts an integer to a Port. @error when the integer is not positive or larger then 65535

func PortFromString

func PortFromString(s string) (Port, error)

PortFromString converts a string to a Port. @error when the string is not an integer or the integral value is a not a valid Port.

func (Port) String

func (p Port) String() string

String returns the string presentation of a Port.

func (Port) Value

func (p Port) Value() uint16

Value return the corresponding uint16 value of a Port.

type PortList

type PortList struct {
	Range []*PortRange `protobuf:"bytes,1,rep,name=range,proto3" json:"range,omitempty"`
	// contains filtered or unexported fields
}

PortList is a list of ports.

func (*PortList) Descriptor deprecated

func (*PortList) Descriptor() ([]byte, []int)

Deprecated: Use PortList.ProtoReflect.Descriptor instead.

func (*PortList) GetRange

func (x *PortList) GetRange() []*PortRange

func (*PortList) ProtoMessage

func (*PortList) ProtoMessage()

func (*PortList) ProtoReflect

func (x *PortList) ProtoReflect() protoreflect.Message

func (*PortList) Reset

func (x *PortList) Reset()

func (*PortList) String

func (x *PortList) String() string

type PortRange

type PortRange struct {

	// The port that this range starts from.
	From uint32 `protobuf:"varint,1,opt,name=from,proto3" json:"from,omitempty"`
	// The port that this range ends with (inclusive).
	To uint32 `protobuf:"varint,2,opt,name=to,proto3" json:"to,omitempty"`
	// contains filtered or unexported fields
}

PortRange represents a range of ports.

func SinglePortRange

func SinglePortRange(p Port) *PortRange

SinglePortRange returns a PortRange contains a single port.

func (*PortRange) Contains

func (p *PortRange) Contains(port Port) bool

Contains returns true if the given port is within the range of a PortRange.

func (*PortRange) Descriptor deprecated

func (*PortRange) Descriptor() ([]byte, []int)

Deprecated: Use PortRange.ProtoReflect.Descriptor instead.

func (*PortRange) FromPort

func (p *PortRange) FromPort() Port

FromPort returns the beginning port of this PortRange.

func (*PortRange) GetFrom

func (x *PortRange) GetFrom() uint32

func (*PortRange) GetTo

func (x *PortRange) GetTo() uint32

func (*PortRange) ProtoMessage

func (*PortRange) ProtoMessage()

func (*PortRange) ProtoReflect

func (x *PortRange) ProtoReflect() protoreflect.Message

func (*PortRange) Reset

func (x *PortRange) Reset()

func (*PortRange) String

func (x *PortRange) String() string

func (*PortRange) ToPort

func (p *PortRange) ToPort() Port

ToPort returns the end port of this PortRange.

type Resolver

type Resolver = net.Resolver

type StatsConn

type StatsConn struct {
	net.Conn
	ReadCounter  *atomic.Uint64
	WriteCounter *atomic.Uint64
}

func NewStatsConn

func NewStatsConn(conn net.Conn, readCounter, writeCounter *atomic.Uint64) *StatsConn

func (*StatsConn) HasNetConn

func (c *StatsConn) HasNetConn() net.Conn

func (*StatsConn) Read

func (c *StatsConn) Read(b []byte) (int, error)

func (*StatsConn) Write

func (c *StatsConn) Write(b []byte) (int, error)

type StatsListener

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

func NewStatsListener

func NewStatsListener(listener net.Listener, readCounter, writeCounter *atomic.Uint64) *StatsListener

func (*StatsListener) Accept

func (l *StatsListener) Accept() (net.Conn, error)

type StatsPacketConn

type StatsPacketConn struct {
	net.PacketConn
	ReadCounter  *atomic.Uint64
	WriteCounter *atomic.Uint64
}

func NewStatsPacketConn

func NewStatsPacketConn(pc net.PacketConn, readCounter, writeCounter *atomic.Uint64) *StatsPacketConn

func (*StatsPacketConn) ReadFrom

func (c *StatsPacketConn) ReadFrom(p []byte) (n int, addr Addr, err error)

func (*StatsPacketConn) WriteTo

func (c *StatsPacketConn) WriteTo(p []byte, addr Addr) (n int, err error)

type TCPAddr

type TCPAddr = net.TCPAddr

type TCPConn

type TCPConn = net.TCPConn

type TCPListener

type TCPListener = net.TCPListener

type UDPAddr

type UDPAddr = net.UDPAddr

type UDPConn

type UDPConn = net.UDPConn

type UnixAddr

type UnixAddr = net.UnixAddr

type UnixConn

type UnixConn = net.UnixConn

type UnixListener

type UnixListener = net.UnixListener

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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