From d69e10202c0aedbcdf818c2f26173a7962ee37bc Mon Sep 17 00:00:00 2001 From: Klemek Date: Sun, 4 Apr 2021 21:57:09 +0200 Subject: [PATCH] bot detector handling requests and disabling hit counter --- src/app.js | 24 ++++++++++++++++++++++-- src/hit_counter.js | 4 ++-- test/hit_counter.test.js | 14 +++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/app.js b/src/app.js index 3364ce2..70eb174 100644 --- a/src/app.js +++ b/src/app.js @@ -61,6 +61,23 @@ module.exports = (config) => { } }, ); + const botDetector = require('./bot_detector')(config); + botDetector.load((status, err) => { + switch (status) { + case botDetector.status.FETCH_OK: + console.log(cons.ok, 'fetched robots list'); + break; + case botDetector.status.FETCH_ERROR: + console.error(cons.error, 'error fetching robots list : ' + err); + break; + case botDetector.status.READ_OK: + console.log(cons.ok, `read robots list: ${botDetector.count}`); + break; + case botDetector.status.READ_ERROR: + console.error(cons.error, 'error reading robots list : ' + err); + break; + } + }); // set view engine from configuration app.set('view engine', config['view_engine']); @@ -145,6 +162,9 @@ module.exports = (config) => { }); app.use(limiter); + //detect robots + app.use(botDetector.handle); + //log request at result end app.use((req, res, next) => { if (config['access_log']) { @@ -168,7 +188,7 @@ module.exports = (config) => { if (err) { showError(req, res, 404); } else { - hc.count(req, '/', () => { + hc.count(req, '/', req.isRobot, () => { render(req, res, homePath, { articles: Object.values(articles) @@ -271,7 +291,7 @@ module.exports = (config) => { showError(req, res, 404); } } else { - hc.count(req, articlePath, () => { + hc.count(req, articlePath, req.isRobot, () => { renderer.render(article.realPath, (err, html) => { if (err) { console.log(cons.error, `failed to render article ${req.path} : ${err}`); diff --git a/src/hit_counter.js b/src/hit_counter.js index f2dc707..1b3cb4c 100644 --- a/src/hit_counter.js +++ b/src/hit_counter.js @@ -8,8 +8,8 @@ module.exports = (config, onConnect, onError) => { const visitors = {}; - const count = (req, path, cb) => { - if (!client.connected) { + const count = (req, path, disable, cb) => { + if (!client.connected || disable) { cb(); } else { const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; diff --git a/test/hit_counter.test.js b/test/hit_counter.test.js index c7e3788..a27c0b9 100644 --- a/test/hit_counter.test.js +++ b/test/hit_counter.test.js @@ -98,7 +98,7 @@ describe('read()', () => { hc.count({ headers: {}, connection: { remoteAddress: 'test1' }, - }, '/test/path/5', () => { + }, '/test/path/5', false, () => { hc.read('/test/path/5', (data) => { expect(data).toBeDefined(); expect(data.current_visitors).toBe(1); @@ -111,7 +111,7 @@ describe('read()', () => { hc.count({ headers: {}, connection: { remoteAddress: 'test1' }, - }, '/test/path/5', () => { + }, '/test/path/5', false, () => { hc.read('/test/path/5', (data) => { expect(data).toBeDefined(); expect(data.current_visitors).toBe(0); @@ -145,7 +145,7 @@ describe('count()', () => { hc.count({ headers: {}, connection: { remoteAddress: 'test1' }, - }, '/test/path/1', () => { + }, '/test/path/1', false, () => { expect(multiCalled).toBeTruthy(); expect(hincrbyCalls).toEqual([ [ @@ -177,11 +177,11 @@ describe('count()', () => { hc.count({ headers: {}, connection: { remoteAddress: 'test2' }, - }, '/test/path/2', () => { + }, '/test/path/2', false, () => { hc.count({ headers: {}, connection: { remoteAddress: 'test2' }, - }, '/test/path/2', () => { + }, '/test/path/2', false, () => { expect(hincrbyCalls).toEqual([ [ '/test/path/2', @@ -223,11 +223,11 @@ describe('count()', () => { hc.count({ headers: {}, connection: { remoteAddress: 'test3' }, - }, '/test/path/3', () => { + }, '/test/path/3', false, () => { hc.count({ headers: {}, connection: { remoteAddress: 'test3' }, - }, '/test/path/3', () => { + }, '/test/path/3', false, () => { expect(hincrbyCalls).toEqual([ [ '/test/path/3',