UNPKG

@lock-dev/vpn-detection

Version:

VPN and proxy detection module for lock.dev security framework

156 lines (151 loc) 4.02 kB
// src/storage/memory.ts import { LRUCache } from "lru-cache"; var MemoryVPNCacheStore = class { constructor(config) { this.cache = new LRUCache({ max: config.cacheSize || 1e4, ttl: config.cacheTtl || 36e5, ttlAutopurge: true }); } async init() { return Promise.resolve(); } async get(ip) { return this.cache.get(ip) || null; } async set(ip, value) { this.cache.set(ip, value); } async close() { return Promise.resolve(); } }; // src/storage/redis.ts var RedisVPNCacheStore = class { constructor(config) { this.keyPrefix = config.redis?.keyPrefix || "vpncache:"; this.config = config.redis; this.ttl = Math.floor((config.cacheTtl || 36e5) / 1e3); } async init() { try { const { createClient } = await import("redis"); if (this.config.url) { this.client = createClient({ url: this.config.url }); } else { this.client = createClient({ socket: { host: this.config.host || "localhost", port: this.config.port || 6379 }, password: this.config.password, database: this.config.database || 0 }); } await this.client.connect(); this.client.on("error", (err) => { console.error("Redis client error:", err); }); } catch (error) { console.error("Failed to initialize Redis client:", error); throw new Error("Redis initialization failed"); } } async get(ip) { try { const data = await this.client.get(this.keyPrefix + ip); return data ? JSON.parse(data) : null; } catch (error) { console.error("Redis get error:", error); return null; } } async set(ip, value) { try { await this.client.set(this.keyPrefix + ip, JSON.stringify(value), { EX: this.ttl }); } catch (error) { console.error("Redis set error:", error); } } async close() { try { if (this.client) { await this.client.quit(); } } catch (error) { console.error("Redis close error:", error); } } }; // src/storage/upstash.ts var UpstashVPNCacheStore = class { constructor(config) { this.keyPrefix = config.upstash?.keyPrefix || "vpncache:"; this.config = config.upstash; this.ttl = Math.floor((config.cacheTtl || 36e5) / 1e3); } async init() { try { const { Redis } = await import("@upstash/redis"); this.client = new Redis({ url: this.config.url, token: this.config.token }); await this.client.ping(); } catch (error) { console.error("Failed to initialize Upstash client:", error); throw new Error("Upstash initialization failed"); } } async get(ip) { try { const data = await this.client.get(this.keyPrefix + ip); return data ? JSON.parse(data) : null; } catch (error) { console.error("Upstash get error:", error); return null; } } async set(ip, value) { try { await this.client.set(this.keyPrefix + ip, JSON.stringify(value), { ex: this.ttl }); } catch (error) { console.error("Upstash set error:", error); } } async close() { return Promise.resolve(); } }; // src/storage/index.ts async function createCacheStore(config) { const storageType = config.storage || "memory"; let store; switch (storageType) { case "redis": if (!config.redis) { throw new Error("Redis configuration is required when using Redis storage"); } store = new RedisVPNCacheStore(config); break; case "upstash": if (!config.upstash) { throw new Error("Upstash configuration is required when using Upstash storage"); } store = new UpstashVPNCacheStore(config); break; case "memory": default: store = new MemoryVPNCacheStore(config); break; } await store.init(); return store; } export { MemoryVPNCacheStore, RedisVPNCacheStore, UpstashVPNCacheStore, createCacheStore };