netstackvm

package
v1.4.4-alpha1112a Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: AGPL-3.0 Imports: 51 Imported by: 0

Documentation

Index

Constants

View Source
const TUN_MTU = 3200

TUN_MTU is the default MTU for TUN device. 1420 is wg default MTU, use it for compatibility.

View Source
const UTUNINDEXSTART = 410

Variables

This section is empty.

Functions

func GetDefaultICMPClient

func GetDefaultICMPClient() *icmpClient.Client

func GetTCPPayload

func GetTCPPayload(buffer *stack.PacketBuffer) []byte

func MaskToIPString

func MaskToIPString(mask net.IPMask) string

func NewNetStackFromConfig

func NewNetStackFromConfig(c *Config) (*stack.Stack, error)

func NewPCAPAdaptor

func NewPCAPAdaptor(ifaceName string, mtu int32, promisc bool) (*pcapAdaptor, error)

NewPCAPAdaptor creates a new pcap adaptor for the given interface. It reuses existing adaptors if one already exists for the interface.

func ReadServerName

func ReadServerName(data []byte) string

Types

type AliveTargetMonitor

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

func StartTargetMonitor

func StartTargetMonitor() (*AliveTargetMonitor, error)

func (*AliveTargetMonitor) GetAliveDomain

func (m *AliveTargetMonitor) GetAliveDomain() []string

func (*AliveTargetMonitor) GetAliveIP

func (m *AliveTargetMonitor) GetAliveIP() map[string]int

type Config

type Config struct {

	// stack options
	IPv4Disabled                bool
	IPv6Disabled                bool
	DHCPDisabled                bool
	ARPDisabled                 bool
	ICMPDisabled                bool
	HandleLocal                 bool
	TCPDisabled                 bool
	UDPDisabled                 bool
	DisallowPacketEndpointWrite bool
	EnableLinkLayer             bool
	OnTCPConnectionRequested    func(*tcpip.FullAddress, *tcpip.FullAddress)
	DisableForwarding           bool

	//dhcp config
	DHCPAcquireTimeout       time.Duration
	DHCPAcquireInterval      time.Duration
	DHCPAcquireRetryInterval time.Duration
	DHCPAcquireCallback      func(ctx context.Context, lost, acquired tcpip.AddressWithPrefix, cfg gvisorDHCP.Config)

	//arp config
	ARPAnnouncementFastInterval time.Duration
	ARPAnnouncementFastTimes    int
	ARPAnnouncementSlowInterval time.Duration

	// nic options
	MainNICIPv4Address        string
	MainNICIPv4AddressNetmask string

	MainNICIPv6Address        string
	MainNICIPv6AddressNetmask string
	MainNICLinkAddress        net.HardwareAddr

	// tcp options
	// DefaultTTL specifies the default TTL used by stack
	DefaultTTL uint8
	// ICMPBurst is the number of ICMP messages that can be sent in a single burst
	ICMPBurst int
	// ICMPLimit is the maximum number of ICMP messages permitted by rate limiter
	ICMPLimit rate.Limit
	// TCPSendBufferSizeMin is the smallest size of a send buffer
	TCPSendBufferSizeMin int
	// TCPSendBufferSizeMax is the maximum permitted size of a send buffer
	TCPSendBufferSizeMax int
	// TCPSendBufferSizeDefault is the default size of the send buffer
	TCPSendBufferSizeDefault int
	// TCPReceiveBufferSizeMin is the smallest size of a receive buffer
	TCPReceiveBufferSizeMin int
	// TCPReceiveBufferSizeMax is the maximum permitted size of a receive buffer
	TCPReceiveBufferSizeMax int
	// TCPReceiveBufferSizeDefault is the default size of the receive buffer
	TCPReceiveBufferSizeDefault int
	// TCPCongestionControl is the congestion control algorithm used by TCP (reno or cubic)
	TCPCongestionControl string
	// TCPDelayEnabled enables/disables Nagle's algorithm for TCP
	TCPDelayEnabled bool
	// TCPModerateReceiveBuffer enables/disables TCP receive buffer auto-tuning
	TCPModerateReceiveBuffer bool
	// TCPSACKEnabled enables/disables TCP selective acknowledgment
	TCPSACKEnabled bool
	// TCPRACKLossDetection specifies the TCP loss detection algorithm
	TCPRACKLossDetection tcpip.TCPRecovery

	// force use system net stack, not dhcp
	ForceSystemNetStack bool
	// contains filtered or unexported fields
}

func NewDefaultConfig

func NewDefaultConfig() *Config

type NIC

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

type NetStackVirtualMachine

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

func GetDefaultSYNScanClient

func GetDefaultSYNScanClient() *NetStackVirtualMachine

func NewSystemNetStackVM

func NewSystemNetStackVM(opts ...Option) (*NetStackVirtualMachine, error)

func NewSystemNetStackVMWithoutDHCP

func NewSystemNetStackVMWithoutDHCP(entryOption ...Option) (*NetStackVirtualMachine, error)

func (*NetStackVirtualMachine) DialTCP

func (m *NetStackVirtualMachine) DialTCP(timeout time.Duration, target string) (net.Conn, error)

func (*NetStackVirtualMachine) GetEntry

func (*NetStackVirtualMachine) GetStack

func (m *NetStackVirtualMachine) GetStack() *stack.Stack

func (*NetStackVirtualMachine) ListenTCP

func (m *NetStackVirtualMachine) ListenTCP(addr string) (net.Listener, error)

func (*NetStackVirtualMachine) SetEntry

type NetStackVirtualMachineEntry

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

func NewNetStackVirtualMachineEntry

func NewNetStackVirtualMachineEntry(opts ...Option) (*NetStackVirtualMachineEntry, error)

func (*NetStackVirtualMachineEntry) AddDefaultRoute

func (vm *NetStackVirtualMachineEntry) AddDefaultRoute(gateway net.IP) error

func (*NetStackVirtualMachineEntry) AllowTCP

func (vm *NetStackVirtualMachineEntry) AllowTCP(destinationAddr string)

func (*NetStackVirtualMachineEntry) AllowTCPWithSrc

func (vm *NetStackVirtualMachineEntry) AllowTCPWithSrc(destinationAddr string, srcAddr string)

func (*NetStackVirtualMachineEntry) DialTCP

func (vm *NetStackVirtualMachineEntry) DialTCP(timeout time.Duration, hostport string) (net.Conn, error)

func (*NetStackVirtualMachineEntry) DisallowTCP

func (vm *NetStackVirtualMachineEntry) DisallowTCP(destinationAddr string)

func (*NetStackVirtualMachineEntry) DisallowTCPWithSrc

func (vm *NetStackVirtualMachineEntry) DisallowTCPWithSrc(destinationAddr string, srcAddr string)

func (*NetStackVirtualMachineEntry) GetMTU

func (vm *NetStackVirtualMachineEntry) GetMTU() int

func (*NetStackVirtualMachineEntry) GetMainNICIPv4Address

func (vm *NetStackVirtualMachineEntry) GetMainNICIPv4Address() net.IP

func (*NetStackVirtualMachineEntry) GetMainNICIPv4Gateway

func (vm *NetStackVirtualMachineEntry) GetMainNICIPv4Gateway() net.IP

func (*NetStackVirtualMachineEntry) GetMainNICIPv4Netmask

func (vm *NetStackVirtualMachineEntry) GetMainNICIPv4Netmask() *net.IPNet

func (*NetStackVirtualMachineEntry) GetMainNICLinkAddress

func (vm *NetStackVirtualMachineEntry) GetMainNICLinkAddress() net.HardwareAddr

func (*NetStackVirtualMachineEntry) GetOSNetStackIPv4

func (vm *NetStackVirtualMachineEntry) GetOSNetStackIPv4() (net.IP, net.IP, net.IPMask)

func (*NetStackVirtualMachineEntry) GetOSNetStackIPv6

func (vm *NetStackVirtualMachineEntry) GetOSNetStackIPv6() (net.IP, net.IP, net.IPMask)

func (*NetStackVirtualMachineEntry) GetPcapInterfaceNeighborRoute

func (vm *NetStackVirtualMachineEntry) GetPcapInterfaceNeighborRoute() (tcpip.Route, error)

GetPcapInterfaceNeighborRoute returns the route of the pcap interface, should call after set main nic ip

func (*NetStackVirtualMachineEntry) GetStack

func (vm *NetStackVirtualMachineEntry) GetStack() *stack.Stack

func (*NetStackVirtualMachineEntry) GetSystemInterface

func (vm *NetStackVirtualMachineEntry) GetSystemInterface() *net.Interface

func (*NetStackVirtualMachineEntry) InheritPcapInterfaceConfig

func (vm *NetStackVirtualMachineEntry) InheritPcapInterfaceConfig() error

InheritPcapInterfaceConfig inherits the IP address and route of the pcap interface( will set default route, netx hop is gateway)

func (*NetStackVirtualMachineEntry) InheritPcapInterfaceIP

func (vm *NetStackVirtualMachineEntry) InheritPcapInterfaceIP() error

InheritPcapInterfaceIP inherits the IP address of the pcap interface, set default mac address for driver(pcap endpoint)

func (*NetStackVirtualMachineEntry) InheritPcapInterfaceNeighborRoute

func (vm *NetStackVirtualMachineEntry) InheritPcapInterfaceNeighborRoute() error

InheritPcapInterfaceNeighborRoute inherits the route of the pcap interface

func (*NetStackVirtualMachineEntry) ListenTCP

func (vm *NetStackVirtualMachineEntry) ListenTCP(hostport string) (net.Listener, error)

func (*NetStackVirtualMachineEntry) MainNICID

func (vm *NetStackVirtualMachineEntry) MainNICID() tcpip.NICID

func (*NetStackVirtualMachineEntry) SetDefaultRoute

func (vm *NetStackVirtualMachineEntry) SetDefaultRoute(gateway net.IP) error

func (*NetStackVirtualMachineEntry) SetFilterForSynScan

func (vm *NetStackVirtualMachineEntry) SetFilterForSynScan()

func (*NetStackVirtualMachineEntry) SetMainNICv4

func (vm *NetStackVirtualMachineEntry) SetMainNICv4(ipAddr net.IP, netmask *net.IPNet, getaway net.IP) error

func (*NetStackVirtualMachineEntry) StartAnnounceARP

func (m *NetStackVirtualMachineEntry) StartAnnounceARP() error

func (*NetStackVirtualMachineEntry) StartDHCP

func (vm *NetStackVirtualMachineEntry) StartDHCP() error

func (*NetStackVirtualMachineEntry) Wait

func (vm *NetStackVirtualMachineEntry) Wait()

func (*NetStackVirtualMachineEntry) WaitDHCPFinished

func (t *NetStackVirtualMachineEntry) WaitDHCPFinished(ctx context.Context) error

type NetstackSniffer

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

func NewNetstackSniffer

func NewNetstackSniffer(vm *NetStackVirtualMachine) *NetstackSniffer

func (*NetstackSniffer) RegisterSniffHandle

func (m *NetstackSniffer) RegisterSniffHandle(protocol tcpip.TransportProtocolNumber, handle func(*stack.PacketBuffer))

type Option

type Option func(*Config) error

func WithARPDisabled

func WithARPDisabled(disabled bool) Option

func WithContext

func WithContext(ctx context.Context) Option

func WithDHCPDisabled

func WithDHCPDisabled(disabled bool) Option

func WithDisableForwarding

func WithDisableForwarding(v bool) Option

func WithDisallowPacketEndpointWrite

func WithDisallowPacketEndpointWrite(disallow bool) Option

func WithEnableLinkLayer

func WithEnableLinkLayer(enable bool) Option

func WithForceSystemNetStack

func WithForceSystemNetStack(force bool) Option

func WithHandleLocal

func WithHandleLocal(handleLocal bool) Option

func WithICMPDisabled

func WithICMPDisabled(disabled bool) Option

func WithIPv4Disabled

func WithIPv4Disabled(disabled bool) Option

func WithIPv6Disabled

func WithIPv6Disabled(disabled bool) Option

func WithMainNICIPAddress

func WithMainNICIPAddress(ipAddress string) Option

func WithMainNICLinkAddress

func WithMainNICLinkAddress(linkAddress string) Option

func WithNetStack

func WithNetStack(netStack *stack.Stack) Option

func WithOnTCPConnectionRequested

func WithOnTCPConnectionRequested(fn func(*tcpip.FullAddress, *tcpip.FullAddress)) Option

func WithPCAPInboundFilter

func WithPCAPInboundFilter(filter func(packet gopacket.Packet) bool) Option

func WithPCAPOutboundFilter

func WithPCAPOutboundFilter(filter func(packet gopacket.Packet) bool) Option

func WithPcapCapabilities

func WithPcapCapabilities(capabilities stack.LinkEndpointCapabilities) Option

func WithPcapDevice

func WithPcapDevice(device string) Option

func WithPcapPromisc

func WithPcapPromisc(promisc bool) Option

func WithRandomMainNICLinkAddress

func WithRandomMainNICLinkAddress() Option

func WithTCPDisabled

func WithTCPDisabled(disabled bool) Option

func WithUDPDisabled

func WithUDPDisabled(disabled bool) Option

type PCAPEndpoint

type PCAPEndpoint struct {
	*channel.Endpoint
	// contains filtered or unexported fields
}

func NewPCAPEndpoint

func NewPCAPEndpoint(ctx context.Context, stackIns *stack.Stack, device string, macAddr net.HardwareAddr, promisc bool) (*PCAPEndpoint, error)

func (*PCAPEndpoint) AllowTCP

func (p *PCAPEndpoint) AllowTCP(addr string)

func (*PCAPEndpoint) AllowTCPWithSrc

func (p *PCAPEndpoint) AllowTCPWithSrc(addr string, src string)

func (*PCAPEndpoint) Attach

func (p *PCAPEndpoint) Attach(dispatcher stack.NetworkDispatcher)

func (*PCAPEndpoint) Capabilities

func (p *PCAPEndpoint) Capabilities() stack.LinkEndpointCapabilities

func (*PCAPEndpoint) Close

func (p *PCAPEndpoint) Close()

func (*PCAPEndpoint) DisallowTCP

func (p *PCAPEndpoint) DisallowTCP(addr string)

func (*PCAPEndpoint) DisallowTCPWithSrc

func (p *PCAPEndpoint) DisallowTCPWithSrc(addr string, src string)

func (*PCAPEndpoint) SetCapabilities

func (p *PCAPEndpoint) SetCapabilities(flag stack.LinkEndpointCapabilities)

func (*PCAPEndpoint) SetGatewayHardwareAddr

func (p *PCAPEndpoint) SetGatewayHardwareAddr(hwAddr net.HardwareAddr)

func (*PCAPEndpoint) SetGatewayIP

func (p *PCAPEndpoint) SetGatewayIP(g net.IP)

func (*PCAPEndpoint) SetLoopback

func (p *PCAPEndpoint) SetLoopback(b bool)

func (*PCAPEndpoint) SetPCAPInboundFilter

func (p *PCAPEndpoint) SetPCAPInboundFilter(filter func(packet gopacket.Packet) bool)

func (*PCAPEndpoint) SetPCAPOutboundFilter

func (p *PCAPEndpoint) SetPCAPOutboundFilter(filter func(packet gopacket.Packet) bool)

func (*PCAPEndpoint) Wait

func (p *PCAPEndpoint) Wait()

type Record

type Record struct {
	IPAddr  string `json:"ip_addr"`  // IP地址
	TunName string `json:"tun_name"` // 隧道名称
}

Record 记录路由管理的记录

type SystemRouteManager

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

SystemRouteManager 系统路由管理器,单例模式 只管理通过AddIPRoute添加的路由记录,不涉及系统默认路由的获取

func GetSystemRouteManager

func GetSystemRouteManager() *SystemRouteManager

GetSystemRouteManager 获取系统路由管理器的单例实例

func (*SystemRouteManager) AddIPRoute

func (m *SystemRouteManager) AddIPRoute(ipAddrs interface{}, tunName string) error

AddIPRoute 添加IP路由 ipAddrs 可以是 []string 或单个 string,在 Yaklang 中使用更方便

func (*SystemRouteManager) DeleteIPRoute

func (m *SystemRouteManager) DeleteIPRoute(ipAddrs interface{}) error

DeleteIPRoute 删除IP路由 ipAddrs 可以是 []string 或单个 string,在 Yaklang 中使用更方便

func (*SystemRouteManager) DeleteRoutesForInterface

func (m *SystemRouteManager) DeleteRoutesForInterface(interfaceName string) error

DeleteRoutesForInterface 删除指定接口的所有路由

func (*SystemRouteManager) GetExistedManagedSystemTableRoute

func (m *SystemRouteManager) GetExistedManagedSystemTableRoute() []*Record

GetExistedManagedSystemTableRoute 获取已存在的管理路由记录

type TCPAliveInfo

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

type TCPKey

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

func (*TCPKey) Key

func (t *TCPKey) Key() string

type TunSpoofingListener

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

func NewTunSpoofingListener

func NewTunSpoofingListener(ctx context.Context, c chan net.Conn) *TunSpoofingListener

func (*TunSpoofingListener) Accept

func (t *TunSpoofingListener) Accept() (net.Conn, error)

func (*TunSpoofingListener) Addr

func (t *TunSpoofingListener) Addr() net.Addr

func (*TunSpoofingListener) Close

func (t *TunSpoofingListener) Close() error

type TunVirtualMachine

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

func NewTunVirtualMachine

func NewTunVirtualMachine(ctx context.Context) (*TunVirtualMachine, error)

func NewTunVirtualMachineFromDevice

func NewTunVirtualMachineFromDevice(ctx context.Context, device tun.Device) (*TunVirtualMachine, error)

NewTunVirtualMachineFromDevice creates a TUN virtual machine from an existing device. It uses the provided device to create a network stack virtual machine.

func (*TunVirtualMachine) Close

func (t *TunVirtualMachine) Close() error

func (*TunVirtualMachine) GetListener

func (t *TunVirtualMachine) GetListener() *TunSpoofingListener

func (*TunVirtualMachine) GetStack

func (t *TunVirtualMachine) GetStack() *stack.Stack

GetStack returns the network stack instance.

func (*TunVirtualMachine) GetTunnelDevice

func (t *TunVirtualMachine) GetTunnelDevice() tun.Device

GetTunnelDevice returns the tunnel device instance.

func (*TunVirtualMachine) GetTunnelName

func (t *TunVirtualMachine) GetTunnelName() string

func (*TunVirtualMachine) HijackDomain

func (t *TunVirtualMachine) HijackDomain(domain string) error

func (*TunVirtualMachine) HijackIP

func (t *TunVirtualMachine) HijackIP(ip string) error

func (*TunVirtualMachine) HijackIPNet

func (t *TunVirtualMachine) HijackIPNet(ipNet *net.IPNet) error

func (*TunVirtualMachine) ListenTCP

func (t *TunVirtualMachine) ListenTCP() (*TunVmTCPListener, error)

func (*TunVirtualMachine) SetHijackTCPHandler

func (t *TunVirtualMachine) SetHijackTCPHandler(handle func(conn netstack.TCPConn)) error

func (*TunVirtualMachine) StartToMergeTCPConnectionChannel

func (t *TunVirtualMachine) StartToMergeTCPConnectionChannel(ctx context.Context, existedChannel chan net.Conn) error

StartToMergeTCPConnectionChannel starts merging TCP connections from TUN device to an external channel. This is a high-level wrapper of SetHijackTCPHandler that uses an external channel + ctx for safety. The channel can be read from other places (e.g., crep MITM system). existedChannel should be of type chan net.Conn.

type TunVmTCPListener

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

func (*TunVmTCPListener) Accept

func (t *TunVmTCPListener) Accept() (netstack.TCPConn, error)

func (*TunVmTCPListener) Close

func (t *TunVmTCPListener) Close() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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