route

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 3 Imported by: 4

Documentation

Overview

Package route implements basic api to interact with system IP tables.

Add and Delete operations are implemented for gateway/interface addresses.

Example (GatewayIP)
package main

import (
	"log"
	"net"

	"github.com/goxray/core/network/route"
)

func main() {
	routes := []*route.Addr{
		route.MustParseAddr("93.184.215.14"), // example.com IP address
	}

	opts := route.Opts{Gateway: net.IP{192, 0, 0, 1}, Routes: routes}

	// Route to 192.0.0.1
	if err := route.Add(opts); err != nil {
		log.Fatal(err)
	}

	// Remove route from the table
	if err := route.Delete(opts); err != nil {
		log.Fatal(err)
	}
}
Example (NetworkInterface)
package main

import (
	"log"

	"github.com/goxray/core/network/route"
)

func main() {
	routes := []*route.Addr{
		route.MustParseAddr("93.184.215.14"), // example.com IP address
	}

	opts := route.Opts{IfName: "en0", Routes: routes}

	// Route to default Mac OS interface `en0`
	if err := route.Add(opts); err != nil {
		log.Fatal(err)
	}

	// Remove route from the table
	if err := route.Delete(opts); err != nil {
		log.Fatal(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(options Opts) error

Add adds route to ip table.

func Delete

func Delete(options Opts) error

Delete deletes route from ip table.

Types

type Addr

type Addr net.IPNet

Addr represents IP and Port.

func MustParseAddr

func MustParseAddr(addr string) *Addr

MustParseAddr is the same as ParseAddr but panics on errors.

func ParseAddr

func ParseAddr(addr string) (*Addr, error)

ParseAddr parses the given addr and transforms it into Addr.

First it tries to parse it as plain net.IP addr if no mask present.

func (*Addr) String

func (a *Addr) String() string

type Opts

type Opts struct {
	IfName  string
	Gateway net.IP
	Routes  []*Addr
}

Opts represent route action options. You must specify either IfName or Gateway, specifying both will trigger Validate error.

func (Opts) Validate

func (o Opts) Validate() error

type Route

type Route struct{}

Route is used to add or delete system routes.

func New

func New() (*Route, error)

func (*Route) Add

func (r *Route) Add(options Opts) error

Add adds route to ip table.

func (*Route) Delete

func (r *Route) Delete(options Opts) error

Delete deletes route from ip table.

Jump to

Keyboard shortcuts

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