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 all 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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ yarn.lock
src/server/pkg/dist/*
src/server/pkg/patches/*
src/server/pkg/web.config
pnpm-lock.yaml
pnpm-lock.yaml
Spacefile
9 changes: 6 additions & 3 deletions src/server/function/twikoo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
const { version: VERSION } = require('./package.json')
const tcb = require('@cloudbase/node-sdk') // 云开发 SDK
const {
$,
getCheerio,
getDomPurify,
md5,
xml2js
getMd5,
getXml2js
} = require('./utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -50,7 +50,10 @@ const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })
const auth = app.auth()
const db = app.database()
const _ = db.command
const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('./utils/constants')
Expand Down
4 changes: 3 additions & 1 deletion src/server/function/twikoo/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const os = require('os')
const path = require('path')
const { isUrl } = require('.')
const { RES_CODE } = require('./constants')
const { axios, FormData } = require('./lib')
const { getAxios, getFormData } = require('./lib')
const axios = getAxios()
const FormData = getFormData()
const logger = require('./logger')

const fn = {
Expand Down
4 changes: 3 additions & 1 deletion src/server/function/twikoo/utils/import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { getRelativeUrl, normalizeMail } = require('.')
const { marked, getDomPurify, md5 } = require('./lib')
const { getMarked, getDomPurify, getMd5 } = require('./lib')
const marked = getMarked()
const md5 = getMd5()

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

let ipRegionSearcher

// IP 属地查询
function getIpRegionSearcher () {
if (!ipRegionSearcher) {
ipRegionSearcher = ipToRegion.create() // 初始化 IP 属地
}
return ipRegionSearcher
}

const fn = {
// 获取 Twikoo 云函数版本
getFuncVersion (VERSION) {
Expand Down Expand Up @@ -119,7 +139,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
97 changes: 60 additions & 37 deletions src/server/function/twikoo/utils/lib.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,62 @@
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') // 发送邮件
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
getCheerio () {
const $ = require('cheerio') // jQuery 服务器版
return $
},
getAkismetClient () {
const { AkismetClient } = require('akismet-api') // 反垃圾 API
return AkismetClient
},
getCryptoJS () {
const CryptoJS = require('crypto-js') // 编解码
return CryptoJS
},
getFormData () {
const FormData = require('form-data') // 图片上传
return FormData
},
getAxios () {
const axios = require('axios') // 发送 REST 请求
return axios
},
getBowser () {
const bowser = require('bowser') // UserAgent 格式化
return bowser
},
getDomPurify () {
// 初始化反 XSS
const { JSDOM } = require('jsdom') // document.window 服务器版
const createDOMPurify = require('dompurify') // 反 XSS
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
return DOMPurify
},
getIpToRegion () {
const ipToRegion = require('@imaegoo/node-ip2region') // IP 属地查询
return ipToRegion
},
getMarked () {
const marked = require('marked') // Markdown 解析
return marked
},
getMd5 () {
const md5 = require('blueimp-md5') // MD5 加解密
return md5
},
getNodemailer () {
const nodemailer = require('nodemailer') // 发送邮件
return nodemailer
},
getPushoo () {
const pushoo = require('pushoo').default // 即时消息通知
return pushoo
},
getTencentcloud () {
const tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
return tencentcloud
},
getXml2js () {
const xml2js = require('xml2js') // XML 解析
return xml2js
}
}
9 changes: 6 additions & 3 deletions src/server/function/twikoo/utils/notify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { equalsMail, getAvatar } = require('.')
const {
$,
nodemailer,
pushoo
getCheerio,
getNodemailer,
getPushoo
} = require('./lib')
const $ = getCheerio()
const nodemailer = getNodemailer()
const pushoo = getPushoo()
const { RES_CODE } = require('./constants')
const logger = require('./logger')

Expand Down
24 changes: 20 additions & 4 deletions src/server/function/twikoo/utils/spam.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
const {
AkismetClient,
CryptoJS,
tencentcloud
getAkismetClient,
getCryptoJS,
getTencentcloud
} = require('./lib')
const AkismetClient = getAkismetClient()
const CryptoJS = getCryptoJS()

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) {
try {
tencentcloud = getTencentcloud() // 腾讯云 API NODEJS SDK
} catch (e) {
logger.warn('加载 "tencentcloud-sdk-nodejs" 失败', e)
}
}
return tencentcloud
}

const fn = {
// 后垃圾评论检测
async postCheckSpam (comment, config) {
Expand All @@ -15,7 +31,7 @@ const fn = {
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)({
credential: { secretId: config.QCLOUD_SECRET_ID, secretKey: config.QCLOUD_SECRET_KEY },
region: 'ap-shanghai',
profile: { httpProfile: { endpoint: 'tms.tencentcloudapi.com' } }
Expand Down
9 changes: 6 additions & 3 deletions src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const getUserIP = require('get-user-ip')
const Lfsa = require('lokijs/src/loki-fs-structured-adapter')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
getCheerio,
getDomPurify,
md5,
xml2js
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -50,7 +50,10 @@ const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')

const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down
9 changes: 6 additions & 3 deletions src/server/self-hosted/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const getUserIP = require('get-user-ip')
const { URL } = require('url')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
getCheerio,
getDomPurify,
md5,
xml2js
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -48,7 +48,10 @@ const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')

const $ = getCheerio()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down
12 changes: 8 additions & 4 deletions src/server/vercel/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const getUserIP = require('get-user-ip')
const { URL } = require('url')
const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
axios,
getCheerio,
getAxios,
getDomPurify,
md5,
xml2js
getMd5,
getXml2js
} = require('twikoo-func/utils/lib')
const {
getFuncVersion,
Expand Down Expand Up @@ -49,7 +49,11 @@ const { sendNotice, emailTest } = require('twikoo-func/utils/notify')
const { uploadImage } = require('twikoo-func/utils/image')
const logger = require('twikoo-func/utils/logger')

const $ = getCheerio()
const axios = getAxios()
const DOMPurify = getDomPurify()
const md5 = getMd5()
const xml2js = getXml2js()

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
Expand Down
Loading