Skip to content

Commit

Permalink
Merge branch 'KartulUdus:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ReuschelCGN authored Nov 4, 2023
2 parents 8296db7 + 353fdd1 commit d2f9c80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 34 additions & 7 deletions src/routes/apiGeofence.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -20,7 +21,7 @@ module.exports = async (fastify, options, next) => {
}

try {
const url = await geofenceTileGenerator.generateGeofenceTile(fastify.geofence, fastify.query.tileserverPregen, req.params.area)
const url = await geofenceTileGenerator.generateGeofenceTile(fastify.geofence.geofence, fastify.query.tileserverPregen, req.params.area)
return {
status: 'ok',
url,
Expand Down Expand Up @@ -140,7 +141,7 @@ module.exports = async (fastify, options, next) => {
}

const areas = {}
for (const fence of fastify.geofence) {
for (const fence of fastify.geofence.geofence) {
areas[fence.name] = require('crypto').createHash('md5').update(JSON.stringify(fence.path)).digest('hex')
}

Expand Down Expand Up @@ -173,7 +174,7 @@ module.exports = async (fastify, options, next) => {

return {
status: 'ok',
geofence: fastify.geofence,
geofence: fastify.geofence.geofence,
}
})

Expand Down Expand Up @@ -202,7 +203,7 @@ module.exports = async (fastify, options, next) => {
type: 'FeatureCollection',
features: [],
}
const inGeoJSON = fastify.geofence
const inGeoJSON = fastify.geofence.geofence

for (let i = 0; i < inGeoJSON.length; i++) {
const inGeofence = inGeoJSON[i]
Expand All @@ -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]) {
Expand All @@ -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()
}
19 changes: 1 addition & 18 deletions src/util/koji.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit d2f9c80

Please sign in to comment.