mirror of
https://github.com/zoriya/flood.git
synced 2025-12-06 07:16:18 +00:00
build: embed mmdb as pure js data (#782)
This commit is contained in:
14
package.json
14
package.json
@@ -35,9 +35,7 @@
|
|||||||
"pkg": {
|
"pkg": {
|
||||||
"assets": [
|
"assets": [
|
||||||
"dist/assets/**/*",
|
"dist/assets/**/*",
|
||||||
"dist/**/*.map",
|
"dist/**/*.map"
|
||||||
"dist/geoip-country.dat",
|
|
||||||
"dist/geoip-country6.dat"
|
|
||||||
],
|
],
|
||||||
"targets": [
|
"targets": [
|
||||||
"node18-linuxstatic-x64",
|
"node18-linuxstatic-x64",
|
||||||
@@ -67,9 +65,6 @@
|
|||||||
"lint-staged": "lint-staged",
|
"lint-staged": "lint-staged",
|
||||||
"prepare": "husky"
|
"prepare": "husky"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
|
||||||
"geoip-country": "^4.1.60"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.22.5",
|
"@babel/core": "^7.22.5",
|
||||||
"@babel/eslint-parser": "^7.22.5",
|
"@babel/eslint-parser": "^7.22.5",
|
||||||
@@ -91,6 +86,7 @@
|
|||||||
"@lingui/format-json": "^4.11.3",
|
"@lingui/format-json": "^4.11.3",
|
||||||
"@lingui/loader": "^4.11.3",
|
"@lingui/loader": "^4.11.3",
|
||||||
"@lingui/react": "^4.11.3",
|
"@lingui/react": "^4.11.3",
|
||||||
|
"@maxmind/geoip2-node": "^5.0.0",
|
||||||
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
|
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
|
||||||
"@seald-io/nedb": "^3.1.0",
|
"@seald-io/nedb": "^3.1.0",
|
||||||
"@types/bencode": "^2.0.1",
|
"@types/bencode": "^2.0.1",
|
||||||
@@ -104,13 +100,12 @@
|
|||||||
"@types/d3-shape": "^3.1.6",
|
"@types/d3-shape": "^3.1.6",
|
||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/fs-extra": "^9.0.13",
|
"@types/fs-extra": "^9.0.13",
|
||||||
"@types/geoip-country": "^4.0.0",
|
|
||||||
"@types/http-errors": "^1.8.2",
|
"@types/http-errors": "^1.8.2",
|
||||||
"@types/jest": "^28.1.8",
|
"@types/jest": "^28.1.8",
|
||||||
"@types/jsonwebtoken": "^9.0.2",
|
"@types/jsonwebtoken": "^9.0.2",
|
||||||
"@types/lodash": "^4.14.195",
|
"@types/lodash": "^4.14.195",
|
||||||
"@types/morgan": "^1.9.4",
|
"@types/morgan": "^1.9.4",
|
||||||
"@types/node": "^14.0.0",
|
"@types/node": "^16.18.105",
|
||||||
"@types/parse-torrent": "^5.8.4",
|
"@types/parse-torrent": "^5.8.4",
|
||||||
"@types/passport": "^1.0.12",
|
"@types/passport": "^1.0.12",
|
||||||
"@types/passport-jwt": "^3.0.9",
|
"@types/passport-jwt": "^3.0.9",
|
||||||
@@ -232,9 +227,6 @@
|
|||||||
"last 2 iOS version",
|
"last 2 iOS version",
|
||||||
"last 2 Safari version"
|
"last 2 Safari version"
|
||||||
],
|
],
|
||||||
"bundleDependencies": [
|
|
||||||
"geoip-country"
|
|
||||||
],
|
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"*": "prettier --ignore-unknown -w"
|
"*": "prettier --ignore-unknown -w"
|
||||||
},
|
},
|
||||||
|
|||||||
343
pnpm-lock.yaml
generated
343
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
16
scripts/update-mmdb.mjs
Normal file
16
scripts/update-mmdb.mjs
Normal 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
1
server/geoip/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
GeoLite2-Country.mmdb
|
||||||
9
server/geoip/data.ts
Normal file
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
3
server/geoip/readme.md
Normal 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`
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import geoip from 'geoip-country';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
@@ -26,6 +25,7 @@ import type {TransferSummary} from '@shared/types/TransferData';
|
|||||||
import type {TransmissionConnectionSettings} from '@shared/schema/ClientConnectionSettings';
|
import type {TransmissionConnectionSettings} from '@shared/schema/ClientConnectionSettings';
|
||||||
import type {SetClientSettingsOptions} from '@shared/types/api/client';
|
import type {SetClientSettingsOptions} from '@shared/types/api/client';
|
||||||
|
|
||||||
|
import * as geoip from '../geoip';
|
||||||
import ClientGatewayService from '../clientGatewayService';
|
import ClientGatewayService from '../clientGatewayService';
|
||||||
import ClientRequestManager from './clientRequestManager';
|
import ClientRequestManager from './clientRequestManager';
|
||||||
import {fetchUrls} from '../../util/fetchUtil';
|
import {fetchUrls} from '../../util/fetchUtil';
|
||||||
@@ -195,7 +195,7 @@ class TransmissionClientGatewayService extends ClientGatewayService {
|
|||||||
.filter((peer) => peer.isDownloadingFrom || peer.isUploadingTo)
|
.filter((peer) => peer.isDownloadingFrom || peer.isUploadingTo)
|
||||||
.map((peer) => ({
|
.map((peer) => ({
|
||||||
address: peer.address,
|
address: peer.address,
|
||||||
country: geoip.lookup(peer.address)?.country || '',
|
country: geoip.lookup(peer.address),
|
||||||
clientVersion: peer.clientName,
|
clientVersion: peer.clientName,
|
||||||
completedPercent: peer.progress * 100,
|
completedPercent: peer.progress * 100,
|
||||||
downloadRate: peer.rateToClient,
|
downloadRate: peer.rateToClient,
|
||||||
|
|||||||
13
server/services/geoip.ts
Normal file
13
server/services/geoip.ts
Normal 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 '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import geoip from 'geoip-country';
|
|
||||||
import {move} from 'fs-extra';
|
import {move} from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import sanitize from 'sanitize-filename';
|
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 {TransferSummary} from '@shared/types/TransferData';
|
||||||
import type {SetClientSettingsOptions} from '@shared/types/api/client';
|
import type {SetClientSettingsOptions} from '@shared/types/api/client';
|
||||||
|
|
||||||
|
import * as geoip from '../geoip';
|
||||||
import {isAllowedPath, sanitizePath} from '../../util/fileUtil';
|
import {isAllowedPath, sanitizePath} from '../../util/fileUtil';
|
||||||
import ClientGatewayService from '../clientGatewayService';
|
import ClientGatewayService from '../clientGatewayService';
|
||||||
import ClientRequestManager from './clientRequestManager';
|
import ClientRequestManager from './clientRequestManager';
|
||||||
@@ -262,7 +262,7 @@ class RTorrentClientGatewayService extends ClientGatewayService {
|
|||||||
processedResponses.map(async (processedResponse) => {
|
processedResponses.map(async (processedResponse) => {
|
||||||
return {
|
return {
|
||||||
...processedResponse,
|
...processedResponse,
|
||||||
country: geoip.lookup(processedResponse.address)?.country || '',
|
country: geoip.lookup(processedResponse.address),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user