netapi

package
v0.3.9-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 20 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeAddressHash

func ComputeAddressHash[t comparable](tt t) uint64

ComputeAddressHash compute hash of address

func IsBlockError added in v0.3.6

func IsBlockError(err error) bool

func LogLevel added in v0.3.6

func LogLevel(err error) slog.Level

func NewDnsConn

func NewDnsConn(ctx context.Context, resolver Resolver) *dnsConn

Types

type Accepter added in v0.3.5

type Accepter interface{ Server }

type AddrInfo

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

type Address

type Address interface {
	// Hostname return hostname of address, eg: www.example.com, 127.0.0.1, ff::ff
	Hostname() string
	// Port return port of address
	Port() uint16
	// IsFqdn return true if address is FQDN
	// fqdn must impl [DomainAddress]
	// otherwise must impl [IPAddress]
	IsFqdn() bool
	// Comparable return hash of address, compute by [ComputeAddressHash]
	Comparable() uint64
	net.Addr
}
var EmptyAddr Address = DomainAddr{}

func ParseAddress

func ParseAddress(network string, addr string) (ad Address, _ error)

func ParseAddressPort

func ParseAddressPort(network string, addr string, port uint16) (ad Address)

func ParseDomainPort added in v0.3.5

func ParseDomainPort(network string, addr string, port uint16) (ad Address)

func ParseIPAddr

func ParseIPAddr(net string, ip net.IP, port uint16) Address

func ParseNetipAddr added in v0.3.7

func ParseNetipAddr(net string, ip netip.Addr, port uint16) Address

func ParseSysAddr

func ParseSysAddr(ad net.Addr) (Address, error)

type AddressNetwork added in v0.3.7

type AddressNetwork byte
const (
	Unknown AddressNetwork = iota
	TCP
	UDP
	IP
)

func ParseAddressNetwork added in v0.3.7

func ParseAddressNetwork(network string) AddressNetwork

func (AddressNetwork) Network added in v0.3.7

func (n AddressNetwork) Network() string

type ChannelHandler

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

func NewChannelHandler

func NewChannelHandler(ctx context.Context) *ChannelHandler

func (*ChannelHandler) HandlePacket

func (h *ChannelHandler) HandlePacket(p *Packet)

func (*ChannelHandler) HandleStream

func (h *ChannelHandler) HandleStream(s *StreamMeta)

func (*ChannelHandler) Packet

func (h *ChannelHandler) Packet() <-chan *Packet

func (*ChannelHandler) Stream

func (h *ChannelHandler) Stream() <-chan *StreamMeta

type ChannelStreamListener added in v0.3.6

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

func NewChannelStreamListener added in v0.3.6

func NewChannelStreamListener(addr net.Addr) *ChannelStreamListener

func (*ChannelStreamListener) Accept added in v0.3.6

func (c *ChannelStreamListener) Accept() (net.Conn, error)

func (*ChannelStreamListener) Addr added in v0.3.6

func (c *ChannelStreamListener) Addr() net.Addr

func (*ChannelStreamListener) Close added in v0.3.6

func (c *ChannelStreamListener) Close() error

func (*ChannelStreamListener) NewConn added in v0.3.6

func (c *ChannelStreamListener) NewConn(conn net.Conn)

type Context added in v0.3.6

type Context struct {
	Source      net.Addr `metrics:"Source"`
	Destination net.Addr `metrics:"Destination"`

	context.Context

	Hash     string `metrics:"Hash"`
	NodeName string `metrics:"NodeName"`

	ModeReason string `metrics:"MODE Reason"`

	Resolver ContextResolver `metrics:"-"`

	ForceMode bypass.Mode `metrics:"-"`
	SniffMode bypass.Mode `metrics:"-"`
	Mode      bypass.Mode `metrics:"MODE"`

	SystemDialer bool `metrics:"-"`
	// contains filtered or unexported fields
}

func GetContext added in v0.3.6

func GetContext(ctx context.Context) *Context

func GetContextOrNil

func GetContextOrNil(ctx context.Context) *Context

func WithContext added in v0.3.6

func WithContext(ctx context.Context) *Context

func (*Context) AddMatchHistory

func (c *Context) AddMatchHistory(s string)

func (*Context) GetComponent

func (s *Context) GetComponent() string

func (*Context) GetDomainString

func (s *Context) GetDomainString() string

func (*Context) GetFakeIP

func (c *Context) GetFakeIP() net.Addr

func (*Context) GetHTTPHost

func (c *Context) GetHTTPHost() string

func (*Context) GetHosts

func (c *Context) GetHosts() net.Addr

func (*Context) GetIPString

func (s *Context) GetIPString() string

func (*Context) GetInbound

func (c *Context) GetInbound() net.Addr

func (*Context) GetInboundName

func (c *Context) GetInboundName() string

func (*Context) GetProcess

func (c *Context) GetProcess() (string, uint, uint)

func (*Context) GetProcessName

func (c *Context) GetProcessName() string

func (*Context) GetProcessPid

func (c *Context) GetProcessPid() uint

func (*Context) GetProcessUid

func (c *Context) GetProcessUid() uint

func (*Context) GetProtocol

func (c *Context) GetProtocol() string

func (*Context) GetTLSServerName

func (c *Context) GetTLSServerName() string

func (*Context) GetTag

func (s *Context) GetTag() string

func (*Context) GetUDPMigrateID

func (s *Context) GetUDPMigrateID() uint64

func (*Context) MatchHistory

func (c *Context) MatchHistory() string

func (*Context) NewMatch

func (c *Context) NewMatch()

func (*Context) SetComponent

func (s *Context) SetComponent(str string)

func (*Context) SetDomainString

func (s *Context) SetDomainString(str string)

func (*Context) SetFakeIP

func (c *Context) SetFakeIP(addr net.Addr)

func (*Context) SetHTTPHost

func (c *Context) SetHTTPHost(str string)

func (*Context) SetHosts

func (c *Context) SetHosts(addr net.Addr)

func (*Context) SetIPString

func (s *Context) SetIPString(str string)

func (*Context) SetInbound

func (c *Context) SetInbound(addr net.Addr)

func (*Context) SetInboundName

func (c *Context) SetInboundName(name string)

func (*Context) SetProcess

func (c *Context) SetProcess(p string, pid, uid uint)

func (*Context) SetProtocol

func (c *Context) SetProtocol(p string)

func (*Context) SetTLSServerName

func (c *Context) SetTLSServerName(str string)

func (*Context) SetTag

func (s *Context) SetTag(str string)

func (*Context) SetUDPMigrateID

func (s *Context) SetUDPMigrateID(id uint64)

func (*Context) SniffHost added in v0.3.6

func (c *Context) SniffHost() string

func (*Context) Value added in v0.3.6

func (c *Context) Value(key any) any

type ContextResolver added in v0.3.6

type ContextResolver struct {
	Resolver Resolver

	Mode        ResolverMode
	SkipResolve bool `metrics:"-"`
	// contains filtered or unexported fields
}

func (ContextResolver) Opts added in v0.3.6

func (r ContextResolver) Opts(reverse bool) []func(*LookupIPOption)

func (*ContextResolver) ResolverResolver

func (r *ContextResolver) ResolverResolver() Resolver

func (*ContextResolver) SetResolverResolver

func (r *ContextResolver) SetResolverResolver(resolver Resolver)

type DNSRawRequest added in v0.3.6

type DNSRawRequest struct {
	WriteBack   func([]byte) error
	Question    *Packet
	Stream      bool
	ForceFakeIP bool
}

type DNSServer added in v0.3.5

type DNSServer interface {
	Server
	DoStream(context.Context, *DNSStreamRequest) error
	Do(context.Context, *DNSRawRequest) error
}

type DNSStreamRequest added in v0.3.8

type DNSStreamRequest struct {
	Conn        net.Conn
	ForceFakeIP bool
}

type DialError added in v0.3.7

type DialError struct {

	// Addr is the network address for which this error occurred.
	// For local operations, like Listen or SetDeadline, Addr is
	// the address of the local endpoint being manipulated.
	// For operations involving a remote network connection, like
	// Dial, Read, or Write, Addr is the remote address of that
	// connection.
	Addr net.Addr

	// Err is the error that occurred during the operation.
	// The Error method panics if the error is nil.
	Err error
	// Op is the operation which caused the error, such as
	// "read" or "write".
	Op string

	// Net is the network type on which this error occurred,
	// such as "tcp" or "udp6".
	Net string

	Sniff string
}

OpError is the error type usually returned by functions in the net package. It describes the operation, network type, and address of an error.

func NewDialError added in v0.3.7

func NewDialError(network string, err error, addr net.Addr) *DialError

func (*DialError) Error added in v0.3.7

func (e *DialError) Error() string

func (*DialError) Unwrap added in v0.3.7

func (e *DialError) Unwrap() error

type DomainAddr

type DomainAddr struct {
	HostnameX      string `json:"hostname,omitempty"`
	AddressNetwork `json:"network,omitempty"`
	PortX          uint16 `json:"port,omitempty"`
}

func (DomainAddr) Comparable

func (d DomainAddr) Comparable() uint64

func (DomainAddr) Hostname

func (d DomainAddr) Hostname() string

func (DomainAddr) IsFqdn added in v0.3.4

func (d DomainAddr) IsFqdn() bool

func (DomainAddr) Port

func (d DomainAddr) Port() uint16

func (DomainAddr) String

func (d DomainAddr) String() string

type DomainAddress added in v0.3.7

type DomainAddress interface{ Address }

type DynamicProxy

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

func NewDynamicProxy

func NewDynamicProxy(p Proxy) *DynamicProxy

func (*DynamicProxy) Close

func (d *DynamicProxy) Close() error

func (*DynamicProxy) Conn

func (d *DynamicProxy) Conn(ctx context.Context, a Address) (net.Conn, error)

func (*DynamicProxy) Dispatch

func (d *DynamicProxy) Dispatch(ctx context.Context, a Address) (Address, error)

func (*DynamicProxy) PacketConn

func (d *DynamicProxy) PacketConn(ctx context.Context, a Address) (net.PacketConn, error)

func (*DynamicProxy) Set

func (d *DynamicProxy) Set(p Proxy)

type EmptyDispatch

type EmptyDispatch struct{}

func (EmptyDispatch) Dispatch

func (EmptyDispatch) Dispatch(_ context.Context, a Address) (Address, error)

type ErrorResolver

type ErrorResolver func(domain string) error

func (ErrorResolver) Close

func (e ErrorResolver) Close() error

func (ErrorResolver) LookupIP

func (e ErrorResolver) LookupIP(_ context.Context, domain string, opts ...func(*LookupIPOption)) (*IPs, error)

func (ErrorResolver) Raw

type ForceFakeIPKey added in v0.3.8

type ForceFakeIPKey struct{}

type Handler

type Handler interface {
	HandleStream(*StreamMeta)
	HandlePacket(*Packet)
}

type IPAddr added in v0.3.5

type IPAddr struct {
	AddrPortX      netip.AddrPort `json:"addr_port"`
	AddressNetwork `json:"network,omitempty"`
}

func (IPAddr) AddrPort added in v0.3.5

func (d IPAddr) AddrPort() netip.AddrPort

func (IPAddr) Comparable

func (d IPAddr) Comparable() uint64

func (IPAddr) Hostname added in v0.3.5

func (d IPAddr) Hostname() string

func (IPAddr) IsFqdn added in v0.3.5

func (d IPAddr) IsFqdn() bool

func (IPAddr) Port added in v0.3.5

func (d IPAddr) Port() uint16

func (IPAddr) String added in v0.3.5

func (d IPAddr) String() string

type IPAddress added in v0.3.6

type IPAddress interface {
	Address
	AddrPort() netip.AddrPort
}

type IPs

type IPs struct {
	AAAA []net.IP
	A    []net.IP
}

func (*IPs) Iter

func (i *IPs) Iter() iter.Seq[net.IP]

func (*IPs) Len

func (i *IPs) Len() int

func (*IPs) PreferA

func (i *IPs) PreferA() net.IP

func (*IPs) PreferAAAA

func (i *IPs) PreferAAAA() net.IP

func (*IPs) Rand

func (i *IPs) Rand() net.IP

func (*IPs) WhoNotEmpty

func (i *IPs) WhoNotEmpty() []net.IP

type Listener added in v0.3.3

type Listener interface {
	PacketListener
	StreamListener
	Server
}

func NewListener added in v0.3.6

func NewListener(s net.Listener, p PacketListener) Listener

type LogConn

type LogConn struct {
	net.Conn
}

func (*LogConn) Read

func (l *LogConn) Read(b []byte) (int, error)

func (*LogConn) SetDeadline

func (l *LogConn) SetDeadline(t time.Time) error

func (*LogConn) SetReadDeadline

func (l *LogConn) SetReadDeadline(t time.Time) error

func (*LogConn) SetWriteDeadline

func (l *LogConn) SetWriteDeadline(t time.Time) error

func (*LogConn) Write

func (l *LogConn) Write(b []byte) (int, error)

type LookupIPOption added in v0.3.4

type LookupIPOption struct {
	Mode ResolverMode
}

type MatchHistory

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

func (*MatchHistory) Add

func (r *MatchHistory) Add(s string)

func (*MatchHistory) New

func (r *MatchHistory) New()

func (*MatchHistory) String

func (r *MatchHistory) String() string

type Packet

type Packet struct {
	Src       net.Addr
	Dst       Address
	WriteBack WriteBack
	// Payload will set to nil when ref count is negative, get it by [Packet.GetPayload]
	// ! DON'T use Payload directly
	Payload     []byte
	MigrateID   uint64
	InboundName string
	// contains filtered or unexported fields
}

func (*Packet) DecRef added in v0.3.7

func (p *Packet) DecRef()

func (*Packet) GetPayload

func (p *Packet) GetPayload() []byte

func (*Packet) IncRef added in v0.3.7

func (p *Packet) IncRef()

type PacketListener added in v0.3.6

type PacketListener interface {
	Server
	Packet(context.Context) (net.PacketConn, error)
}

type PacketProxy

type PacketProxy interface {
	PacketConn(context.Context, Address) (net.PacketConn, error)
}

type PacketSniffer added in v0.3.8

type PacketSniffer interface {
	Packet(*Context, []byte)
}

type Process added in v0.3.8

type Process struct {
	Path string
	Pid  uint
	Uid  uint
}

type ProcessDumper added in v0.3.5

type ProcessDumper interface {
	ProcessName(network string, src, dst Address) (Process, error)
}

type Proxy

type Proxy interface {
	StreamProxy
	PacketProxy
	Dispatch(context.Context, Address) (Address, error)
	io.Closer
}

func NewErrProxy

func NewErrProxy(err error) Proxy

type Resolver

type Resolver interface {
	// LookupIP returns a list of ip addresses
	LookupIP(ctx context.Context, domain string, opts ...func(*LookupIPOption)) (*IPs, error)
	// Raw returns a dns message
	//
	// ! The returned message may be cached, so it should not be modified
	// ! Please clone the message if you need to modify it
	Raw(ctx context.Context, req dnsmessage.Question) (dnsmessage.Message, error)
	io.Closer
}

Resolver is a dns resolver

TODO merge LookupIP and Raw, new interface for Resolver

type ResolverMode added in v0.3.6

type ResolverMode int
const (
	ResolverModeNoSpecified ResolverMode = iota
	ResolverModePreferIPv6
	ResolverModePreferIPv4
)

type Server

type Server interface {
	io.Closer
}

type SkipRouteKey added in v0.3.8

type SkipRouteKey struct{}

type Sniff

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

type StreamListener added in v0.3.6

type StreamListener interface {
	Server
	Stream(context.Context) (net.Listener, error)
}

type StreamMeta

type StreamMeta struct {
	Source      net.Addr
	Destination net.Addr
	Inbound     net.Addr
	InboundName string

	Src     net.Conn
	Address Address
}

type StreamProxy

type StreamProxy interface {
	Conn(context.Context, Address) (net.Conn, error)
}

type WriteBack added in v0.3.3

type WriteBack interface {
	WriteBack(b []byte, addr net.Addr) (int, error)
}

type WriteBackFunc added in v0.3.7

type WriteBackFunc func(b []byte, addr net.Addr) (int, error)

func (WriteBackFunc) WriteBack added in v0.3.7

func (f WriteBackFunc) WriteBack(b []byte, addr net.Addr) (int, error)

Jump to

Keyboard shortcuts

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