From 53242d6170a5388faf356e2cec9028e46acd6cae Mon Sep 17 00:00:00 2001 From: BTMuli Date: Fri, 5 May 2023 18:18:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=94=20add(ds):=20=E6=B7=BB=E5=8A=A0=20?= =?UTF-8?q?ds=20=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 43 +++++++++++++++++++++++++++------------ package.json | 1 + src/core/utils/TGUtils.ts | 6 +++++- src/core/utils/getDS.ts | 33 ++++++++++++++++++++++++++++++ src/core/utils/tools.ts | 29 +++++++++++++++++++++++++- 5 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 src/core/utils/getDS.ts diff --git a/package-lock.json b/package-lock.json index e44b02ba..a8e237c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "clipboard": "^2.0.11", "pinia": "^2.0.35", "pinia-plugin-persistedstate": "^3.1.0", + "qs": "^6.11.1", "tauri-plugin-sql-api": "github:tauri-apps/tauri-plugin-sql", "vue": "^3.2.47", "vue-json-viewer": "^3.0.4", @@ -1547,6 +1548,18 @@ "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "dev": true }, + "node_modules/body-parser/node_modules/qs": { + "version": "6.11.0", + "resolved": "https://registry.npmmirror.com/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "dev": true, + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + } + }, "node_modules/boolbase": { "version": "1.0.0", "resolved": "https://registry.npmmirror.com/boolbase/-/boolbase-1.0.0.tgz", @@ -1655,7 +1668,6 @@ "version": "1.0.2", "resolved": "https://registry.npmmirror.com/call-bind/-/call-bind-1.0.2.tgz", "integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==", - "dev": true, "dependencies": { "function-bind": "^1.1.1", "get-intrinsic": "^1.0.2" @@ -3000,6 +3012,18 @@ "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "dev": true }, + "node_modules/express/node_modules/qs": { + "version": "6.11.0", + "resolved": "https://registry.npmmirror.com/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "dev": true, + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + } + }, "node_modules/extract-zip": { "version": "2.0.1", "resolved": "https://registry.npmmirror.com/extract-zip/-/extract-zip-2.0.1.tgz", @@ -3247,8 +3271,7 @@ "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmmirror.com/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", - "dev": true + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" }, "node_modules/function.prototype.name": { "version": "1.1.5", @@ -3284,7 +3307,6 @@ "version": "1.2.0", "resolved": "https://registry.npmmirror.com/get-intrinsic/-/get-intrinsic-1.2.0.tgz", "integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==", - "dev": true, "dependencies": { "function-bind": "^1.1.1", "has": "^1.0.3", @@ -3556,7 +3578,6 @@ "version": "1.0.3", "resolved": "https://registry.npmmirror.com/has/-/has-1.0.3.tgz", "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", - "dev": true, "dependencies": { "function-bind": "^1.1.1" }, @@ -3601,7 +3622,6 @@ "version": "1.0.3", "resolved": "https://registry.npmmirror.com/has-symbols/-/has-symbols-1.0.3.tgz", "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", - "dev": true, "engines": { "node": ">= 0.4" } @@ -4536,8 +4556,7 @@ "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmmirror.com/object-inspect/-/object-inspect-1.12.3.tgz", - "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==", - "dev": true + "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==" }, "node_modules/object-keys": { "version": "1.1.1", @@ -5002,10 +5021,9 @@ } }, "node_modules/qs": { - "version": "6.11.0", - "resolved": "https://registry.npmmirror.com/qs/-/qs-6.11.0.tgz", - "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", - "dev": true, + "version": "6.11.1", + "resolved": "https://registry.npmmirror.com/qs/-/qs-6.11.1.tgz", + "integrity": "sha512-0wsrzgTz/kAVIeuxSjnpGC56rzYtr6JT/2BwEvMaPhFIoYa1aGO8LbzuU1R0uUYQkLpWBTOj0l/CLAJB64J6nQ==", "dependencies": { "side-channel": "^1.0.4" }, @@ -5456,7 +5474,6 @@ "version": "1.0.4", "resolved": "https://registry.npmmirror.com/side-channel/-/side-channel-1.0.4.tgz", "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==", - "dev": true, "dependencies": { "call-bind": "^1.0.0", "get-intrinsic": "^1.0.2", diff --git a/package.json b/package.json index a3c4ff3b..7ef75896 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "clipboard": "^2.0.11", "pinia": "^2.0.35", "pinia-plugin-persistedstate": "^3.1.0", + "qs": "^6.11.1", "tauri-plugin-sql-api": "github:tauri-apps/tauri-plugin-sql", "vue": "^3.2.47", "vue-json-viewer": "^3.0.4", diff --git a/src/core/utils/TGUtils.ts b/src/core/utils/TGUtils.ts index c4d2ead4..429cd091 100644 --- a/src/core/utils/TGUtils.ts +++ b/src/core/utils/TGUtils.ts @@ -2,17 +2,21 @@ * @file core utils TGUtils.ts * @description 应用用到的工具函数 * @author BTMuli - * @since Alpha v0.1.2 + * @since Alpha v0.2.0 */ import { parseAnnoContent } from "./parseAnno"; import { getAnnoCard } from "./getAnnoCard"; +import { getDS } from "./getDS"; const TGUtils = { Anno: { getCard: getAnnoCard, parseContent: parseAnnoContent, }, + User: { + getDS, + }, }; export default TGUtils; diff --git a/src/core/utils/getDS.ts b/src/core/utils/getDS.ts new file mode 100644 index 00000000..1f412a52 --- /dev/null +++ b/src/core/utils/getDS.ts @@ -0,0 +1,33 @@ +/** + * @description ds 算法 + * @since Alpha v0.2.0 + * @author BTMuli + * @since Alpha v0.2.0 + */ + +// Node +import { stringify } from "querystring"; +// Tauri.Genshin +import { md5 } from "./tools"; +import TGConstant from "../constant/TGConstant"; + +/** + * @description 获取 ds + * @since Alpha v0.2.0 + * @version 2.34.1 + * @param {string} query 查询字符串 + * @param {string} body 请求体 + * @returns {string} ds + */ +export function getDS (query: string, body: string): string { + const params = { + salt: TGConstant.SALT.Other.X4, + t: Math.floor(Date.now() / 1000).toString(), + r: Math.floor(Math.random() * 900000 + 100000).toString(), + b: body, + q: query, + }; + const md5Str = md5(stringify(params)); + const ds = `${params.t},${params.r},${md5Str}`; + return ds; +} diff --git a/src/core/utils/tools.ts b/src/core/utils/tools.ts index bb04d25e..5b5e356d 100644 --- a/src/core/utils/tools.ts +++ b/src/core/utils/tools.ts @@ -2,9 +2,11 @@ * @file core utils tools.ts * @description 应用用到的工具函数 * @author BTMuli - * @since Alpha v0.1.2 + * @since Alpha v0.2.0 */ +import crypto from "crypto"; + /** * @description 转义正则表达式 * @since Alpha v0.1.2 @@ -23,3 +25,28 @@ export function decodeRegExp (data: string): string { res = res.replace(/&/g, "&"); return res; } + +/** + * @description 获取随机字符串 + * @since Alpha v0.2.0 + * @param {number} length 字符串长度 + * @returns {string} 随机字符串 + */ +export function getRandomString (length: number): string { + const str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + let res = ""; + for (let i = 0; i < length; i++) { + res += str[Math.floor(Math.random() * str.length)]; + } + return res; +} + +/** + * @description md5 加密 + * @since Alpha v0.2.0 + * @param {string} data 要加密的内容 + * @returns {string} 加密后的内容 + */ +export function md5 (data: string): string { + return crypto.createHash("md5").update(data).digest("hex"); +}