Skip to content

Commit

Permalink
build: embed mmdb as pure js data (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Aug 13, 2024
1 parent e40881c commit c3b9a6c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 207 deletions.
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
"pkg": {
"assets": [
"dist/assets/**/*",
"dist/**/*.map",
"dist/geoip-country.dat",
"dist/geoip-country6.dat"
"dist/**/*.map"
],
"targets": [
"node18-linuxstatic-x64",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -232,9 +227,6 @@
"last 2 iOS version",
"last 2 Safari version"
],
"bundleDependencies": [
"geoip-country"
],
"lint-staged": {
"*": "prettier --ignore-unknown -w"
},
Expand Down
343 changes: 151 additions & 192 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions scripts/update-mmdb.mjs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions server/geoip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GeoLite2-Country.mmdb
9 changes: 9 additions & 0 deletions server/geoip/data.ts

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions server/geoip/readme.md
Original file line number Diff line number Diff line change
@@ -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`
4 changes: 2 additions & 2 deletions server/services/Transmission/clientGatewayService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import geoip from 'geoip-country';
import path from 'path';

import type {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions server/services/geoip.ts
Original file line number Diff line number Diff line change
@@ -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 '';
}
}
4 changes: 2 additions & 2 deletions server/services/rTorrent/clientGatewayService.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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),
};
}),
);
Expand Down

0 comments on commit c3b9a6c

Please sign in to comment.