ipsearch

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

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 12 Imported by: 0

README

Fork of haoel/ipsearch with IPv6 support.

A simple library to search for IP(v4 & v6) addresses for one IP database: IP CIDR List.

1. IP Database

The library use the cidr list of herrbischoff/country-ip-blocks.

2. Usage

2.1 Check an IP address is in the CIDR list
package main

import (
	"fmt"
	"github.com/guuzaa/ipsearch"
)

// ipv4 version
func main() {
	search, err := ipsearch.NewIPv4SearchWithFile("./data/ipv4/cn.cidr")
	if err != nil {
		panic(err)
	}

	ipStr := "114.114.114.114"
	ip := search.Search(ipStr)
	if ip != nil {
		fmt.Printf("IP [%s] is in China\n", ipStr)
	} else {
		fmt.Printf("IP [%s] is not China\n", ipStr)
	}
}

// ipv6 version
func main() {
	search, err := ipsearch.NewIPv6SearchWithFile("./data/ipv6/cn.cidr")
	if err != nil {
		panic(err)
	}

	ipStr := "2001:da8::2"
	ip := search.Search(ipStr)
	if ip != nil {
		fmt.Printf("IP [%s] is in China\n", ipStr)
	} else {
		fmt.Printf("IP [%s] is not in China\n", ipStr)
	}
}

2.2 Check an IP address is in the country
package main

import (
	"fmt"
	"github.com/guuzaa/ipsearch"
)

// ipv4 version
func main() {
	search, err := ipsearch.NewIPv4SearchWithCountry("cn")
	if err != nil {
		panic(err)
	}

	ipStr := "114.114.114.114"
	ip := search.Search(ipStr)
	if ip != nil {
		fmt.Printf("IP [%s] is in China\n", ipStr)
	} else {
		fmt.Printf("IP [%s] is not China\n", ipStr)
	}
}

// ipv6 version
func main() {
	search, err := ipsearch.NewIPv6SearchWithCountry("cn")
	if err != nil {
		panic(err)
	}

	ipStr := "2001:da8::2"
	ip := search.Search(ipStr)
	if ip != nil {
		fmt.Printf("IP [%s] is in China\n", ipStr)
	} else {
		fmt.Printf("IP [%s] is not in China\n", ipStr)
	}
}

3. Implementation

As haoel did, the ipsearch package is using the Hash Table and Binary Search algorithm. Apart from that, we use the net/ip package to parse IP address.

4. Licence

This project is MIT licensed. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIPv4Segment

func GetIPv4Segment(ipStr string, segment int) uint8

GetIPv4Segment returns the segment of an IPv4 address

func GetIPv6Segment

func GetIPv6Segment(ipStr string, segment int) uint16

GetIPv6Segment returns the segment of an IPv6 address

func IPv4CIDRRange

func IPv4CIDRRange(cidr string) (uint32, uint32)

IPv4CIDRRange returns the start and end IP v4 address for a CIDR range

func IPv4InCIDR

func IPv4InCIDR(ipStr string, cidr string) bool

IPv4InCIDR checks if an IPv4 address is in a CIDR range

func IPv4IntToStr

func IPv4IntToStr(ip uint32) string

IPv4IntToStr converts an integer to a string IPv4 address

func IPv4StrToInt

func IPv4StrToInt(ipStr string) uint32

IPv4StrToInt converts a string IPv4 address to an integer

func IPv6CIDRRange

func IPv6CIDRRange(cidr string) (num.U128, num.U128)

IPv6CIDRRange returns the start and end IPv6 address for a CIDR range

func IPv6InCIDR

func IPv6InCIDR(ipStr string, cidr string) bool

IPv6InCIDR checks if an IPv6 address is in a CIDR range

func IPv6IntToStr

func IPv6IntToStr(ip num.U128) string

IPv6IntToStr converts an integer to a string IPv6 address

func IPv6StrToInt

func IPv6StrToInt(ipStr string) num.U128

IPv6StrToInt converts a string IPv6 address to an integer

func ReadFile

func ReadFile(filename string) ([]string, error)

ReadFile reads a ip cidr list file and returns a slice of strings, one for each line.

func ReadFileFromURL

func ReadFileFromURL(url string) ([]string, error)

ReadFileFromURL from a URL and returns a slice of strings, one for each line.

Types

type IPv4Range

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

IPv4Range represents an IPv4 CIDR range.

func NewIPv4Range

func NewIPv4Range(cidr string) *IPv4Range

NewIPv4Range creates a new IPv4 CIDR range

func NewIPv4RangeSlice

func NewIPv4RangeSlice(lines []string) []*IPv4Range

NewIPv4RangeSlice creates a new slice of IPv4Range.

func (*IPv4Range) CIDR

func (ip *IPv4Range) CIDR() string

CIDR returns the CIDR of the IP range

func (*IPv4Range) Range

func (ip *IPv4Range) Range() string

Range returns the range of the IPv4 address in string format

func (*IPv4Range) String

func (ip *IPv4Range) String() string

type IPv4RangeList

type IPv4RangeList []*IPv4Range

IPv4RangeList is a list of IPv4 CIDR ranges.

func NewIPv4RangeList

func NewIPv4RangeList(lines []string) IPv4RangeList

func (*IPv4RangeList) Append

func (list *IPv4RangeList) Append(rge *IPv4Range)

Append adds a IPv4 CIDR to the list

func (*IPv4RangeList) InsertSorted

func (list *IPv4RangeList) InsertSorted(rge *IPv4Range)

InsertSorted inserts a IPv4 range to the list, keeping the list sorted

func (*IPv4RangeList) Len

func (list *IPv4RangeList) Len() int

Len returns the length of the list of IPv4 CIDR ranges

func (*IPv4RangeList) Range

func (list *IPv4RangeList) Range() string

Range returns a string representation of the list of IPv4 address ranges

func (*IPv4RangeList) Search

func (list *IPv4RangeList) Search(ipStr string) *IPv4Range

Search searches if an IP address in the list

func (*IPv4RangeList) Sort

func (list *IPv4RangeList) Sort()

Sort sorts the list of IPv4 CIDR ranges

func (*IPv4RangeList) String

func (list *IPv4RangeList) String() string

String returns a string representation of the list of IPv4 CIDR ranges

type IPv4RangeMapList

type IPv4RangeMapList map[uint8]*IPv4RangeList

IPv4RangeMapList is a map of lists of IPv4 CIDR ranges

func NewIPv4RangeMapList

func NewIPv4RangeMapList() IPv4RangeMapList

NewIPv4RangeMapList creates a new map of lists of IPv4 CIDR ranges

func (IPv4RangeMapList) Append

func (m IPv4RangeMapList) Append(ipRange *IPv4Range)

Append adds an IPv4 CIDR range to the map of lists of IPv4 CIDR ranges

func (IPv4RangeMapList) AppendBatch

func (m IPv4RangeMapList) AppendBatch(ipRanges []*IPv4Range)

AppendBatch adds a list of IPv4 CIDR ranges to the map of lists of IPv4 CIDR ranges

func (IPv4RangeMapList) InsertSorted

func (m IPv4RangeMapList) InsertSorted(ipRange *IPv4Range)

InsertSorted inserts a IPv4 CIDR range to the map of lists of IPv4 CIDR ranges, keeping the lists sorted

func (IPv4RangeMapList) InsertSortedCIDRs

func (m IPv4RangeMapList) InsertSortedCIDRs(ipRanges []*IPv4Range)

InsertSortedCIDRs inserts a list of IPv4 CIDR ranges to the map of IPv4 CIDR ranges lists, keeping the lists sorted

func (IPv4RangeMapList) Search

func (m IPv4RangeMapList) Search(ipStr string) *IPv4Range

Search searches if an IPv4 address is in the map of lists

func (IPv4RangeMapList) Sort

func (m IPv4RangeMapList) Sort()

Sort sorts the map of lists of IPv4 CIDR ranges

type IPv4Search

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

IPv4Search is a struct that contains a map of IPv4 ranges.

func NewIPv4Search

func NewIPv4Search(lines []string) *IPv4Search

NewIPv4Search creates a new IPv4Search struct.

func NewIPv4SearchWithCountry

func NewIPv4SearchWithCountry(country string) (*IPv4Search, error)

NewIPv4SearchWithCountry creates a new IPv4Search struct from a country code.

func NewIPv4SearchWithFile

func NewIPv4SearchWithFile(path string) (*IPv4Search, error)

NewIPv4SearchWithFile creates a new IPv4Search struct from a file.

func NewIPv4SearchWithFileFromURL

func NewIPv4SearchWithFileFromURL(url string) (*IPv4Search, error)

NewIPv4SearchWithFileFromURL creates a new IPv4Search struct from a URL.

func (*IPv4Search) Search

func (s *IPv4Search) Search(ip string) *IPv4Range

Search searches if an IPv4 address is in the map of lists of IPv4 ranges.

type IPv6Range

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

IPv6Range represents an IPv6 CIDR range.

func NewIPv6Range

func NewIPv6Range(cidr string) *IPv6Range

NewIPv6Range creates a new IPv6 CIDR range

func NewIPv6RangeSlice

func NewIPv6RangeSlice(cidrs []string) []*IPv6Range

func (*IPv6Range) CIDR

func (ip *IPv6Range) CIDR() string

CIDR returns the CIDR of the IP range

func (*IPv6Range) Range

func (ip *IPv6Range) Range() string

Range returns the range of the IPv6 address in string format

func (*IPv6Range) String

func (ip *IPv6Range) String() string

type IPv6RangeList

type IPv6RangeList []*IPv6Range

IPv6RangeList is a list of IPv6 CIDR ranges.

func NewIPv6RangeList

func NewIPv6RangeList(lines []string) IPv6RangeList

func (*IPv6RangeList) Append

func (lst *IPv6RangeList) Append(rge *IPv6Range)

Append adds a IPv6 CIDR to the list

func (*IPv6RangeList) InsertSorted

func (lst *IPv6RangeList) InsertSorted(rge *IPv6Range)

InsertSorted inserts a IPv6 range to the list, keeping the list sorted

func (*IPv6RangeList) Len

func (lst *IPv6RangeList) Len() int

Len returns the length of the list of IPv6 CIDR ranges

func (*IPv6RangeList) Range

func (lst *IPv6RangeList) Range() string

Range returns a string representation of the list of IPv6 address ranges

func (*IPv6RangeList) Search

func (lst *IPv6RangeList) Search(ipStr string) *IPv6Range

Search searches if an IPv6 address in the list

func (*IPv6RangeList) Sort

func (lst *IPv6RangeList) Sort()

Sort sorts the list of IPv6 CIDR ranges

func (*IPv6RangeList) String

func (lst *IPv6RangeList) String() string

String returns a string representation of the list of IPv6 CIDR ranges

type IPv6RangeMapList

type IPv6RangeMapList map[uint16]*IPv6RangeList

IPv6RangeMapList is a map of lists of IPv6 CIDR ranges

func NewIPv6RangeMapList

func NewIPv6RangeMapList() IPv6RangeMapList

NewIPv6RangeMapList creates a new map of lists of IPv6 CIDR ranges

func (IPv6RangeMapList) Append

func (m IPv6RangeMapList) Append(ipRange *IPv6Range)

Append adds an IPv6 CIDR range to the map of lists of IPv6 CIDR ranges

func (IPv6RangeMapList) AppendBatch

func (m IPv6RangeMapList) AppendBatch(ipRanges []*IPv6Range)

AppendBatch adds a list of IPv6 CIDR ranges to the map of lists of IPv6 CIDR

func (IPv6RangeMapList) InsertSorted

func (m IPv6RangeMapList) InsertSorted(ipRange *IPv6Range)

func (IPv6RangeMapList) InsertSortedCIDRs

func (m IPv6RangeMapList) InsertSortedCIDRs(ipRanges []*IPv6Range)

InsertSortedCIDRs inserts a list of IPv6 CIDR ranges to the map of IPv6 CIDR lists keeping the lists sorted

func (IPv6RangeMapList) Search

func (m IPv6RangeMapList) Search(ipStr string) *IPv6Range

Search searches if an IPv6 address is in the map of lists

func (IPv6RangeMapList) Sort

func (m IPv6RangeMapList) Sort()

Sort sorts the map of lists of IPv6 CIDR ranges

type IPv6Search

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

IPv6Search is a struct that contains a map of IPv6 ranges.

func NewIPv6Search

func NewIPv6Search(lines []string) *IPv6Search

NewIPv6Search creates a new IPv6Search struct.

func NewIPv6SearchWithCountry

func NewIPv6SearchWithCountry(country string) (*IPv6Search, error)

NewIPv6SearchWithCountry creates a new IPv6Search struct from a country code.

func NewIPv6SearchWithFile

func NewIPv6SearchWithFile(path string) (*IPv6Search, error)

NewIPv6SearchWithFile creates a new IPv6Search struct from a file.

func NewIPv6SearchWithFileFromURL

func NewIPv6SearchWithFileFromURL(url string) (*IPv6Search, error)

NewIPv6SearchWithFileFromURL creates a new IPv6Search struct from a URL.

func (*IPv6Search) Search

func (s *IPv6Search) Search(ip string) *IPv6Range

Search searches if an IPv6 address is in the map of lists of IPv6 ranges.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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