From 9d1802843219de65623923dbb29b5056ea7863ee Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Tue, 2 Sep 2025 18:28:08 -0400 Subject: [PATCH] feat: make mississippi checker optional --- SETUP.md | 2 +- scripts/download-ip2location-databases.js | 5 ++++ src/ip2location.ts | 29 ++++++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/SETUP.md b/SETUP.md index 4d23baf..fd99993 100644 --- a/SETUP.md +++ b/SETUP.md @@ -67,7 +67,7 @@ Configurations are loaded through environment variables. `.env` files are suppor | Name | Description | Optional | | --------------------------------------------- | ---------------------------------------------------------------------------------------------- | -------- | | `PN_ACT_CONFIG_HTTP_PORT` | The HTTP port the server listens on | No | -| `PN_ACT_CONFIG_IP2LOCATION_TOKEN` | Download token for https://lite.ip2location.com. Used to download the local IP databases | No | +| `PN_ACT_CONFIG_IP2LOCATION_TOKEN` | Download token for https://lite.ip2location.com. Used to download the local IP databases | Yes | | `PN_ACT_CONFIG_MONGO_CONNECTION_STRING` | MongoDB connection string | No | | `PN_ACT_CONFIG_MONGOOSE_CONNECT_OPTIONS_PATH` | Path to a `.json` file containing Mongoose connection options | Yes | | `PN_ACT_CONFIG_REDIS_URL` | Redis URL | Yes | diff --git a/scripts/download-ip2location-databases.js b/scripts/download-ip2location-databases.js index 163cfe2..0985f56 100644 --- a/scripts/download-ip2location-databases.js +++ b/scripts/download-ip2location-databases.js @@ -17,6 +17,11 @@ const databases = { }; async function main() { + if (!process.env.PN_ACT_CONFIG_IP2LOCATION_TOKEN) { + // * Optional + return; + } + for (const name in databases) { const database = databases[name]; const response = await fetch(`https://www.ip2location.com/download/?token=${process.env.PN_ACT_CONFIG_IP2LOCATION_TOKEN}&file=${name}`); diff --git a/src/ip2location.ts b/src/ip2location.ts index f5f87b3..8c24728 100644 --- a/src/ip2location.ts +++ b/src/ip2location.ts @@ -1,20 +1,37 @@ import path from 'node:path'; import net from 'node:net'; +import fs from 'fs-extra'; import * as IP2Location from 'ip2location-nodejs'; +import { LOG_WARN } from '@/logger'; class IP2LocationManager { - private ipv4: IP2Location.IP2Location; - private ipv6: IP2Location.IP2Location; + private ipv4?: IP2Location.IP2Location; + private ipv6?: IP2Location.IP2Location; constructor() { - this.ipv4 = new IP2Location.IP2Location(); - this.ipv6 = new IP2Location.IP2Location(); + const ipv4Path = path.join(__dirname, 'IP2LOCATION-LITE-DB3.IPV4.BIN'); + const ipv6Path = path.join(__dirname, 'IP2LOCATION-LITE-DB3.IPV6.BIN'); - this.ipv4.open(path.join(__dirname, 'IP2LOCATION-LITE-DB3.IPV4.BIN')); - this.ipv6.open(path.join(__dirname, 'IP2LOCATION-LITE-DB3.IPV6.BIN')); + if (!fs.existsSync(ipv4Path)) { + LOG_WARN('Could not find IP2LOCATION-LITE-DB3.IPV4.BIN. IP location checking disabled. To enable, run `node scripts/download-ip2location-databases.js` and restart the server.'); + } else { + this.ipv4 = new IP2Location.IP2Location(); + this.ipv4.open(ipv4Path); + } + + if (!fs.existsSync(ipv6Path)) { + LOG_WARN('Could not find IP2LOCATION-LITE-DB3.IPV6.BIN. IP location checking disabled. To enable, run `node scripts/download-ip2location-databases.js` and restart the server.'); + } else { + this.ipv6 = new IP2Location.IP2Location(); + this.ipv6.open(ipv6Path); + } } public lookup(ip: string): { country: string; region: string } | null { + if (!this.ipv4 || !this.ipv6) { + return null; + } + const ipVersion = net.isIP(ip); let result;