-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathutils.js
71 lines (65 loc) · 2.13 KB
/
utils.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
const {CronJob} = require('cron')
const moment = require('moment')
const request = require('request')
const zlib = require('zlib')
const maxmind = require('maxmind')
const fs = require('fs')
const conf = require('nconf')
conf.file({file: 'cfg.json'})
conf.defaults({
port: 3013,
bind: '127.0.0.1',
servers: [{
name: 'Server', host: '127.0.0.1', man_port: 7656
}],
ipFile: './GeoLite2-City.mmdb',
username: 'admin',
password: 'admin',
web: {
dateFormat: 'HH:mm:ss - DD.MM.YY'
}
})
const envVars = process.env
conf.set('username', envVars.AUTH_USERNAME || conf.get('username'))
conf.set('password', envVars.AUTH_PASSWORD || conf.get('password'))
const web = conf.get('web')
web.dateFormat = envVars.VPN_DATE_FORMAT || web.dateFormat
conf.set('web', web)
if (envVars.VPN_NAME && envVars.VPN_HOST && envVars.VPN_MAN_PORT)
conf.set('servers', [{name: envVars.VPN_NAME, host: envVars.VPN_HOST, man_port: envVars.VPN_MAN_PORT}])
const log = (...args) => console.log(...[moment().format(conf.get('web').dateFormat), ...args])
const loadIPdatabase = () => {
const ipFile = conf.get('ipFile')
const loadFile = res => maxmind.open('./GeoLite2-City.mmdb', (err, lookup) => {
if (err)
log(err)
else
res(ip => (ip ? lookup.get(ip) : false))
})
return new Promise(resolve => {
fs.stat(ipFile, (err, stat) => {
const now = new Date().getTime()
// Cached version to expire after a month from file date
const expire = new Date((stat ? stat.ctime : '')).getTime() + 30 * 24 * 60 * 60 * 1000
if (err || now > expire) {
const req = request('https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz')
req.on('response', resp => {
if (resp.statusCode === 200)
req.pipe(zlib.createGunzip()).pipe(fs.createWriteStream(ipFile))
.on('finish', () => {
loadFile(resolve)
})
else
loadFile(resolve)
})
} else
loadFile(resolve)
})
})
}
const _ = new CronJob({
cronTime: '00 10 * 10 * *',
onTick: loadIPdatabase,
start: true
})
module.exports = {log, loadIPdatabase, conf}