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

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),
};
}),
);