bot detector handling requests and disabling hit counter

This commit is contained in:
Klemek
2021-04-04 21:57:09 +02:00
parent 140e472e29
commit d69e10202c
3 changed files with 31 additions and 11 deletions
+22 -2
View File
@@ -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}`);
+2 -2
View File
@@ -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;
+7 -7
View File
@@ -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',