diff --git a/libs/pixelItRepo.js b/libs/pixelItRepo.js index 04187d9..5ea6ae2 100644 --- a/libs/pixelItRepo.js +++ b/libs/pixelItRepo.js @@ -105,7 +105,7 @@ async function getBMPNewst() { } } -async function saveStats(telemetry) { +async function saveTelemetry(telemetry) { try { await connection.execute( `REPLACE INTO pixel_it_telemetry @@ -167,6 +167,18 @@ async function saveBMP(bmp) { } } +async function isTelemetryUser(uuid) { + let sqlResult + try { + sqlResult = await connection.query(`SELECT EXISTS(SELECT uuid FROM pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY AND UUID = ? LIMIT 1) as isTelemetryUser`, uuid); + console.log(sqlResult[0]) + return sqlResult[0][0].isTelemetryUser == 1 + } catch (error) { + log.error('isTelemetryUser: {error}', { error: error }) + return false + } +}; + async function getUserMapData() { let sqlResult const result = []; @@ -219,8 +231,9 @@ module.exports = { getBMPByID, getBMPAll, getBMPNewst, - saveStats, + saveTelemetry, saveBMP, getUserMapData, getStatistics, + isTelemetryUser, }; diff --git a/main.js b/main.js index 902b6c4..133e986 100644 --- a/main.js +++ b/main.js @@ -71,30 +71,54 @@ app.post('/api/Telemetry', telemetryLimiter, async (req, res) => { return; } + if (!req.body.uuid || req.body.uuid.length != 40) { + log.error('{apiPath}: UUID is not valide', { apiPath: 'Telemetry', sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, client, }); + res.status(400).send('Not valid body'); + return; + } + (async () => { req.body.geoip = await geoip.lookup(sourceIP); log.info(`{apiPath}: ${JSON.stringify(req.body)}`, { apiPath: 'Telemetry', sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, client, }); - repo.saveStats(req.body); + repo.saveTelemetry(req.body); })(); res.sendStatus(200); }); -app.get('/api/UserMapData', async (req, res) => { +app.get(['/api/UserMap'], async (req, res) => { const sourceIP = tools.getIPFromRequest(req); const rawUrl = tools.getRawURLFromRequest(req); - const userMapData = (await cache.getOrSet('UserMapData', () => { return repo.getUserMapData() }, 30)) ?? []; - log.info('{apiPath}: {count} User successfully delivered', { apiPath: 'UserMapData', count: userMapData.length, sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, }); - res.send(userMapData); + const isTelemetryUser = (await cache.getOrSet(`IsTelemetryUser_UUID:${req.query.uuid}`, () => { return repo.isTelemetryUser(req.query.uuid) }, 30)) ?? {}; + + if (!isTelemetryUser && isTelemetryUser == false) { + res.send({ coords: [], error: { telemetryUser: false } }); + log.info('{apiPath}: UserMap NOT delivered, reason: no telemetry user!', { apiPath: 'UserMap', sourceIP, rawUrl, useragent: req.useragent, uuid: req.query.uuid, rateLimit: req.rateLimit, }); + return; + } + + const userMapData = (await cache.getOrSet('UserMap', () => { return repo.getUserMapData() }, 30)) ?? []; + + log.info('{apiPath}: UserMap ({count} user) successfully delivered', { apiPath: 'UserMap', count: userMapData.length, sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, }); + res.send({ coords: userMapData }); }); app.get('/api/Statistics', async (req, res) => { const sourceIP = tools.getIPFromRequest(req); const rawUrl = tools.getRawURLFromRequest(req); + + const isTelemetryUser = (await cache.getOrSet(`IsTelemetryUser_UUID:${req.query.uuid}`, () => { return repo.isTelemetryUser(req.query.uuid) }, 30)) ?? {}; + + if (!isTelemetryUser && isTelemetryUser == false) { + res.send({ error: { telemetryUser: false } }); + log.info('{apiPath}: Statistics NOT delivered, reason: no telemetry user!', { apiPath: 'Statistics', sourceIP, rawUrl, useragent: req.useragent, uuid: req.query.uuid, rateLimit: req.rateLimit, }); + return; + } + const statistics = (await cache.getOrSet('Statistics', () => { return repo.getStatistics() }, 30)) ?? {}; - log.info('{apiPath}: Statistics successfully delivered', { apiPath: 'Statistics', sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, }); + log.info('{apiPath}: Statistics successfully delivered', { apiPath: 'Statistics', sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, uuid: req.query.uuid, }); res.send(statistics); });