diff --git a/src/app.js b/src/app.js index 0807aa157..b06df49e8 100644 --- a/src/app.js +++ b/src/app.js @@ -902,8 +902,12 @@ async function run() { setTimeout(processPogoEvents, 30000) setTimeout(processPossibleShiny, 30000) - let watchGeofence = Array.isArray(config.geofence.path) ? config.geofence.path : [config.geofence.path] - watchGeofence = watchGeofence.map((x) => path.join(__dirname, `../${x}`)) + let watchGeofence = Array.isArray(config.geofence.path) + ? config.geofence.path + : [config.geofence.path] + watchGeofence = watchGeofence.map((x) => (x.startsWith('http') + ? path.join(__dirname, '../.cache', `${x.replace(/\//g, '__')}.json`) + : path.join(__dirname, `../${x}`))) chokidar.watch(watchGeofence, { awaitWriteFinish: true, diff --git a/src/routes/apiGeofence.js b/src/routes/apiGeofence.js index e4d9cdc0a..f3a373494 100644 --- a/src/routes/apiGeofence.js +++ b/src/routes/apiGeofence.js @@ -1,4 +1,5 @@ const geofenceTileGenerator = require('../lib/geofenceTileGenerator') +const { getKojiFences } = require('../util/koji') module.exports = async (fastify, options, next) => { fastify.get('/api/geofence/:area/map', options, async (req) => { @@ -225,10 +226,10 @@ module.exports = async (fastify, options, next) => { const outPath = [] if (inGeofence.multipath) { for (let j = 0; j < inGeofence.multipath.length; j++) { - const path = inGeofence.multipath[j] + const geofencePath = inGeofence.multipath[j] const outSubPath = [] - for (let k = 0; k < path.length; k++) { - const coord = path[k] + for (let k = 0; k < geofencePath.length; k++) { + const coord = geofencePath[k] outSubPath.push([coord[1], coord[0]]) } if (outSubPath.at(-1)[0] !== outSubPath[0][0] || outSubPath.at(-1)[1] !== outSubPath[0][1]) { @@ -255,5 +256,31 @@ module.exports = async (fastify, options, next) => { } }) + fastify.get('/api/geofence/reload', options, async (req) => { + fastify.logger.info(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`) + + if (fastify.config.server.ipWhitelist.length && !fastify.config.server.ipWhitelist.includes(req.ip)) { + return { + webserver: 'unhappy', + reason: `ip ${req.ip} not in whitelist`, + } + } + if (fastify.config.server.ipBlacklist.length && fastify.config.server.ipBlacklist.includes(req.ip)) { + return { + webserver: 'unhappy', + reason: `ip ${req.ip} in blacklist`, + } + } + + const secret = req.headers['x-poracle-secret'] + if (!secret || !fastify.config.server.apiSecret || secret !== fastify.config.server.apiSecret) { + return { status: 'authError', reason: 'incorrect or missing api secret' } + } + + await getKojiFences() + + return { status: 'ok' } + }) + next() } diff --git a/src/util/koji.js b/src/util/koji.js index 63d277dce..2d8ddf724 100644 --- a/src/util/koji.js +++ b/src/util/koji.js @@ -1,28 +1,11 @@ const fs = require('fs') const { resolve } = require('path') const fetch = require('node-fetch') -const json5 = require('json5') +const config = require('config') const { log } = require('../lib/logger') -const parseConfigSafe = () => { - let config = {} - try { - if (fs.existsSync(resolve(__dirname, '../../config/local.json'))) { - const string = fs.readFileSync(resolve(__dirname, '../../config/local.json')).toString() - config = json5.parse(string) - } else { - log.warn('[KŌJI] Local.json was not found, skipping') - } - return config - } catch (e) { - log.error('[KŌJI] Could not parse local config', e) - return config - } -} - const getKojiFences = async () => { - const config = parseConfigSafe() try { if (config?.geofence?.kojiOptions?.bearerToken) { const fences = Array.isArray(config.geofence?.path)