netstackgo

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: GPL-3.0 Imports: 17 Imported by: 0

README

netstackgo

An easy-to-use netstack, wrapped by gvisor and wireguard-go, which supports macOS/Linux/Windows.

go get -u netstackgo

usage

type myHandler struct{}

func (*myHandler) HandleTCPConn(info *netstackgo.ConnTuple, conn net.Conn) {
	log.Printf("tcp, src: %s, dst: %s", info.Src(), info.Dst())
	// do something...
}
func (*myHandler) HandleUDPConn(info *netstackgo.ConnTuple, conn net.PacketConn) {
	log.Printf("udp, src: %s, dst: %s", info.Src(), info.Dst())
	// do something...
}

func main() {
	nt := netstackgo.New(tun.TunConfig{
		Name: "tun2",
		Addr: "192.18.0.1/16",
		MTU:  tun.DefaultMTU,
	})
	if err := nt.Start(); err != nil {
		log.Fatal(err)
	}
	defer nt.Close()
	nt.RegisterConnHandler(&myHandler{})
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, syscall.SIGINT)
	<-interrupt
}

PS: Windows user requires downloading wintun.dll from https://www.wintun.net

credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnHandler

type ConnHandler interface {
	HandleTCPConn(*ConnTuple, net.Conn)
	HandleUDPConn(*ConnTuple, net.PacketConn)
}

type ConnTuple

type ConnTuple struct {
	SrcIP   netip.Addr
	SrcPort uint16
	DstIP   netip.Addr
	DstPort uint16
}

func (*ConnTuple) Dst

func (t *ConnTuple) Dst() string

func (*ConnTuple) DstAddrPort

func (t *ConnTuple) DstAddrPort() netip.AddrPort

func (*ConnTuple) Src

func (t *ConnTuple) Src() string

type TunNetstack

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

func New

func New(tunCfg tun.TunConfig) *TunNetstack

func (*TunNetstack) Close

func (ns *TunNetstack) Close() error

func (*TunNetstack) RegisterConnHandler

func (ns *TunNetstack) RegisterConnHandler(handler ConnHandler)

func (*TunNetstack) Start

func (ns *TunNetstack) Start() (err error)

Directories

Path Synopsis
tun
core/device/iobased
Package iobased provides the implementation of io.ReadWriter based data-link layer endpoints.
Package iobased provides the implementation of io.ReadWriter based data-link layer endpoints.

Jump to

Keyboard shortcuts

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