control

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: AGPL-3.0 Imports: 60 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NsName       = "daens"
	HostVethName = "dae0"
	NsVethName   = "dae0peer"
)
View Source
const (
	DnsNatTimeout  = 17 * time.Second // RFC 5452
	AnyfromTimeout = 5 * time.Second  // Do not cache too long.
	MaxRetry       = 2
)
View Source
const (
	MaxDnsLookupDepth = 3
)
View Source
const (
	PacketSnifferTtl = 3 * time.Second
)
View Source
const SysctlPrefixPath = "/proc/sys/"
View Source
const UdpTaskQueueLength = 128

Variables

View Source
var (
	CheckBatchUpdateFeatureOnce sync.Once
	SimulateBatchUpdate         bool
	SimulateBatchUpdateLpmTrie  bool
)
View Source
var (
	UnspecifiedAddressA    = netip.MustParseAddr("0.0.0.0")
	UnspecifiedAddressAAAA = netip.MustParseAddr("::")
)
View Source
var (
	DefaultUdpTaskPool = NewUdpTaskPool()
	ReemitWorkers      *ants.Pool
)
View Source
var DefaultAnyfromPool = NewAnyfromPool()
View Source
var (
	DefaultNatTimeout = 3 * time.Minute
)
View Source
var DefaultPacketSnifferSessionMgr = NewPacketSnifferPool()
View Source
var DefaultUdpEndpointPool = NewUdpEndpointPool()
View Source
var (
	ErrUnsupportedQuestionType = fmt.Errorf("unsupported question type")
)

Functions

func BpfMapBatchDelete added in v0.2.0

func BpfMapBatchDelete(m *ebpf.Map, keys interface{}) (n int, err error)

BpfMapBatchDelete deletes keys and ignores ErrKeyNotExist.

func BpfMapBatchUpdate

func BpfMapBatchUpdate(m *ebpf.Map, keys interface{}, values interface{}, opts *ebpf.BatchOptions) (n int, err error)

func CheckIpforward

func CheckIpforward(ifname string) error

func CheckSendRedirects

func CheckSendRedirects(ifname string) error

func ChooseNatTimeout

func ChooseNatTimeout(data []byte, sniffDns bool) (dmsg *dnsmessage.Msg, timeout time.Duration)
func DeleteLink(name string) error

func DeleteNamedNetns added in v0.5.1

func DeleteNamedNetns(name string) error

func FormatDnsRsc

func FormatDnsRsc(ans []dnsmessage.RR) string

func FormatL4Proto

func FormatL4Proto(l4proto uint8) string

func GenerateObjects

func GenerateObjects(output string)

func InitDaeNetns added in v0.5.1

func InitDaeNetns(log *logrus.Logger)

func InitSysctlManager added in v0.5.1

func InitSysctlManager(log *logrus.Logger) (err error)

func Mac2String

func Mac2String(mac []uint8) string

func ParseFixedDomainTtl added in v0.2.0

func ParseFixedDomainTtl(ks []config.KeyableString) (map[string]int, error)

func ParseGroupOverrideOption added in v0.8.0

func ParseGroupOverrideOption(group config.Group, global config.Global, log *logrus.Logger) (*dialer.GlobalOption, error)

func ParsePortRange

func ParsePortRange(b []byte) (portStart, portEnd uint16)

func ProcessName2String

func ProcessName2String(pname []uint8) string

func QtypeToString added in v0.2.1

func QtypeToString(qtype uint16) string

func RefineAddrPortToShow

func RefineAddrPortToShow(addrPort netip.AddrPort) (srcToShow string)

func RefineSourceToShow

func RefineSourceToShow(src netip.AddrPort, dst netip.Addr) (srcToShow string)

func RelayTCP

func RelayTCP(lConn, rConn netproxy.Conn) (err error)

func RetrieveOriginalDest

func RetrieveOriginalDest(oob []byte) netip.AddrPort

func SetForwarding added in v0.1.4

func SetForwarding(ifname string, val string)

func SetIpv4forward added in v0.1.4

func SetIpv4forward(val string) error

func SetSendRedirects added in v0.1.4

func SetSendRedirects(ifname string, val string)

Types

type Anyfrom added in v0.4.0

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

func (*Anyfrom) ReadFrom added in v0.4.0

func (a *Anyfrom) ReadFrom(b []byte) (int, net.Addr, error)

func (*Anyfrom) ReadFromUDP added in v0.4.0

func (a *Anyfrom) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)

func (*Anyfrom) ReadFromUDPAddrPort added in v0.4.0

func (a *Anyfrom) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error)

func (*Anyfrom) ReadMsgUDP added in v0.4.0

func (a *Anyfrom) ReadMsgUDP(b []byte, oob []byte) (n int, oobn int, flags int, addr *net.UDPAddr, err error)

func (*Anyfrom) ReadMsgUDPAddrPort added in v0.4.0

func (a *Anyfrom) ReadMsgUDPAddrPort(b []byte, oob []byte) (n int, oobn int, flags int, addr netip.AddrPort, err error)

func (*Anyfrom) RefreshTtl added in v0.4.0

func (a *Anyfrom) RefreshTtl()

func (*Anyfrom) SupportGso added in v0.4.0

func (a *Anyfrom) SupportGso(size int) bool

func (*Anyfrom) SyscallConn added in v0.4.0

func (a *Anyfrom) SyscallConn() (syscall.RawConn, error)

func (*Anyfrom) WriteMsgUDP added in v0.4.0

func (a *Anyfrom) WriteMsgUDP(b []byte, oob []byte, addr *net.UDPAddr) (n int, oobn int, err error)

func (*Anyfrom) WriteMsgUDPAddrPort added in v0.4.0

func (a *Anyfrom) WriteMsgUDPAddrPort(b []byte, oob []byte, addr netip.AddrPort) (n int, oobn int, err error)

func (*Anyfrom) WriteTo added in v0.4.0

func (a *Anyfrom) WriteTo(b []byte, addr net.Addr) (n int, err error)

func (*Anyfrom) WriteToUDP added in v0.4.0

func (a *Anyfrom) WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)

func (*Anyfrom) WriteToUDPAddrPort added in v0.4.0

func (a *Anyfrom) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (n int, err error)

type AnyfromPool added in v0.4.0

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

AnyfromPool is a full-cone udp listener pool

func NewAnyfromPool added in v0.4.0

func NewAnyfromPool() *AnyfromPool

func (*AnyfromPool) GetOrCreate added in v0.4.0

func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfrom, isNew bool, err error)

type ControlPlane

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

func NewControlPlane

func NewControlPlane(
	log *logrus.Logger,
	_bpf interface{},
	dnsCache map[string]*DnsCache,
	tagToNodeList map[string][]string,
	groups []config.Group,
	routingA *config.Routing,
	global *config.Global,
	dnsConfig *config.Dns,
	externGeoDataDirs []string,
) (*ControlPlane, error)

func (*ControlPlane) AbortConnections added in v0.4.0

func (c *ControlPlane) AbortConnections() (err error)

func (*ControlPlane) ActivateCheck added in v0.5.1

func (c *ControlPlane) ActivateCheck()

func (*ControlPlane) ChooseDialTarget

func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip.AddrPort, domain string) (dialTarget string, shouldReroute bool, dialIp bool)

func (*ControlPlane) CloneDnsCache added in v0.1.2

func (c *ControlPlane) CloneDnsCache() map[string]*DnsCache

func (*ControlPlane) Close

func (c *ControlPlane) Close() (err error)

func (*ControlPlane) EjectBpf

func (c *ControlPlane) EjectBpf() *bpfObjects

EjectBpf will resect bpf from destroying life-cycle of control plane.

func (*ControlPlane) InjectBpf

func (c *ControlPlane) InjectBpf(bpf *bpfObjects)

func (*ControlPlane) ListenAndServe

func (c *ControlPlane) ListenAndServe(readyChan chan<- bool, port uint16) (listener *Listener, err error)

func (*ControlPlane) Route

func (c *ControlPlane) Route(src, dst netip.AddrPort, domain string, l4proto consts.L4ProtoType, routingResult *bpfRoutingResult) (outboundIndex consts.OutboundIndex, mark uint32, must bool, err error)

func (*ControlPlane) RouteDialTcp added in v0.2.0

func (c *ControlPlane) RouteDialTcp(p *RouteDialParam) (conn netproxy.Conn, err error)

func (*ControlPlane) Serve

func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err error)

type DaeNetns added in v0.5.1

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

func GetDaeNetns added in v0.5.1

func GetDaeNetns() *DaeNetns

func (*DaeNetns) Close added in v0.5.1

func (ns *DaeNetns) Close() (err error)

func (*DaeNetns) Dae0 added in v0.6.0

func (ns *DaeNetns) Dae0() netlink.Link

func (*DaeNetns) Dae0Peer added in v0.6.0

func (ns *DaeNetns) Dae0Peer() netlink.Link

func (*DaeNetns) NetnsID added in v0.6.0

func (ns *DaeNetns) NetnsID() (int, error)

func (*DaeNetns) Setup added in v0.5.1

func (ns *DaeNetns) Setup() (err error)

func (*DaeNetns) With added in v0.5.1

func (ns *DaeNetns) With(f func() error) (err error)

type DialOption added in v0.2.2

type DialOption struct {
	Target        string
	Dialer        *dialer.Dialer
	Outbound      *ob.DialerGroup
	Network       string
	SniffedDomain string
}

type DnsCache

type DnsCache struct {
	DomainBitmap     []uint32
	Answer           []dnsmessage.RR
	Deadline         time.Time
	OriginalDeadline time.Time // This field is not impacted by `fixed_domain_ttl`.
}

func (*DnsCache) FillInto

func (c *DnsCache) FillInto(req *dnsmessage.Msg)

func (*DnsCache) IncludeAnyIp added in v0.1.8

func (c *DnsCache) IncludeAnyIp() bool

func (*DnsCache) IncludeIp

func (c *DnsCache) IncludeIp(ip netip.Addr) bool

type DnsController

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

func NewDnsController

func NewDnsController(routing *dns.Dns, option *DnsControllerOption) (c *DnsController, err error)

func (*DnsController) Handle_

func (c *DnsController) Handle_(dnsMessage *dnsmessage.Msg, req *udpRequest) (err error)

func (*DnsController) LookupDnsRespCache

func (c *DnsController) LookupDnsRespCache(cacheKey string, ignoreFixedTtl bool) (cache *DnsCache)

func (*DnsController) LookupDnsRespCache_

func (c *DnsController) LookupDnsRespCache_(msg *dnsmessage.Msg, cacheKey string, ignoreFixedTtl bool) (resp []byte)

LookupDnsRespCache_ will modify the msg in place.

func (*DnsController) NormalizeAndCacheDnsResp_ added in v0.2.2

func (c *DnsController) NormalizeAndCacheDnsResp_(msg *dnsmessage.Msg) (err error)

NormalizeAndCacheDnsResp_ handle DNS resp in place.

func (*DnsController) RemoveDnsRespCache added in v0.1.6

func (c *DnsController) RemoveDnsRespCache(cacheKey string)

func (*DnsController) UpdateDnsCacheDeadline added in v0.2.0

func (c *DnsController) UpdateDnsCacheDeadline(host string, dnsTyp uint16, answers []dnsmessage.RR, deadline time.Time) (err error)

func (*DnsController) UpdateDnsCacheTtl added in v0.2.0

func (c *DnsController) UpdateDnsCacheTtl(host string, dnsTyp uint16, answers []dnsmessage.RR, ttl int) (err error)

type DnsControllerOption

type DnsControllerOption struct {
	Log                   *logrus.Logger
	CacheAccessCallback   func(cache *DnsCache) (err error)
	CacheRemoveCallback   func(cache *DnsCache) (err error)
	NewCache              func(fqdn string, answers []dnsmessage.RR, deadline time.Time, originalDeadline time.Time) (cache *DnsCache, err error)
	BestDialerChooser     func(req *udpRequest, upstream *dns.Upstream) (*dialArgument, error)
	TimeoutExceedCallback func(dialArgument *dialArgument, err error)
	IpVersionPrefer       int
	FixedDomainTtl        map[string]int
}

type IpVersionPrefer added in v0.1.6

type IpVersionPrefer int
const (
	IpVersionPrefer_No IpVersionPrefer = 0
	IpVersionPrefer_4  IpVersionPrefer = 4
	IpVersionPrefer_6  IpVersionPrefer = 6
)

type Listener

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

func (*Listener) Close

func (l *Listener) Close() error

type PacketSniffer added in v0.4.0

type PacketSniffer struct {
	*sniffing.Sniffer

	Mu sync.Mutex
	// contains filtered or unexported fields
}

type PacketSnifferKey added in v0.4.0

type PacketSnifferKey struct {
	LAddr netip.AddrPort
	RAddr netip.AddrPort
}

type PacketSnifferOptions added in v0.4.0

type PacketSnifferOptions struct {
	Ttl time.Duration
}

type PacketSnifferPool added in v0.4.0

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

PacketSnifferPool is a full-cone udp conn pool

func NewPacketSnifferPool added in v0.4.0

func NewPacketSnifferPool() *PacketSnifferPool

func (*PacketSnifferPool) Get added in v0.4.0

func (*PacketSnifferPool) GetOrCreate added in v0.4.0

func (p *PacketSnifferPool) GetOrCreate(key PacketSnifferKey, createOption *PacketSnifferOptions) (qs *PacketSniffer, isNew bool)

func (*PacketSnifferPool) Remove added in v0.4.0

func (p *PacketSnifferPool) Remove(key PacketSnifferKey, sniffer *PacketSniffer) (err error)

type ProgField

type ProgField struct {
	Name string
	Ebpf string
}

type RouteDialParam added in v0.2.0

type RouteDialParam struct {
	Outbound    consts.OutboundIndex
	Domain      string
	Mac         [6]uint8
	Dscp        uint8
	ProcessName [16]uint8
	Src         netip.AddrPort
	Dest        netip.AddrPort
	Mark        uint32
}

type RoutingMatcher

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

func (*RoutingMatcher) Match

func (m *RoutingMatcher) Match(
	sourceAddr []byte,
	destAddr []byte,
	sourcePort uint16,
	destPort uint16,
	ipVersion consts.IpVersionType,
	l4proto consts.L4ProtoType,
	domain string,
	processName [16]uint8,
	tos uint8,
	mac []byte,
) (outboundIndex consts.OutboundIndex, mark uint32, must bool, err error)

Match is modified from kern/tproxy.c; please keep sync.

type RoutingMatcherBuilder

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

func NewRoutingMatcherBuilder

func NewRoutingMatcherBuilder(log *logrus.Logger, rules []*config_parser.RoutingRule, outboundName2Id map[string]uint8, bpf *bpfObjects, fallback config.FunctionOrString) (b *RoutingMatcherBuilder, err error)

func (*RoutingMatcherBuilder) BuildKernspace

func (b *RoutingMatcherBuilder) BuildKernspace(log *logrus.Logger) (err error)

func (*RoutingMatcherBuilder) BuildUserspace

func (b *RoutingMatcherBuilder) BuildUserspace() (matcher *RoutingMatcher, err error)

type RscWrapper

type RscWrapper struct {
	Rsc dnsmessage.RR
}

func (RscWrapper) String

func (w RscWrapper) String() string

type SysctlKey added in v0.6.0

type SysctlKey string

func (SysctlKey) Get added in v0.6.0

func (k SysctlKey) Get() (value string, err error)

func (SysctlKey) Set added in v0.6.0

func (k SysctlKey) Set(value string, watch bool) (err error)

type SysctlManager added in v0.5.1

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

func NewSysctlManager added in v0.5.1

func NewSysctlManager(log *logrus.Logger) (*SysctlManager, error)

func (*SysctlManager) Keyf added in v0.6.0

func (s *SysctlManager) Keyf(format string, a ...any) SysctlKey

type UdpEndpoint

type UdpEndpoint struct {
	NatTimeout time.Duration

	Dialer   *dialer.Dialer
	Outbound *outbound.DialerGroup

	// Non-empty indicates this UDP Endpoint is related with a sniffed domain.
	SniffedDomain string
	DialTarget    string
	// contains filtered or unexported fields
}

func (*UdpEndpoint) Close

func (ue *UdpEndpoint) Close() error

func (*UdpEndpoint) WriteTo

func (ue *UdpEndpoint) WriteTo(b []byte, addr string) (int, error)

type UdpEndpointOptions

type UdpEndpointOptions struct {
	Handler    UdpHandler
	NatTimeout time.Duration
	// GetTarget is useful only if the underlay does not support Full-cone.
	GetDialOption func() (option *DialOption, err error)
}

type UdpEndpointPool

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

UdpEndpointPool is a full-cone udp conn pool

func NewUdpEndpointPool

func NewUdpEndpointPool() *UdpEndpointPool

func (*UdpEndpointPool) Get added in v0.7.0

func (p *UdpEndpointPool) Get(lAddr netip.AddrPort) (udpEndpoint *UdpEndpoint, ok bool)

func (*UdpEndpointPool) GetOrCreate

func (p *UdpEndpointPool) GetOrCreate(lAddr netip.AddrPort, createOption *UdpEndpointOptions) (udpEndpoint *UdpEndpoint, isNew bool, err error)

func (*UdpEndpointPool) Remove

func (p *UdpEndpointPool) Remove(lAddr netip.AddrPort, udpEndpoint *UdpEndpoint) (err error)

type UdpHandler

type UdpHandler func(data []byte, from netip.AddrPort) error

type UdpTask added in v0.7.0

type UdpTask = func()

type UdpTaskPool added in v0.7.0

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

func NewUdpTaskPool added in v0.7.0

func NewUdpTaskPool() *UdpTaskPool

func (*UdpTaskPool) EmitTask added in v0.7.0

func (p *UdpTaskPool) EmitTask(key string, task UdpTask)

EmitTask: Make sure packets with the same key (4 tuples) will be sent in order.

type UdpTaskQueue added in v0.7.0

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

UdpTaskQueue make sure packets with the same key (4 tuples) will be sent in order.

func (*UdpTaskQueue) Push added in v0.7.0

func (q *UdpTaskQueue) Push(task UdpTask)

type WriteCloser

type WriteCloser interface {
	CloseWrite() error
}

Jump to

Keyboard shortcuts

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