Skip to content

Commit 875447f

Browse files
committed
update:夸克扫码
1 parent a1cac5c commit 875447f

File tree

7 files changed

+70
-29
lines changed

7 files changed

+70
-29
lines changed

Diff for: README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ nodejs作为服务端的drpy实现。全面升级异步写法
1313

1414
## 更新记录
1515

16-
### 20241221
16+
### 20241222
1717

18-
更新至V1.0.17
18+
更新至V1.0.18
1919

20-
1. 增加`ENV`管理cookie
21-
2. 解决源排序问题
20+
1. 夸克扫码获取cookie功能修复
2221

2322
[点此查看完整更新记录](docs/updateRecord.md)
2423

Diff for: config/env.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ali_cookie": "test",
3-
"quark_cookie": "test",
3+
"quark_cookie": "__pus=; __puus=",
44
"uc_cookie": "test",
55
"bili_cookie": "test"
66
}

Diff for: controllers/web.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {readFileSync, existsSync} from 'fs';
22
import path from 'path';
33
import {ENV} from '../utils/env.js';
4+
import COOKIE from '../utils/cookieManager.js';
45

56
const COOKIE_AUTH_CODE = process.env.COOKIE_AUTH_CODE || 'drpys';
67

@@ -45,8 +46,19 @@ export default (fastify, options, done) => {
4546
});
4647
}
4748

49+
let cookie_obj = COOKIE.parse(value);
50+
let cookie_str = value;
51+
52+
if (key === 'quark_cookie') {
53+
// console.log(cookie_obj);
54+
cookie_str = COOKIE.stringify({
55+
__pus: cookie_obj.__pus || '',
56+
__puus: cookie_obj.__puus || '',
57+
});
58+
console.log(cookie_str);
59+
}
4860
// 调用 ENV.set 设置环境变量
49-
ENV.set(key, value);
61+
ENV.set(key, cookie_str);
5062

5163
// 返回成功响应
5264
return reply.code(200).send({

Diff for: docs/updateRecord.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# drpyS更新记录
22

3+
### 20241222
4+
5+
更新至V1.0.18
6+
7+
1. 修复`cookie管理工具`扫码获取夸克和UC的cookie不正确的问题,感谢 [@Hiram-Wong](https://github.com/Hiram-Wong)
8+
2. `COOKIE.parse` 支持列表,修复 `COOKIE.stringify` 可以直接将obj转为正确的cookie字符串,区别于 `COOKIE.serialize` 方法
9+
3. 夸克cookie入库自动清洗,只保留有效部分
10+
311
### 20241221
412

513
更新至V1.0.17

Diff for: js/_fq3.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ const rule = {
1919
log(ck);
2020
// log(cookieToJson(ck));
2121
let ck_obj = COOKIE.parse(ck[0]);
22+
let ck_obj1 = COOKIE.parse(ck);
2223
log(ck_obj);
24+
log(ck_obj1);
2325
log(ck_obj.BAIDUID);
24-
log(COOKIE.stringify("name", 'echo', {
26+
log(COOKIE.serialize("name", 'echo', {
2527
httpOnly: true,
2628
maxAge: 60 * 60 * 24 * 7, // 1 week
2729
}));

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drpy-node",
3-
"version": "1.0.17",
3+
"version": "1.0.18",
44
"main": "index.js",
55
"type": "module",
66
"scripts": {

Diff for: utils/cookieManager.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,48 @@ const NullObject = (() => {
7676
* Parse the given cookie header string into an object
7777
* The object has the various cookies as keys(names) => values
7878
*/
79-
export function parse(str, options) {
79+
export function parse(input, options) {
8080
const obj = new NullObject();
81-
const len = str.length;
82-
if (len < 2) return obj;
8381
const dec = options?.decode || decode;
84-
let index = 0;
85-
do {
86-
const eqIdx = str.indexOf("=", index);
87-
if (eqIdx === -1) break;
88-
const colonIdx = str.indexOf(";", index);
89-
const endIdx = colonIdx === -1 ? len : colonIdx;
90-
if (eqIdx > endIdx) {
91-
index = str.lastIndexOf(";", eqIdx - 1) + 1;
92-
continue;
93-
}
94-
const keyStartIdx = startIndex(str, index, eqIdx);
95-
const keyEndIdx = endIndex(str, eqIdx, keyStartIdx);
96-
const key = str.slice(keyStartIdx, keyEndIdx);
97-
if (obj[key] === undefined) {
82+
83+
// If input is an array, process each string
84+
const headers = Array.isArray(input) ? input : [input];
85+
86+
for (const str of headers) {
87+
const len = str.length;
88+
if (len < 2) continue;
89+
90+
let index = 0;
91+
do {
92+
const eqIdx = str.indexOf("=", index);
93+
if (eqIdx === -1) break;
94+
const colonIdx = str.indexOf(";", index);
95+
const endIdx = colonIdx === -1 ? len : colonIdx;
96+
97+
if (eqIdx > endIdx) {
98+
index = str.lastIndexOf(";", eqIdx - 1) + 1;
99+
continue;
100+
}
101+
102+
const keyStartIdx = startIndex(str, index, eqIdx);
103+
const keyEndIdx = endIndex(str, eqIdx, keyStartIdx);
104+
const key = str.slice(keyStartIdx, keyEndIdx); // Keep original case
105+
98106
let valStartIdx = startIndex(str, eqIdx + 1, endIdx);
99107
let valEndIdx = endIndex(str, endIdx, valStartIdx);
100108
const value = dec(str.slice(valStartIdx, valEndIdx));
109+
110+
// Always overwrite with the latest value for the same key
101111
obj[key] = value;
102-
}
103-
index = endIdx + 1;
104-
} while (index < len);
112+
113+
index = endIdx + 1;
114+
} while (index < len);
115+
}
116+
105117
return obj;
106118
}
107119

120+
108121
function startIndex(str, index, max) {
109122
do {
110123
const code = str.charCodeAt(index);
@@ -226,6 +239,13 @@ function isDate(val) {
226239
return __toString.call(val) === "[object Date]";
227240
}
228241

242+
export function stringify(obj, encode = 0) {
243+
return Object.entries(obj)
244+
.map(([key, value]) => encode ? serialize(key, value) : decodeURIComponent(serialize(key, value)))
245+
.join("; ")
246+
}
247+
248+
229249
// Export as default object for named and default usage
230-
const COOKIE = {parse, serialize, stringify: serialize};
250+
const COOKIE = {parse, serialize, stringify};
231251
export default COOKIE;

0 commit comments

Comments
 (0)