2023-03-02 01:42:47 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2022-11-04 05:33:37 +01:00
|
|
|
/* eslint-disable no-console */
|
2020-09-27 05:21:16 +02:00
|
|
|
require('dotenv').config();
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const https = require('https');
|
|
|
|
const zlib = require('zlib');
|
|
|
|
const tar = require('tar');
|
|
|
|
|
2023-03-29 04:24:36 +02:00
|
|
|
const db = 'GeoLite2-City';
|
|
|
|
|
|
|
|
let url = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${db}.tar.gz`;
|
2020-09-27 05:21:16 +02:00
|
|
|
|
|
|
|
if (process.env.MAXMIND_LICENSE_KEY) {
|
|
|
|
url =
|
|
|
|
`https://download.maxmind.com/app/geoip_download` +
|
2023-03-29 04:24:36 +02:00
|
|
|
`?edition_id=${db}&license_key=${process.env.MAXMIND_LICENSE_KEY}&suffix=tar.gz`;
|
2020-09-27 05:21:16 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 04:24:36 +02:00
|
|
|
const dest = path.resolve(__dirname, '../geo');
|
2020-09-27 05:21:16 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync(dest)) {
|
|
|
|
fs.mkdirSync(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
const download = url =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
https.get(url, res => {
|
|
|
|
resolve(res.pipe(zlib.createGunzip({})).pipe(tar.t()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
download(url).then(
|
|
|
|
res =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
res.on('entry', entry => {
|
|
|
|
if (entry.path.endsWith('.mmdb')) {
|
|
|
|
const filename = path.join(dest, path.basename(entry.path));
|
|
|
|
entry.pipe(fs.createWriteStream(filename));
|
|
|
|
|
|
|
|
console.log('Saved geo database:', filename);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on('error', e => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
res.on('finish', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|