tun

package
v0.0.0-...-397e212 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProtoTCP = 6
	ProtoUDP = 17
)
View Source
const (
	TCPFlagFIN = 0x01
	TCPFlagSYN = 0x02
	TCPFlagRST = 0x04
	TCPFlagACK = 0x10
)
View Source
const (
	// TUN device MTU — matches common VPN MTU to avoid fragmentation.
	DefaultMTU = 1500
	// TUN subnet used for the virtual interface.
	TunAddr    = "10.0.85.1"
	TunGateway = "10.0.85.0"
	TunMask    = "255.255.255.0"
	TunCIDR    = "10.0.85.1/24"
)

Variables

This section is empty.

Functions

func IPVersion

func IPVersion(pkt []byte) int

IPVersion extracts the IP version from a raw packet.

Types

type Device

type Device struct {
	Iface      *water.Interface
	Name       string
	ServerIP   string
	OriginalGW string
	OriginalIF string
}

Device wraps a TUN interface with its configuration.

func NewDevice

func NewDevice(serverIP string) (*Device, error)

NewDevice creates and configures a TUN interface. serverIP is the SOCKS5 server's LAN IP — we add a static route for it so the proxy control traffic bypasses the TUN.

func (*Device) Read

func (d *Device) Read(buf []byte) (int, error)

Read reads a single IP packet from the TUN device.

func (*Device) Setup

func (d *Device) Setup() error

Setup configures the TUN interface IP, brings it up, and reroutes the default gateway through the tunnel. It preserves the original gateway so it can be restored on teardown.

func (*Device) Teardown

func (d *Device) Teardown()

Teardown restores the original routing table and closes the TUN device.

func (*Device) Write

func (d *Device) Write(buf []byte) (int, error)

Write writes a single IP packet to the TUN device.

type Engine

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

Engine reads IP packets from a TUN device, identifies TCP/UDP flows, and forwards them through the binary tunnel protocol.

func NewEngine

func NewEngine(dev *Device, tunnelClient *tunnel.Client) *Engine

NewEngine creates a packet forwarding engine.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) error

Run starts the packet read loop. Blocks until ctx is cancelled.

type IPv4Header

type IPv4Header struct {
	Version    int
	IHL        int // header length in bytes
	TotalLen   int
	Protocol   int // 6=TCP, 17=UDP
	SrcIP      net.IP
	DstIP      net.IP
	Raw        []byte
	PayloadOff int // offset where transport header begins
}

IPv4Header represents a parsed IPv4 header.

func ParseIPv4

func ParseIPv4(pkt []byte) (*IPv4Header, error)

ParseIPv4 parses an IPv4 header from a raw packet.

type TCPHeader

type TCPHeader struct {
	SrcPort  uint16
	DstPort  uint16
	SeqNum   uint32
	AckNum   uint32
	DataOff  int // header length in bytes
	Flags    uint8
	Window   uint16
	Checksum uint16
}

TCPHeader represents key fields of a TCP header.

func ParseTCP

func ParseTCP(data []byte) (*TCPHeader, error)

ParseTCP parses a TCP header from the transport payload.

type UDPHeader

type UDPHeader struct {
	SrcPort uint16
	DstPort uint16
	Length  uint16
}

UDPHeader represents a UDP header.

func ParseUDP

func ParseUDP(data []byte) (*UDPHeader, error)

ParseUDP parses a UDP header from the transport payload.

Jump to

Keyboard shortcuts

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