netutil

package
v0.0.0-...-2658d01 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package netutil provides net-related utility functions/types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParseCIDR

func MustParseCIDR(cidr string) *net.IPNet

Types

type ErrBlocked

type ErrBlocked struct {
	Host    string
	Network NamedNetwork
}

ErrBlocked is an error returned by NetworkBlocklist.Control (thus net.Dialer.DialContext) when outgoing host is blocked by NetworkBlocklist.

func (ErrBlocked) Error

func (e ErrBlocked) Error() string

type NamedNetwork

type NamedNetwork struct {
	IPNet *net.IPNet
	Name  string
}

type NetworkBlocklist

type NetworkBlocklist struct {
	V4 []NamedNetwork
	V6 []NamedNetwork
}

NetworkBlocklist is a blocklist that blocks dialing to specified networks.

var PrivateNetworkBlocklist NetworkBlocklist

PrivateNetworkBlocklist is a blocklist that blocks dialing to private networks.

func (NetworkBlocklist) Control

func (l NetworkBlocklist) Control(network, address string, c syscall.RawConn) error

Control is intended to be passed to net.Dialer.Control in order to block dialing to networks specified in l.

Example
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DialContext = (&net.Dialer{
	Timeout:   30 * time.Second,
	KeepAlive: 30 * time.Second,
	Control:   PrivateNetworkBlocklist.Control,
}).DialContext

client := &http.Client{
	Transport: transport,
}

_, err := client.Get("http://[::1]/")
fmt.Println(err)
Output:

Get "http://[::1]/": dial tcp [::1]:80: host is blocked (Loopback Address)

Jump to

Keyboard shortcuts

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