ipchecking

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ipchecking wrapper over net/netip to compare both IP and CIRD.

Example
package main

import (
	"fmt"

	"github.com/fliot/fail2ban/ipchecking"
)

func main() {
	// Parse multiple IPs/CIDRS
	ips, err := ipchecking.ParseNetIPs([]string{
		"127.0.0.1",
		"10.0.0.0/24", // 10.0.0.1-10.0.0.254
		"::1",
		"2001:db8::/32",
	})
	if err != nil {
		panic(err)
	}

	// Check if an IP is either in the list, or in the list networks
	fmt.Println(ips.Contains(""))                        // false (empty string is not an IP)
	fmt.Println(ips.Contains("127.0.0.1"))               // true
	fmt.Println(ips.Contains("127.0.0.2"))               // false
	fmt.Println(ips.Contains("10.0.0.42"))               // true
	fmt.Println(ips.Contains("::1"))                     // true
	fmt.Println(ips.Contains("2001:db8:beba:cafe::1:2")) // true
	fmt.Println(ips.Contains("64:ff9b::127.0.0.1"))      // false

}
Output:

false
true
false
true
true
true
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NetIP

type NetIP struct {
	Net  *netip.Prefix
	Addr netip.Addr
}

NetIP struct that holds an NetIP IP address, and a IP network. If the network is nil, the NetIP is a single IP.

func ParseNetIP

func ParseNetIP(ip string) (NetIP, error)

ParseNetIP Parse a string to extract the netip.

func (NetIP) Contains

func (ip NetIP) Contains(i string) bool

Contains Check is the IP is the same or in the same subnet.

func (NetIP) String

func (ip NetIP) String() string

String convert IP struct to string.

type NetIPs

type NetIPs []NetIP

func ParseNetIPs

func ParseNetIPs(iplist []string) (NetIPs, error)

ParseNetIPs Parse a slice string to extract the netip. Returns an error on the first IP that failed to parse.

func (NetIPs) Contains

func (netIPs NetIPs) Contains(ip string) bool

Contains Check is the IP is the same or in the same subnet.

Jump to

Keyboard shortcuts

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