divert

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

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 5 Imported by: 3

README

go-divert

golang client for windivert

Documnet

Example:
package main

import (
    "fmt"
    "log"

    "github.com/lysShub/divert-go"
    "gvisor.dev/gvisor/pkg/tcpip/header" // go get gvisor.dev/gvisor@go
)

func main() {
    divert.MustLoad(divert.DLL)
    defer divert.Release()

    d, err := divert.Open("tcp.Syn and !loopback", divert.Network, 0, divert.Sniff|divert.ReadOnly)
    if err != nil {
        log.Fatal(err)
    }

    var b = make([]byte, 1536)
    var addr divert.Address
    for {
        n, err := d.Recv(b[:cap(b)], &addr)
        if err != nil {
            if errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) {
                continue
            }
            log.Fatal(err)
        }

        if !addr.IPv6() {
            if n >= header.IPv4MinimumSize+header.TCPMinimumSize {
                iphdr := header.IPv4(b[:n])
                tcphdr := header.TCP(iphdr[iphdr.HeaderLength():])

                fmt.Printf("%s:%d --> %s:%d \n",
                    iphdr.SourceAddress().String(),
                    tcphdr.SourcePort(),
                    iphdr.DestinationAddress().String(),
                    tcphdr.DestinationPort(),
                )
            }
        } else {
            if n >= header.IPv6MinimumSize+header.TCPMinimumSize {
                iphdr := header.IPv6(b[:n])
                tcphdr := header.TCP(iphdr[header.IPv6MinimumSize:])

                fmt.Printf("%s:%d --> %s:%d \n",
                    iphdr.SourceAddress().String(),
                    tcphdr.SourcePort(),
                    iphdr.DestinationAddress().String(),
                    tcphdr.DestinationPort(),
                )
            }
        }
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address struct {
	Timestamp int64

	Layer // Packet's layer.
	Event // Packet event.

	// UINT32 Sniffed : 1;                   /* Packet was sniffed? */
	// UINT32 Outbound : 1;                  /* Packet is outound? */
	// UINT32 Loopback : 1;                  /* Packet is loopback? */
	// UINT32 Impostor : 1;                  /* Packet is impostor? */
	// UINT32 IPv6 : 1;                      /* Packet is IPv6? */
	// UINT32 IPChecksum : 1;                /* Packet has valid IPv4 checksum? */
	// UINT32 TCPChecksum : 1;               /* Packet has valid TCP checksum? */
	// UINT32 UDPChecksum : 1;               /* Packet has valid UDP checksum? */
	Flags
	// contains filtered or unexported fields
}

func (*Address) Flow

func (a *Address) Flow() *DataFlow

func (*Address) Network

func (a *Address) Network() *DateNetwork

func (*Address) Reflect

func (a *Address) Reflect() *DataReflect

func (*Address) Socket

func (a *Address) Socket() *DataSocket

type DataFlow

type DataFlow struct {
	EndpointId       uint64 // Endpoint ID.
	ParentEndpointId uint64 // Parent endpoint ID.
	ProcessId        uint32 // Process ID.

	LocalPort  uint16 // Local port.
	RemotePort uint16 // Remote port.
	Protocol   Proto  // Protocol.
	// contains filtered or unexported fields
}

func (*DataFlow) LocalAddr

func (d *DataFlow) LocalAddr() netip.Addr

func (*DataFlow) LocalAddrPort

func (d *DataFlow) LocalAddrPort() netip.AddrPort

func (*DataFlow) RemoteAddr

func (d *DataFlow) RemoteAddr() netip.Addr

func (*DataFlow) RemoteAddrPort

func (d *DataFlow) RemoteAddrPort() netip.AddrPort

type DataReflect

type DataReflect struct {
	Timestamp int64  // Handle open time.
	ProcessId uint32 // Handle process ID.
	Layer     Layer  // Handle layer.
	Flags     uint64 // Handle flags.
	Priority  int16  // Handle priority.
}

type DataSocket

type DataSocket = DataFlow

type DateNetwork

type DateNetwork struct {
	IfIdx    uint32 // Packet's interface index.
	SubIfIdx uint32 // Packet's sub-interface index.
}

type ErrClosed

type ErrClosed struct{}

func (ErrClosed) Error

func (ErrClosed) Error() string

type ErrLoaded

type ErrLoaded struct{}

func (ErrLoaded) Error

func (ErrLoaded) Error() string

func (ErrLoaded) Temporary

func (ErrLoaded) Temporary() bool

type ErrNotLoad

type ErrNotLoad struct{}

func (ErrNotLoad) Error

func (ErrNotLoad) Error() string

type ErrShutdown

type ErrShutdown struct{}

func (ErrShutdown) Error

func (ErrShutdown) Error() string

type Event

type Event uint8
const (
	NetworkPacket  Event = iota /* Network packet. */
	FlowEstablishd              /* Flow established. */
	FlowDeleted                 /* Flow deleted. */
	SocketBind                  /* Socket bind. */
	SocketConnect               /* Socket connect. */
	SocketListen                /* Socket listen. */
	SocketAccept                /* Socket accept. */
	SocketClose                 /* Socket close. */
	ReflectOpen                 /* WinDivert handle opened. */
	ReflectClose                /* WinDivert handle closed. */
)

func (Event) Layer

func (e Event) Layer() Layer

func (Event) Op

func (e Event) Op() string

func (Event) String

func (e Event) String() string

type Flag

type Flag uint64
const (
	Sniff     Flag = 0x0001 // copy data, like pcap
	Drop      Flag = 0x0002
	RecvOnly  Flag = 0x0004
	ReadOnly  Flag = RecvOnly
	SendOnly  Flag = 0x0008
	WriteOnly Flag = SendOnly
	NoInstall Flag = 0x0010
	Fragments Flag = 0x0020
)

type Flags

type Flags uint8

func (Flags) IPChecksum

func (f Flags) IPChecksum() bool

func (Flags) IPv6

func (f Flags) IPv6() bool

func (Flags) Impostor

func (f Flags) Impostor() bool

func (Flags) Loopback

func (f Flags) Loopback() bool

func (Flags) Outbound

func (f Flags) Outbound() bool

func (*Flags) SetIPChecksum

func (f *Flags) SetIPChecksum(sum bool)

func (*Flags) SetIPv6

func (f *Flags) SetIPv6(ipv6 bool)

func (*Flags) SetImpostor

func (f *Flags) SetImpostor(impostor bool)

func (*Flags) SetLoopback

func (f *Flags) SetLoopback(loop bool)

func (*Flags) SetOutbound

func (f *Flags) SetOutbound(out bool)

func (*Flags) SetTCPChecksum

func (f *Flags) SetTCPChecksum(sum bool)

func (*Flags) SetUDPChecksum

func (f *Flags) SetUDPChecksum(sum bool)

func (Flags) Sniffed

func (f Flags) Sniffed() bool

func (Flags) TCPChecksum

func (f Flags) TCPChecksum() bool

func (Flags) UDPChecksum

func (f Flags) UDPChecksum() bool

type Layer

type Layer uint8
const (
	Network Layer = iota
	NetworkForward
	Flow
	Socket
	Reflect
	Unknown
)

func (Layer) String

func (i Layer) String() string

type Mem

type Mem struct {
	DLL []byte
	Sys []byte
}

type PARAM

type PARAM uint32
const (
	QueueLength  PARAM = iota /* Packet queue length. */
	QueueTime                 /* Packet queue time. */
	QueueSize                 /* Packet queue size. */
	VersionMajor              /* Driver version (major). */
	VersionMinor              /* Driver version (minor). */
)

type Proto

type Proto uint8
const (
	HOPOPTS  Proto = 0  // hopopts
	ICMP     Proto = 1  // icmp
	TCP      Proto = 6  // tcp
	UDP      Proto = 17 // udp
	ROUTING  Proto = 43 // routing
	FRAGMENT Proto = 44 // fragment
	AH       Proto = 51 // ah
	ICMPV6   Proto = 58 // icmp6
	NONE     Proto = 59 // none
	DSTOPTS  Proto = 60 // dstopts
)

func (Proto) String

func (i Proto) String() string

type Shutdown

type Shutdown uint32
const (
	Recv Shutdown = iota + 1 /* Shutdown recv. */
	Send                     /* Shutdown send. */
	Both                     /* Shutdown recv and send. */
)

Jump to

Keyboard shortcuts

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