godivert

package module
v0.0.0-...-a78d3b8 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

README

GoDivert

A high-performance, pure Go implementation of the WinDivert user-mode library. This package allows you to capture and inject network packets on Windows without the complexity of CGO or external C dependencies.

Features

🚀 Zero CGO Dependency: Built entirely in pure Go using syscall/windows APIs. No GCC or MinGW required for your build pipeline.

🐹 Idiomatic Go: Designed with a Go-first mentality. Includes friendly error handling and structured types instead of raw C pointers.

📦 Embedded Driver: Utilizes go:embed to bundle the .sys drivers directly into your binary. No more manual file management.

⚡ Optimized Loading: Advanced driver loading and unloading logic to ensure stability and prevent resource leaks.

📚 Educational & Clean: Highly readable codebase designed to be a learning resource for Windows kernel-mode/user-mode communication in Go.

Quick Start

Installation
go get github.com/one-api/godivert
simple example
package main

import (
	"github.com/one-api/godivert"
)

func main() {

	// Open driver
	filter := "!loopback and (tcp or udp)"
	divert, err := godivert.New(filter, godivert.LayerNetwork, 0, 0)
	if err != nil {
		panic(err)
	}
	defer divert.Close()

	// Buffer for packet data
	packet := make([]byte, 2000)
	var addr godivert.Address

	// Recv Send Loop
	for i := 0; ; i++ {
		readLen, err := divert.Recv(packet, &addr)
		if err != nil {
			panic(err)
		}
		_, err = divert.Send(packet[:readLen], &addr)
		if err != nil {
			panic(err)
		}
	}
}

Note: This demo code requires Administrator Privileges to run

Filter Syntax Examples

// Match outbound TCP traffic on port 443
outbound && tcp.DstPort == 443

// Match DNS queries
udp.DstPort == 53

// Inbound HTTP traffic
inbound && (tcp.DstPort == 80 || tcp.DstPort == 8080)

Testing

Run tests with:

go test ./...

References

Acknowledgments

This project is a Go implementation based on the excellent work of Basil and the WinDivert project.

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). See the LICENSE file for details.

This project embeds the WinDivert32.sys and WinDivert64.sys binaries, which are part of the WinDivert project. These binaries are licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0). Copyright (c) Basil. For more information, please refer to the LICENSE-WINDIVERT file.

Contact

If you have any questions, feedback, please feel free to reach out:

Documentation

Index

Constants

View Source
const (
	LayerNetwork        = types.LayerNetwork
	LayerNetworkForward = types.LayerNetworkForward
	LayerFlow           = types.LayerFlow
	LayerSocket         = types.LayerSocket
	LayerReflect        = types.LayerReflect
	LayerMax            = types.LayerMax
)
View Source
const (
	FlagSniff     = types.FlagSniff
	FlagDrop      = types.FlagDrop
	FlagRecvOnly  = types.FlagRecvOnly
	FlagReadOnly  = types.FlagReadOnly
	FlagSendOnly  = types.FlagSendOnly
	FlagWriteOnly = types.FlagWriteOnly
	FlagNoInstall = types.FlagNoInstall
	FlagFragments = types.FlagFragments
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address = types.Address

type Divert

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

func New

func New(filter string, layer Layer, priority int16, flags Flag) (*Divert, error)

func NewWithName

func NewWithName(name string, filter string, layer Layer, priority int16, flags Flag) (*Divert, error)

func (*Divert) Close

func (d *Divert) Close() error

func (*Divert) Recv

func (d *Divert) Recv(buffer []byte, address *Address) (uint32, error)

func (*Divert) Send

func (d *Divert) Send(buffer []byte, address *Address) (uint32, error)

type Flag

type Flag = types.Flag

type Layer

type Layer = types.Layer

Directories

Path Synopsis
examples
demo command

Jump to

Keyboard shortcuts

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