tun

package module
v0.0.0-...-6e5915b Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2023 License: GPL-2.0 Imports: 33 Imported by: 1

README

sing-tun

Simple transparent proxy library.

For Linux, Windows, macOS and iOS.

License

Copyright (C) 2022 by nekohasekai <contact-sagernet@sekai.icu>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Documentation

Index

Constants

View Source
const (
	EventInterfaceUpdate  = 1
	EventAndroidVPNUpdate = 2
	EventNoRoute          = 4
)
View Source
const PacketOffset = 0
View Source
const WithGVisor = false

Variables

View Source
var ErrGVisorNotIncluded = E.New(`gVisor is not included in this build, rebuild with -tags with_gvisor`)
View Source
var ErrNoRoute = E.New("no route to internet")

Functions

func CalculateInterfaceName

func CalculateInterfaceName(name string) (tunName string)

func NetworkFromName

func NetworkFromName(name string) uint8

func NetworkName

func NetworkName(network uint8) string

Types

type DefaultInterfaceMonitor

type DefaultInterfaceMonitor interface {
	Start() error
	Close() error
	DefaultInterfaceName(destination netip.Addr) string
	DefaultInterfaceIndex(destination netip.Addr) int
	DefaultInterface(destination netip.Addr) (string, int)
	OverrideAndroidVPN() bool
	AndroidVPNEnabled() bool
	RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback]
	UnregisterCallback(element *list.Element[DefaultInterfaceUpdateCallback])
}

func NewDefaultInterfaceMonitor

func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, logger logger.Logger, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error)

type DefaultInterfaceMonitorOptions

type DefaultInterfaceMonitorOptions struct {
	OverrideAndroidVPN    bool
	UnderNetworkExtension bool
}

type DefaultInterfaceUpdateCallback

type DefaultInterfaceUpdateCallback = func(event int)

type Handler

type Handler interface {
	N.TCPConnectionHandler
	N.UDPConnectionHandler
	E.Handler
}

type NativeTun

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

func (*NativeTun) Close

func (t *NativeTun) Close() error

func (*NativeTun) CreateVectorisedWriter

func (t *NativeTun) CreateVectorisedWriter() N.VectorisedWriter

func (*NativeTun) Read

func (t *NativeTun) Read(p []byte) (n int, err error)

func (*NativeTun) Write

func (t *NativeTun) Write(p []byte) (n int, err error)

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
	Inet4Address       []netip.Prefix
	Inet6Address       []netip.Prefix
	MTU                uint32
	AutoRoute          bool
	StrictRoute        bool
	Inet4RouteAddress  []netip.Prefix
	Inet6RouteAddress  []netip.Prefix
	IncludeInterface   []string
	ExcludeInterface   []string
	IncludeUID         []ranges.Range[uint32]
	ExcludeUID         []ranges.Range[uint32]
	IncludeAndroidUser []int
	IncludePackage     []string
	ExcludePackage     []string
	InterfaceMonitor   DefaultInterfaceMonitor
	TableIndex         int
	FileDescriptor     int
	Logger             logger.Logger
}

func (*Options) BuildAndroidRules

func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler E.Handler)

func (*Options) ExcludedRanges

func (o *Options) ExcludedRanges() (uidRanges []ranges.Range[uint32])

type PackageManager

type PackageManager interface {
	Start() error
	Close() error
	IDByPackage(packageName string) (uint32, bool)
	IDBySharedPackage(sharedPackage string) (uint32, bool)
	PackageByID(id uint32) (string, bool)
	SharedPackageByID(id uint32) (string, bool)
}

func NewPackageManager

func NewPackageManager(callback PackageManagerCallback) (PackageManager, error)

type PackageManagerCallback

type PackageManagerCallback interface {
	OnPackagesUpdated(packages int, sharedUsers int)
	E.Handler
}

type Session

type Session struct {
	SourceAddress      netip.Addr
	DestinationAddress netip.Addr
	SourcePort         uint16
	DestinationPort    uint16
}

type Stack

type Stack interface {
	Start() error
	Close() error
}

func NewGVisor

func NewGVisor(
	options StackOptions,
) (Stack, error)

func NewLWIP

func NewLWIP(
	options StackOptions,
) (Stack, error)

func NewMixed

func NewMixed(
	options StackOptions,
) (Stack, error)

func NewStack

func NewStack(
	stack string,
	options StackOptions,
) (Stack, error)

func NewSystem

func NewSystem(options StackOptions) (Stack, error)

type StackOptions

type StackOptions struct {
	Context                context.Context
	Tun                    Tun
	Name                   string
	MTU                    uint32
	Inet4Address           []netip.Prefix
	Inet6Address           []netip.Prefix
	EndpointIndependentNat bool
	UDPTimeout             int64
	Handler                Handler
	Logger                 logger.Logger
	ForwarderBindInterface bool
	InterfaceFinder        control.InterfaceFinder
}

type System

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

func (*System) Close

func (s *System) Close() error

func (*System) Start

func (s *System) Start() error

type TCPNat

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

func NewNat

func NewNat(ctx context.Context, timeout time.Duration) *TCPNat

func (*TCPNat) Lookup

func (n *TCPNat) Lookup(source netip.AddrPort, destination netip.AddrPort) uint16

func (*TCPNat) LookupBack

func (n *TCPNat) LookupBack(port uint16) *TCPSession

type TCPSession

type TCPSession struct {
	Source      netip.AddrPort
	Destination netip.AddrPort
	LastActive  time.Time
}

type Tun

type Tun interface {
	io.ReadWriter
	CreateVectorisedWriter() N.VectorisedWriter
	Close() error
}

func New

func New(options Options) (Tun, error)

type WinTun

type WinTun interface {
	Tun
	ReadPacket() ([]byte, func(), error)
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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