@lock-sdk/geo-block
Version:
Geographic blocking module for Lock security framework
120 lines (119 loc) • 2.88 kB
TypeScript
export declare enum GeoBlockEventType {
GEO_BLOCKED = "geo_blocked"
}
/**
* Configuration for geo-blocking
*/
export interface GeoBlockConfig {
/**
* Mode of operation - allow only specified countries or block specified countries
* @default 'blocklist'
*/
mode: 'allowlist' | 'blocklist';
/**
* List of country codes to allow or block (ISO 3166-1 alpha-2)
* @example ['US', 'CA', 'GB']
*/
countries: string[];
/**
* Provider for IP-to-location lookup
* @default 'maxmind'
*/
provider: 'maxmind' | 'ipapi' | 'ipstack' | 'custom';
/**
* Path to MaxMind database file if using 'maxmind' provider
*/
maxmindDbPath?: string;
/**
* API key for providers that require authentication
*/
apiKey?: string;
/**
* Custom implementation for IP lookup
*/
customLookup?: (ip: string) => Promise<GeoInfo>;
/**
* Headers to check for client IP address, in order of preference
* @default ['cf-connecting-ip', 'x-forwarded-for', 'x-real-ip']
*/
ipHeaders?: string[];
/**
* Use fallback to remote address if headers aren't present
* @default true
*/
useRemoteAddress?: boolean;
/**
* HTTP status code to return when blocked
* @default 403
*/
blockStatusCode?: number;
/**
* Message to return when blocked
* @default 'Access denied based on your location'
*/
blockMessage?: string;
/**
* Whether to include the country code in the block response
* @default true
*/
includeCountryInResponse?: boolean;
/**
* Time in milliseconds to cache IP lookups
* @default 3600000 (1 hour)
*/
cacheTtl?: number;
/**
* Maximum number of IP addresses to cache
* @default 10000
*/
cacheSize?: number;
/**
* How to handle provider failures
* - 'open': Allow requests when geo lookup fails (default)
* - 'closed': Block requests when geo lookup fails
* @default open
*/
failBehavior?: 'open' | 'closed';
}
/**
* Geographic information for an IP address
*/
export interface GeoInfo {
/**
* ISO 3166-1 alpha-2 country code
* @example 'US'
*/
country?: string;
/**
* Region or state
* @example 'CA'
*/
region?: string;
/**
* City name
* @example 'San Francisco'
*/
city?: string;
/**
* Latitude coordinate
*/
latitude?: number;
/**
* Longitude coordinate
*/
longitude?: number;
}
/**
* Provider interface for geo lookups
*/
export interface GeoLookupProvider {
/**
* Initialize the provider
*/
init(): Promise<void>;
/**
* Look up geographic information for an IP address
* @param ip IP address to look up
*/
lookup(ip: string): Promise<GeoInfo>;
}