Documentation
¶
Overview ¶
Package vtun provides a virtual tunnel implementation built on gVisor's netstack. It creates userspace network interfaces that support TCP, UDP, and ICMP, with built-in DNS resolution. TCP name dials use a dual-stack connection race so an unavailable address family does not block a usable address from the other family.
Index ¶
- type Opts
- type PingAddr
- type PingConn
- func (pc *PingConn) Close() error
- func (pc *PingConn) LocalAddr() net.Addr
- func (pc *PingConn) Read(p []byte) (n int, err error)
- func (pc *PingConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (pc *PingConn) RemoteAddr() net.Addr
- func (pc *PingConn) SetDeadline(t time.Time) error
- func (pc *PingConn) SetReadDeadline(t time.Time) error
- func (pc *PingConn) SetWriteDeadline(t time.Time) error
- func (pc *PingConn) Write(p []byte) (n int, err error)
- func (pc *PingConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type SourceRoute
- type VTun
- func (vt *VTun) BatchSize() int
- func (vt *VTun) Close() error
- func (vt *VTun) Dial(ctx context.Context, network, address string) (net.Conn, error)
- func (vt *VTun) DialPing(laddr, raddr *PingAddr) (*PingConn, error)
- func (vt *VTun) DialPingAddr(laddr, raddr netip.Addr) (*PingConn, error)
- func (vt *VTun) DialTCP(ctx context.Context, network, laddr, raddr string) (gonnect.TCPConn, error)
- func (vt *VTun) DialTCPAddrPort(ctx context.Context, addr netip.AddrPort) (*gonet.TCPConn, error)
- func (vt *VTun) DialUDP(ctx context.Context, network, laddr, raddr string) (conn gonnect.UDPConn, err error)
- func (vt *VTun) DialUDPAddrPort(laddr, raddr netip.AddrPort) (*gonet.UDPConn, error)
- func (vt *VTun) Down() error
- func (vt *VTun) Events() <-chan tun.Event
- func (vt *VTun) File() *os.File
- func (vt *VTun) GetDnsServers() []netip.Addr
- func (vt *VTun) InterfaceAddrs() ([]net.Addr, error)
- func (vt *VTun) InterfaceMulticastAddrs() ([]net.Addr, error)
- func (vt *VTun) Interfaces() ([]gonnect.NetworkInterface, error)
- func (vt *VTun) InterfacesByIndex(index int) ([]gonnect.NetworkInterface, error)
- func (vt *VTun) InterfacesByName(name string) ([]gonnect.NetworkInterface, error)
- func (vt *VTun) IsNative() bool
- func (vt *VTun) IsUp() (bool, error)
- func (vt *VTun) Listen(ctx context.Context, network, address string) (net.Listener, error)
- func (vt *VTun) ListenMulticastUDP(ctx context.Context, network, address string, opts gonnect.MulticastOptions) (gonnect.MulticastPacketConn, error)
- func (vt *VTun) ListenPacket(ctx context.Context, network, address string) (gonnect.PacketConn, error)
- func (vt *VTun) ListenPacketConfig(ctx context.Context, lc *gonnect.ListenConfig, network, address string) (gonnect.PacketConn, error)
- func (vt *VTun) ListenPing(laddr *PingAddr) (*PingConn, error)
- func (vt *VTun) ListenPingAddr(laddr netip.Addr) (*PingConn, error)
- func (vt *VTun) ListenTCP(ctx context.Context, network, laddr string) (listener gonnect.TCPListener, err error)
- func (vt *VTun) ListenTCPAddrPort(addr netip.AddrPort) (*gonet.TCPListener, error)
- func (vt *VTun) ListenUDP(ctx context.Context, network, laddr string) (conn gonnect.UDPConn, err error)
- func (vt *VTun) ListenUDPAddrPort(laddr netip.AddrPort) (*gonet.UDPConn, error)
- func (vt *VTun) ListenUDPConfig(ctx context.Context, lc *gonnect.ListenConfig, network, laddr string) (gonnect.UDPConn, error)
- func (vt *VTun) LocalAddrs() []netip.Addr
- func (vt *VTun) LookupAddr(ctx context.Context, addr string) (names []string, err error)
- func (vt *VTun) LookupCNAME(ctx context.Context, host string) (cname string, err error)
- func (vt *VTun) LookupHost(ctx context.Context, host string) ([]string, error)
- func (vt *VTun) LookupIP(ctx context.Context, network, address string) ([]net.IP, error)
- func (vt *VTun) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
- func (vt *VTun) LookupMX(ctx context.Context, name string) ([]*net.MX, error)
- func (vt *VTun) LookupNS(ctx context.Context, name string) ([]*net.NS, error)
- func (vt *VTun) LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
- func (vt *VTun) LookupPort(ctx context.Context, network, service string) (port int, err error)
- func (vt *VTun) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)
- func (vt *VTun) LookupTXT(ctx context.Context, name string) ([]string, error)
- func (vt *VTun) MRO() int
- func (vt *VTun) MTU() (int, error)
- func (vt *VTun) MWO() int
- func (vt *VTun) Name() (string, error)
- func (vt *VTun) PacketDial(ctx context.Context, network, raddr string) (conn gonnect.PacketConn, err error)
- func (vt *VTun) Read(buf [][]byte, sizes []int, offset int) (int, error)
- func (vt *VTun) SetDnsServers(servers []netip.Addr)
- func (vt *VTun) SetLookup(fn gonnect.LookupIP)
- func (vt *VTun) SetMTU(mtu int) error
- func (vt *VTun) SetSourceRoutes(routes []SourceRoute) error
- func (vt *VTun) SourceRoutes() []SourceRoute
- func (vt *VTun) Up() error
- func (vt *VTun) Write(buf [][]byte, offset int) (int, error)
- func (vt *VTun) WriteNotify()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Opts ¶
type Opts struct {
// EndpointCh specifies the size of the endpoint channel buffer. Defaults to 1024.
EndpointCh int
// EventCh specifies the size of the event channel buffer. Defaults to 10.
EventCh int
// LocalAddrs contains the local IP addresses to assign to the tunnel.
// If not provided, a random address from rarely used subnets will be generated.
LocalAddrs []netip.Addr
// SourceRoutes selects a local source address from the remote destination.
// Routes are independent for IPv4 and IPv6, and the longest prefix wins.
// A family with source routes has no implicit default route.
SourceRoutes []SourceRoute
// DnsServers contains the DNS servers to use for name resolution.
// Default: 8.8.8.8, 8.8.4.4, 1.1.1.1, 1.0.0.1, 9.9.9.9
DnsServers []netip.Addr
// Lookup provides a custom DNS lookup function.
// If not set, uses the built-in simple DNS resolver.
Lookup gonnect.LookupIP
// TCPFallbackDelay is the time that Dial and DialTCP wait before they
// start the next resolved TCP candidate. The default is 250 ms. A negative
// value disables timed fallback, but a definitive failure still starts the
// next candidate immediately.
TCPFallbackDelay time.Duration
// Name specifies the name of the tunnel interface. Default: "vtun".
Name string
// NoLoopbackAddr prevents adding loopback addresses (127.0.0.1 and ::1) to the
// local addresses. This is useful when using the spoofer to prevent "martian
// packet" errors when the stack chooses a loopback address as the source.
NoLoopbackAddr bool
// NetStackOpts provides additional netstack configuration options.
NetStackOpts *helpers.Opts
MWO, MRO int
}
Opts contains configuration options for creating a VTun virtual tunnel.
type PingAddr ¶
PingAddr represents an ICMP ping address.
type PingConn ¶
type PingConn struct {
// contains filtered or unexported fields
}
PingConn sends ICMP Echo Request messages and reads Echo Reply payloads.
func (*PingConn) ReadFrom ¶
ReadFrom reads an ICMP Echo Reply payload and returns the remote address.
func (*PingConn) RemoteAddr ¶
RemoteAddr returns the remote address of the ping connection.
func (*PingConn) SetDeadline ¶
SetDeadline sets both read and write deadlines for the ping connection.
func (*PingConn) SetReadDeadline ¶
SetReadDeadline sets the read deadline for the ping connection.
func (*PingConn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline for the ping connection (unimplemented).
type SourceRoute ¶ added in v0.6.0
type SourceRoute struct {
// Destination is the remote prefix that selects Source. VTun masks this
// prefix before it compares or installs the route.
Destination netip.Prefix
// Source is an address from Opts.LocalAddrs.
Source netip.Addr
}
SourceRoute selects a preferred local source address for a destination prefix. VTun uses the route with the longest matching destination prefix.
Example (DualStack) ¶
package main
import (
"fmt"
"net/netip"
"github.com/asciimoth/gonnect-netstack/vtun"
)
func main() {
options := vtun.Opts{
LocalAddrs: []netip.Addr{
netip.MustParseAddr("10.0.0.2"),
netip.MustParseAddr("2001:db8::2"),
},
SourceRoutes: []vtun.SourceRoute{
{Destination: netip.MustParsePrefix("0.0.0.0/0"), Source: netip.MustParseAddr("10.0.0.2")},
{Destination: netip.MustParsePrefix("::/0"), Source: netip.MustParseAddr("2001:db8::2")},
},
}
fmt.Println(len(options.SourceRoutes))
}
Output: 2
type VTun ¶
type VTun struct {
// contains filtered or unexported fields
}
VTun represents a virtual tunnel network interface built on gVisor's netstack. It provides TCP/UDP dialing and listening capabilities, DNS resolution, ICMP ping support, and implements the gonnect Network and Resolver interfaces. It was originally borrowed from wireguard-go and then significantly modified to fit the gonnect ecosystem.
func (*VTun) BatchSize ¶
BatchSize returns the preferred number of packets that can be read or written in a single call. For netTun, this is always 1.
func (*VTun) Close ¶
Close closes the netTun device by removing the NIC, closing the stack, removing notifications, closing the endpoint, and closing the channels.
func (*VTun) Dial ¶
Dial connects to address through the VTun. TCP names use the same dual-stack connection race as DialTCP. UDP behavior is unchanged.
func (*VTun) DialPing ¶
DialPing creates an ICMP ping connection with the specified local and remote PingAddr.
func (*VTun) DialPingAddr ¶
DialPingAddr creates an ICMP ping connection with the specified local and remote addresses. Writes send Echo Request messages. Reads return Echo Reply payloads.
func (*VTun) DialTCP ¶
func (vt *VTun) DialTCP( ctx context.Context, network, laddr, raddr string, ) (gonnect.TCPConn, error)
DialTCP establishes a TCP connection. For a name with addresses from both IP families, it races the resolved candidates and returns the first connection that completes its TCP handshake. It starts the next candidate after TCPFallbackDelay, or immediately after a definitive failure. The caller context is the time limit for the complete race.
The tcp4 and tcp6 networks use only the requested family. An IP literal is dialed once without a DNS lookup. A specific laddr overrides the source route selected for the remote destination and also restricts the race to the local address family.
func (*VTun) DialTCPAddrPort ¶
DialTCPAddrPort starts a TCP connection to a numeric address. This low-level method returns the endpoint after gVisor starts the connect operation. Use DialTCP when success must mean that the TCP handshake is complete. When a source policy is active, the destination selects the local address.
func (*VTun) DialUDPAddrPort ¶
DialUDPAddrPort establishes a UDP connection with the specified local and remote addresses and ports. The connection can be used for sending and receiving UDP packets through the VTun's network stack.
func (*VTun) File ¶
File returns nil as the netTun device does not have an associated file descriptor.
func (*VTun) GetDnsServers ¶
GetDnsServers returns the list of configured DNS servers.
func (*VTun) InterfaceMulticastAddrs ¶ added in v0.4.15
func (*VTun) Interfaces ¶
func (vt *VTun) Interfaces() ([]gonnect.NetworkInterface, error)
func (*VTun) InterfacesByIndex ¶
func (vt *VTun) InterfacesByIndex(index int) ([]gonnect.NetworkInterface, error)
func (*VTun) InterfacesByName ¶
func (vt *VTun) InterfacesByName(name string) ([]gonnect.NetworkInterface, error)
func (*VTun) ListenMulticastUDP ¶ added in v0.4.15
func (vt *VTun) ListenMulticastUDP( ctx context.Context, network, address string, opts gonnect.MulticastOptions, ) (gonnect.MulticastPacketConn, error)
func (*VTun) ListenPacket ¶
func (*VTun) ListenPacketConfig ¶ added in v0.4.15
func (vt *VTun) ListenPacketConfig( ctx context.Context, lc *gonnect.ListenConfig, network, address string, ) (gonnect.PacketConn, error)
func (*VTun) ListenPing ¶
ListenPing creates an ICMP ping listener bound to the specified local PingAddr.
func (*VTun) ListenPingAddr ¶
ListenPingAddr creates an ICMP ping connection bound to the specified local address.
func (*VTun) ListenTCPAddrPort ¶
ListenTCPAddrPort listens for incoming TCP connections on the specified address and port. Supports wildcard binding:
- 0.0.0.0:port or :port binds to first local IPv4 address
- [::]:port binds to first local IPv6 address
func (*VTun) ListenUDPAddrPort ¶
ListenUDPAddrPort listens for incoming UDP packets on the specified local address and port. Supports wildcard binding:
- 0.0.0.0:port or :port binds to first local IPv4 address
- [::]:port binds to first local IPv6 address
- Port-only addresses (e.g., netip.AddrPortFrom(netip.IPv4Unspecified(), 53)) bind to first local address
func (*VTun) ListenUDPConfig ¶ added in v0.4.15
func (*VTun) LocalAddrs ¶
LocalAddrs returns a copy of the local addresses configured on this VTun. This is useful for dial operations where you need to know the VTun's address instead of using hardcoded constants.
func (*VTun) LookupAddr ¶
func (*VTun) LookupCNAME ¶
func (*VTun) LookupHost ¶
LookupHost performs a DNS lookup for host and returns the addresses that the VTun protocol configuration supports. It resolves A and AAAA records in parallel when both families are enabled. The result order is not a carrier reachability decision. Dial and DialTCP schedule and race TCP candidates.
func (*VTun) LookupIPAddr ¶
func (*VTun) LookupNetIP ¶
func (*VTun) LookupPort ¶
func (*VTun) PacketDial ¶ added in v0.3.3
func (*VTun) Read ¶
Read reads a single packet from the incomingPacket channel and writes it to the first buffer. It returns 1 for one packet read and the size of the packet.
func (*VTun) SetDnsServers ¶
SetDnsServers configures the DNS servers to use for name resolution.
func (*VTun) SetMTU ¶ added in v0.6.2
SetMTU changes the netstack NIC MTU without replacing the VTun. It updates the value reported by MTU and emits EventMTUUpdate after a material change.
func (*VTun) SetSourceRoutes ¶ added in v0.6.0
func (vt *VTun) SetSourceRoutes(routes []SourceRoute) error
SetSourceRoutes replaces the source-route table for new flows. The method validates the complete new table before it changes the active table. Existing TCP, connected UDP, and connected ICMP flows keep their selected sources.
func (*VTun) SourceRoutes ¶ added in v0.6.0
func (vt *VTun) SourceRoutes() []SourceRoute
SourceRoutes returns a copy of the active, normalized source-route table.
func (*VTun) Write ¶
Write writes packets to the device endpoint. It determines the IP version from the first nibble of each packet and injects it as an inbound packet to the appropriate protocol handler.
func (*VTun) WriteNotify ¶
func (vt *VTun) WriteNotify()
WriteNotify is called when the endpoint has data available. It reads a packet from the endpoint and sends it to the incomingPacket channel.