source

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package source provides data loading functionality for cloudip. It handles fetching, caching, and parsing of IP range data.

Index

Constants

View Source
const (
	// DefaultDataURL is the default data URL (GitHub raw content).
	DefaultDataURL = "https://github.com/rezmoss/cloudip-db/raw/main/data/cloudip.msgpack"

	// DefaultVersionURL is the default version URL.
	DefaultVersionURL = "https://github.com/rezmoss/cloudip-db/raw/main/data/version.json"

	// MaxDownloadSize limits downloads to 10 MB.
	MaxDownloadSize = 10 * 1024 * 1024

	// DefaultTimeout for HTTP requests.
	DefaultTimeout = 30 * time.Second
)

Variables

View Source
var ErrNoEmbeddedData = errors.New("no embedded data available")

ErrNoEmbeddedData is returned when no embedded data is available.

Functions

func ClearCache

func ClearCache(dir string) error

ClearCache removes all cached data.

func Decompress

func Decompress(data []byte) ([]byte, error)

Decompress decompresses data if it's gzip compressed. Returns the original data if not compressed.

func DefaultCacheDir

func DefaultCacheDir() string

DefaultCacheDir returns the default cache directory.

func FetchData

func FetchData(ctx context.Context, client *http.Client, url string) ([]byte, error)

FetchData downloads the MessagePack data from the given URL.

func GetEmbeddedData

func GetEmbeddedData() ([]byte, error)

GetEmbeddedData returns the embedded MessagePack data.

func IsCacheStale

func IsCacheStale(dir string, maxAge time.Duration) bool

IsCacheStale returns true if the cache is older than the given duration.

func LoadFromCache

func LoadFromCache(dir string) ([]byte, error)

LoadFromCache loads data from the cache directory.

func SaveToCache

func SaveToCache(dir string, data []byte, version string) error

SaveToCache saves data to the cache directory. The version parameter is optional and used for metadata.

Types

type CacheMetadata

type CacheMetadata struct {
	Version    string `json:"version"`
	FetchedAt  int64  `json:"fetched_at"`
	Size       int64  `json:"size"`
	DataSource string `json:"data_source"` // "network" or "embedded"
}

CacheMetadata stores information about cached data.

func GetCacheMetadata

func GetCacheMetadata(dir string) (*CacheMetadata, error)

GetCacheMetadata reads the cache metadata.

type Database

type Database struct {
	Version   string   `msgpack:"version"`
	BuildTime int64    `msgpack:"build_time"`
	Providers []string `msgpack:"providers"`
	Ranges    []Range  `msgpack:"ranges"`
}

Database represents the parsed MessagePack database structure.

func ParseDatabase

func ParseDatabase(data []byte) (*Database, error)

ParseDatabase parses MessagePack data into a Database struct. It automatically detects and handles gzip compression.

type Range

type Range struct {
	CIDR     string `msgpack:"cidr"`
	Provider int    `msgpack:"p"`
	Region   string `msgpack:"r,omitempty"`
	Service  string `msgpack:"s,omitempty"`
}

Range represents a single IP range from the database.

type VersionInfo

type VersionInfo struct {
	Version   string `json:"version"`
	BuildTime int64  `json:"build_time"`
	SHA256    string `json:"sha256"`
	Ranges    int    `json:"ranges"`
	Size      int64  `json:"size"`
	SizeGzip  int64  `json:"size_gzip"`
}

VersionInfo contains metadata about the data version.

func FetchVersion

func FetchVersion(ctx context.Context, client *http.Client, url string) (*VersionInfo, error)

FetchVersion downloads the version information from the given URL.

Jump to

Keyboard shortcuts

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