twikoo-vercel 1.5.0 → 1.5.3

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/api/index.js +34 -5
  2. package/package.json +1 -1
package/api/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /*!
2
- * Twikoo vercel function v1.5.0
2
+ * Twikoo vercel function
3
3
  * (c) 2020-present iMaeGoo
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  // 三方依赖 / 3rd party dependencies
8
+ const { version: VERSION } = require('../package.json')
8
9
  const { URL } = require('url')
9
10
  const MongoClient = require('mongodb').MongoClient
10
11
  const md5 = require('blueimp-md5') // MD5 加解密
@@ -29,7 +30,6 @@ const window = new JSDOM('').window
29
30
  const DOMPurify = createDOMPurify(window)
30
31
 
31
32
  // 常量 / constants
32
- const VERSION = '1.5.0'
33
33
  const RES_CODE = {
34
34
  SUCCESS: 0,
35
35
  NO_PARAM: 100,
@@ -46,6 +46,7 @@ const RES_CODE = {
46
46
  AKISMET_ERROR: 1030,
47
47
  UPLOAD_FAILED: 1040
48
48
  }
49
+ const MAX_REQUEST_TIMES = parseInt(process.env.TWIKOO_THROTTLE) || 250
49
50
 
50
51
  // 全局变量 / variables
51
52
  let db = null
@@ -54,15 +55,18 @@ let transporter
54
55
  let request
55
56
  let response
56
57
  let accessToken
58
+ const requestTimes = {}
57
59
 
58
60
  module.exports = async (requestArg, responseArg) => {
59
61
  request = requestArg
60
62
  response = responseArg
61
63
  const event = request.body || {}
64
+ console.log('请求IP:', request.headers['x-real-ip'])
62
65
  console.log('请求方法:', event.event)
63
66
  console.log('请求参数:', event)
64
67
  let res = {}
65
68
  try {
69
+ protect()
66
70
  anonymousSignIn()
67
71
  await connectToDatabase(process.env.MONGODB_URI)
68
72
  await readConfig()
@@ -1000,7 +1004,12 @@ async function noticePushoo (comment) {
1000
1004
  const sendResult = await pushoo(config.PUSHOO_CHANNEL, {
1001
1005
  token: config.PUSHOO_TOKEN,
1002
1006
  title: pushContent.subject,
1003
- content: pushContent.content
1007
+ content: pushContent.content,
1008
+ options: {
1009
+ bark: {
1010
+ url: pushContent.url
1011
+ }
1012
+ }
1004
1013
  })
1005
1014
  console.log('即时消息通知结果:', sendResult)
1006
1015
  }
@@ -1024,7 +1033,8 @@ function getIMPushContent (comment) {
1024
1033
  原文链接:[${POST_URL}](${POST_URL})`
1025
1034
  return {
1026
1035
  subject,
1027
- content
1036
+ content,
1037
+ url: POST_URL
1028
1038
  }
1029
1039
  }
1030
1040
 
@@ -1168,6 +1178,12 @@ async function limitFilter () {
1168
1178
 
1169
1179
  // 预垃圾评论检测
1170
1180
  function preCheckSpam (comment) {
1181
+ // 长度限制
1182
+ let limitLength = parseInt(config.LIMIT_LENGTH)
1183
+ if (Number.isNaN(limitLength)) limitLength = 500
1184
+ if (limitLength && comment.length > limitLength) {
1185
+ throw new Error('评论内容过长')
1186
+ }
1171
1187
  if (config.AKISMET_KEY === 'MANUAL_REVIEW') {
1172
1188
  // 人工审核
1173
1189
  console.log('已使用人工审核模式,评论审核后才会发表~')
@@ -1498,7 +1514,8 @@ async function getConfig () {
1498
1514
  REQUIRED_FIELDS: config.REQUIRED_FIELDS,
1499
1515
  HIDE_ADMIN_CRYPT: config.HIDE_ADMIN_CRYPT,
1500
1516
  HIGHLIGHT: config.HIGHLIGHT || 'true',
1501
- HIGHLIGHT_THEME: config.HIGHLIGHT_THEME
1517
+ HIGHLIGHT_THEME: config.HIGHLIGHT_THEME,
1518
+ LIMIT_LENGTH: config.LIMIT_LENGTH
1502
1519
  }
1503
1520
  }
1504
1521
  }
@@ -1535,6 +1552,18 @@ async function setConfig (event) {
1535
1552
  }
1536
1553
  }
1537
1554
 
1555
+ function protect () {
1556
+ // 防御
1557
+ const ip = request.headers['x-real-ip']
1558
+ requestTimes[ip] = (requestTimes[ip] || 0) + 1
1559
+ if (requestTimes[ip] > MAX_REQUEST_TIMES) {
1560
+ console.log(`${ip} 当前请求次数为 ${requestTimes[ip]},已超过最大请求次数`)
1561
+ throw new Error('Too Many Requests')
1562
+ } else {
1563
+ console.log(`${ip} 当前请求次数为 ${requestTimes[ip]}`)
1564
+ }
1565
+ }
1566
+
1538
1567
  // 读取配置
1539
1568
  async function readConfig () {
1540
1569
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.5.0",
3
+ "version": "1.5.3",
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",