Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使twikoo支持Cloudflare worker的兼容性改动 #695

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/function/twikoo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { version: VERSION } = require('./package.json')
const tcb = require('@cloudbase/node-sdk') // 云开发 SDK
const {
$,
getDomPurify,
md5,
xml2js
} = require('./utils/lib')
Expand Down Expand Up @@ -44,6 +43,7 @@ const { postCheckSpam } = require('./utils/spam')
const { sendNotice, emailTest } = require('./utils/notify')
const { uploadImage } = require('./utils/image')
const logger = require('./utils/logger')
const { getDomPurify } = require('./utils/dom')

// 云函数 SDK / tencent cloudbase sdk
const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })
Expand Down
5 changes: 3 additions & 2 deletions src/server/function/twikoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-func",
"version": "1.6.32",
"version": "1.6.33",
imaegoo marked this conversation as resolved.
Show resolved Hide resolved
"description": "A simple comment system.",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -27,6 +27,7 @@
"nodemailer": "^6.4.17",
"pushoo": "latest",
"tencentcloud-sdk-nodejs": "^4.0.65",
"xml2js": "^0.6.0"
"xml2js": "^0.6.0",
"xss": "^1.0.15"
imaegoo marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions src/server/function/twikoo/utils/cloudflare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function isInCloudflare () {
return typeof addEventListener === "function"

Check failure on line 2 in src/server/function/twikoo/utils/cloudflare.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote
}

module.exports = {
isInCloudflare
}
32 changes: 32 additions & 0 deletions src/server/function/twikoo/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { isInCloudflare } = require('./cloudflare')
const xss = require('xss')

let createDOMPurify, JSDOM

if (!isInCloudflare()) {
createDOMPurify = require('dompurify') // 反 XSS
JSDOM = require('jsdom') // document.window 服务器版
}

function getXssPurify () {
return {
sanitize(input) {

Check failure on line 13 in src/server/function/twikoo/utils/dom.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
return xss(input)
}
}
}

function getDomPurify () {
if (createDOMPurify) {
// 初始化反 XSS
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
return DOMPurify
} else {
return getXssPurify()
}
}

module.exports = {
getDomPurify
}
3 changes: 2 additions & 1 deletion src/server/function/twikoo/utils/import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { getRelativeUrl, normalizeMail } = require('.')
const { marked, getDomPurify, md5 } = require('./lib')
const { marked, md5 } = require('./lib')
const { getDomPurify } = require('./dom')

const fn = {
// 兼容 Leancloud 两种 JSON 导出格式
Expand Down
12 changes: 9 additions & 3 deletions src/server/function/twikoo/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const { URL } = require('url')
const { axios, FormData, bowser, ipToRegion, md5 } = require('./lib')
const { axios, FormData, bowser, md5 } = require('./lib')
const { RES_CODE } = require('./constants')
const ipRegionSearcher = ipToRegion.create() // 初始化 IP 属地
const logger = require('./logger')

let ipRegionSearcher

// IP 属地查询
function getIpRegionSearcher () {
return ipRegionSearcher ?? (ipRegionSearcher = require('@imaegoo/node-ip2region').create())
}

const fn = {
// 获取 Twikoo 云函数版本
getFuncVersion (VERSION) {
Expand Down Expand Up @@ -119,7 +125,7 @@ const fn = {
ip = ip.replace(/^::ffff:/, '')
// Zeabur 返回的地址带端口号,去掉端口号。TODO: 不知道该怎么去掉 IPv6 地址后面的端口号
ip = ip.replace(/:[0-9]*$/, '')
const { region } = ipRegionSearcher.binarySearchSync(ip)
const { region } = getIpRegionSearcher().binarySearchSync(ip)
const [country,, province, city, isp] = region.split('|')
// 有省显示省,没有省显示国家
const area = province.trim() && province !== '0' ? province : country
Expand Down
16 changes: 0 additions & 16 deletions src/server/function/twikoo/utils/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,22 @@ const $ = require('cheerio') // jQuery 服务器版
const { AkismetClient } = require('akismet-api') // 反垃圾 API
const CryptoJS = require('crypto-js') // 编解码
const FormData = require('form-data') // 图片上传
const { JSDOM } = require('jsdom') // document.window 服务器版
const axios = require('axios') // 发送 REST 请求
const bowser = require('bowser') // UserAgent 格式化
const createDOMPurify = require('dompurify') // 反 XSS
const ipToRegion = require('@imaegoo/node-ip2region') // IP 属地查询
const marked = require('marked') // Markdown 解析
const md5 = require('blueimp-md5') // MD5 加解密
const nodemailer = require('nodemailer') // 发送邮件
imaegoo marked this conversation as resolved.
Show resolved Hide resolved
const pushoo = require('pushoo').default // 即时消息通知
const tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
const xml2js = require('xml2js') // XML 解析

function getDomPurify () {
// 初始化反 XSS
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
return DOMPurify
}

module.exports = {
$,
AkismetClient,
CryptoJS,
FormData,
axios,
bowser,
getDomPurify,
ipToRegion,
marked,
md5,
nodemailer,
pushoo,
tencentcloud,
xml2js
}
14 changes: 12 additions & 2 deletions src/server/function/twikoo/utils/spam.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const {
AkismetClient,
CryptoJS,

Check failure on line 3 in src/server/function/twikoo/utils/spam.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected trailing comma
tencentcloud
} = require('./lib')
const logger = require('./logger')
let tencentcloud

function getTencentCloud () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个getTencentCloud是不是和getTencentcloud名字太过接近了😁?

if (tencentcloud) return tencentcloud
try {
tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
} catch (e) {
logger.log('加载 "tencentcloud-sdk-nodejs" 失败', e)
imaegoo marked this conversation as resolved.
Show resolved Hide resolved
}
return tencentcloud
}

const fn = {
// 后垃圾评论检测
Expand All @@ -15,7 +25,7 @@
isSpam = true
} else if (config.QCLOUD_SECRET_ID && config.QCLOUD_SECRET_KEY) {
// 腾讯云内容安全
const client = new tencentcloud.tms.v20200713.Client({
const client = new getTencentCloud().tms.v20200713.Client({

Check failure on line 28 in src/server/function/twikoo/utils/spam.js

View workflow job for this annotation

GitHub Actions / build (18.x)

A constructor name should not start with a lowercase letter
credential: { secretId: config.QCLOUD_SECRET_ID, secretKey: config.QCLOUD_SECRET_KEY },
region: 'ap-shanghai',
profile: { httpProfile: { endpoint: 'tms.tencentcloudapi.com' } }
Expand Down
2 changes: 1 addition & 1 deletion src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const Lfsa = require('lokijs/src/loki-fs-structured-adapter')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
getDomPurify,
md5,
xml2js
} = require('twikoo-func/utils/lib')
Expand Down Expand Up @@ -49,6 +48,7 @@ const { postCheckSpam } = require('twikoo-func/utils/spam')
const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')
const { getDomPurify } = require('twikoo-func/utils/dom')

const DOMPurify = getDomPurify()

Expand Down
2 changes: 1 addition & 1 deletion src/server/vercel/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
axios,
getDomPurify,
md5,
xml2js
} = require('twikoo-func/utils/lib')
Expand Down Expand Up @@ -48,6 +47,7 @@ const { postCheckSpam } = require('twikoo-func/utils/spam')
const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')
const { getDomPurify } = require('twikoo-func/utils/dom')

const DOMPurify = getDomPurify()

Expand Down
Loading