forked from runk/node-geolite2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
155 lines (132 loc) · 4.04 KB
/
utils.js
File metadata and controls
155 lines (132 loc) · 4.04 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const path = require('path');
const fs = require('fs');
const getConfigWithDir = () => {
const cwd = process.env['INIT_CWD'] || process.cwd();
let dir = cwd;
// Find a package.json with geolite2 configuration key at or above the level
// of this directory.
while (fs.existsSync(dir)) {
const packageJSON = path.join(dir, 'package.json');
if (fs.existsSync(packageJSON)) {
const contents = require(packageJSON);
const config = contents['geolite2'];
if (config) return { config, dir };
}
const parentDir = path.resolve(dir, '..');
if (parentDir === dir) break;
dir = parentDir;
}
console.log(
"INFO: geolite2 cannot find configuration in package.json file, using defaults.\n" +
"INFO: geolite2 expects to have 'MAXMIND_ACCOUNT_ID' and 'MAXMIND_LICENSE_KEY' to be present in environment variables when package.json is unavailable.",
);
console.log(
'INFO: geolite2 expected package.json to be present at a parent of:\n%s',
cwd
);
};
const getConfig = () => {
const configWithDir = getConfigWithDir();
if (!configWithDir) return;
return configWithDir.config;
};
const getAccountId = () => {
const envId = process.env.MAXMIND_ACCOUNT_ID;
if (envId) return envId;
const config = getConfig();
if (!config) return;
return config['account-id'];
}
const getLicense = () => {
const envKey = process.env.MAXMIND_LICENSE_KEY;
if (envKey) return envKey;
const configWithDir = getConfigWithDir();
if (!configWithDir) return;
const { config, dir } = configWithDir;
const licenseKey = config['license-key'];
if (licenseKey) return licenseKey;
const configFile = config['license-file'];
if (!configFile) return;
const configFilePath = path.join(dir, configFile);
return fs.existsSync(configFilePath)
? fs.readFileSync(configFilePath, 'utf8').trim()
: undefined;
};
const getSelectedDbs = () => {
const aliases = ['ASN', 'City', 'Country'];
const defaultEditions = ['GeoLite2-ASN', 'GeoLite2-City', 'GeoLite2-Country'];
const validEditions = [
'GeoIP-Anonymous-Plus',
'GeoIP-Network-Optimization-City',
'GeoIP2-Anonymous-IP',
'GeoIP2-City',
'GeoIP2-City-Africa',
'GeoIP2-City-Asia-Pacific',
'GeoIP2-City-Europe',
'GeoIP2-City-North-America',
'GeoIP2-City-Shield',
'GeoIP2-City-South-America',
'GeoIP2-Connection-Type',
'GeoIP2-Country',
'GeoIP2-Country-Shield',
'GeoIP2-DensityIncome',
'GeoIP2-Domain',
'GeoIP2-Enterprise',
'GeoIP2-Enterprise-Shield',
'GeoIP2-IP-Risk',
'GeoIP2-ISP',
'GeoIP2-Precision-Enterprise',
'GeoIP2-Precision-Enterprise-Shield',
'GeoIP2-Static-IP-Score',
'GeoIP2-User-Connection-Type',
'GeoIP2-User-Count',
'GeoLite2-ASN',
'GeoLite2-City',
'GeoLite2-Country',
];
const config = getConfig();
const selectedWithPossibleAliases =
config != null && config['selected-dbs'] != null
? config['selected-dbs']
: defaultEditions;
if (!Array.isArray(selectedWithPossibleAliases)) {
console.error('selected-dbs property must be an array.');
process.exit(1);
}
if (selectedWithPossibleAliases.length === 0) return defaultEditions;
const selectedEditions = selectedWithPossibleAliases.map((element) => {
index = aliases.indexOf(element);
if (index > -1) {
return `GeoLite2-${element}`;
} else {
return element;
}
});
const validValuesText = validEditions.join(', ');
if (selectedEditions.length > validEditions.length) {
console.error(
'Property selected-dbs has too many values, there are only %d valid values: %s',
validEditions.length,
validValuesText,
);
process.exit(1);
}
for (const value of selectedEditions) {
const index = validEditions.indexOf(value);
if (index === -1) {
console.error(
'Invalid value in selected-dbs: %s The only valid values are: %s',
value,
validValuesText,
);
process.exit(1);
}
}
return selectedEditions;
};
module.exports = {
getConfig,
getAccountId,
getLicense,
getSelectedDbs,
};