tunnel

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package tunnel provides TUN tunnel integration for Android VpnService. It supports attaching to an existing TUN file descriptor provided by Android.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTunnelAlreadyRunning = &TunnelError{Op: "start", Err: errors.New("tunnel already running")}
	ErrTunnelNotRunning     = &TunnelError{Op: "stop", Err: errors.New("tunnel not running")}
	ErrInvalidFD            = &TunnelError{Op: "attach", Err: errors.New("invalid TUN file descriptor")}
	ErrTun2socksNotFound    = &TunnelError{Op: "startup", Err: errors.New("tun2socks binary not found")}
	ErrPlatformNotSupported = &TunnelError{Op: "init", Err: errors.New("TUN tunnel not supported on this platform")}
)

Predefined errors

Functions

func IsAndroidTUNSupported

func IsAndroidTUNSupported() bool

IsAndroidTUNSupported returns true if Android TUN backend is supported

func ValidateFD

func ValidateFD(fd int) error

ValidateFD checks if a TUN file descriptor is valid

Types

type Tunnel

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

Tunnel represents a TUN tunnel instance

func New

func New() *Tunnel

New creates a new Tunnel instance (not started)

func (*Tunnel) Close

func (t *Tunnel) Close() error

Close stops the tunnel and cleans up resources

func (*Tunnel) GetMTU

func (t *Tunnel) GetMTU() int

GetMTU returns the MTU of the TUN interface

func (*Tunnel) GetTUNFD

func (t *Tunnel) GetTUNFD() int

GetTUNFD returns the TUN file descriptor (for advanced use)

func (*Tunnel) GetTunnelInfo

func (t *Tunnel) GetTunnelInfo() map[string]interface{}

GetTunnelInfo returns information about the running tunnel

func (*Tunnel) GetUptime

func (t *Tunnel) GetUptime() time.Duration

GetUptime returns how long the tunnel has been running

func (*Tunnel) IsTunnelRunning

func (t *Tunnel) IsTunnelRunning() bool

IsTunnelRunning returns true if tunnel is currently running

func (*Tunnel) Restart

func (t *Tunnel) Restart() error

Restart restarts the tunnel with the same configuration

func (*Tunnel) StartTunnelFromFD

func (t *Tunnel) StartTunnelFromFD(fd int, mtu int) error

StartTunnelFromFD starts a TUN tunnel using an existing TUN file descriptor. This is the main entry point for Android VpnService integration.

Parameters:

  • fd: TUN file descriptor from Android VpnService.protect()
  • mtu: MTU for the TUN interface (use 0 for default 1500)

Returns error if tunnel fails to start. Does NOT kill the process on failure.

func (*Tunnel) StartTunnelFromFDWithConfig

func (t *Tunnel) StartTunnelFromFDWithConfig(fd int, mtu int, cfg TunnelConfig) error

StartTunnelFromFDWithConfig starts tunnel with full configuration

func (*Tunnel) StopTunnel

func (t *Tunnel) StopTunnel() error

StopTunnel stops the TUN tunnel gracefully

type TunnelConfig

type TunnelConfig struct {
	// TUN file descriptor from Android VpnService
	FD int
	// MTU for the TUN interface (default: 1500)
	MTU int
	// Path to tun2socks binary (optional, uses embedded if empty)
	Tun2socksPath string
	// SOCKS5 server address (default: 127.0.0.1:1080)
	Socks5Addr string
	// DNS server address (default: 8.8.8.8:53)
	DNS string
	// Enable verbose logging
	Verbose bool
}

TunnelConfig holds tunnel configuration

type TunnelError

type TunnelError struct {
	Op  string
	Err error
}

TunnelError represents tunnel-related errors

func (*TunnelError) Error

func (e *TunnelError) Error() string

func (*TunnelError) Unwrap

func (e *TunnelError) Unwrap() error

Jump to

Keyboard shortcuts

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