Documentation
¶
Overview ¶
contract.go: Wires the geoip plugin into the forge-dashboard contract surface via plugin.ContractContributor.
Package geoip provides GeoIP location resolution for authentication events. It resolves IP addresses to geographic locations and stores the result in context for downstream plugins (geofence, impossible travel, etc.).
Index ¶
- Variables
- func Haversine(lat1, lon1, lat2, lon2 float64) float64
- func WithGeoLocation(ctx context.Context, loc *GeoLocation) context.Context
- type Cache
- type Config
- type GeoLocation
- type MaxMindProvider
- type Plugin
- func (p *Plugin) DeclareSettings(m *settings.Manager) error
- func (p *Plugin) Name() string
- func (p *Plugin) OnAfterSignIn(ctx context.Context, u *user.User, s *session.Session) error
- func (p *Plugin) OnBeforeSignIn(_ context.Context, req *account.SignInRequest) error
- func (p *Plugin) OnBeforeSignUp(_ context.Context, req *account.SignUpRequest) error
- func (p *Plugin) OnInit(_ context.Context, engine plugin.Engine) error
- func (p *Plugin) OnShutdown(_ context.Context) error
- func (p *Plugin) RegisterContract(d *dispatcher.Dispatcher, reg contract.Registry, wreg contract.WardenRegistry, ...) error
- func (p *Plugin) Resolve(ipStr string) *GeoLocation
- type Provider
- type TestProvider
Constants ¶
This section is empty.
Variables ¶
var ( // SettingCacheTTLHours controls how long GeoIP lookup results are cached. SettingCacheTTLHours = settings.Define("geoip.cache_ttl_hours", 24, settings.WithDisplayName("Cache TTL (hours)"), settings.WithDescription("How long GeoIP lookup results are cached, in hours"), settings.WithCategory("GeoIP"), settings.WithScopes(settings.ScopeGlobal), settings.WithHelpText("How long GeoIP lookup results are cached, in hours"), settings.WithOrder(10), ) )
Functions ¶
func Haversine ¶
Haversine calculates the distance in kilometers between two geographic coordinates using the Haversine formula.
func WithGeoLocation ¶
func WithGeoLocation(ctx context.Context, loc *GeoLocation) context.Context
WithGeoLocation stores a GeoLocation in the context.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe TTL cache for GeoLocation lookups.
func (*Cache) Get ¶
func (c *Cache) Get(ip string) *GeoLocation
Get returns a cached GeoLocation or nil if not found/expired.
The returned value is a copy. Handing out the stored pointer would make this type thread-safe only for callers that never write: one caller mutating what it got back would corrupt the entry every other caller sees for the rest of the ttl, and that write races every concurrent Get, since it happens outside the lock. Plugin.Resolve returns this value straight through to anomaly, geofence, impossibletravel and vpndetect, so the pointer would be shared across plugins as well as goroutines.
func (*Cache) Set ¶
func (c *Cache) Set(ip string, loc *GeoLocation)
Set stores a GeoLocation in the cache. The value is copied on the way in for the mirror-image reason Get copies on the way out: otherwise a caller that mutates loc after Set returns rewrites the cached entry behind the lock's back.
type Config ¶
type Config struct {
// DatabasePath is the path to the MaxMind GeoLite2-City.mmdb file.
DatabasePath string
// CacheTTL is how long GeoIP results are cached (default: 24h).
CacheTTL time.Duration
}
Config configures the GeoIP plugin.
type GeoLocation ¶
type GeoLocation struct {
IP string `json:"ip"`
Country string `json:"country"` // ISO 3166-1 alpha-2
CountryName string `json:"country_name"` // human-readable
Region string `json:"region,omitempty"`
City string `json:"city,omitempty"`
Timezone string `json:"timezone,omitempty"`
ISP string `json:"isp,omitempty"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
ASN int `json:"asn,omitempty"`
IsProxy bool `json:"is_proxy,omitempty"`
IsVPN bool `json:"is_vpn,omitempty"`
IsTor bool `json:"is_tor,omitempty"`
}
GeoLocation represents the geographic location resolved from an IP address.
func GeoLocationFrom ¶
func GeoLocationFrom(ctx context.Context) *GeoLocation
GeoLocationFrom extracts the GeoLocation from context. Returns nil if not present (e.g. geoip plugin not loaded).
type MaxMindProvider ¶
type MaxMindProvider struct {
// contains filtered or unexported fields
}
MaxMindProvider resolves IPs using a MaxMind GeoLite2 MMDB file.
func NewMaxMindProvider ¶
func NewMaxMindProvider(dbPath string) (*MaxMindProvider, error)
NewMaxMindProvider opens a MaxMind MMDB file.
func (*MaxMindProvider) Close ¶
func (p *MaxMindProvider) Close() error
Close closes the MMDB reader.
func (*MaxMindProvider) Lookup ¶
func (p *MaxMindProvider) Lookup(ip net.IP) (*GeoLocation, error)
Lookup resolves an IP address to a GeoLocation.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin is the GeoIP location resolution plugin. It resolves IP addresses to geographic locations and stores results in context for downstream geo-security plugins.
func NewTestPlugin ¶
func NewTestPlugin(mapping map[string]*GeoLocation) *Plugin
NewTestPlugin creates a fully configured Plugin with a test provider and cache. This is intended for use in tests across geo-security plugin packages that depend on a working geoip.Plugin without a real MaxMind database.
func (*Plugin) DeclareSettings ¶
DeclareSettings implements plugin.SettingsProvider. It registers all geoip-related configurable settings.
func (*Plugin) OnAfterSignIn ¶
OnAfterSignIn enriches audit/relay events with geo metadata.
func (*Plugin) OnBeforeSignIn ¶
OnBeforeSignIn resolves geo for the sign-in request and stores it in context.
func (*Plugin) OnBeforeSignUp ¶
OnBeforeSignUp resolves geo for the sign-up request.
func (*Plugin) OnShutdown ¶
OnShutdown closes the GeoIP provider.
func (*Plugin) RegisterContract ¶
func (p *Plugin) RegisterContract( d *dispatcher.Dispatcher, reg contract.Registry, wreg contract.WardenRegistry, engine plugin.Engine, ) error
func (*Plugin) Resolve ¶
func (p *Plugin) Resolve(ipStr string) *GeoLocation
Resolve looks up the geographic location for an IP string. Results are cached. Returns nil for private/loopback IPs or if no provider is configured.
type Provider ¶
type Provider interface {
Lookup(ip net.IP) (*GeoLocation, error)
Close() error
}
Provider resolves an IP address to a GeoLocation.
type TestProvider ¶
type TestProvider struct {
Mapping map[string]*GeoLocation
}
TestProvider is a mock GeoIP provider for use in tests. It maps IP string representations to pre-configured GeoLocation values.
func NewTestProvider ¶
func NewTestProvider(m map[string]*GeoLocation) *TestProvider
NewTestProvider creates a TestProvider with the given IP-to-GeoLocation mapping.
func (*TestProvider) Close ¶
func (tp *TestProvider) Close() error
Close is a no-op for the test provider.
func (*TestProvider) Lookup ¶
func (tp *TestProvider) Lookup(ip net.IP) (*GeoLocation, error)
Lookup returns the pre-configured GeoLocation for the given IP.