Skip to content

Commit 306726e

Browse files
committed
update:增加cookie管理
1 parent 92efd66 commit 306726e

File tree

19 files changed

+466
-45
lines changed

19 files changed

+466
-45
lines changed

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ ENV='staging'
77
LOG_WITH_FILE = 1
88
# trace/debug/info/warn/error/fatal
99
LOG_LEVEL = info
10+
COOKIE_AUTH_CODE = drpys

README.md

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

1414
## 更新记录
1515

16-
### 20241220
16+
### 20241221
1717

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

20-
1. 更新细节,增加一些函数
21-
2. 增加订阅码自定义排序功能
20+
1. 增加`ENV`管理cookie
21+
2. 解决源排序问题
2222

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

apps/cookie-butler/index.html

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="zh">
33
<head>
4-
<meta charset="UTF-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
4+
<meta charset="UTF-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
66
<title>Cookie管理</title>
7-
<link rel="stylesheet" href="./static/css/style.css" />
8-
<link rel="stylesheet" href="./static/css/daisyui.min.css" />
7+
<link rel="stylesheet" href="./static/css/style.css"/>
8+
<link rel="stylesheet" href="./static/css/daisyui.min.css"/>
99
<script src="./static/js/tailwindcss.min.js"></script>
1010
<script src="./static/js/axios.min.js"></script>
1111
<script src="./static/js/qrcode.min.js"></script>
@@ -24,7 +24,7 @@
2424

2525
<!-- QR Code -->
2626
<div class="qrcode-container text-center mb-4">
27-
<img id="qrcode" src="./static/img/qrcode_expired.jpg" alt="二维码" class="mx-auto" />
27+
<img id="qrcode" src="./static/img/qrcode_expired.jpg" alt="二维码" class="mx-auto"/>
2828
</div>
2929

3030
<!-- Divider -->
@@ -35,7 +35,6 @@
3535
id="cookie-res"
3636
class="textarea textarea-bordered w-full h-40 p-4 bg-white border-2 border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
3737
placeholder="扫码确认后将在此处展示cookie"
38-
readonly
3938
></textarea>
4039

4140
<!-- Buttons with improved design -->
@@ -85,9 +84,40 @@
8584

8685
// Store cookie (just an alert for now)
8786
function storeCookie() {
88-
alert("入库功能暂未实现!");
87+
// 获取当前激活的 li
88+
const activeLi = document.querySelector("ul.menu a.active");
89+
if (activeLi) {
90+
const textValue = document.getElementById("cookie-res").value || '';
91+
const active_name = activeLi.textContent.trim();
92+
const active_key = activeLi.getAttribute('data-platform').trim() + '_cookie';
93+
console.log(`准备入库cookie:${active_name} ${active_key},值为:${textValue}`);
94+
95+
const cookie_auth_code = prompt('cookie入库功能需要管理员授权码,请你正确输入后继续');
96+
if (cookie_auth_code) {
97+
// 使用 axios 发送 POST 请求
98+
axios.post('/admin/cookie-set', {
99+
cookie_auth_code: cookie_auth_code,
100+
key: active_key,
101+
value: textValue
102+
})
103+
.then(response => {
104+
if (response.data.success) {
105+
alert(`Cookie 入库成功:${active_name} (${active_key})`);
106+
} else {
107+
alert(`入库失败:${response.data.message}`);
108+
}
109+
})
110+
.catch(error => {
111+
console.error('请求失败:', error);
112+
alert(`入库失败,服务器出现问题,请稍后再试。\n${error.response.data.message}`);
113+
});
114+
}
115+
} else {
116+
alert('至少选中一个cookie入库项目');
117+
}
89118
}
90119

120+
91121
// Initialize the page
92122
initializePage();
93123
</script>

config/env.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ali_cookie": "test",
3+
"quark_cookie": "test",
4+
"uc_cookie": "test",
5+
"bili_cookie": "test"
6+
}

controllers/fastlogger.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ dotenv.config(); //加载 .env 文件
1010

1111
const LOG_WITH_FILE = process.env.LOG_WITH_FILE;
1212
const LOG_LEVEL = process.env.LOG_LEVEL && ['trace', 'debug', 'info', 'warn', 'error', 'fatal'].includes(process.env.LOG_LEVEL) ? process.env.LOG_LEVEL : 'info';
13+
const COOKIE_AUTH_CODE = process.env.COOKIE_AUTH_CODE || 'drpys';
1314
console.log('LOG_WITH_FILE:', LOG_WITH_FILE);
1415
console.log('LOG_LEVEL:', LOG_LEVEL);
16+
console.log('COOKIE_AUTH_CODE:', COOKIE_AUTH_CODE);
1517
let _logger = true;
1618

1719
// 自定义时间戳函数,格式为 YYYY-MM-DD HH:mm:ss

controllers/web.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {readFileSync, existsSync} from 'fs';
22
import path from 'path';
3+
import {ENV} from '../utils/env.js';
4+
5+
const COOKIE_AUTH_CODE = process.env.COOKIE_AUTH_CODE || 'drpys';
36

47
export default (fastify, options, done) => {
58
// 读取 views 目录下的 encoder.html 文件并返回
@@ -21,5 +24,45 @@ export default (fastify, options, done) => {
2124
}
2225
});
2326

27+
fastify.post('/admin/cookie-set', async (request, reply) => {
28+
try {
29+
// 从请求体中获取参数
30+
const {cookie_auth_code, key, value} = request.body;
31+
32+
// 验证参数完整性
33+
if (!cookie_auth_code || !key || !value) {
34+
return reply.code(400).send({
35+
success: false,
36+
message: 'Missing required parameters: cookie_auth_code, key, or value',
37+
});
38+
}
39+
40+
// 验证 cookie_auth_code 是否正确
41+
if (cookie_auth_code !== COOKIE_AUTH_CODE) {
42+
return reply.code(403).send({
43+
success: false,
44+
message: 'Invalid cookie_auth_code',
45+
});
46+
}
47+
48+
// 调用 ENV.set 设置环境变量
49+
ENV.set(key, value);
50+
51+
// 返回成功响应
52+
return reply.code(200).send({
53+
success: true,
54+
message: 'Cookie value has been successfully set',
55+
data: {key, value},
56+
});
57+
} catch (error) {
58+
// 捕获异常并返回错误响应
59+
console.error('Error setting cookie:', error.message);
60+
return reply.code(500).send({
61+
success: false,
62+
message: 'Internal server error',
63+
});
64+
}
65+
});
66+
2467
done();
2568
};

docs/updateRecord.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# drpyS更新记录
22

3+
### 20241221
4+
5+
更新至V1.0.17
6+
7+
1. 修复`req`函数在请求错误返回的content可能存在json情况的问题
8+
2. 增加`ENV`对象。用于在实际过程中get和set设置系统环境变量如各种cookie
9+
3. 完善Cookie管理器的扫码和输入后入库功能逻辑
10+
4. 引入自然排序算法库解决生成的配置中源的顺序问题
11+
312
### 20241220
413

514
更新至V1.0.16

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ registerRoutes(fastify, {
3232
jsDir: path.join(__dirname, 'js'),
3333
jxDir: path.join(__dirname, 'jx'),
3434
viewsDir: path.join(__dirname, 'views'),
35+
configDir: path.join(__dirname, 'config'),
3536
PORT,
3637
MAX_TEXT_SIZE,
3738
indexFilePath: path.join(__dirname, 'index.json'),

0 commit comments

Comments
 (0)