-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathconfig.js
475 lines (442 loc) · 13.5 KB
/
config.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* eslint-disable no-console */
process.env.NODE_CONFIG_DIR = `${__dirname}/../configs`
const fs = require('fs')
const { resolve } = require('path')
const dotenv = require('dotenv')
const { default: center } = require('@turf/center')
dotenv.config()
const config = require('config')
const allowedMenuItems = [
'gyms',
'nests',
'pokestops',
'pokemon',
'wayfarer',
'scanAreas',
'weather',
'admin',
'settings',
]
try {
const refLength = +fs.readFileSync(
resolve(__dirname, '../../../.configref'),
'utf8',
)
const defaultLength = fs.readFileSync(
resolve(__dirname, '../configs/default.json'),
'utf8',
).length
if (refLength !== defaultLength) {
console.error(
'[CONFIG] It looks like you have modified the `default.json` file, you should not do this! Make all of your config changes in your `local.json` file.',
)
}
} catch (e) {
console.error(
'[CONFIG] Error trying to read either the default.json or .ref file',
e,
)
}
if (!fs.existsSync(resolve(`${__dirname}/../configs/local.json`))) {
// add database env variables from .env or docker-compose
const {
SCANNER_DB_HOST,
SCANNER_DB_PORT,
SCANNER_DB_NAME,
SCANNER_DB_USERNAME,
SCANNER_DB_PASSWORD,
REACT_MAP_DB_HOST,
REACT_MAP_DB_PORT,
REACT_MAP_DB_USERNAME,
REACT_MAP_DB_PASSWORD,
REACT_MAP_DB_NAME,
MANUAL_DB_HOST,
MANUAL_DB_PORT,
MANUAL_DB_NAME,
MANUAL_DB_USERNAME,
MANUAL_DB_PASSWORD,
MAP_GENERAL_START_LAT,
MAP_GENERAL_START_LON,
} = process.env
const hasScannerDb =
SCANNER_DB_HOST &&
SCANNER_DB_PORT &&
SCANNER_DB_NAME &&
SCANNER_DB_USERNAME &&
SCANNER_DB_PASSWORD
const hasReactMapDb =
REACT_MAP_DB_HOST &&
REACT_MAP_DB_PORT &&
REACT_MAP_DB_USERNAME &&
REACT_MAP_DB_PASSWORD &&
REACT_MAP_DB_NAME
const hasManualDb =
MANUAL_DB_HOST &&
MANUAL_DB_PORT &&
MANUAL_DB_NAME &&
MANUAL_DB_USERNAME &&
MANUAL_DB_PASSWORD
if (hasScannerDb) {
config.database.schemas.push({
host: SCANNER_DB_HOST,
port: +SCANNER_DB_PORT,
database: SCANNER_DB_NAME,
username: SCANNER_DB_USERNAME,
password: SCANNER_DB_PASSWORD,
useFor: [
'device',
'gym',
'pokemon',
'pokestop',
'scanCell',
'spawnpoint',
'weather',
],
})
} else {
console.error(
'Missing scanner database config! \nCheck to make sure you have SCANNER_DB_HOST,SCANNER_DB_PORT, SCANNER_DB_NAME, SCANNER_DB_USERNAME, and SCANNER_DB_PASSWORD',
)
}
if (hasReactMapDb) {
config.database.schemas.push({
host: REACT_MAP_DB_HOST,
port: +REACT_MAP_DB_PORT,
database: REACT_MAP_DB_NAME,
username: REACT_MAP_DB_USERNAME,
password: REACT_MAP_DB_PASSWORD,
useFor: ['session', 'user'],
})
} else {
console.log(
'Missing ReactMap specific table, attempting to use the manual database instead.',
)
}
if (hasManualDb) {
config.database.schemas.push({
host: MANUAL_DB_HOST,
port: +MANUAL_DB_PORT,
database: MANUAL_DB_NAME,
username: MANUAL_DB_USERNAME,
password: MANUAL_DB_PASSWORD,
useFor: hasReactMapDb
? ['nest', 'portal']
: ['session', 'user', 'nest', 'portal'],
})
} else {
console.error(
'Missing manual database config! \nCheck to make sure you have MANUAL_DB_HOST,MANUAL_DB_PORT, MANUAL_DB_NAME, MANUAL_DB_USERNAME, and MANUAL_DB_PASSWORD',
)
}
if (!MAP_GENERAL_START_LAT || !MAP_GENERAL_START_LON) {
console.warn(
'Missing, MAP_GENERAL_START_LAT OR MAP_GENERAL_START_LON\nYou will be able to proceed but you should add these values to your docker-compose file',
)
}
}
if (fs.existsSync(resolve(`${__dirname}/../configs/config.json`))) {
console.log(
'[CONFIG] Config v1 (config.json) found, it is fine to leave it but make sure you are using and updating local.json instead.',
)
}
const checkExtraJsons = (fileName, domain = '') => {
const generalJson = fs.existsSync(
resolve(`${__dirname}/../configs/${fileName}.json`),
)
? JSON.parse(
fs.readFileSync(resolve(__dirname, `../configs/${fileName}.json`)),
)
: {}
if (Object.keys(generalJson).length) {
console.log(
`[CONFIG] config ${fileName}.json found, overwriting your config.map.${fileName} with the found data.`,
)
}
if (
domain &&
fs.existsSync(resolve(`${__dirname}/../configs/${fileName}/${domain}.json`))
) {
const domainJson =
JSON.parse(
fs.readFileSync(
resolve(__dirname, `../configs/${fileName}/${domain}.json`),
),
) || {}
if (Object.keys(domainJson).length) {
console.log(
`[CONFIG] config ${fileName}/${domain}.json found, overwriting your config.map.${fileName} with the found data.`,
)
}
return {
components: [],
...generalJson,
...domainJson,
}
}
return generalJson
}
const mergeMapConfig = (obj) => {
if (process.env.TELEGRAM_BOT_NAME && !obj?.customRoutes?.telegramBotName) {
if (obj.customRoutes)
obj.customRoutes.telegramBotName = process.env.TELEGRAM_BOT_NAME
console.warn(
'[CONFIG] TELEGRAM_BOT_NAME has been moved from the .env file to your config, telegramBotEnvRef is now deprecated.\nplease use customRoutes.telegramBotName instead\n(Move them from your .env file to your config file)',
)
}
if (obj?.customRoutes?.telegramBotEnvRef) {
console.warn(
'[CONFIG] TELEGRAM_BOT_NAME has been moved from the .env file to your config, telegramBotEnvRef is now deprecated.\nplease use customRoutes.telegramBotName instead\n(Move them from your .env file to your config file)',
)
obj.customRoutes.telegramBotName =
process.env[obj.customRoutes.telegramBotEnvRef]
}
;['messageOfTheDay', 'donationPage', 'loginPage'].forEach((category) => {
if (obj?.[category]?.components) {
obj[category].components.forEach((component) => {
if (component.type === 'telegram' && component.telegramBotEnvRef) {
console.warn(
'[CONFIG] telegramBotEnvRef is deprecated, please use telegramBotName instead\n',
category,
)
console.warn('OLD:\n', component)
component.telegramBotName = process.env[component.telegramBotEnvRef]
delete component.telegramBotEnvRef
console.warn('NEW:\n', component)
}
})
}
})
if (
obj?.holidayEffects &&
!Array.isArray(obj?.holidayEffects) &&
typeof obj?.holidayEffects === 'object'
) {
console.warn(
'[CONFIG] holidayEffects has been changed to an array, please update your config. Check out `server/src/configs/default.json` for an example.',
)
obj.holidayEffects = []
}
const menuOrder = obj?.general?.menuOrder
? obj.general.menuOrder.filter((x) => allowedMenuItems.includes(x))
: []
allowedMenuItems.forEach((item) => {
if (!menuOrder.includes(item)) {
menuOrder.push(item)
}
})
return {
localeSelection: obj.localeSelection,
...obj,
...obj.general,
menuOrder,
...obj.customRoutes,
...obj.links,
...obj.misc,
messageOfTheDay: {
...config.map.messageOfTheDay,
...obj.messageOfTheDay,
...checkExtraJsons('messageOfTheDay', obj.domain),
},
donationPage: {
...config.map.donationPage,
...obj.donationPage,
...checkExtraJsons('donationPage', obj.domain),
},
loginPage: {
...config.map.loginPage,
...obj.loginPage,
...checkExtraJsons('loginPage', obj.domain),
},
general: undefined,
customRoutes: undefined,
links: undefined,
misc: undefined,
}
}
// Merge sub-objects for the map object
config.map = mergeMapConfig(config.map)
// Create multiDomain Objects
config.multiDomainsObj = Object.fromEntries(
config.multiDomains.map((d) => [d.domain, mergeMapConfig(d)]),
)
// Consolidate Auth Methods
// Create Authentication Objects
config.authMethods = [
...new Set(
config.authentication.strategies
.filter((strategy) => strategy.enabled)
.map((strategy) => {
config.authentication[strategy.name] = strategy
return strategy.type
}),
),
]
// Check if empty
;['tileServers', 'navigation'].forEach((opt) => {
if (!config[opt].length) {
console.warn(
`[${opt}] is empty, you need to add options to it or remove the empty array from your config.`,
)
}
})
const manualGeojson = {
type: 'FeatureCollection',
features: config.manualAreas
.filter((area) =>
['lat', 'lon', 'name'].every((k) => k in area && !area.hidden),
)
.map((area) => {
const { lat, lon, ...rest } = area
return {
type: 'Feature',
properties: {
center: [lat, lon],
manual: true,
key: rest.parent ? `${rest.parent}-${rest.name}` : rest.name,
...rest,
},
geometry: {
type: 'Polygon',
coordinates: [[[lon, lat]]],
},
}
}),
}
// Load each areas.json
const loadScanPolygons = (fileName, domain) => {
const geojson = fs.existsSync(resolve(`${__dirname}/../configs/${fileName}`))
? JSON.parse(fs.readFileSync(resolve(__dirname, `../configs/${fileName}`)))
: { features: [] }
return {
...geojson,
features: [
...manualGeojson.features.filter(
(f) => !f.properties.domain || f.properties.domain === domain,
),
...geojson.features.map((f) => ({
...f,
properties: {
...f.properties,
key: f.properties.parent
? `${f.properties.parent}-${f.properties.name}`
: f.properties.name,
center: center(f).geometry.coordinates.reverse(),
},
})),
].sort((a, b) => a.properties.name.localeCompare(b.properties.name)),
}
}
// Check if an areas.json exists
config.scanAreas = {
main: loadScanPolygons(config.map.geoJsonFileName),
...Object.fromEntries(
config.multiDomains.map((d) => [
d.general?.geoJsonFileName ? d.domain : 'main',
loadScanPolygons(
d.general?.geoJsonFileName || config.map.geoJsonFileName,
),
]),
),
}
config.scanAreasMenu = Object.fromEntries(
Object.entries(config.scanAreas).map(([domain, areas]) => {
const parents = { '': { children: [], name: '' } }
const noHidden = {
...areas,
features: areas.features.filter((f) => !f.properties.hidden),
}
// Finds unique parents and determines if the parents have their own properties
noHidden.features.forEach((feature) => {
if (feature.properties.parent) {
parents[feature.properties.parent] = {
name: feature.properties.parent,
details: areas.features.find(
(area) => area.properties.name === feature.properties.parent,
),
children: [],
}
}
})
// Finds the children of each parent
noHidden.features.forEach((feature) => {
if (feature.properties.parent) {
parents[feature.properties.parent].children.push(feature)
} else if (!parents[feature.properties.name]) {
parents[''].children.push(feature)
}
})
// Create blanks for better formatting when there's an odd number of children
Object.values(parents).forEach(({ children }) => {
if (children.length % 2 === 1) {
children.push({
type: 'Feature',
properties: { name: '', manual: !!config.manualAreas.length },
})
}
})
return [
domain,
Object.values(parents).sort((a, b) => a.name.localeCompare(b.name)),
]
}),
)
config.scanAreasObj = Object.fromEntries(
Object.values(config.scanAreas)
.flatMap((areas) => areas.features)
.map((feature) => [feature.properties.name, feature]),
)
config.api.pvp.leagueObj = Object.fromEntries(
config.api.pvp.leagues.map((league) => [league.name, league.cp]),
)
const hasLittle = config.api.pvp.leagues.find(
(league) => league.name === 'little',
)
if (hasLittle) {
config.api.pvp.leagueObj.little = hasLittle.littleCupRules
? 500
: { little: false, cap: 500 }
}
const aliasObj = Object.fromEntries(
config.authentication.aliases.map((alias) => [alias.name, alias.role]),
)
const replaceAliases = (role) => aliasObj[role] ?? role
Object.keys(config.authentication.perms).forEach((perm) => {
config.authentication.perms[perm].roles =
config.authentication.perms[perm].roles.map(replaceAliases)
})
config.authentication.areaRestrictions =
config.authentication.areaRestrictions.map(({ roles, areas }) => ({
roles: roles.map(replaceAliases),
areas,
}))
config.authentication.strategies = config.authentication.strategies.map(
(strategy) => ({
...strategy,
allowedGuilds: Array.isArray(strategy.allowedGuilds)
? strategy.allowedGuilds.map(replaceAliases)
: undefined,
blockedGuilds: Array.isArray(strategy.blockedGuilds)
? strategy.blockedGuilds.map(replaceAliases)
: undefined,
groups: Array.isArray(strategy.groups)
? strategy.groups.map(replaceAliases)
: undefined,
allowedUsers: Array.isArray(strategy.allowedUsers)
? strategy.allowedUsers.map(replaceAliases)
: undefined,
}),
)
if (
!config.authentication.strategies.length ||
!config.authentication.strategies.find((strategy) => strategy.enabled)
) {
const enabled = Object.keys(config.authentication.perms).filter(
(perm) => config.authentication.perms[perm].enabled,
)
console.warn(
'[CONFIG] No authentication strategies enabled, adding the following perms to alwaysEnabledPerms array:\n',
enabled,
)
config.authentication.alwaysEnabledPerms = enabled
}
module.exports = config