twikoo-vercel 1.6.7 → 1.6.8

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 +18 -5
  2. package/package.json +3 -2
package/api/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  const { version: VERSION } = require('../package.json')
8
8
  const MongoClient = require('mongodb').MongoClient
9
+ const getUserIP = require('get-user-ip')
9
10
  const { URL } = require('url')
10
11
  const { v4: uuidv4 } = require('uuid') // 用户 id 生成
11
12
  const {
@@ -22,6 +23,7 @@ const {
22
23
  getUrlsQuery,
23
24
  parseComment,
24
25
  parseCommentForAdmin,
26
+ getMailMd5,
25
27
  getAvatar,
26
28
  isQQ,
27
29
  addQQMailSuffix,
@@ -58,7 +60,7 @@ const requestTimes = {}
58
60
 
59
61
  module.exports = async (request, response) => {
60
62
  const event = request.body || {}
61
- console.log('请求 IP:', request.headers['x-real-ip'])
63
+ console.log('请求 IP:', getIp(request))
62
64
  console.log('请求函数:', event.event)
63
65
  console.log('请求参数:', event)
64
66
  let res = {}
@@ -623,7 +625,7 @@ async function parse (comment, request) {
623
625
  mailMd5: comment.mail ? md5(comment.mail) : '',
624
626
  link: comment.link ? comment.link : '',
625
627
  ua: comment.ua,
626
- ip: request.headers['x-real-ip'],
628
+ ip: getIp(request),
627
629
  master: isBloggerMail,
628
630
  url: comment.url,
629
631
  href: comment.href,
@@ -651,7 +653,7 @@ async function limitFilter (request) {
651
653
  const count = await db
652
654
  .collection('comment')
653
655
  .countDocuments({
654
- ip: request.headers['x-real-ip'],
656
+ ip: getIp(request),
655
657
  created: { $gt: Date.now() - 600000 }
656
658
  })
657
659
  if (count > limitPerMinute) {
@@ -803,7 +805,7 @@ async function getRecentComments (event) {
803
805
  url: comment.url,
804
806
  nick: comment.nick,
805
807
  avatar: getAvatar(comment, config),
806
- mailMd5: comment.mailMd5 || md5(comment.mail),
808
+ mailMd5: getMailMd5(comment),
807
809
  link: comment.link,
808
810
  comment: comment.comment,
809
811
  commentText: $(comment.comment).text(),
@@ -835,7 +837,7 @@ async function setConfig (event) {
835
837
 
836
838
  function protect (request) {
837
839
  // 防御
838
- const ip = request.headers['x-real-ip']
840
+ const ip = getIp(request)
839
841
  requestTimes[ip] = (requestTimes[ip] || 0) + 1
840
842
  if (requestTimes[ip] > MAX_REQUEST_TIMES) {
841
843
  console.log(`${ip} 当前请求次数为 ${requestTimes[ip]},已超过最大请求次数`)
@@ -915,3 +917,14 @@ async function createCollections () {
915
917
  }
916
918
  return res
917
919
  }
920
+
921
+ function getIp (request) {
922
+ try {
923
+ const { TWIKOO_IP_HEADERS } = process.env
924
+ const headers = TWIKOO_IP_HEADERS ? JSON.parse(TWIKOO_IP_HEADERS) : []
925
+ return getUserIP(request, headers)
926
+ } catch (e) {
927
+ console.error('获取 IP 错误信息:', e)
928
+ }
929
+ return getUserIP(request)
930
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.6.7",
3
+ "version": "1.6.8",
4
4
  "description": "A simple comment system.",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",
@@ -11,8 +11,9 @@
11
11
  },
12
12
  "homepage": "https://twikoo.js.org",
13
13
  "dependencies": {
14
+ "get-user-ip": "^1.0.1",
14
15
  "mongodb": "^3.6.3",
15
- "twikoo-func": "1.6.7",
16
+ "twikoo-func": "1.6.8",
16
17
  "uuid": "^8.3.2"
17
18
  }
18
19
  }