bot detector handling requests and disabling hit counter
This commit is contained in:
+22
-2
@@ -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
|
// set view engine from configuration
|
||||||
app.set('view engine', config['view_engine']);
|
app.set('view engine', config['view_engine']);
|
||||||
@@ -145,6 +162,9 @@ module.exports = (config) => {
|
|||||||
});
|
});
|
||||||
app.use(limiter);
|
app.use(limiter);
|
||||||
|
|
||||||
|
//detect robots
|
||||||
|
app.use(botDetector.handle);
|
||||||
|
|
||||||
//log request at result end
|
//log request at result end
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (config['access_log']) {
|
if (config['access_log']) {
|
||||||
@@ -168,7 +188,7 @@ module.exports = (config) => {
|
|||||||
if (err) {
|
if (err) {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
} else {
|
} else {
|
||||||
hc.count(req, '/', () => {
|
hc.count(req, '/', req.isRobot, () => {
|
||||||
render(req, res, homePath,
|
render(req, res, homePath,
|
||||||
{
|
{
|
||||||
articles: Object.values(articles)
|
articles: Object.values(articles)
|
||||||
@@ -271,7 +291,7 @@ module.exports = (config) => {
|
|||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hc.count(req, articlePath, () => {
|
hc.count(req, articlePath, req.isRobot, () => {
|
||||||
renderer.render(article.realPath, (err, html) => {
|
renderer.render(article.realPath, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
|
|||||||
+2
-2
@@ -8,8 +8,8 @@ module.exports = (config, onConnect, onError) => {
|
|||||||
|
|
||||||
const visitors = {};
|
const visitors = {};
|
||||||
|
|
||||||
const count = (req, path, cb) => {
|
const count = (req, path, disable, cb) => {
|
||||||
if (!client.connected) {
|
if (!client.connected || disable) {
|
||||||
cb();
|
cb();
|
||||||
} else {
|
} else {
|
||||||
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ describe('read()', () => {
|
|||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test1' },
|
connection: { remoteAddress: 'test1' },
|
||||||
}, '/test/path/5', () => {
|
}, '/test/path/5', false, () => {
|
||||||
hc.read('/test/path/5', (data) => {
|
hc.read('/test/path/5', (data) => {
|
||||||
expect(data).toBeDefined();
|
expect(data).toBeDefined();
|
||||||
expect(data.current_visitors).toBe(1);
|
expect(data.current_visitors).toBe(1);
|
||||||
@@ -111,7 +111,7 @@ describe('read()', () => {
|
|||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test1' },
|
connection: { remoteAddress: 'test1' },
|
||||||
}, '/test/path/5', () => {
|
}, '/test/path/5', false, () => {
|
||||||
hc.read('/test/path/5', (data) => {
|
hc.read('/test/path/5', (data) => {
|
||||||
expect(data).toBeDefined();
|
expect(data).toBeDefined();
|
||||||
expect(data.current_visitors).toBe(0);
|
expect(data.current_visitors).toBe(0);
|
||||||
@@ -145,7 +145,7 @@ describe('count()', () => {
|
|||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test1' },
|
connection: { remoteAddress: 'test1' },
|
||||||
}, '/test/path/1', () => {
|
}, '/test/path/1', false, () => {
|
||||||
expect(multiCalled).toBeTruthy();
|
expect(multiCalled).toBeTruthy();
|
||||||
expect(hincrbyCalls).toEqual([
|
expect(hincrbyCalls).toEqual([
|
||||||
[
|
[
|
||||||
@@ -177,11 +177,11 @@ describe('count()', () => {
|
|||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test2' },
|
connection: { remoteAddress: 'test2' },
|
||||||
}, '/test/path/2', () => {
|
}, '/test/path/2', false, () => {
|
||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test2' },
|
connection: { remoteAddress: 'test2' },
|
||||||
}, '/test/path/2', () => {
|
}, '/test/path/2', false, () => {
|
||||||
expect(hincrbyCalls).toEqual([
|
expect(hincrbyCalls).toEqual([
|
||||||
[
|
[
|
||||||
'/test/path/2',
|
'/test/path/2',
|
||||||
@@ -223,11 +223,11 @@ describe('count()', () => {
|
|||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test3' },
|
connection: { remoteAddress: 'test3' },
|
||||||
}, '/test/path/3', () => {
|
}, '/test/path/3', false, () => {
|
||||||
hc.count({
|
hc.count({
|
||||||
headers: {},
|
headers: {},
|
||||||
connection: { remoteAddress: 'test3' },
|
connection: { remoteAddress: 'test3' },
|
||||||
}, '/test/path/3', () => {
|
}, '/test/path/3', false, () => {
|
||||||
expect(hincrbyCalls).toEqual([
|
expect(hincrbyCalls).toEqual([
|
||||||
[
|
[
|
||||||
'/test/path/3',
|
'/test/path/3',
|
||||||
|
|||||||
Reference in New Issue
Block a user