dsh-cookie-cleaner 0.6.4 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +260 -242
- package/dsh.plugin.json +1 -1
- package/lib/client.js +123 -7
- package/lib/client.js.map +1 -1
- package/package.json +1 -1
- package/src/api-routes.js +144 -138
- package/src/cli.js +281 -236
- package/src/client/card.tsx +310 -253
- package/src/client/locales.ts +91 -84
- package/src/client/routes.ts +138 -118
- package/src/core.js +669 -531
- package/src/index.js +426 -394
- package/src/legacy-injection.js +137 -0
- package/src/patch-guard.js +509 -416
- package/src/patch-read.js +251 -199
- package/src/runtime-guard.js +14 -4
package/src/api-routes.js
CHANGED
|
@@ -1,138 +1,144 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-cookie-cleaner — 卡片到宿主的桥路由(三个端点,全部 POST-only)。
|
|
3
|
-
*
|
|
4
|
-
* 为什么走 `/api` 而不是 typert RPC:第三方卡片的宿主侧数据面在 perm-gate/free-search 上
|
|
5
|
-
* 已跑通这一形态(`webServer.register({kind:'exact'})` + fetch POST),且面板只需几个
|
|
6
|
-
* 幂等动作,不值得为它引入 remote 服务面。
|
|
7
|
-
*
|
|
8
|
-
* **没有端口写端点**(用户裁决 2026-11-05):本插件只改配置里的 cookie 有效期,
|
|
9
|
-
* 端口属启动行为面,面板与命令面都不再提供写入(面板不调它时留个写端点只是攻击面)。
|
|
10
|
-
*
|
|
11
|
-
* 路由派生式与 `src/client/routes.ts` **必须逐字符一致**,由 `tests/api-routes.test.js`
|
|
12
|
-
* 用同一段字面量在两份源里双向核对——两半各持一份派生式而测试不钉,是 URL 漂移的老坑。
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** 命名空间 = npm 包名(settings ns、卡 id、桥路径三处同串)。 */
|
|
16
|
-
export const NS = 'dsh-cookie-cleaner'
|
|
17
|
-
|
|
18
|
-
/** 桥路由:体检 / 修复 / 回滚 + 运行期守护读/写。(端口写入面板不提供,不开端点) */
|
|
19
|
-
export function bridgeRoutesFor(ns = NS) {
|
|
20
|
-
return Object.freeze({
|
|
21
|
-
check: `/api/${ns}/patch/check`,
|
|
22
|
-
fix: `/api/${ns}/patch/fix`,
|
|
23
|
-
revert: `/api/${ns}/patch/revert`,
|
|
24
|
-
guardGet: `/api/${ns}/guard/get`,
|
|
25
|
-
guardSet: `/api/${ns}/guard/set`,
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
* @
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
endpoint(routes.
|
|
136
|
-
endpoint(routes.
|
|
137
|
-
|
|
138
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-cookie-cleaner — 卡片到宿主的桥路由(三个端点,全部 POST-only)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么走 `/api` 而不是 typert RPC:第三方卡片的宿主侧数据面在 perm-gate/free-search 上
|
|
5
|
+
* 已跑通这一形态(`webServer.register({kind:'exact'})` + fetch POST),且面板只需几个
|
|
6
|
+
* 幂等动作,不值得为它引入 remote 服务面。
|
|
7
|
+
*
|
|
8
|
+
* **没有端口写端点**(用户裁决 2026-11-05):本插件只改配置里的 cookie 有效期,
|
|
9
|
+
* 端口属启动行为面,面板与命令面都不再提供写入(面板不调它时留个写端点只是攻击面)。
|
|
10
|
+
*
|
|
11
|
+
* 路由派生式与 `src/client/routes.ts` **必须逐字符一致**,由 `tests/api-routes.test.js`
|
|
12
|
+
* 用同一段字面量在两份源里双向核对——两半各持一份派生式而测试不钉,是 URL 漂移的老坑。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** 命名空间 = npm 包名(settings ns、卡 id、桥路径三处同串)。 */
|
|
16
|
+
export const NS = 'dsh-cookie-cleaner'
|
|
17
|
+
|
|
18
|
+
/** 桥路由:体检 / 修复 / 回滚 + 运行期守护读/写。(端口写入面板不提供,不开端点) */
|
|
19
|
+
export function bridgeRoutesFor(ns = NS) {
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
check: `/api/${ns}/patch/check`,
|
|
22
|
+
fix: `/api/${ns}/patch/fix`,
|
|
23
|
+
revert: `/api/${ns}/patch/revert`,
|
|
24
|
+
guardGet: `/api/${ns}/guard/get`,
|
|
25
|
+
guardSet: `/api/${ns}/guard/set`,
|
|
26
|
+
// v0.7.1:历史注入残留(旧版写进 profile 的 port 表达式)的读 / 清。
|
|
27
|
+
legacyGet: `/api/${ns}/legacy/get`,
|
|
28
|
+
legacyClean: `/api/${ns}/legacy/clean`,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const MAX_BODY_BYTES = 64 * 1024
|
|
33
|
+
|
|
34
|
+
function sendJson(res, code, body) {
|
|
35
|
+
res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache' })
|
|
36
|
+
res.end(JSON.stringify(body))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 读 JSON body(有上限,超限直接拒——面板不该发大东西)。 */
|
|
40
|
+
function readJsonBody(req) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const chunks = []
|
|
43
|
+
let size = 0
|
|
44
|
+
req.on('data', (chunk) => {
|
|
45
|
+
size += chunk.length
|
|
46
|
+
if (size > MAX_BODY_BYTES) {
|
|
47
|
+
reject(new Error('body too large'))
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
chunks.push(chunk)
|
|
51
|
+
})
|
|
52
|
+
req.on('end', () => {
|
|
53
|
+
if (chunks.length === 0) { resolve({}); return }
|
|
54
|
+
try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8'))) }
|
|
55
|
+
catch (e) { reject(new Error(`invalid json body: ${e.message}`)) }
|
|
56
|
+
})
|
|
57
|
+
req.on('error', reject)
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* cookie 库现状只留卡片需要看的字段。
|
|
63
|
+
* 读得到就带真实条数与请求头占用折算(`header`),并带出它是实时还是借句柄快照;
|
|
64
|
+
* 读不到才只给 verdict/reason,不能谎报条数。
|
|
65
|
+
* @param {() => any} [stat]
|
|
66
|
+
*/
|
|
67
|
+
export function cookieBrief(stat) {
|
|
68
|
+
if (typeof stat !== 'function') return undefined
|
|
69
|
+
try {
|
|
70
|
+
const s = stat()
|
|
71
|
+
if (s === undefined || s === null) return undefined
|
|
72
|
+
return {
|
|
73
|
+
verdict: s.verdict,
|
|
74
|
+
reason: s.reason,
|
|
75
|
+
path: s.path,
|
|
76
|
+
authCookies: s.authCookies,
|
|
77
|
+
totalCookies: s.totalCookies,
|
|
78
|
+
bytes: s.bytes,
|
|
79
|
+
header: s.header,
|
|
80
|
+
source: s.source,
|
|
81
|
+
snapshot: s.snapshot === true,
|
|
82
|
+
liveUnavailable: s.liveUnavailable === true,
|
|
83
|
+
liveReason: s.liveReason,
|
|
84
|
+
}
|
|
85
|
+
} catch { return undefined }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 注册路由。
|
|
90
|
+
* @param {unknown} webServer `webServer` 服务实例(只要求有 register)
|
|
91
|
+
* @param {{check:(o:object)=>object, plan:(o:object)=>object, fix:(o:object)=>Promise<object>, revert:()=>Promise<object>}} guard
|
|
92
|
+
* @param {{lang?:string, targetDays?:number, guardMode?:string, cookie?:() => any}} [defaults] 宿主设置里的默认档
|
|
93
|
+
* @returns {Array<() => void>} 注销函数
|
|
94
|
+
*/
|
|
95
|
+
export function registerPatchRoutes(webServer, guard, defaults = {}) {
|
|
96
|
+
const register = typeof webServer?.register === 'function' ? webServer.register.bind(webServer) : undefined
|
|
97
|
+
if (register === undefined) return []
|
|
98
|
+
const routes = bridgeRoutesFor(defaults.ns ?? NS)
|
|
99
|
+
const offs = []
|
|
100
|
+
|
|
101
|
+
/** 统一的一跳:POST-only、参数从 body 取、异常包成结构化错误。 */
|
|
102
|
+
const endpoint = (path, run) => {
|
|
103
|
+
offs.push(register({
|
|
104
|
+
kind: 'exact',
|
|
105
|
+
path,
|
|
106
|
+
handler: (rawReq, rawRes) => {
|
|
107
|
+
const req = rawReq
|
|
108
|
+
const res = rawRes
|
|
109
|
+
if (req.method !== 'POST') {
|
|
110
|
+
sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
readJsonBody(req).then(async (body) => {
|
|
114
|
+
const options = {
|
|
115
|
+
targetDays: Number.isInteger(body?.targetDays) && body.targetDays >= 1 ? body.targetDays : defaults.targetDays,
|
|
116
|
+
guardMode: body?.guardMode === 'expression' ? 'expression' : defaults.guardMode,
|
|
117
|
+
lang: typeof body?.lang === 'string' ? body.lang : defaults.lang,
|
|
118
|
+
}
|
|
119
|
+
const value = await run(options, body)
|
|
120
|
+
sendJson(res, 200, { ok: true, value })
|
|
121
|
+
}).catch((e) => {
|
|
122
|
+
sendJson(res, 200, { ok: false, code: 'internal', message: String(e && e.message ? e.message : e) })
|
|
123
|
+
})
|
|
124
|
+
},
|
|
125
|
+
}))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
endpoint(routes.check, (options) => {
|
|
129
|
+
const result = guard.check(options)
|
|
130
|
+
const cookie = cookieBrief(defaults.cookie)
|
|
131
|
+
// 目标寿命跟结果同屏:面板要说清「修复按钮写的是哪个值」,不让用户去猜设置里那一格
|
|
132
|
+
const withTarget = { ...result, targetDays: options.targetDays ?? defaults.targetDays }
|
|
133
|
+
return cookie === undefined ? withTarget : { ...withTarget, cookie }
|
|
134
|
+
})
|
|
135
|
+
endpoint(routes.fix, (options) => guard.fix(options))
|
|
136
|
+
endpoint(routes.revert, () => guard.revert())
|
|
137
|
+
// 运行期守护:读当前生效值 / 写入白名单四键(走 guard 的 configEditor 编辑通道)
|
|
138
|
+
endpoint(routes.guardGet, () => (typeof defaults.getGuard === 'function' ? defaults.getGuard() : { available: false }))
|
|
139
|
+
endpoint(routes.guardSet, (_options, body) => (typeof defaults.setGuard === 'function' ? defaults.setGuard(body ?? {}) : { kind: 'no-entry' }))
|
|
140
|
+
// 历史注入残留:读现状 / 清理(清理本身还要过 canCleanLegacy 的能力闸)
|
|
141
|
+
endpoint(routes.legacyGet, () => (typeof defaults.getLegacy === 'function' ? defaults.getLegacy() : { available: false, found: false }))
|
|
142
|
+
endpoint(routes.legacyClean, () => (typeof defaults.cleanLegacy === 'function' ? defaults.cleanLegacy() : { kind: 'no-editor' }))
|
|
143
|
+
return offs
|
|
144
|
+
}
|