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 protocols, with built-in DNS resolution capabilities. VTun implements multiple gonnect interfaces including Network, Resolver, InterfaceNetwork, UpDown, and tun.Tun.
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 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) (conn gonnect.TCPConn, err 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) 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) ListenPacket(ctx context.Context, 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) 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) Up() error
- func (vt *VTun) Write(buf [][]byte, offset int) (int, error)
- func (vt *VTun) WriteNotify()
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
// 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
// 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 represents a connection for sending and receiving ICMP ping packets.
func (*PingConn) ReadFrom ¶
ReadFrom reads data from the ping connection 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 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) 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. This can be used to send and receive ICMP echo requests and replies.
func (*VTun) DialTCP ¶
func (vt *VTun) DialTCP( ctx context.Context, network, laddr, raddr string, ) (conn gonnect.TCPConn, err error)
DialTCPAddrPort establishes a TCP connection to the specified address and port. Laddr is always ignored.
func (*VTun) DialTCPAddrPort ¶
DialTCPAddrPort establishes a TCP connection to the specified address and port. The connection is created through the VTun's network stack.
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) 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) ListenPacket ¶
func (*VTun) ListenPing ¶
ListenPing creates an ICMP ping listener bound to the specified local PingAddr.
func (*VTun) ListenPingAddr ¶
ListenPingAddr creates an ICMP ping listener bound to the specified local address. It can be used to receive ICMP echo requests and send replies.
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) 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 the given host name and returns a list of IP addresses. It resolves both A and AAAA records in parallel if both IPv4 and IPv6 are enabled on the VTun.
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) 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.