feat: make mississippi checker optional

This commit is contained in:
Jonathan Barrow
2025-09-02 18:28:08 -04:00
parent 1ef20edd72
commit 9d18028432
3 changed files with 29 additions and 7 deletions

View File

@@ -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 |

View File

@@ -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}`);

View File

@@ -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;