@dsh-mobile/mobile-remote 3.3.0 → 3.3.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/FAQ.md +2 -2
- package/LICENSE +0 -0
- package/README.md +6 -5
- package/docs/PRIVACY-POLICY.en.md +17 -16
- package/docs/PRIVACY-POLICY.md +17 -16
- package/docs/SUPPORT.md +1 -1
- package/docs/TERMS-OF-SERVICE.en.md +5 -3
- package/docs/TERMS-OF-SERVICE.md +5 -3
- package/docs/USER-GUIDE.zh-CN.md +6 -6
- package/lib/account-usage.js +0 -0
- package/lib/api-helpers.js +6 -2
- package/lib/assistant-prefs.js +57 -37
- package/lib/backup.js +56 -20
- package/lib/client.js +272 -74
- package/lib/config.js +6 -4
- package/lib/git-read-bundled-provider.js +21 -1
- package/lib/git-read-service.js +0 -0
- package/lib/index.js +206 -47
- package/lib/memory/context.js +5 -0
- package/lib/memory/store.js +55 -8
- package/lib/persona-cards.js +5 -1
- package/lib/proactive.js +4 -1
- package/lib/protected-paths.js +172 -12
- package/lib/protocol.js +4 -0
- package/lib/relay/connector.js +113 -4
- package/lib/relay/devices.js +42 -10
- package/lib/relay/on-demand.js +8 -3
- package/lib/relay/pairing.js +45 -6
- package/lib/relay/phone.js +5 -3
- package/lib/relay/ws-server.js +245 -0
- package/lib/session-title-cache.js +0 -0
- package/lib/session-title-refresh.js +0 -0
- package/package.json +6 -6
package/lib/assistant-prefs.js
CHANGED
|
@@ -37,64 +37,82 @@ function validWindow(start, end) {
|
|
|
37
37
|
*/
|
|
38
38
|
export function createAssistantPrefs({ dir, defaults }) {
|
|
39
39
|
const file = join(dir, "assistant.json");
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Only what the app (or the user) explicitly set (A-21): the effective value of anything else is
|
|
42
|
+
* worked out on every read from the config and the defaults, so a later config change still
|
|
43
|
+
* applies. Files written before 3.3.1 (no `v: 2`) stored the merged object, so in them a
|
|
44
|
+
* `breakReminder: true` may be a default that was copied in, not the app's switch (A-13): it is not
|
|
45
|
+
* taken as explicit. Everything else in an old file is kept as explicit, as before.
|
|
46
|
+
*/
|
|
47
|
+
let explicit = null;
|
|
41
48
|
|
|
42
|
-
function
|
|
43
|
-
if (
|
|
44
|
-
|
|
49
|
+
function readExplicit() {
|
|
50
|
+
if (explicit) return explicit;
|
|
51
|
+
let raw;
|
|
45
52
|
try {
|
|
46
|
-
|
|
53
|
+
raw = JSON.parse(readFileSync(file, "utf8"));
|
|
54
|
+
} catch {
|
|
55
|
+
raw = null;
|
|
56
|
+
}
|
|
57
|
+
const legacy = raw && raw.v !== 2;
|
|
58
|
+
const e = {};
|
|
59
|
+
if (raw && typeof raw === "object") {
|
|
60
|
+
if (MODES.includes(raw.mode)) e.mode = raw.mode;
|
|
61
|
+
if (PERSONAS.includes(raw.persona)) e.persona = raw.persona;
|
|
47
62
|
// 安静时段单独校验:文件里可能是旧版本写的、也可能是手改坏的。
|
|
48
63
|
// 只有两个端点都合法才采用,否则整体回落到配置值——半个窗口比没有更糟。
|
|
49
|
-
const quiet = raw
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
cached = {
|
|
55
|
-
mode: MODES.includes(raw?.mode) ? raw.mode : fallback.mode,
|
|
56
|
-
persona: PERSONAS.includes(raw?.persona) ? raw.persona : fallback.persona,
|
|
57
|
-
quietHours: quietOk
|
|
58
|
-
? { start: quietOk.start, end: quietOk.end }
|
|
59
|
-
: { start: "23:30", end: "08:30" },
|
|
60
|
-
// T-15: the user's own off switch for the break reminder (on unless they turned it off).
|
|
61
|
-
breakReminder: typeof raw?.breakReminder === "boolean" ? raw.breakReminder : true,
|
|
62
|
-
// T-31: the active persona card (an id). Absent = the old colleague/character switch decides.
|
|
63
|
-
...(PERSONA_CARD_ID.test(raw?.personaCard ?? "") ? { personaCard: raw.personaCard } : {}),
|
|
64
|
-
};
|
|
65
|
-
} catch {
|
|
66
|
-
// 文件不存在或损坏:回到配置默认值,不因为一个坏文件让助手不可用。
|
|
67
|
-
cached = {
|
|
68
|
-
mode: fallback.mode,
|
|
69
|
-
persona: fallback.persona,
|
|
70
|
-
quietHours: fallback.quietHours ?? { start: "23:30", end: "08:30" },
|
|
71
|
-
breakReminder: true,
|
|
72
|
-
};
|
|
64
|
+
const quiet = raw.quietHours;
|
|
65
|
+
if (quiet && validWindow(quiet.start, quiet.end)) e.quietHours = { start: quiet.start, end: quiet.end };
|
|
66
|
+
if (typeof raw.breakReminder === "boolean" && !(legacy && raw.breakReminder === true)) e.breakReminder = raw.breakReminder;
|
|
67
|
+
// T-31: the active persona card (an id). Absent = the old colleague/character switch decides.
|
|
68
|
+
if (PERSONA_CARD_ID.test(raw.personaCard ?? "")) e.personaCard = raw.personaCard;
|
|
73
69
|
}
|
|
74
|
-
|
|
70
|
+
explicit = e;
|
|
71
|
+
return explicit;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Explicit values over the config's defaults over the built-in defaults. */
|
|
75
|
+
function effective() {
|
|
76
|
+
const e = readExplicit();
|
|
77
|
+
const fallback = defaults();
|
|
78
|
+
return {
|
|
79
|
+
mode: e.mode ?? fallback.mode,
|
|
80
|
+
persona: e.persona ?? fallback.persona,
|
|
81
|
+
quietHours: { ...(e.quietHours ?? fallback.quietHours ?? { start: "23:30", end: "08:30" }) },
|
|
82
|
+
// T-15: the user's own off switch for the break reminder (on unless they turned it off).
|
|
83
|
+
breakReminder: e.breakReminder ?? true,
|
|
84
|
+
...(e.personaCard ? { personaCard: e.personaCard } : {}),
|
|
85
|
+
};
|
|
75
86
|
}
|
|
76
87
|
|
|
77
88
|
function save(next) {
|
|
78
89
|
mkdirSync(dir, { recursive: true });
|
|
79
90
|
// 原子写:先写临时文件再 rename,避免断电/崩溃留下半个 JSON。
|
|
80
91
|
const tmp = `${file}.tmp`;
|
|
81
|
-
writeFileSync(tmp, JSON.stringify(next, null, 2), "utf8");
|
|
92
|
+
writeFileSync(tmp, JSON.stringify({ v: 2, ...next }, null, 2), "utf8");
|
|
82
93
|
renameSync(tmp, file);
|
|
83
|
-
|
|
94
|
+
explicit = next;
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
return {
|
|
87
98
|
file,
|
|
88
99
|
/** 当前有效的模式与人格。 */
|
|
89
|
-
get: () => (
|
|
100
|
+
get: () => effective(),
|
|
101
|
+
/**
|
|
102
|
+
* Has the app ever sent its break-reminder switch (A-13)? Store apps up to 3.2.7 have no such
|
|
103
|
+
* switch, so for them the reminder stays off unless the PC config turns it on.
|
|
104
|
+
*/
|
|
105
|
+
breakReminderSetByApp: () => typeof readExplicit().breakReminder === "boolean",
|
|
90
106
|
|
|
91
107
|
/**
|
|
92
108
|
* 改一项或两项。非法值直接拒绝,不「猜一个最近的」:
|
|
93
109
|
* 猜错会让用户以为切换成功,实际还在原来的模式里。
|
|
110
|
+
* Returns the effective prefs and `changed`: whether any effective value differs (A-20: the
|
|
111
|
+
* app's automatic re-sync on every reconnect changes nothing and is not someone's activity).
|
|
94
112
|
*/
|
|
95
113
|
set(patch) {
|
|
96
|
-
const
|
|
97
|
-
const next = { ...
|
|
114
|
+
const before = JSON.stringify(effective());
|
|
115
|
+
const next = { ...readExplicit() };
|
|
98
116
|
if (patch?.mode !== undefined) {
|
|
99
117
|
if (!MODES.includes(patch.mode)) {
|
|
100
118
|
throw new Error(`mode 必须是 ${MODES.join(" / ")}`);
|
|
@@ -126,8 +144,10 @@ export function createAssistantPrefs({ dir, defaults }) {
|
|
|
126
144
|
if (typeof patch.breakReminder !== "boolean") throw new Error("breakReminder 必须是 true / false");
|
|
127
145
|
next.breakReminder = patch.breakReminder;
|
|
128
146
|
}
|
|
129
|
-
|
|
130
|
-
|
|
147
|
+
const explicitChanged = JSON.stringify(next) !== JSON.stringify(readExplicit());
|
|
148
|
+
if (explicitChanged) save(next);
|
|
149
|
+
const after = effective();
|
|
150
|
+
return { ...after, changed: JSON.stringify(after) !== before };
|
|
131
151
|
},
|
|
132
152
|
};
|
|
133
153
|
}
|
package/lib/backup.js
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* 恢复是**只增不删**的:已有条目按 (scope, text) 判重后跳过,绝不因为备份里
|
|
21
21
|
* 没有某条就删掉本机现有的。这样即使用户拿错了备份文件,也不会丢东西。
|
|
22
22
|
*/
|
|
23
|
-
import { createCipheriv, createDecipheriv, randomBytes,
|
|
23
|
+
import { createCipheriv, createDecipheriv, randomBytes, scrypt } from "node:crypto";
|
|
24
24
|
import { SCOPES } from "./memory/store.js";
|
|
25
25
|
import { parseHm } from "./notify-copy.js";
|
|
26
26
|
|
|
@@ -97,17 +97,53 @@ export function buildBackup({ store, prefs, now = () => Date.now() }) {
|
|
|
97
97
|
// scrypt 派生的密钥做 AES-256-GCM 加密,文件里只剩格式标记与密文:拿到文件但不知道
|
|
98
98
|
// 口令的人读不到任何一条记忆。口令只在请求里出现一次,插件不保存它。
|
|
99
99
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
100
|
+
/**
|
|
101
|
+
* 口令最短长度,按字符(code point)数,不按 UTF-16 单元(A-16)。A backup file can be attacked
|
|
102
|
+
* offline at full speed, so this follows NIST SP 800-63B-4 for single-factor passwords: at least
|
|
103
|
+
* 15 characters, plus a check against obviously guessable ones (`passphraseProblem`).
|
|
104
|
+
*/
|
|
105
|
+
export const MIN_BACKUP_PASSPHRASE = 15;
|
|
102
106
|
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
/** A few long passphrases that are on every list; the shape checks below catch most of the rest. */
|
|
108
|
+
const COMMON_PASSPHRASES = new Set([
|
|
109
|
+
"passwordpassword", "password1234567", "123456789012345", "1234567890123456", "qwertyuiopasdfg",
|
|
110
|
+
"qwertyuiopasdfgh", "1q2w3e4r5t6y7u8i", "iloveyouiloveyou", "abcdefghijklmno", "abcdefghijklmnop",
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
/** Why `passphrase` is too weak for an offline-attackable backup, or null if it is acceptable. */
|
|
114
|
+
export function passphraseProblem(passphrase) {
|
|
115
|
+
if (typeof passphrase !== "string") return `口令至少 ${MIN_BACKUP_PASSPHRASE} 个字符 / the passphrase needs at least ${MIN_BACKUP_PASSPHRASE} characters`;
|
|
116
|
+
const chars = [...passphrase.normalize("NFC")];
|
|
117
|
+
if (chars.length < MIN_BACKUP_PASSPHRASE) return `口令至少 ${MIN_BACKUP_PASSPHRASE} 个字符 / the passphrase needs at least ${MIN_BACKUP_PASSPHRASE} characters`;
|
|
118
|
+
const folded = chars.join("").toLowerCase();
|
|
119
|
+
if (new Set(chars.filter((c) => c.trim() !== "")).size < 5 || /^(.+?)\1+$/su.test(folded) || COMMON_PASSPHRASES.has(folded.replace(/\s+/g, ""))) {
|
|
120
|
+
return "这个口令太容易猜 / this passphrase is too easy to guess: use a few unrelated words";
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Written now: scrypt N=2^17, r=8, p=1 (OWASP's minimum for scrypt, A-16). 3.3.0 wrote N=2^15;
|
|
127
|
+
* those backups still open. No other parameter set is accepted from a file (T-63: a file must not
|
|
128
|
+
* be able to make the plugin spend unbounded time or memory).
|
|
129
|
+
*/
|
|
130
|
+
const KDF = { name: "scrypt", N: 1 << 17, r: 8, p: 1, keyLen: 32 };
|
|
131
|
+
const ACCEPTED_KDF = [KDF, { name: "scrypt", N: 1 << 15, r: 8, p: 1, keyLen: 32 }];
|
|
132
|
+
export const BACKUP_KDF = Object.freeze({ ...KDF });
|
|
133
|
+
|
|
134
|
+
/** One scrypt at a time (each takes ~128 MiB and a few hundred ms), off the event loop. */
|
|
135
|
+
let kdfQueue = Promise.resolve();
|
|
136
|
+
const deriveKey = (passphrase, salt, kdf = KDF) => {
|
|
137
|
+
const run = () =>
|
|
138
|
+
new Promise((resolve, reject) =>
|
|
139
|
+
scrypt(String(passphrase).normalize("NFC"), salt, kdf.keyLen ?? 32, { N: kdf.N, r: kdf.r, p: kdf.p, maxmem: 256 * kdf.N * kdf.r + 1024 * 1024 }, (err, key) =>
|
|
140
|
+
err ? reject(err) : resolve(key),
|
|
141
|
+
),
|
|
142
|
+
);
|
|
143
|
+
const next = kdfQueue.then(run, run);
|
|
144
|
+
kdfQueue = next.catch(() => {});
|
|
145
|
+
return next;
|
|
146
|
+
};
|
|
111
147
|
|
|
112
148
|
/** 备份文件是不是加了口令的。 */
|
|
113
149
|
export function isEncryptedBackup(payload) {
|
|
@@ -117,15 +153,15 @@ export function isEncryptedBackup(payload) {
|
|
|
117
153
|
/**
|
|
118
154
|
* 给备份加口令。返回 `{ format, version, encrypted: {...} }`,可直接存成文件。
|
|
119
155
|
* @param {object} backup buildBackup 的结果
|
|
120
|
-
* @param {string} passphrase 至少 MIN_BACKUP_PASSPHRASE
|
|
156
|
+
* @param {string} passphrase 至少 MIN_BACKUP_PASSPHRASE 个字符,且不是 passphraseProblem 认为好猜的
|
|
157
|
+
* @returns {Promise<object>}
|
|
121
158
|
*/
|
|
122
|
-
export function encryptBackup(backup, passphrase) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
159
|
+
export async function encryptBackup(backup, passphrase) {
|
|
160
|
+
const problem = passphraseProblem(passphrase);
|
|
161
|
+
if (problem) throw Object.assign(new Error(problem), { code: "weak-passphrase" });
|
|
126
162
|
const salt = randomBytes(16);
|
|
127
163
|
const iv = randomBytes(12);
|
|
128
|
-
const key = deriveKey(passphrase, salt);
|
|
164
|
+
const key = await deriveKey(passphrase, salt);
|
|
129
165
|
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
130
166
|
// 格式标记作为附加认证数据:密文被挪到别的格式里会解不开。
|
|
131
167
|
cipher.setAAD(Buffer.from(`${BACKUP_FORMAT}/${BACKUP_VERSION}`));
|
|
@@ -147,7 +183,7 @@ export function encryptBackup(backup, passphrase) {
|
|
|
147
183
|
* 解开加了口令的备份。口令不对、文件被改过、参数不认识都抛错(`code` 区分)。
|
|
148
184
|
* @returns {object} 原始备份对象(再交给 planRestore / applyRestore)
|
|
149
185
|
*/
|
|
150
|
-
export function decryptBackup(envelope, passphrase) {
|
|
186
|
+
export async function decryptBackup(envelope, passphrase) {
|
|
151
187
|
const e = envelope?.encrypted;
|
|
152
188
|
if (!e || e.cipher !== "aes-256-gcm" || e.kdf?.name !== "scrypt") {
|
|
153
189
|
throw Object.assign(new Error("不认识的加密格式 / unknown encryption format"), { code: "bad-backup" });
|
|
@@ -160,7 +196,7 @@ export function decryptBackup(envelope, passphrase) {
|
|
|
160
196
|
const N = Number(e.kdf.N);
|
|
161
197
|
const r = Number(e.kdf.r);
|
|
162
198
|
const p = Number(e.kdf.p);
|
|
163
|
-
if (
|
|
199
|
+
if (!ACCEPTED_KDF.some((k) => k.N === N && k.r === r && k.p === p)) {
|
|
164
200
|
throw Object.assign(new Error("加密参数不是本插件写的 / encryption parameters not written by this plugin"), { code: "bad-backup" });
|
|
165
201
|
}
|
|
166
202
|
const tag = Buffer.from(String(e.tag), "base64");
|
|
@@ -168,7 +204,7 @@ export function decryptBackup(envelope, passphrase) {
|
|
|
168
204
|
throw Object.assign(new Error("口令不对,或文件已损坏 / wrong passphrase, or the file is damaged"), { code: "bad-passphrase" });
|
|
169
205
|
}
|
|
170
206
|
try {
|
|
171
|
-
const key = deriveKey(passphrase, Buffer.from(String(e.kdf.salt), "base64"), { N, r, p, keyLen: 32 });
|
|
207
|
+
const key = await deriveKey(passphrase, Buffer.from(String(e.kdf.salt), "base64"), { N, r, p, keyLen: 32 });
|
|
172
208
|
// authTagLength: a truncated tag is refused instead of checked on fewer bits (DEP0182).
|
|
173
209
|
const decipher = createDecipheriv("aes-256-gcm", key, Buffer.from(String(e.iv), "base64"), { authTagLength: 16 });
|
|
174
210
|
decipher.setAAD(Buffer.from(`${envelope.format}/${envelope.version}`));
|