dsh-cookie-cleaner 0.2.3 → 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.
@@ -7,11 +7,11 @@
7
7
  import fs from 'node:fs'
8
8
  import path from 'node:path'
9
9
  import { fileURLToPath } from 'node:url'
10
- import { HOSTS, STATUS, orRange, ENGINES_RANGE } from './hosts.mjs'
10
+ import { HOSTS, STATUS, ENGINES_RANGE, PEER_RANGE } from './hosts.mjs'
11
11
 
12
12
  const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
13
13
  const check = process.argv.includes('--check')
14
- const range = orRange(HOSTS)
14
+ const range = PEER_RANGE
15
15
  const releases = Object.fromEntries(HOSTS.map((host) => [host, STATUS]))
16
16
  const failures = []
17
17
 
package/src/api-routes.js CHANGED
@@ -1,133 +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
- })
25
- }
26
-
27
- const MAX_BODY_BYTES = 64 * 1024
28
-
29
- function sendJson(res, code, body) {
30
- res.writeHead(code, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-cache' })
31
- res.end(JSON.stringify(body))
32
- }
33
-
34
- /** 读 JSON body(有上限,超限直接拒——面板不该发大东西)。 */
35
- function readJsonBody(req) {
36
- return new Promise((resolve, reject) => {
37
- const chunks = []
38
- let size = 0
39
- req.on('data', (chunk) => {
40
- size += chunk.length
41
- if (size > MAX_BODY_BYTES) {
42
- reject(new Error('body too large'))
43
- return
44
- }
45
- chunks.push(chunk)
46
- })
47
- req.on('end', () => {
48
- if (chunks.length === 0) { resolve({}); return }
49
- try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8'))) }
50
- catch (e) { reject(new Error(`invalid json body: ${e.message}`)) }
51
- })
52
- req.on('error', reject)
53
- })
54
- }
55
-
56
- /**
57
- * cookie 库现状只留卡片需要看的字段。
58
- * 读得到就带真实条数与请求头占用折算(`header`),并带出它是实时还是借句柄快照;
59
- * 读不到才只给 verdict/reason,不能谎报条数。
60
- * @param {() => any} [stat]
61
- */
62
- export function cookieBrief(stat) {
63
- if (typeof stat !== 'function') return undefined
64
- try {
65
- const s = stat()
66
- if (s === undefined || s === null) return undefined
67
- return {
68
- verdict: s.verdict,
69
- reason: s.reason,
70
- path: s.path,
71
- authCookies: s.authCookies,
72
- totalCookies: s.totalCookies,
73
- bytes: s.bytes,
74
- header: s.header,
75
- source: s.source,
76
- snapshot: s.snapshot === true,
77
- liveUnavailable: s.liveUnavailable === true,
78
- liveReason: s.liveReason,
79
- }
80
- } catch { return undefined }
81
- }
82
-
83
- /**
84
- * 注册路由。
85
- * @param {unknown} webServer `webServer` 服务实例(只要求有 register)
86
- * @param {{check:(o:object)=>object, plan:(o:object)=>object, fix:(o:object)=>Promise<object>, revert:()=>Promise<object>}} guard
87
- * @param {{lang?:string, targetDays?:number, guardMode?:string, cookie?:() => any}} [defaults] 宿主设置里的默认档
88
- * @returns {Array<() => void>} 注销函数
89
- */
90
- export function registerPatchRoutes(webServer, guard, defaults = {}) {
91
- const register = typeof webServer?.register === 'function' ? webServer.register.bind(webServer) : undefined
92
- if (register === undefined) return []
93
- const routes = bridgeRoutesFor(defaults.ns ?? NS)
94
- const offs = []
95
-
96
- /** 统一的一跳:POST-only、参数从 body 取、异常包成结构化错误。 */
97
- const endpoint = (path, run) => {
98
- offs.push(register({
99
- kind: 'exact',
100
- path,
101
- handler: (rawReq, rawRes) => {
102
- const req = rawReq
103
- const res = rawRes
104
- if (req.method !== 'POST') {
105
- sendJson(res, 405, { ok: false, code: 'method-not-allowed' })
106
- return
107
- }
108
- readJsonBody(req).then(async (body) => {
109
- const options = {
110
- targetDays: Number.isInteger(body?.targetDays) && body.targetDays >= 1 ? body.targetDays : defaults.targetDays,
111
- guardMode: body?.guardMode === 'expression' ? 'expression' : defaults.guardMode,
112
- lang: typeof body?.lang === 'string' ? body.lang : defaults.lang,
113
- }
114
- const value = await run(options)
115
- sendJson(res, 200, { ok: true, value })
116
- }).catch((e) => {
117
- sendJson(res, 200, { ok: false, code: 'internal', message: String(e && e.message ? e.message : e) })
118
- })
119
- },
120
- }))
121
- }
122
-
123
- endpoint(routes.check, (options) => {
124
- const result = guard.check(options)
125
- const cookie = cookieBrief(defaults.cookie)
126
- // 目标寿命跟结果同屏:面板要说清「修复按钮写的是哪个值」,不让用户去猜设置里那一格
127
- const withTarget = { ...result, targetDays: options.targetDays ?? defaults.targetDays }
128
- return cookie === undefined ? withTarget : { ...withTarget, cookie }
129
- })
130
- endpoint(routes.fix, (options) => guard.fix(options))
131
- endpoint(routes.revert, () => guard.revert())
132
- return offs
133
- }
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
+ }