Documentation
¶
Overview ¶
Package source provides data loading functionality for cloudip. It handles fetching, caching, and parsing of IP range data.
Index ¶
- Constants
- Variables
- func ClearCache(dir string) error
- func Decompress(data []byte) ([]byte, error)
- func DefaultCacheDir() string
- func FetchData(ctx context.Context, client *http.Client, url string) ([]byte, error)
- func GetEmbeddedData() ([]byte, error)
- func IsCacheStale(dir string, maxAge time.Duration) bool
- func LoadFromCache(dir string) ([]byte, error)
- func SaveToCache(dir string, data []byte, version string) error
- type CacheMetadata
- type Database
- type Range
- type VersionInfo
Constants ¶
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 ¶
var ErrNoEmbeddedData = errors.New("no embedded data available")
ErrNoEmbeddedData is returned when no embedded data is available.
Functions ¶
func Decompress ¶
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 GetEmbeddedData ¶
GetEmbeddedData returns the embedded MessagePack data.
func IsCacheStale ¶
IsCacheStale returns true if the cache is older than the given duration.
func LoadFromCache ¶
LoadFromCache loads data from the cache directory.
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 ¶
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 ¶
FetchVersion downloads the version information from the given URL.