tkserver 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.
package/README.md CHANGED
@@ -19,3 +19,5 @@ tkserver
19
19
  | `TWIKOO_DATA` | 数据库存储路径 | `./data` |
20
20
  | `TWIKOO_PORT` | 端口号 | `8080` |
21
21
  | `TWIKOO_THROTTLE` | IP 请求限流,当同一 IP 短时间内请求次数超过阈值将对该 IP 返回错误 | `250` |
22
+ | `TWIKOO_LOCALHOST_ONLY` | 为`true`时只监听本地请求,使得 nginx 等服务器反代之后不暴露原始端口 | `null` |
23
+ | `TWIKOO_IP_HEADERS` | 在一些特殊情况下使用,如使用了`CloudFlare CDN` 它会将请求 IP 写到请求头的 `cf-connecting-ip` 字段上,为了能够正确的获取请求 IP 你可以写成 `['headers.cf-connecting-ip']` | `[]` |
package/index.js CHANGED
@@ -8,6 +8,7 @@ const { version: VERSION } = require('./package.json')
8
8
  const fs = require('fs')
9
9
  const path = require('path')
10
10
  const Loki = require('lokijs')
11
+ const getUserIP = require('get-user-ip')
11
12
  const Lfsa = require('lokijs/src/loki-fs-structured-adapter')
12
13
  const { v4: uuidv4 } = require('uuid') // 用户 id 生成
13
14
  const {
@@ -23,6 +24,7 @@ const {
23
24
  getUrlsQuery,
24
25
  parseComment,
25
26
  parseCommentForAdmin,
27
+ getMailMd5,
26
28
  getAvatar,
27
29
  isQQ,
28
30
  addQQMailSuffix,
@@ -789,7 +791,7 @@ async function getRecentComments (event) {
789
791
  url: comment.url,
790
792
  nick: comment.nick,
791
793
  avatar: getAvatar(comment, config),
792
- mailMd5: comment.mailMd5 || md5(comment.mail),
794
+ mailMd5: getMailMd5(comment),
793
795
  link: comment.link,
794
796
  comment: comment.comment,
795
797
  commentText: $(comment.comment).text(),
@@ -893,7 +895,14 @@ async function createCollections () {
893
895
  }
894
896
 
895
897
  function getIp (request) {
896
- return request.headers['x-forwarded-for'] || request.socket.remoteAddress || ''
898
+ try {
899
+ const { TWIKOO_IP_HEADERS } = process.env
900
+ const headers = TWIKOO_IP_HEADERS ? JSON.parse(TWIKOO_IP_HEADERS) : []
901
+ return getUserIP(request, headers)
902
+ } catch (e) {
903
+ console.error('获取 IP 错误信息:', e)
904
+ }
905
+ return getUserIP(request)
897
906
  }
898
907
 
899
908
  function clearRequestTimes () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tkserver",
3
- "version": "1.6.7",
3
+ "version": "1.6.8",
4
4
  "description": "A simple comment system.",
5
5
  "keywords": [
6
6
  "twikoo",
@@ -24,8 +24,9 @@
24
24
  "registry": "https://registry.npmjs.org/"
25
25
  },
26
26
  "dependencies": {
27
+ "get-user-ip": "^1.0.1",
27
28
  "lokijs": "^1.5.12",
28
- "twikoo-func": "1.6.7",
29
+ "twikoo-func": "1.6.8",
29
30
  "uuid": "^8.3.2"
30
31
  }
31
32
  }
package/server.js CHANGED
@@ -30,7 +30,8 @@ server.on('request', async function (request, response) {
30
30
  })
31
31
 
32
32
  const port = parseInt(process.env.TWIKOO_PORT) || 8080
33
+ const host = process.env.TWIKOO_LOCALHOST_ONLY === 'true' ? 'localhost' : '::'
33
34
 
34
- server.listen(port, function () {
35
- console.log(`Twikoo function started on port ${port}`)
35
+ server.listen(port, host, function () {
36
+ console.log(`Twikoo function started on host ${host} port ${port}`)
36
37
  })