Documentation
¶
Index ¶
- Constants
- Variables
- func BroadcastAddr(inet4Address []netip.Prefix) netip.Addr
- func BuildUnreachable(packet []byte, source netip.Addr, headroom int) ([]byte, bool)
- func CalculateInterfaceName(name string) (tunName string)
- func GSOSplit(in []byte, options GSOOptions, outBufs [][]byte, sizes []int, outOffset int) (int, error)
- func HasNextAddress(prefix netip.Prefix, count int) bool
- func PacketDestination(packet []byte) netip.Addr
- func PacketFillHeader(packet []byte, ipVersion int)
- func PacketIPVersion(packet []byte) int
- type AutoRedirect
- type AutoRedirectOptions
- type DarwinTUN
- type DefaultInterfaceMonitor
- type DefaultInterfaceMonitorOptions
- type DefaultInterfaceUpdateCallback
- type FlowAction
- type FlowCloseReason
- type FlowHandle
- type FlowTracker
- type FlowVerdict
- type ForwardDispatcher
- type ForwardWriteback
- type GSOOptions
- type GSOType
- type Handler
- type LinuxTUN
- type NATFiltering
- type NATMapping
- type NativeTun
- func (t *NativeTun) BatchRead(buffers [][]byte, offset int, readN []int) (int, error)
- func (t *NativeTun) BatchSize() int
- func (t *NativeTun) BatchWrite(buffers [][]byte, offset int) (int, error)
- func (t *NativeTun) Close() error
- func (t *NativeTun) FrontHeadroom() int
- func (t *NativeTun) Name() (string, error)
- func (t *NativeTun) Read(p []byte) (n int, err error)
- func (t *NativeTun) Start() error
- func (t *NativeTun) TXChecksumOffload() bool
- func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error
- func (t *NativeTun) Write(p []byte) (n int, err error)
- type NetworkUpdateCallback
- type NetworkUpdateMonitor
- type Options
- func (o *Options) BuildAndroidRules(packageManager PackageManager)
- func (o *Options) BuildAutoRouteRanges(underNetworkExtension bool) ([]netip.Prefix, error)
- func (o *Options) DNSModeOrDefault() string
- func (o *Options) DNSServerAddress() ([]netip.Addr, error)
- func (o *Options) ExcludedRanges() (uidRanges []ranges.Range[uint32])
- func (o *Options) Inet4DNSAddress() ([]netip.Addr, error)
- func (o *Options) Inet4GatewayAddr() netip.Addr
- func (o *Options) Inet6DNSAddress() ([]netip.Addr, error)
- func (o *Options) Inet6GatewayAddr() netip.Addr
- type PackageManager
- type PackageManagerCallback
- type PackageManagerOptions
- type Port
- type PortWithSelectorRange
- type Return
- type Session
- type Stack
- type StackOptions
- type System
- type TCPNat
- type TCPSession
- type Tun
- type UDPEgressConn
- func (c *UDPEgressConn) Close() error
- func (c *UDPEgressConn) LocalAddr() net.Addr
- func (c *UDPEgressConn) ReadFromUDPAddrPort(buffer []byte) (int, netip.AddrPort, error)
- func (c *UDPEgressConn) SetDeadline(t time.Time) error
- func (c *UDPEgressConn) SetReadDeadline(t time.Time) error
- func (c *UDPEgressConn) SetWriteDeadline(t time.Time) error
- func (c *UDPEgressConn) WriteToUDPAddrPort(buffer []byte, destination netip.AddrPort) (int, error)
- type UDPEgressPool
- type UDPEgressPoolOptions
- type UDPNat
- func (s *UDPNat) Close() error
- func (s *UDPNat) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destination M.Socksaddr, ...)
- func (s *UDPNat) NewPacketBatch(buffers []*buf.Buffer, sources []M.Socksaddr, destination M.Socksaddr, ...)
- func (s *UDPNat) Purge()
- func (s *UDPNat) PurgeExpired()
- func (s *UDPNat) Start() error
- type UDPNatOptions
- type UDPNatPrepareFunc
- type WinTun
Constants ¶
const ( DefaultAutoRedirectInputMark = 0x2023 DefaultAutoRedirectOutputMark = 0x2024 DefaultAutoRedirectResetMark = 0x2025 DefaultAutoRedirectNFQueue = 100 )
const ( DefaultIPRoute2TableIndex = 2022 DefaultIPRoute2RuleIndex = 9000 DefaultIPRoute2AutoRedirectFallbackRuleIndex = 32768 )
const ( DNSModeDisabled = "disabled" DNSModeNative = "native" DNSModeHijack = "hijack" )
const FlagAndroidVPNUpdate = 1 << iota
const PacketOffset = 0
const WithGVisor = false
Variables ¶
var ErrGVisorNotIncluded = E.New(`gVisor is not included in this build, rebuild with -tags with_gvisor`)
var ErrIncludeAllNetworks = E.New("`system` and `mixed` stack are not available when `includeAllNetworks` is enabled. See https://github.com/SagerNet/sing-tun/issues/25")
var ErrNetlinkBanned = E.New(
"netlink socket in Android is banned by Google, " +
"use the root or system (ADB) user to run sing-box, " +
"or switch to the sing-box Android graphical interface client",
)
var ErrNoRoute = E.New("no route to internet")
var ErrTooManySegments = errors.New("too many segments")
ErrTooManySegments is returned by Device.Read() when segmentation overflows the length of supplied buffers. This error should not cause reads to cease.
Functions ¶
func BuildUnreachable ¶ added in v0.9.0
func CalculateInterfaceName ¶
func GSOSplit ¶ added in v0.6.0
func GSOSplit(in []byte, options GSOOptions, outBufs [][]byte, sizes []int, outOffset int) (int, error)
GSOSplit splits packets from 'in' into outBufs[<index>][outOffset:], writing the size of each element into sizes. It returns the number of buffers populated, and/or an error. Callers may pass an 'in' slice that overlaps with the first element of outBuffers, i.e. &in[0] may be equal to &outBufs[0][outOffset]. GSONone is a valid options.GSOType regardless of the value of options.NeedsCsum. Length of each outBufs element must be greater than or equal to the length of 'in', otherwise output may be silently truncated.
func PacketDestination ¶ added in v0.6.0
func PacketFillHeader ¶ added in v0.6.0
func PacketIPVersion ¶ added in v0.6.0
Types ¶
type AutoRedirect ¶ added in v0.4.1
func NewAutoRedirect ¶ added in v0.4.1
func NewAutoRedirect(options AutoRedirectOptions) (AutoRedirect, error)
type AutoRedirectOptions ¶ added in v0.4.1
type AutoRedirectOptions struct {
TunOptions *Options
Context context.Context
Handler Handler
Logger logger.Logger
NetworkMonitor NetworkUpdateMonitor
InterfaceFinder control.InterfaceFinder
TableName string
DisableNFTables bool
CustomRedirectPort func() int
RouteAddressSet *[]*netipx.IPSet
RouteExcludeAddressSet *[]*netipx.IPSet
}
type DefaultInterfaceMonitor ¶
type DefaultInterfaceMonitor interface {
Start() error
Close() error
DefaultInterface() *control.Interface
OverrideAndroidVPN() bool
AndroidVPNEnabled() bool
RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback]
UnregisterCallback(element *list.Element[DefaultInterfaceUpdateCallback])
RegisterMyInterface(interfaceName string)
MyInterfaces() []string
}
func NewDefaultInterfaceMonitor ¶
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, logger logger.Logger, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error)
type DefaultInterfaceMonitorOptions ¶
type DefaultInterfaceMonitorOptions struct {
InterfaceFinder control.InterfaceFinder
OverrideAndroidVPN bool
UnderNetworkExtension bool
}
type FlowAction ¶ added in v0.9.0
type FlowAction uint8
const ( ActionAccept FlowAction = iota ActionFlow ActionReject ActionDrop ActionBypass ActionHijackDNS )
type FlowCloseReason ¶ added in v0.9.0
type FlowCloseReason uint8
const ( FlowCloseReset FlowCloseReason = iota FlowCloseFinished FlowCloseTimeout )
func (FlowCloseReason) String ¶ added in v0.9.0
func (r FlowCloseReason) String() string
type FlowHandle ¶ added in v0.9.0
type FlowHandle interface {
CloseFlow()
}
type FlowTracker ¶ added in v0.9.0
type FlowTracker interface {
AttachFlow(handle FlowHandle)
CountForward(n int)
CountReverse(n int)
FlowEstablished()
CloseFlow(reason FlowCloseReason)
}
type FlowVerdict ¶ added in v0.9.0
type FlowVerdict struct {
Action FlowAction
Port Port
Destination netip.AddrPort
UDPTimeout time.Duration
NewTracker func() FlowTracker
}
type ForwardDispatcher ¶ added in v0.9.0
type ForwardDispatcher struct {
// contains filtered or unexported fields
}
func NewForwardDispatcher ¶ added in v0.9.0
func NewForwardDispatcher(handler Handler, writeback ForwardWriteback, logger logger.Logger, udpTimeout time.Duration, icmpTimeout time.Duration) *ForwardDispatcher
func (*ForwardDispatcher) Close ¶ added in v0.9.0
func (d *ForwardDispatcher) Close()
func (*ForwardDispatcher) Dispatch ¶ added in v0.9.0
func (d *ForwardDispatcher) Dispatch(packet []byte) bool
func (*ForwardDispatcher) Flush ¶ added in v0.9.0
func (d *ForwardDispatcher) Flush()
func (*ForwardDispatcher) ResetNetwork ¶ added in v0.9.0
func (d *ForwardDispatcher) ResetNetwork()
type ForwardWriteback ¶ added in v0.9.0
type GSOOptions ¶ added in v0.6.0
type GSOOptions struct {
// GSOType represents the type of segmentation offload.
GSOType GSOType
// HdrLen is the sum of the layer 3 and 4 header lengths. This field may be
// zero when GSOType == GSONone.
HdrLen uint16
// CsumStart is the head byte index of the packet data to be checksummed,
// i.e. the start of the TCP or UDP header.
CsumStart uint16
// CsumOffset is the offset from CsumStart where the 2-byte checksum value
// should be placed.
CsumOffset uint16
// GSOSize is the size of each segment exclusive of HdrLen. The tail segment
// may be smaller than this value.
GSOSize uint16
// NeedsCsum may be set where GSOType == GSONone. When set, the checksum
// at CsumStart + CsumOffset must be a partial checksum, i.e. the
// pseudo-header sum.
NeedsCsum bool
}
GSOOptions is loosely modeled after struct virtio_net_hdr from the VIRTIO specification. It is a common representation of GSO metadata that can be applied to support packet GSO across tun.Device implementations.
type GSOType ¶ added in v0.6.0
type GSOType int
GSOType represents the type of segmentation offload.
type Handler ¶
type Handler interface {
JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) FlowVerdict
NewDNSPacket(payload []byte, source M.Socksaddr, destination M.Socksaddr, writer N.PacketWriter)
N.TCPConnectionHandlerEx
N.UDPConnectionHandlerEx
}
type NATFiltering ¶ added in v0.9.0
type NATFiltering uint8
const ( NATFilteringEndpointIndependent NATFiltering = iota NATFilteringAddressDependent NATFilteringAddressAndPortDependent )
type NATMapping ¶ added in v0.9.0
type NATMapping uint8
const ( NATMappingEndpointIndependent NATMapping = iota NATMappingAddressDependent NATMappingAddressAndPortDependent )
type NativeTun ¶
type NativeTun struct {
// contains filtered or unexported fields
}
func (*NativeTun) BatchWrite ¶ added in v0.2.0
func (*NativeTun) FrontHeadroom ¶ added in v0.2.0
func (*NativeTun) TXChecksumOffload ¶ added in v0.2.0
func (*NativeTun) UpdateRouteOptions ¶ added in v0.6.0
type NetworkUpdateCallback ¶
type NetworkUpdateCallback = func()
type NetworkUpdateMonitor ¶
type NetworkUpdateMonitor interface {
Start() error
Close() error
RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback]
UnregisterCallback(element *list.Element[NetworkUpdateCallback])
}
func NewNetworkUpdateMonitor ¶
func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error)
type Options ¶
type Options struct {
Name string
NetNs string
Inet4Address []netip.Prefix
Inet6Address []netip.Prefix
MTU uint32
GSO bool
AutoRoute bool
InterfaceScope bool
Inet4Gateway netip.Addr
Inet6Gateway netip.Addr
DNSMode string
DNSAddress []netip.Addr
IPRoute2TableIndex int
IPRoute2RuleIndex int
IPRoute2AutoRedirectFallbackRuleIndex int
AutoRedirectMarkMode bool
AutoRedirectInputMark uint32
AutoRedirectOutputMark uint32
AutoRedirectResetMark uint32
AutoRedirectNFQueue uint16
ExcludeMPTCP bool
Inet4LoopbackAddress []netip.Addr
Inet6LoopbackAddress []netip.Addr
StrictRoute bool
Inet4RouteAddress []netip.Prefix
Inet6RouteAddress []netip.Prefix
Inet4RouteExcludeAddress []netip.Prefix
Inet6RouteExcludeAddress []netip.Prefix
IncludeInterface []string
ExcludeInterface []string
IncludeUID []ranges.Range[uint32]
ExcludeUID []ranges.Range[uint32]
IncludeAndroidUser []int
IncludePackage []string
ExcludePackage []string
IncludeMACAddress []net.HardwareAddr
ExcludeMACAddress []net.HardwareAddr
InterfaceFinder control.InterfaceFinder
InterfaceMonitor DefaultInterfaceMonitor
FileDescriptor int
Logger logger.Logger
// For library usages.
EXP_DisableDNSHijack bool
EXP_ExternalConfiguration bool
// For gvisor stack, it should be enabled when MTU is less than 32768; otherwise it should be less than or equal to 8192.
// The above condition is just an estimate and not exact, calculated on M4 pro.
EXP_MultiPendingPackets bool
// Will cause the darwin network to die, do not use.
EXP_SendMsgX bool
// contains filtered or unexported fields
}
func (*Options) BuildAndroidRules ¶
func (o *Options) BuildAndroidRules(packageManager PackageManager)
func (*Options) BuildAutoRouteRanges ¶ added in v0.1.21
func (*Options) DNSModeOrDefault ¶ added in v0.9.0
func (*Options) DNSServerAddress ¶ added in v0.9.0
func (*Options) ExcludedRanges ¶
func (*Options) Inet4DNSAddress ¶ added in v0.9.0
func (*Options) Inet4GatewayAddr ¶ added in v0.4.1
func (*Options) Inet6DNSAddress ¶ added in v0.9.0
func (*Options) Inet6GatewayAddr ¶ added in v0.4.1
type PackageManager ¶
type PackageManager interface {
Start() error
Close() error
IDByPackage(packageName string) (uint32, bool)
PackageByID(id uint32) (string, bool)
PackagesByID(id uint32) ([]string, bool)
}
func NewPackageManager ¶
func NewPackageManager(options PackageManagerOptions) (PackageManager, error)
type PackageManagerCallback ¶
type PackageManagerOptions ¶ added in v0.4.1
type PackageManagerOptions struct {
Callback PackageManagerCallback
// Logger is the logger to log errors
// optional
Logger logger.Logger
}
type PortWithSelectorRange ¶ added in v0.9.0
type Stack ¶
func NewGVisor ¶
func NewGVisor( options StackOptions, ) (Stack, error)
func NewMixed ¶ added in v0.1.12
func NewMixed( options StackOptions, ) (Stack, error)
func NewSystem ¶
func NewSystem(options StackOptions) (Stack, error)
type StackOptions ¶
type StackOptions struct {
Context context.Context
Tun Tun
TunOptions Options
UDPTimeout time.Duration
ICMPTimeout time.Duration
UDPMapping NATMapping
UDPFiltering NATFiltering
UDPNATMax uint32
Handler Handler
Logger logger.Logger
ForwarderBindInterface bool
IncludeAllNetworks bool
InterfaceFinder control.InterfaceFinder
}
type System ¶
type System struct {
// contains filtered or unexported fields
}
func (*System) ResetNetwork ¶ added in v0.9.0
func (s *System) ResetNetwork()
type TCPNat ¶
type TCPNat struct {
// contains filtered or unexported fields
}
func (*TCPNat) LookupBack ¶
func (n *TCPNat) LookupBack(port uint16) *TCPSession
type TCPSession ¶
type Tun ¶
type UDPEgressConn ¶ added in v0.9.0
type UDPEgressConn struct {
// contains filtered or unexported fields
}
func NewUDPEgressConn ¶ added in v0.9.0
func NewUDPEgressConn(anchor *net.UDPConn, pool *UDPEgressPool) *UDPEgressConn
func (*UDPEgressConn) Close ¶ added in v0.9.0
func (c *UDPEgressConn) Close() error
func (*UDPEgressConn) LocalAddr ¶ added in v0.9.0
func (c *UDPEgressConn) LocalAddr() net.Addr
func (*UDPEgressConn) ReadFromUDPAddrPort ¶ added in v0.9.0
func (*UDPEgressConn) SetDeadline ¶ added in v0.9.0
func (c *UDPEgressConn) SetDeadline(t time.Time) error
func (*UDPEgressConn) SetReadDeadline ¶ added in v0.9.0
func (c *UDPEgressConn) SetReadDeadline(t time.Time) error
func (*UDPEgressConn) SetWriteDeadline ¶ added in v0.9.0
func (c *UDPEgressConn) SetWriteDeadline(t time.Time) error
func (*UDPEgressConn) WriteToUDPAddrPort ¶ added in v0.9.0
type UDPEgressPool ¶ added in v0.9.0
type UDPEgressPool struct {
// contains filtered or unexported fields
}
func NewUDPEgressPool ¶ added in v0.9.0
func NewUDPEgressPool(options UDPEgressPoolOptions) *UDPEgressPool
func (*UDPEgressPool) Close ¶ added in v0.9.0
func (p *UDPEgressPool) Close()
func (*UDPEgressPool) LookupEgress ¶ added in v0.9.0
func (p *UDPEgressPool) LookupEgress(destination netip.AddrPort) *net.UDPConn
func (*UDPEgressPool) ReceiveEgress ¶ added in v0.9.0
func (*UDPEgressPool) SetEgressPort ¶ added in v0.9.0
func (p *UDPEgressPool) SetEgressPort(port uint16) bool
type UDPEgressPoolOptions ¶ added in v0.9.0
type UDPEgressPoolOptions struct {
Logger logger.Logger
Network string
Control control.Func
InterfaceFinder control.InterfaceFinder
InterfaceMonitor DefaultInterfaceMonitor
ExcludeInterface string
IsExempt func() bool
}
type UDPNat ¶ added in v0.9.0
type UDPNat struct {
// contains filtered or unexported fields
}
func NewUDPNat ¶ added in v0.9.0
func NewUDPNat(options UDPNatOptions) *UDPNat
func (*UDPNat) NewPacketBatch ¶ added in v0.9.0
func (*UDPNat) PurgeExpired ¶ added in v0.9.0
func (s *UDPNat) PurgeExpired()
type UDPNatOptions ¶ added in v0.9.0
type UDPNatOptions struct {
Handler N.UDPConnectionHandlerEx
Prepare UDPNatPrepareFunc
Timeout time.Duration
Mapping NATMapping
Filtering NATFiltering
MaxSize uint32
InterfaceFinder control.InterfaceFinder
ExcludeInterface []string
}
type UDPNatPrepareFunc ¶ added in v0.9.0
Source Files
¶
- flow.go
- flow_dispatch.go
- flow_dns.go
- flow_mtu.go
- flow_nat.go
- flow_parse.go
- flow_reject.go
- flow_rewrite.go
- monitor.go
- monitor_linux.go
- monitor_linux_default.go
- monitor_shared.go
- netns_linux.go
- nfqueue_linux.go
- packages.go
- packages_stub.go
- redirect.go
- redirect_iptables.go
- redirect_linux.go
- redirect_nftables.go
- redirect_nftables_docker.go
- redirect_nftables_exprs.go
- redirect_nftables_rules.go
- redirect_nftables_rules_openwrt.go
- redirect_route_linux.go
- redirect_server.go
- stack.go
- stack_gvisor_stub.go
- stack_system.go
- stack_system_nat.go
- stack_system_nonwindows.go
- stack_system_packet.go
- tun.go
- tun_linux.go
- tun_linux_flags.go
- tun_nondarwin.go
- tun_offload.go
- tun_offload_errors.go
- tun_offload_linux.go
- tun_rules.go
- udp_egress.go
- udp_egress_conn.go
- udp_nat.go
- udp_nat_cleanup.go
Directories
¶
| Path | Synopsis |
|---|---|
|
checksum
Package checksum provides the implementation of the encoding and decoding of network protocol headers.
|
Package checksum provides the implementation of the encoding and decoding of network protocol headers. |
|
header
Package header provides the implementation of the encoding and decoding of network protocol headers.
|
Package header provides the implementation of the encoding and decoding of network protocol headers. |
|
seqnum
Package seqnum defines the types and methods for TCP sequence numbers such that they fit in 32-bit words and work properly when overflows occur.
|
Package seqnum defines the types and methods for TCP sequence numbers such that they fit in 32-bit words and work properly when overflows occur. |
|
internal
|
|