tun

package module
v0.0.0-...-0e93635 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: GPL-2.0 Imports: 30 Imported by: 2

README

cropped for sing-tun

This project is based on the sing-tun project, with a lot of cuts and streamlining. Only the gVisor mode has been retained and made more generic.

Due to the removal of many customizations and some referenced code, it is for personal use only, and only supports Linux, macOS and Windows.

usage

go get github.com/josexy/cropstun

Here is a simple example:

type myHandler struct{}

func (*myHandler) HandleTCPConnection(conn net.Conn, info tun.Metadata) error {
	log.Printf("tcp, src: %s, dst: %s", info.Source, info.Destination)
	// do something...
	return nil
}

func (*myHandler) HandleUDPConnection(conn net.PacketConn, info tun.Metadata) error {
	log.Printf("udp, src: %s, dst: %s", info.Source, info.Destination)
	// do something...
	return nil
}

func main() {
	tunOpt := new(tun.Options)
	tunIf, err := tun.NewTunDevice([]netip.Prefix{netip.MustParsePrefix("198.18.0.1/16")}, tunOpt)
	if err != nil {
		log.Fatal(err)
	}
	stack, err := tun.NewStack(tun.StackOptions{
		Tun:        tunIf,
		TunOptions: tunOpt,
		Handler:    &myHandler{},
	})
	if err != nil {
		log.Fatal(err)
	}
	if err = stack.Start(); err != nil {
		log.Fatal(err)
	}
	inter := make(chan os.Signal, 1)
	signal.Notify(inter, syscall.SIGINT)
	<-inter
	stack.Close()
	time.Sleep(time.Second)
}

Credits

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 (
	DefaultMTU                = 9000
	DefaultIPRoute2TableIndex = 4000
	DefaultIPRoute2RuleIndex  = 10086
)

Variables

This section is empty.

Functions

func AddrFromAddress

func AddrFromAddress(address tcpip.Address) netip.Addr

func AddressFromAddr

func AddressFromAddr(destination netip.Addr) tcpip.Address

func CalculateInterfaceName

func CalculateInterfaceName(name string) (tunName string)

Types

type GVisor

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

func (*GVisor) Close

func (t *GVisor) Close() error

func (*GVisor) Start

func (t *GVisor) Start() error

func (*GVisor) TunDevice

func (t *GVisor) TunDevice() Tun

type GVisorTun

type GVisorTun interface {
	Tun
	NewEndpoint() (stack.LinkEndpoint, error)
}

type Handler

type Handler interface {
	TCPConnectionHandler
	UDPConnectionHandler
}

type LinuxTUN

type LinuxTUN interface {
	Tun
	TXChecksumOffload() bool
}

type Metadata

type Metadata struct {
	Source      netip.AddrPort
	Destination netip.AddrPort
}

type NativeTun

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

func (*NativeTun) Close

func (t *NativeTun) Close() (err error)

func (*NativeTun) NewEndpoint

func (t *NativeTun) NewEndpoint() (stack.LinkEndpoint, error)

func (*NativeTun) Read

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

func (*NativeTun) SetupDNS

func (t *NativeTun) SetupDNS(addrs []netip.Addr) error

func (*NativeTun) TXChecksumOffload

func (t *NativeTun) TXChecksumOffload() bool

func (*NativeTun) TeardownDNS

func (t *NativeTun) TeardownDNS() error

func (*NativeTun) Write

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

func (*NativeTun) WriteVectorised

func (t *NativeTun) WriteVectorised(buffers []*buf.Buffer) error

type Options

type Options struct {
	Name               string
	Inet4Address       []netip.Prefix
	Inet6Address       []netip.Prefix
	MTU                uint32
	IPRoute2TableIndex int
	IPRoute2RuleIndex  int
	AutoRoute          bool
}

func (*Options) BuildAutoRouteRanges

func (o *Options) BuildAutoRouteRanges() ([]netip.Prefix, error)

type Stack

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

func NewStack

func NewStack(options StackOptions) (Stack, error)

type StackOptions

type StackOptions struct {
	Tun        Tun
	TunOptions *Options
	Handler    Handler
}

type TCPConn

type TCPConn interface {
	net.Conn
}

type TCPConnectionHandler

type TCPConnectionHandler interface {
	HandleTCPConnection(TCPConn, Metadata) error
}

type Tun

type Tun interface {
	io.ReadWriteCloser
	N.VectorisedWriter
	SetupDNS([]netip.Addr) error
	TeardownDNS() error
}

func New

func New(options *Options) (Tun, error)

func NewTunDevice

func NewTunDevice(cidrs []netip.Prefix, options *Options) (Tun, error)

type UDPConn

type UDPConn interface {
	net.Conn
	net.PacketConn
}

type UDPConnectionHandler

type UDPConnectionHandler interface {
	HandleUDPConnection(UDPConn, Metadata) error
}

type WinTun

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

Directories

Path Synopsis
buf
example
internal
referenced from https://github.com/MetaCubeX/mihomo/blob/Alpha/component/process/
referenced from https://github.com/MetaCubeX/mihomo/blob/Alpha/component/process/

Jump to

Keyboard shortcuts

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