generated from NodeBB/nodebb-plugin-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.js
124 lines (107 loc) · 2.71 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const nconf = require.main.require('nconf');
const winston = require.main.require('winston');
const controllers = require('./lib/controllers');
const settings = require.main.require('./src/meta/settings');
const routeHelpers = require.main.require('./src/routes/helpers');
const privsGlobal = require.main.require("./src/privileges/global")
const request = require('request');
const plugin = {};
let plugin_data = {};
const hooks = [{
method: 'post',
url: '/api/v3/topics'
}];
function checkHooks(method, url) {
if (method) {
for (const i in hooks) {
if (method === hooks[i].method && url.startsWith(hooks[i].url)) {
return true;
}
}
}
return false;
}
function checkSwitch() {
if (plugin_data.switch !== 'on') {
return false;
}
if (plugin_data.server_key === undefined) {
return false;
}
if (plugin_data.client_key === undefined) {
return false;
}
return true;
}
async function checkToken(token) {
return new Promise((resolve) => {
request.post('https://www.recaptcha.net/recaptcha/api/siteverify',
{
form: {
secret: plugin_data.server_key,
response: token
}
}, (err, rsp, body) => {
if (rsp.statusCode === 200) {
let data = JSON.parse(body);
if (data.success) {
resolve(true);
}
resolve(false);
}
resolve(false);
});
});
}
plugin.init = async (params) => {
const {
router,
middleware/* , controllers */
} = params;
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/antibot', middleware, [], controllers.renderAdminPage);
plugin_data = await settings.get('antibot');
};
plugin.addAdminNavigation = (header) => {
header.plugins.push({
route: '/plugins/antibot',
icon: 'fa-tint',
name: 'AntiBot',
});
return header;
};
plugin.hookFooter = (data) => {
if (!checkSwitch()) {
return data;
}
let { templateValues } = data;
templateValues._header.tags.meta.push({
name: 'antibot_client_key',
content: plugin_data.client_key
});
return data;
};
plugin.reqHook = async function(data){
if (!checkSwitch()) {
return data;
}
let {req} = data;
if (req.isAuthenticated() && await privsGlobal.can("antibot:skip",req.user.uid)){
return data;
}
let method = req.method.toLowerCase()
let url = req.baseUrl
if (checkHooks(method,url)){
let token = data.req.headers["x-captcha-token"];
if (await checkToken(token)) {
return data;
}
throw new Error('验证失败');
}
}
plugin.globalPrivileges = async function(data){
let {privileges} = data;
privileges.set("antibot:skip", { label: "允许跳过验证码"})
return data;
}
module.exports = plugin;