Skip to content

Commit

Permalink
only active telemetry users can see statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
o0shojo0o committed Nov 8, 2023
1 parent 430dc16 commit b9e499d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
17 changes: 15 additions & 2 deletions libs/pixelItRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function getBMPNewst() {
}
}

async function saveStats(telemetry) {
async function saveTelemetry(telemetry) {
try {
await connection.execute(
`REPLACE INTO pixel_it_telemetry
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -219,8 +231,9 @@ module.exports = {
getBMPByID,
getBMPAll,
getBMPNewst,
saveStats,
saveTelemetry,
saveBMP,
getUserMapData,
getStatistics,
isTelemetryUser,
};
36 changes: 30 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit b9e499d

Please sign in to comment.