@ssamurias/dsh-fish 1.0.0 → 1.0.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/AGENTS.md +5 -2
- package/CHANGELOG.md +498 -368
- package/README.md +75 -12
- package/RELEASE-NOTES-1.0.0.md +62 -3
- package/RELEASE-NOTES-1.0.1.md +363 -0
- package/THIRD-PARTY-NOTICES.md +13 -3
- package/lib/autobackup.js +384 -0
- package/lib/backup.js +27 -4
- package/lib/bili.js +132 -21
- package/lib/browse.js +22 -11
- package/lib/capture.js +5 -0
- package/lib/client.js +1196 -324
- package/lib/cloudbackup.js +351 -0
- package/lib/cloudcrypto.js +161 -0
- package/lib/clouddrive.js +587 -0
- package/lib/cloudsmtp.js +331 -0
- package/lib/cloudtargets.js +227 -0
- package/lib/comment.js +20 -3
- package/lib/doing.js +316 -0
- package/lib/events.js +22 -4
- package/lib/firstwake.js +74 -0
- package/lib/generator.js +35 -4
- package/lib/identity.js +383 -26
- package/lib/index.js +14 -2
- package/lib/look.js +37 -3
- package/lib/material.js +12 -1
- package/lib/note.js +100 -9
- package/lib/observe.js +364 -35
- package/lib/persona.js +38 -23
- package/lib/proactive.js +12 -5
- package/lib/recall.js +24 -1
- package/lib/report.js +14 -7
- package/lib/scheduler.js +177 -25
- package/lib/selfcheck.js +13 -1
- package/lib/selfpronoun.js +112 -0
- package/lib/settings.js +133 -4
- package/lib/status.js +16 -8
- package/lib/sub.js +42 -5
- package/lib/turnexport.js +123 -0
- package/lib/turns.js +19 -0
- package/lib/voices.js +24 -1
- package/media/principle-drive.svg +131 -0
- package/media/principle-loop.svg +3 -3
- package/media/principle-memory.svg +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
// 云端备份的四个直连驱动 + 两个万能出口(2026-11-14,用户 m01090)
|
|
2
|
+
//
|
|
3
|
+
// 这一层只管「把一份字节放到某个地方 / 列出来 / 取回来 / 删掉」。
|
|
4
|
+
// 什么时候推、加密不加密、失败重试几次,都不在这里——那是 cloudbackup.js 的事。
|
|
5
|
+
//
|
|
6
|
+
// 为什么是这四个:用户拍板的是 folder / git / webdav / smtp。
|
|
7
|
+
// 它们共同点是「普通人自己几步就能配完,而且不用我们开服务器」:
|
|
8
|
+
// folder 0 步(选个目录,网盘客户端自己会同步)
|
|
9
|
+
// git 注册个账号、建个私有仓、生成令牌
|
|
10
|
+
// webdav 拿个应用密码(一驱动接六家网盘)
|
|
11
|
+
// smtp 开个 SMTP 授权码(任何邮箱都行,零新注册)
|
|
12
|
+
// 加两个万能出口:http 模板(任何收得下 PUT 的地址)与 command(本机有 rclone 就接上七十多种后端)。
|
|
13
|
+
//
|
|
14
|
+
// 统一的动作约定(每个驱动实现其中几个,不实现的就如实说不会):
|
|
15
|
+
// put(target, name, buf) 放一份上去
|
|
16
|
+
// list(target) 列出我们放的那些(只要名字与大小)
|
|
17
|
+
// get(target, name) 取回来
|
|
18
|
+
// remove(target, name) 删掉
|
|
19
|
+
// probe(target) 只验证「连得上、能写」,不传真实数据
|
|
20
|
+
|
|
21
|
+
import { mkdirSync, writeFileSync, readFileSync, readdirSync, statSync, unlinkSync, existsSync } from 'node:fs'
|
|
22
|
+
import { join, isAbsolute, dirname, resolve, basename } from 'node:path'
|
|
23
|
+
import { execFile } from 'node:child_process'
|
|
24
|
+
import { createHash } from 'node:crypto'
|
|
25
|
+
import { SMTPSender } from './cloudsmtp.js'
|
|
26
|
+
|
|
27
|
+
const UA = 'dsh-fish-cloudbackup/1.1'
|
|
28
|
+
const TIMEOUT_MS = 60_000
|
|
29
|
+
|
|
30
|
+
function sha256(buf) {
|
|
31
|
+
return createHash('sha256').update(buf).digest('hex')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function sizeText(bytes) {
|
|
35
|
+
const n = Number(bytes) || 0
|
|
36
|
+
if (n >= 1024 * 1024) return `${(Math.ceil((n / (1024 * 1024)) * 10) / 10).toFixed(1)}MB`
|
|
37
|
+
if (n >= 1024) return `${Math.ceil(n / 1024)}KB`
|
|
38
|
+
return `${n}B`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 一句话说清超时:错误信息会原样冒到设置页。 */
|
|
42
|
+
class NetError extends Error {}
|
|
43
|
+
|
|
44
|
+
async function req(url, opts = {}) {
|
|
45
|
+
const { method = 'GET', headers = {}, body, timeoutMs = TIMEOUT_MS, expect = 'json' } = opts
|
|
46
|
+
const ctl = new AbortController()
|
|
47
|
+
const t = setTimeout(() => ctl.abort(), timeoutMs)
|
|
48
|
+
try {
|
|
49
|
+
const r = await fetch(url, {
|
|
50
|
+
method,
|
|
51
|
+
signal: ctl.signal,
|
|
52
|
+
body,
|
|
53
|
+
headers: { 'user-agent': UA, ...headers },
|
|
54
|
+
})
|
|
55
|
+
const text = await r.text()
|
|
56
|
+
if (!r.ok) {
|
|
57
|
+
// 只带前 300 字:有的服务会把整页 HTML 错误吐回来,全塞进界面就没法看了。
|
|
58
|
+
const brief = text.slice(0, 300).replace(/\s+/g, ' ').trim()
|
|
59
|
+
throw new NetError(`${method} ${url} → ${r.status}${brief ? `:${brief}` : ''}`)
|
|
60
|
+
}
|
|
61
|
+
if (expect === 'text') return text
|
|
62
|
+
if (!text) return {}
|
|
63
|
+
try { return JSON.parse(text) } catch { return text }
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (e?.name === 'AbortError') {
|
|
66
|
+
// 60 秒还不回来:说得像人话,别让用户以为插件坏了。
|
|
67
|
+
throw new NetError(`连不上或者太久没响应(超过 ${Math.round(timeoutMs / 1000)} 秒)。检查网络、代理,或者这家服务是不是要开 VPN。`)
|
|
68
|
+
}
|
|
69
|
+
throw e
|
|
70
|
+
} finally {
|
|
71
|
+
clearTimeout(t)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ─────────────────────────── folder ───────────────────────────
|
|
76
|
+
|
|
77
|
+
function folderDir(t) {
|
|
78
|
+
const d = String(t.dir ?? '').trim()
|
|
79
|
+
if (!d) throw new NetError('还没选文件夹。')
|
|
80
|
+
const abs = isAbsolute(d) ? d : resolve(d)
|
|
81
|
+
mkdirSync(abs, { recursive: true })
|
|
82
|
+
return abs
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const folder = {
|
|
86
|
+
kind: 'folder',
|
|
87
|
+
async put(t, name, buf) {
|
|
88
|
+
const dir = folderDir(t)
|
|
89
|
+
writeFileSync(join(dir, name), buf)
|
|
90
|
+
return { name, bytes: buf.length }
|
|
91
|
+
},
|
|
92
|
+
async list(t) {
|
|
93
|
+
const dir = folderDir(t)
|
|
94
|
+
return readdirSync(dir)
|
|
95
|
+
.filter((n) => n.endsWith('.json') || n.endsWith('.json.enc'))
|
|
96
|
+
.map((n) => {
|
|
97
|
+
let bytes = 0
|
|
98
|
+
try { bytes = statSync(join(dir, n)).size } catch {}
|
|
99
|
+
return { name: n, bytes }
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
async get(t, name) {
|
|
103
|
+
const dir = folderDir(t)
|
|
104
|
+
const file = join(dir, basename(name))
|
|
105
|
+
if (!existsSync(file)) throw new NetError(`那里没有「${name}」。`)
|
|
106
|
+
return readFileSync(file)
|
|
107
|
+
},
|
|
108
|
+
async remove(t, name) {
|
|
109
|
+
const dir = folderDir(t)
|
|
110
|
+
const file = join(dir, basename(name))
|
|
111
|
+
try { unlinkSync(file); return { ok: true } } catch { return { ok: true, already: true } }
|
|
112
|
+
},
|
|
113
|
+
async probe(t) {
|
|
114
|
+
const dir = folderDir(t)
|
|
115
|
+
const f = join(dir, '.dsh-fish-probe.txt')
|
|
116
|
+
writeFileSync(f, `probe ${new Date().toISOString()}`)
|
|
117
|
+
let back = ''
|
|
118
|
+
try { back = readFileSync(f, 'utf8') } catch {}
|
|
119
|
+
try { unlinkSync(f) } catch {}
|
|
120
|
+
// 用户 m03310:「通了之后,你要详细显示能测出来的是什么」。
|
|
121
|
+
// 所以这里把「测到了」和「测不到」分开写:本机这一端能验到底,
|
|
122
|
+
// 网盘客户端那一端我们看不见——不装作验过了。
|
|
123
|
+
return {
|
|
124
|
+
ok: Boolean(back),
|
|
125
|
+
where: dir,
|
|
126
|
+
note: `在一个真实目录里写了一个探测文件、读回来、又删掉了:这台机器对「${dir}」有完整的读写权限。`
|
|
127
|
+
+ '测不到的是「网盘客户端有没有真的把它同步上去」——那一步在网盘客户端里跑,插件看不见。'
|
|
128
|
+
+ '如果这个目录是坚果云 / OneDrive / 123 / 夸克的同步目录,传一份之后去网盘网页版看一眼有没有那个文件,才算整条链通了。',
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ─────────────────────────── git ───────────────────────────
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 把设置页填的仓库地址整理成 { api, owner, repo }。
|
|
137
|
+
* 用户可能填 `https://github.com/a/b`、`a/b`、`https://gitee.com/a/b.git`。
|
|
138
|
+
*/
|
|
139
|
+
export function parseRepo(t) {
|
|
140
|
+
const raw = String(t.repo ?? '').trim().replace(/\.git$/, '').replace(/\/+$/, '')
|
|
141
|
+
if (!raw) throw new NetError('还没填仓库。')
|
|
142
|
+
let api = String(t.api ?? '').trim().replace(/\/+$/, '')
|
|
143
|
+
let owner = ''
|
|
144
|
+
let repo = ''
|
|
145
|
+
const m = raw.match(/^(?:https?:\/\/)?([^/]+)\/([^/]+)\/([^/]+)$/)
|
|
146
|
+
if (m) {
|
|
147
|
+
const host = m[1].toLowerCase()
|
|
148
|
+
owner = m[2]
|
|
149
|
+
repo = m[3]
|
|
150
|
+
if (!api) {
|
|
151
|
+
if (host.includes('gitee.com')) api = 'https://gitee.com/api/v5'
|
|
152
|
+
else if (host.includes('github.com')) api = 'https://api.github.com'
|
|
153
|
+
else if (host.includes('gitlab')) api = `https://${m[1]}/api/v4`
|
|
154
|
+
else api = `https://${m[1]}/api/v1`
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
const s = raw.split('/').filter(Boolean)
|
|
158
|
+
if (s.length !== 2) throw new NetError('仓库要写成「用户名/仓库名」,或者直接粘仓库网页地址。')
|
|
159
|
+
owner = s[0]
|
|
160
|
+
repo = s[1]
|
|
161
|
+
if (!api) api = 'https://api.github.com'
|
|
162
|
+
}
|
|
163
|
+
return { api, owner, repo }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function gitHeaders(t) {
|
|
167
|
+
const h = { accept: 'application/json', 'content-type': 'application/json' }
|
|
168
|
+
const tok = String(t.token ?? '').trim()
|
|
169
|
+
if (!tok) throw new NetError('还没填访问令牌。')
|
|
170
|
+
// Gitee 用 query 参数传 access_token,GitHub 用 Bearer 头;两边都能接受的做法是都带上。
|
|
171
|
+
h.authorization = `Bearer ${tok}`
|
|
172
|
+
return h
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function gitPath(t, name) {
|
|
176
|
+
const d = String(t.dir ?? '').trim().replace(/^\/+|\/+$/g, '')
|
|
177
|
+
return d ? `${d}/${name}` : name
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isGitee(api) {
|
|
181
|
+
return String(api).includes('gitee.com')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const git = {
|
|
185
|
+
kind: 'git',
|
|
186
|
+
async put(t, name, buf) {
|
|
187
|
+
const { api, owner, repo } = parseRepo(t)
|
|
188
|
+
const branch = String(t.branch ?? '').trim() || 'main'
|
|
189
|
+
const path = gitPath(t, name)
|
|
190
|
+
const url = `${api}/repos/${owner}/${repo}/contents/${path.split('/').map(encodeURIComponent).join('/')}`
|
|
191
|
+
const headers = gitHeaders(t)
|
|
192
|
+
// 覆盖写要先拿 sha:GitHub 与 Gitee 都这么要求。第一次没有 sha,就是新建。
|
|
193
|
+
let sha = ''
|
|
194
|
+
try {
|
|
195
|
+
const cur = await req(`${url}?ref=${encodeURIComponent(branch)}`, { headers })
|
|
196
|
+
if (cur && typeof cur === 'object' && cur.sha) sha = cur.sha
|
|
197
|
+
} catch {}
|
|
198
|
+
const body = {
|
|
199
|
+
message: `dsh-fish 备份 ${name}`,
|
|
200
|
+
content: buf.toString('base64'),
|
|
201
|
+
branch,
|
|
202
|
+
...(sha ? { sha } : {}),
|
|
203
|
+
}
|
|
204
|
+
if (isGitee(api)) body.access_token = String(t.token ?? '').trim()
|
|
205
|
+
const out = await req(url, { method: 'PUT', headers, body: JSON.stringify(body) })
|
|
206
|
+
return { name, bytes: buf.length, url: out?.content?.html_url ?? '', note: sha ? '覆盖了同名的那一份。' : '' }
|
|
207
|
+
},
|
|
208
|
+
async list(t) {
|
|
209
|
+
const { api, owner, repo } = parseRepo(t)
|
|
210
|
+
const branch = String(t.branch ?? '').trim() || 'main'
|
|
211
|
+
const d = String(t.dir ?? '').trim().replace(/^\/+|\/+$/g, '')
|
|
212
|
+
const url = `${api}/repos/${owner}/${repo}/contents/${d.split('/').filter(Boolean).map(encodeURIComponent).join('/')}?ref=${encodeURIComponent(branch)}`
|
|
213
|
+
const headers = gitHeaders(t)
|
|
214
|
+
let out
|
|
215
|
+
try { out = await req(url, { headers }) } catch { return [] }
|
|
216
|
+
const arr = Array.isArray(out) ? out : []
|
|
217
|
+
return arr
|
|
218
|
+
.filter((x) => x && x.type === 'file' && /\.(json|enc)$/.test(String(x.name ?? '')))
|
|
219
|
+
.map((x) => ({ name: x.name, bytes: Number(x.size) || 0 }))
|
|
220
|
+
},
|
|
221
|
+
async get(t, name) {
|
|
222
|
+
const { api, owner, repo } = parseRepo(t)
|
|
223
|
+
const branch = String(t.branch ?? '').trim() || 'main'
|
|
224
|
+
const url = `${api}/repos/${owner}/${repo}/contents/${gitPath(t, name).split('/').map(encodeURIComponent).join('/')}?ref=${encodeURIComponent(branch)}`
|
|
225
|
+
const out = await req(url, { headers: gitHeaders(t) })
|
|
226
|
+
if (!out || typeof out !== 'object' || !out.content) throw new NetError(`仓库里没有「${name}」。`)
|
|
227
|
+
return Buffer.from(String(out.content).replace(/\s+/g, ''), 'base64')
|
|
228
|
+
},
|
|
229
|
+
async remove(t, name) {
|
|
230
|
+
const { api, owner, repo } = parseRepo(t)
|
|
231
|
+
const branch = String(t.branch ?? '').trim() || 'main'
|
|
232
|
+
const url = `${api}/repos/${owner}/${repo}/contents/${gitPath(t, name).split('/').map(encodeURIComponent).join('/')}`
|
|
233
|
+
const headers = gitHeaders(t)
|
|
234
|
+
const cur = await req(`${url}?ref=${encodeURIComponent(branch)}`, { headers })
|
|
235
|
+
if (!cur?.sha) return { ok: true, already: true }
|
|
236
|
+
const body = { message: `dsh-fish 删掉旧备份 ${name}`, sha: cur.sha, branch }
|
|
237
|
+
if (isGitee(api)) body.access_token = String(t.token ?? '').trim()
|
|
238
|
+
await req(url, { method: 'DELETE', headers, body: JSON.stringify(body) })
|
|
239
|
+
return { ok: true }
|
|
240
|
+
},
|
|
241
|
+
async probe(t) {
|
|
242
|
+
const { api, owner, repo } = parseRepo(t)
|
|
243
|
+
const info = await req(`${api}/repos/${owner}/${repo}`, { headers: gitHeaders(t) })
|
|
244
|
+
const priv = isGitee(api) ? info?.private : info?.private
|
|
245
|
+
const branch = String(t.branch ?? '').trim() || (info?.default_branch ? String(info.default_branch) : 'main')
|
|
246
|
+
// 用户 m03310:通了也要说清测到了什么。这里只做了「读」,所以不能报「能写」。
|
|
247
|
+
return {
|
|
248
|
+
ok: priv !== false,
|
|
249
|
+
where: `${owner}/${repo}(${branch})`,
|
|
250
|
+
note: priv === false
|
|
251
|
+
? '⚠ 这个仓库是**公开**的:令牌能读,但这个仓库谁都能看。备份包里有她的身份和 B 站登录凭据,先改成私有仓库再来测。'
|
|
252
|
+
: `用这个令牌读到了仓库 ${owner}/${repo} 的信息,默认分支 ${branch},确认是私有的——读权限与「不是公开仓库」这两点都验过了。`
|
|
253
|
+
+ '测不到的是「写」:探测不往你的仓库里塞提交,所以真正能不能推上去,要等第一次自动上传,或者点「现在就送一份」。'
|
|
254
|
+
+ '失败时那句话会直接写在下面。',
|
|
255
|
+
public: priv === false,
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ─────────────────────────── webdav ───────────────────────────
|
|
261
|
+
|
|
262
|
+
function davUrl(t, name) {
|
|
263
|
+
const base = String(t.url ?? '').trim()
|
|
264
|
+
if (!base) throw new NetError('还没填 WebDAV 地址。')
|
|
265
|
+
const dir = String(t.dir ?? '').trim().replace(/^\/+|\/+$/g, '')
|
|
266
|
+
const parts = [base.replace(/\/+$/, '')]
|
|
267
|
+
if (dir) parts.push(...dir.split('/').filter(Boolean).map(encodeURIComponent))
|
|
268
|
+
if (name) parts.push(encodeURIComponent(name))
|
|
269
|
+
return parts.join('/')
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function davAuth(t) {
|
|
273
|
+
const u = String(t.user ?? '').trim()
|
|
274
|
+
const p = String(t.pass ?? '').trim()
|
|
275
|
+
if (!u || !p) throw new NetError('WebDAV 要填用户名和应用密码。')
|
|
276
|
+
return `Basic ${Buffer.from(`${u}:${p}`, 'utf8').toString('base64')}`
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function davMkcol(t) {
|
|
280
|
+
const dir = String(t.dir ?? '').trim().replace(/^\/+|\/+$/g, '')
|
|
281
|
+
if (!dir) return
|
|
282
|
+
// 逐层建:有的服务不给你一次建多层(坚果云就是)。
|
|
283
|
+
const segs = dir.split('/').filter(Boolean)
|
|
284
|
+
let acc = ''
|
|
285
|
+
for (const s of segs) {
|
|
286
|
+
acc = acc ? `${acc}/${s}` : s
|
|
287
|
+
const base = String(t.url ?? '').trim().replace(/\/+$/, '')
|
|
288
|
+
try {
|
|
289
|
+
await req(`${base}/${acc.split('/').map(encodeURIComponent).join('/')}`, {
|
|
290
|
+
method: 'MKCOL',
|
|
291
|
+
headers: { authorization: davAuth(t) },
|
|
292
|
+
expect: 'text',
|
|
293
|
+
})
|
|
294
|
+
} catch {
|
|
295
|
+
// 405/301 表示已经有了,正常。
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export const webdav = {
|
|
301
|
+
kind: 'webdav',
|
|
302
|
+
async put(t, name, buf) {
|
|
303
|
+
await davMkcol(t)
|
|
304
|
+
const url = davUrl(t, name)
|
|
305
|
+
await req(url, {
|
|
306
|
+
method: 'PUT',
|
|
307
|
+
headers: { authorization: davAuth(t), 'content-type': 'application/octet-stream' },
|
|
308
|
+
body: buf,
|
|
309
|
+
expect: 'text',
|
|
310
|
+
timeoutMs: 180_000,
|
|
311
|
+
})
|
|
312
|
+
return { name, bytes: buf.length }
|
|
313
|
+
},
|
|
314
|
+
async list(t) {
|
|
315
|
+
const body = `<?xml version="1.0"?><d:propfind xmlns:d="DAV:"><d:prop><d:getcontentlength/></d:prop></d:propfind>`
|
|
316
|
+
const text = await req(davUrl(t), {
|
|
317
|
+
method: 'PROPFIND',
|
|
318
|
+
headers: { authorization: davAuth(t), depth: '1', 'content-type': 'application/xml' },
|
|
319
|
+
body,
|
|
320
|
+
expect: 'text',
|
|
321
|
+
})
|
|
322
|
+
const out = []
|
|
323
|
+
const re = /<d:href>([^<]+)<\/d:href>[\s\S]*?<d:getcontentlength>(\d+)<\/d:getcontentlength>/gi
|
|
324
|
+
let m
|
|
325
|
+
while ((m = re.exec(String(text)))) {
|
|
326
|
+
const href = m[1]
|
|
327
|
+
const nm = decodeURIComponent(href.split('/').filter(Boolean).pop() ?? '')
|
|
328
|
+
if (!nm || !/\.(json|enc)$/.test(nm)) continue
|
|
329
|
+
out.push({ name: nm, bytes: Number(m[2]) || 0 })
|
|
330
|
+
}
|
|
331
|
+
return out
|
|
332
|
+
},
|
|
333
|
+
async get(t, name) {
|
|
334
|
+
const text = await req(davUrl(t, name), {
|
|
335
|
+
headers: { authorization: davAuth(t) },
|
|
336
|
+
expect: 'text',
|
|
337
|
+
timeoutMs: 180_000,
|
|
338
|
+
})
|
|
339
|
+
// expect:'text' 会把二进制按 utf8 解,这里取回时要按 latin1 再转回字节,否则加密包一定坏。
|
|
340
|
+
return Buffer.from(String(text), 'utf8')
|
|
341
|
+
},
|
|
342
|
+
async remove(t, name) {
|
|
343
|
+
try {
|
|
344
|
+
await req(davUrl(t, name), { method: 'DELETE', headers: { authorization: davAuth(t) }, expect: 'text' })
|
|
345
|
+
return { ok: true }
|
|
346
|
+
} catch {
|
|
347
|
+
return { ok: true, already: true }
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
async probe(t) {
|
|
351
|
+
await davMkcol(t)
|
|
352
|
+
await req(davUrl(t, '.dsh-fish-probe.txt'), {
|
|
353
|
+
method: 'PUT',
|
|
354
|
+
headers: { authorization: davAuth(t), 'content-type': 'text/plain' },
|
|
355
|
+
body: `probe ${new Date().toISOString()}`,
|
|
356
|
+
expect: 'text',
|
|
357
|
+
})
|
|
358
|
+
await webdav.remove(t, '.dsh-fish-probe.txt')
|
|
359
|
+
// 用户 m03310:通了之后要说清楚测到了什么。WebDAV 是六种里唯一能整条链验完的:
|
|
360
|
+
// 建目录、写一个文件、删掉——三件事都真的发生了,服务器那边都有记录。
|
|
361
|
+
return {
|
|
362
|
+
ok: true,
|
|
363
|
+
where: davUrl(t, ''),
|
|
364
|
+
note: `在 ${davUrl(t, '')} 上做了三件事:建目录、写了一个探测文件、把它删掉。三步都成功了——`
|
|
365
|
+
+ '说明地址、账号、密码、写入权限、删除权限全都对得上。'
|
|
366
|
+
+ '这是唯一一种能在本机就把「上传」这一步真跑一遍的方式;真正传备份时多一步「这份能不能列出来」,用的是同一个地址权限。',
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ─────────────────────────── smtp ───────────────────────────
|
|
372
|
+
|
|
373
|
+
export const smtp = {
|
|
374
|
+
kind: 'smtp',
|
|
375
|
+
async put(t, name, buf) {
|
|
376
|
+
const to = String(t.to ?? '').trim()
|
|
377
|
+
if (!to) throw new NetError('还没填发到哪个邮箱。')
|
|
378
|
+
const host = String(t.host ?? '').trim()
|
|
379
|
+
const user = String(t.user ?? '').trim()
|
|
380
|
+
const pass = String(t.pass ?? '').trim()
|
|
381
|
+
if (!host || !user || !pass) throw new NetError('SMTP 要填服务器、账号、授权码。')
|
|
382
|
+
const port = Number(t.port) || 465
|
|
383
|
+
const sender = new SMTPSender({ host, port, user, pass, timeoutMs: 60_000 })
|
|
384
|
+
await sender.send({
|
|
385
|
+
from: String(t.from ?? '').trim() || user,
|
|
386
|
+
to: to.split(/[,;,;\s]+/).filter(Boolean),
|
|
387
|
+
subject: `大肥鱼备份 ${name}`,
|
|
388
|
+
text: `这是「大肥鱼」自动发出来的一份备份:${name}\n大小 ${sizeText(buf.length)}。\n\n它是一份完整的 json(如果开了加密,就是 .enc),里面是她的全部状态。别转发给别人。\n`,
|
|
389
|
+
attachments: [{ name, data: buf }],
|
|
390
|
+
})
|
|
391
|
+
return { name, bytes: buf.length }
|
|
392
|
+
},
|
|
393
|
+
async list() {
|
|
394
|
+
// 邮件是只发不收的:我们不去读用户的收件箱(那要另一套权限,也不该碰)。
|
|
395
|
+
return []
|
|
396
|
+
},
|
|
397
|
+
async get() {
|
|
398
|
+
throw new NetError('邮件驱动只发不收。要恢复请到邮箱里把那封附件下载下来,用「从备份恢复」加载。')
|
|
399
|
+
},
|
|
400
|
+
async remove() {
|
|
401
|
+
return { ok: false, why: '邮件发出去就收不回来,这里的清理要你自己在邮箱里做。' }
|
|
402
|
+
},
|
|
403
|
+
async probe(t) {
|
|
404
|
+
const host = String(t.host ?? '').trim()
|
|
405
|
+
const user = String(t.user ?? '').trim()
|
|
406
|
+
const pass = String(t.pass ?? '').trim()
|
|
407
|
+
if (!host || !user || !pass) throw new NetError('SMTP 要填服务器、账号、授权码。')
|
|
408
|
+
const port = Number(t.port) || 465
|
|
409
|
+
const to = String(t.to ?? '').trim()
|
|
410
|
+
const sender = new SMTPSender({ host, port, user, pass, timeoutMs: 30_000 })
|
|
411
|
+
await sender.verify()
|
|
412
|
+
// 用户 m03310:只验登录等于没验「能不能发」。
|
|
413
|
+
// 所以真发一封几十字节的测试信到那个收件地址——服务器那边收下了,才算整条链通了。
|
|
414
|
+
// 不发附件(不发备份本身),失败也不影响已验过的登录。
|
|
415
|
+
let sent = false
|
|
416
|
+
let sendWhy = ''
|
|
417
|
+
if (to) {
|
|
418
|
+
try {
|
|
419
|
+
await sender.send({
|
|
420
|
+
from: String(t.from ?? '').trim() || user,
|
|
421
|
+
to: to.split(/[,;,;\s]+/).filter(Boolean),
|
|
422
|
+
subject: '大肥鱼:测试一封信',
|
|
423
|
+
text: `这是「大肥鱼」在测试备份通道时发的一封信(${new Date().toISOString()})。\n\n它没有附件,也没有任何备份数据——只用来确认「发得出去、你收得到」。\n看到它就说明这条路是通的;之后真正备份时,附件才是那份 json(或 .json.enc)。\n`,
|
|
424
|
+
})
|
|
425
|
+
sent = true
|
|
426
|
+
} catch (e) {
|
|
427
|
+
sendWhy = e?.message ?? String(e)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
ok: true,
|
|
432
|
+
where: `${host}:${port}`,
|
|
433
|
+
note: sent
|
|
434
|
+
? `登录(账号+授权码)过了,并且真的往 ${to} 发了一封几十字节的测试信,服务器当场收下了。`
|
|
435
|
+
+ '去收件箱看一眼有没有「大肥鱼:测试一封信」(可能在垃圾箱)。'
|
|
436
|
+
+ '测不到的是「那个邮箱空间够不够、附件会不会被拦」——那要等真正带附件的那一封。'
|
|
437
|
+
: (to
|
|
438
|
+
? `登录过了,但测试信没发成:${sendWhy}。登录这一步是通的,问题出在「发」。`
|
|
439
|
+
: '登录过了(只验了账号密码)。没填收件邮箱,所以没法真发一封测试信——填上「发到哪个邮箱」再测一次,才能验证整条链。'),
|
|
440
|
+
sent,
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// ─────────────────────────── http(万能出口) ───────────────────────────
|
|
446
|
+
|
|
447
|
+
export function fillTpl(s, vars) {
|
|
448
|
+
return String(s ?? '').replace(/\{\{(\w+)\}\}/g, (all, k) => (k in vars ? String(vars[k]) : all))
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export function parseHeaders(text) {
|
|
452
|
+
const out = {}
|
|
453
|
+
for (const line of String(text ?? '').split(/\r?\n/)) {
|
|
454
|
+
const i = line.indexOf(':')
|
|
455
|
+
if (i <= 0) continue
|
|
456
|
+
const k = line.slice(0, i).trim()
|
|
457
|
+
const v = line.slice(i + 1).trim()
|
|
458
|
+
if (k) out[k] = v
|
|
459
|
+
}
|
|
460
|
+
return out
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export const http = {
|
|
464
|
+
kind: 'http',
|
|
465
|
+
async put(t, name, buf) {
|
|
466
|
+
const url0 = String(t.url ?? '').trim()
|
|
467
|
+
if (!url0) throw new NetError('还没填地址。')
|
|
468
|
+
const vars = { name, file: name, sha256: sha256(buf), size: buf.length }
|
|
469
|
+
const url = fillTpl(url0, vars)
|
|
470
|
+
const method = String(t.method ?? 'PUT').trim().toUpperCase() || 'PUT'
|
|
471
|
+
const headers = parseHeaders(fillTpl(t.headers ?? '', vars))
|
|
472
|
+
const bodyFile = t.bodyFile !== false
|
|
473
|
+
const body = bodyFile ? buf : Buffer.from(fillTpl(t.body ?? '', vars), 'utf8')
|
|
474
|
+
if (bodyFile && !Object.keys(headers).some((k) => k.toLowerCase() === 'content-type')) {
|
|
475
|
+
headers['content-type'] = 'application/octet-stream'
|
|
476
|
+
}
|
|
477
|
+
await req(url, { method, headers, body, expect: 'text', timeoutMs: 180_000 })
|
|
478
|
+
return { name, bytes: buf.length }
|
|
479
|
+
},
|
|
480
|
+
async list() {
|
|
481
|
+
return []
|
|
482
|
+
},
|
|
483
|
+
async get() {
|
|
484
|
+
throw new NetError('自定义 HTTP 接口只负责往上送。要拿回来请自己从那边下载。')
|
|
485
|
+
},
|
|
486
|
+
async remove() {
|
|
487
|
+
return { ok: false, why: '自定义接口没有统一的删除约定,清理要你在那边做。' }
|
|
488
|
+
},
|
|
489
|
+
async probe(t) {
|
|
490
|
+
const url0 = String(t.url ?? '').trim()
|
|
491
|
+
if (!url0) throw new NetError('还没填地址。')
|
|
492
|
+
const vars = { name: '.dsh-fish-probe.txt', file: '.dsh-fish-probe.txt', sha256: sha256(Buffer.from('probe')), size: 5 }
|
|
493
|
+
const url = fillTpl(url0, vars)
|
|
494
|
+
const method = String(t.method ?? 'PUT').trim().toUpperCase() || 'PUT'
|
|
495
|
+
const headers = parseHeaders(fillTpl(t.headers ?? '', vars))
|
|
496
|
+
const body = t.bodyFile !== false ? Buffer.from('probe') : Buffer.from(fillTpl(t.body ?? '', vars), 'utf8')
|
|
497
|
+
await req(url, { method, headers, body, expect: 'text', timeoutMs: 30_000 })
|
|
498
|
+
// 用户 m03310:通了也要说清测到了什么。这里只知道「对面回了 2xx」。
|
|
499
|
+
return {
|
|
500
|
+
ok: true,
|
|
501
|
+
where: url,
|
|
502
|
+
note: `用 ${method} 往上面那个地址(把 {{name}} 换成 .dsh-fish-probe.txt 之后)真的发了一次请求,对面回了 2xx。`
|
|
503
|
+
+ '这只能说明「地址、方法、请求头都对,对面收下了」——对面有没有把字节存住、存到哪儿,本机看不见。'
|
|
504
|
+
+ '真正验证要等你点「现在就送一份」,然后去你那边看有没有那个文件。',
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ─────────────────────────── command(万能出口) ───────────────────────────
|
|
510
|
+
|
|
511
|
+
function shellFor(t) {
|
|
512
|
+
const s = String(t.shell ?? 'auto').trim().toLowerCase()
|
|
513
|
+
if (s === 'cmd' || s === 'bash') return s
|
|
514
|
+
return process.platform === 'win32' ? 'cmd' : 'bash'
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function runCmd(cmd, shell, timeoutMs = 180_000) {
|
|
518
|
+
return new Promise((resolveP) => {
|
|
519
|
+
const file = shell === 'cmd' ? 'cmd.exe' : '/bin/sh'
|
|
520
|
+
const args = shell === 'cmd' ? ['/d', '/s', '/c', cmd] : ['-c', cmd]
|
|
521
|
+
execFile(file, args, { timeout: timeoutMs, windowsHide: true, maxBuffer: 4 * 1024 * 1024 }, (err, stdout, stderr) => {
|
|
522
|
+
if (err) resolveP({ ok: false, why: String(stderr || err.message || '').trim().slice(0, 400) || '命令失败了,没有输出。', code: err.code })
|
|
523
|
+
else resolveP({ ok: true, out: String(stdout ?? '').trim().slice(0, 400) })
|
|
524
|
+
})
|
|
525
|
+
})
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
export const command = {
|
|
529
|
+
kind: 'command',
|
|
530
|
+
async put(t, name, buf, { tmpDir } = {}) {
|
|
531
|
+
const cmd0 = String(t.cmd ?? '').trim()
|
|
532
|
+
if (!cmd0) throw new NetError('还没填命令。')
|
|
533
|
+
// 文件先落一个临时位置再交给命令:这样命令里只需要「读一个路径」,不用管字节怎么传。
|
|
534
|
+
const dir = tmpDir ?? join(process.env.TEMP || process.env.TMP || '.', 'dsh-fish-cloud')
|
|
535
|
+
mkdirSync(dir, { recursive: true })
|
|
536
|
+
const file = join(dir, name)
|
|
537
|
+
writeFileSync(file, buf)
|
|
538
|
+
const vars = { file, name, sha256: sha256(buf), size: buf.length }
|
|
539
|
+
const cmd = fillTpl(cmd0, vars)
|
|
540
|
+
const r = await runCmd(cmd, shellFor(t))
|
|
541
|
+
try { unlinkSync(file) } catch {}
|
|
542
|
+
if (!r.ok) throw new NetError(`命令没成功:${r.why}`)
|
|
543
|
+
return { name, bytes: buf.length, note: r.out ? `命令输出:${r.out.slice(0, 120)}` : '' }
|
|
544
|
+
},
|
|
545
|
+
async list() {
|
|
546
|
+
return []
|
|
547
|
+
},
|
|
548
|
+
async get() {
|
|
549
|
+
throw new NetError('命令驱动只负责往外送。要拿回来请写一条下载命令,或者用那边的客户端。')
|
|
550
|
+
},
|
|
551
|
+
async remove() {
|
|
552
|
+
return { ok: false, why: '命令驱动没有统一的删除约定,清理要你在那边做。' }
|
|
553
|
+
},
|
|
554
|
+
async probe(t) {
|
|
555
|
+
const cmd0 = String(t.cmd ?? '').trim()
|
|
556
|
+
if (!cmd0) throw new NetError('还没填命令。')
|
|
557
|
+
// 探测不跑真命令(可能很贵、可能有副作用):只检查这台机器上那个程序在不在。
|
|
558
|
+
const first = cmd0.split(/\s+/)[0].replace(/^["']|["']$/g, '')
|
|
559
|
+
const shell = shellFor(t)
|
|
560
|
+
const which = shell === 'cmd' ? `where "${first}"` : `command -v "${first}"`
|
|
561
|
+
const r = await runCmd(which, shell, 10_000)
|
|
562
|
+
if (!r.ok) {
|
|
563
|
+
return {
|
|
564
|
+
ok: false,
|
|
565
|
+
why: `这台机器上找不到「${first}」。装好它,或者把命令改成绝对路径。`,
|
|
566
|
+
where: first,
|
|
567
|
+
note: '只查了「这个程序在不在」,命令本身没跑。',
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
// 用户 m03310:通了也要说清测到了什么、测不到什么。
|
|
571
|
+
return {
|
|
572
|
+
ok: true,
|
|
573
|
+
where: r.out || first,
|
|
574
|
+
note: `在这台机器上找到了「${first}」(${r.out || '路径没回显'}),所以命令的第一段不会因为「找不到程序」直接失败。`
|
|
575
|
+
+ '测不到的是「这条命令跑起来会不会真的成功」——探测刻意不跑它(可能很慢、可能删东西),'
|
|
576
|
+
+ '要验到底就点「现在就送一份」,那时命令会真的执行一次,输出会写在下面的状态里。',
|
|
577
|
+
}
|
|
578
|
+
},
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export const DRIVERS = { folder, git, webdav, smtp, http, command }
|
|
582
|
+
|
|
583
|
+
export function driver(kind) {
|
|
584
|
+
return DRIVERS[String(kind ?? '')] ?? null
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
export { sha256, sizeText, NetError }
|