This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-downloader.js
111 lines (92 loc) · 3.95 KB
/
db-downloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict';
async function maxmind(type){
const https = require('https'); // or 'https' for https:// URLs
const fs = require('fs');
console.log(process.argv);
//prompt input
// const argv = require('yargs-parser')(process.argv.slice(3));
let dbType = type === 'asn' ? 'asn' : 'city';
console.log(dbType);
const mmUid = 530667;
const mmKey = 'eUvNCKA9SwdtNdQj';
const asnUri = `https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=${mmKey}&suffix=tar.gz`;
const cityUri = `https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${mmKey}&suffix=tar.gz`;
console.log(asnUri, cityUri);
let tarFilename = dbType === 'asn' ? './data/GeoLite2-ASN.dl.tar.gz' : './data/GeoLite2-City.dl.tar.gz';
const dl = require('./modules/dl.js');
var promise = new Promise((resolve, reject) => {
dl(dbType === 'asn' ? asnUri : cityUri, tarFilename)
.then((res) => {
console.log(res);
console.log('Download completed.');
console.log('Begin extracting.');
const path = require('path');
const decompress = require('decompress');
const decompressTargz = require('decompress-targz');
decompress(tarFilename, './data/', {
plugins : [
decompressTargz()
],
map: fobj => { console.log(path.parse(fobj.path)); fobj.path = path.parse(fobj.path).base; return fobj },
filter: fobj => { return path.extname(fobj.path) === '.mmdb' } ,
}).then((files) => {
console.log(files);
console.log('Files decompressed');
resolve(files);
}).finally(() => {
console.info('Remove downloaded tar file.');
fs.unlinkSync(tarFilename);
});
})
.catch((err) => {
console.log('Error downloading');
console.error(err);
reject(err);
});
});
return promise;
}
async function ip2location(type){
const fs = require('fs');
let dlToken = 'JG0h3ZT3pnGMahjdEM6aIoZeY4NBcyXJ8yiZfc36LOsYlMWIhhCzALjdL4dBbQAN';
let dbCode = 'PX10LITEBIN';
//https://www.ip2location.com/download/?token={DOWNLOAD_TOKEN}&file={DATABASE_CODE}
let dbUrl = `https://www.ip2location.com/download/?token=${dlToken}&file=${dbCode}`;
// let tarFilename = dbType === 'asn' ? './data/IP2PROXY-LITE-PX10.BIN.dl.zip' : './data/GeoLite2-City.dl.tar.gz';
let tarFilename = './data/IP2PROXY-LITE-PX10.BIN.dl.zip';
const dl = require('./modules/dl.js');
console.log(dbUrl);
var promise = new Promise((resolve, reject) => {
dl(dbUrl, tarFilename)
.then((res) => {
console.log(res);
console.log('Download completed.');
console.log('Begin extracting.');
const path = require('path');
const decompress = require('decompress');
decompress(tarFilename, './data/', {
map: fobj => { console.log(path.parse(fobj.path)); fobj.path = path.parse(fobj.path).base; return fobj },
filter: fobj => { return path.extname(fobj.path) === '.BIN' } ,
}).then((files) => {
console.log(files);
console.log('Files decompressed');
resolve(files);
}).finally(() => {
console.info('Remove downloaded zip file.');
fs.unlinkSync(tarFilename);
});
})
.catch((err) => {
console.log('Error downloading');
console.error(err);
reject(err);
});
});
return promise;
}
module.exports = {maxmind: maxmind, ip2location: ip2location};
const argv = require('yargs-parser')(process.argv.slice(2));
if(argv.from === 'maxmind' || argv.from === 'ip2location'){
let method = argv.from;
let rs = module.exports[method](argv.type);
}