internal

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FileASN4        = "ip2asn-v4.tsv.gz"
	FileASN6        = "ip2asn-v6.tsv.gz"
	FileASNCombined = "ip2asn-combined.tsv.gz"
	FileCountry4    = "ip2country-v4.tsv.gz"
	FileCountry6    = "ip2country-v6.tsv.gz"
)

Supported source filenames from iptoasn.com.

Variables

This section is empty.

Functions

func Build

func Build(cfg Config) error

Build downloads data (if needed) and creates the MMDB file.

func Download

func Download(urlStr string) (io.ReadCloser, error)

Download fetches a URL and returns a reader for the uncompressed content. The caller is responsible for closing the returned ReadCloser.

func OpenFile

func OpenFile(path string) (io.ReadCloser, error)

OpenFile opens a local file and returns a reader for the uncompressed content. The caller is responsible for closing the returned ReadCloser.

Types

type ASNRecord

type ASNRecord struct {
	StartIP      netip.Addr
	EndIP        netip.Addr
	ASN          uint32
	Country      string
	Organization string
}

ASNRecord represents a parsed line from ip2asn files.

func ParseASNRecords

func ParseASNRecords(r io.Reader) ([]ASNRecord, error)

ParseASNRecords parses ip2asn TSV data from a reader. Format: start_ip end_ip asn country description Records with ASN 0 ("Not routed") are included so users can distinguish between "IP not in database" and "IP known but not routed".

type Config

type Config struct {
	OutputFile string
	DataType   DataType
	IPVersion  IPVersion
	SourceFile string // Local TSV/TSV.gz file
	Download   bool   // If true, download from iptoasn.com
}

Config holds the build configuration.

func (Config) SourceURLs

func (c Config) SourceURLs() []string

SourceURLs returns the URLs to download based on configuration.

func (Config) Validate

func (c Config) Validate() error

Validate checks the configuration for errors.

type CountryRecord

type CountryRecord struct {
	StartIP netip.Addr
	EndIP   netip.Addr
	Country string
}

CountryRecord represents a parsed line from ip2country files.

func ParseCountryRecords

func ParseCountryRecords(r io.Reader) ([]CountryRecord, error)

ParseCountryRecords parses ip2country TSV data from a reader. Format: start_ip end_ip country Records with country "None" are included so users can distinguish between "IP not in database" and "IP known but no country assigned".

type DataType

type DataType string

DataType specifies what data to include in the MMDB.

const (
	DataTypeASN     DataType = "asn"
	DataTypeCountry DataType = "country"
)

type IPVersion

type IPVersion string

IPVersion specifies which IP versions to include.

const (
	IPVersionBoth IPVersion = "both"
	IPVersion4    IPVersion = "4"
	IPVersion6    IPVersion = "6"
)

Jump to

Keyboard shortcuts

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