build: embed mmdb as pure js data (#782)

This commit is contained in:
Trim21
2024-08-14 05:48:02 +08:00
committed by GitHub
parent e40881c62e
commit c3b9a6c6aa
9 changed files with 200 additions and 207 deletions

View File

@@ -35,9 +35,7 @@
"pkg": {
"assets": [
"dist/assets/**/*",
"dist/**/*.map",
"dist/geoip-country.dat",
"dist/geoip-country6.dat"
"dist/**/*.map"
],
"targets": [
"node18-linuxstatic-x64",
@@ -67,9 +65,6 @@
"lint-staged": "lint-staged",
"prepare": "husky"
},
"dependencies": {
"geoip-country": "^4.1.60"
},
"devDependencies": {
"@babel/core": "^7.22.5",
"@babel/eslint-parser": "^7.22.5",
@@ -91,6 +86,7 @@
"@lingui/format-json": "^4.11.3",
"@lingui/loader": "^4.11.3",
"@lingui/react": "^4.11.3",
"@maxmind/geoip2-node": "^5.0.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
"@seald-io/nedb": "^3.1.0",
"@types/bencode": "^2.0.1",
@@ -104,13 +100,12 @@
"@types/d3-shape": "^3.1.6",
"@types/express": "^4.17.17",
"@types/fs-extra": "^9.0.13",
"@types/geoip-country": "^4.0.0",
"@types/http-errors": "^1.8.2",
"@types/jest": "^28.1.8",
"@types/jsonwebtoken": "^9.0.2",
"@types/lodash": "^4.14.195",
"@types/morgan": "^1.9.4",
"@types/node": "^14.0.0",
"@types/node": "^16.18.105",
"@types/parse-torrent": "^5.8.4",
"@types/passport": "^1.0.12",
"@types/passport-jwt": "^3.0.9",
@@ -232,9 +227,6 @@
"last 2 iOS version",
"last 2 Safari version"
],
"bundleDependencies": [
"geoip-country"
],
"lint-staged": {
"*": "prettier --ignore-unknown -w"
},

343
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

16
scripts/update-mmdb.mjs Normal file
View File

@@ -0,0 +1,16 @@
import * as zlib from 'node:zlib';
import * as fs from 'node:fs';
const buf = fs.readFileSync('server/geoip/GeoLite2-Country.mmdb');
const compressed = zlib.brotliCompressSync(buf).toString('base64');
fs.writeFileSync(
'server/geoip/data.ts',
`
import * as zlib from 'node:zlib';
// data is brotli compressed GeoLite2-Country.mmdb in base64 format
export const data = zlib.brotliDecompressSync(Buffer.from('${compressed}', 'base64'));
`,
);

1
server/geoip/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
GeoLite2-Country.mmdb

9
server/geoip/data.ts Normal file

File diff suppressed because one or more lines are too long

3
server/geoip/readme.md Normal file
View File

@@ -0,0 +1,3 @@
download `GeoLite2-Country.mmdb` from maxmind and put it here.
Then run `node ./scripts/update-mmdb.mjs` to update `data.ts`

View File

@@ -1,4 +1,3 @@
import geoip from 'geoip-country';
import path from 'path';
import type {
@@ -26,6 +25,7 @@ import type {TransferSummary} from '@shared/types/TransferData';
import type {TransmissionConnectionSettings} from '@shared/schema/ClientConnectionSettings';
import type {SetClientSettingsOptions} from '@shared/types/api/client';
import * as geoip from '../geoip';
import ClientGatewayService from '../clientGatewayService';
import ClientRequestManager from './clientRequestManager';
import {fetchUrls} from '../../util/fetchUtil';
@@ -195,7 +195,7 @@ class TransmissionClientGatewayService extends ClientGatewayService {
.filter((peer) => peer.isDownloadingFrom || peer.isUploadingTo)
.map((peer) => ({
address: peer.address,
country: geoip.lookup(peer.address)?.country || '',
country: geoip.lookup(peer.address),
clientVersion: peer.clientName,
completedPercent: peer.progress * 100,
downloadRate: peer.rateToClient,

13
server/services/geoip.ts Normal file
View File

@@ -0,0 +1,13 @@
import {Reader} from '@maxmind/geoip2-node';
import * as data from '../geoip/data';
const r = Reader.openBuffer(data.data);
export function lookup(s: string): string {
try {
return r.country(s)?.country?.isoCode ?? '';
} catch {
return '';
}
}

View File

@@ -1,5 +1,4 @@
import fs from 'fs';
import geoip from 'geoip-country';
import {move} from 'fs-extra';
import path from 'path';
import sanitize from 'sanitize-filename';
@@ -31,6 +30,7 @@ import type {TorrentTracker} from '@shared/types/TorrentTracker';
import type {TransferSummary} from '@shared/types/TransferData';
import type {SetClientSettingsOptions} from '@shared/types/api/client';
import * as geoip from '../geoip';
import {isAllowedPath, sanitizePath} from '../../util/fileUtil';
import ClientGatewayService from '../clientGatewayService';
import ClientRequestManager from './clientRequestManager';
@@ -262,7 +262,7 @@ class RTorrentClientGatewayService extends ClientGatewayService {
processedResponses.map(async (processedResponse) => {
return {
...processedResponse,
country: geoip.lookup(processedResponse.address)?.country || '',
country: geoip.lookup(processedResponse.address),
};
}),
);