twikoo-func 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +25 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -50,20 +50,24 @@ const RES_CODE = {
50
50
  UPLOAD_FAILED: 1040
51
51
  }
52
52
  const ADMIN_USER_ID = 'admin'
53
+ const MAX_REQUEST_TIMES = parseInt(process.env.TWIKOO_THROTTLE) || 250
53
54
 
54
55
  // 全局变量 / variables
55
56
  // 警告:全局定义的变量,会被云函数缓存,请慎重定义全局变量
56
57
  // 参考 https://docs.cloudbase.net/cloud-function/deep-principle.html 中的 “实例复用”
57
58
  let config
58
59
  let transporter
60
+ const requestTimes = {}
59
61
 
60
62
  // 云函数入口点 / entry point
61
63
  exports.main = async (event, context) => {
64
+ console.log('请求IP:', auth.getClientIP())
62
65
  console.log('请求方法:', event.event)
63
66
  console.log('请求参数:', event)
64
67
  let res = {}
65
- await readConfig()
66
68
  try {
69
+ protect()
70
+ await readConfig()
67
71
  switch (event.event) {
68
72
  case 'GET_FUNC_VERSION':
69
73
  res = getFuncVersion()
@@ -1168,6 +1172,12 @@ async function limitFilter () {
1168
1172
 
1169
1173
  // 预垃圾评论检测
1170
1174
  function preCheckSpam (comment) {
1175
+ // 长度限制
1176
+ let limitLength = parseInt(config.LIMIT_LENGTH)
1177
+ if (Number.isNaN(limitLength)) limitLength = 500
1178
+ if (limitLength && comment.length > limitLength) {
1179
+ throw new Error('评论内容过长')
1180
+ }
1171
1181
  if (config.AKISMET_KEY === 'MANUAL_REVIEW') {
1172
1182
  // 人工审核
1173
1183
  console.log('已使用人工审核模式,评论审核后才会发表~')
@@ -1497,7 +1507,8 @@ function getConfig () {
1497
1507
  REQUIRED_FIELDS: config.REQUIRED_FIELDS,
1498
1508
  HIDE_ADMIN_CRYPT: config.HIDE_ADMIN_CRYPT,
1499
1509
  HIGHLIGHT: config.HIGHLIGHT || 'true',
1500
- HIGHLIGHT_THEME: config.HIGHLIGHT_THEME
1510
+ HIGHLIGHT_THEME: config.HIGHLIGHT_THEME,
1511
+ LIMIT_LENGTH: config.LIMIT_LENGTH
1501
1512
  }
1502
1513
  }
1503
1514
  }
@@ -1534,6 +1545,18 @@ async function setConfig (event) {
1534
1545
  }
1535
1546
  }
1536
1547
 
1548
+ function protect () {
1549
+ // 防御
1550
+ const ip = auth.getClientIP()
1551
+ requestTimes[ip] = (requestTimes[ip] || 0) + 1
1552
+ if (requestTimes[ip] > MAX_REQUEST_TIMES) {
1553
+ console.log(`${ip} 当前请求次数为 ${requestTimes[ip]},已超过最大请求次数`)
1554
+ throw new Error('Too Many Requests')
1555
+ } else {
1556
+ console.log(`${ip} 当前请求次数为 ${requestTimes[ip]}`)
1557
+ }
1558
+ }
1559
+
1537
1560
  // 读取配置
1538
1561
  async function readConfig () {
1539
1562
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-func",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "A simple comment system based on Tencent CloudBase (tcb).",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",