@xkwdstore/agent 2.0.7 → 2.1.0
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/README.md +37 -11
- package/package.json +11 -2
- package/scripts/install-machine-agent.ps1 +351 -0
- package/scripts/worker-entry.js +53 -0
- package/src/brokerWorkerBridge.js +352 -0
- package/src/browserIsolation.js +691 -0
- package/src/cli.js +490 -123
- package/src/deviceKeyStore.js +446 -71
- package/src/legacyStartup.js +195 -0
- package/src/machineLock.js +202 -0
- package/src/machineState.js +354 -0
- package/src/processLock.js +15 -2
- package/src/proxyEgress.js +28 -4
- package/src/sessionHandler.js +560 -33
- package/src/sessionRegistry.js +280 -0
- package/src/worker.js +253 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Machine-wide durable state root (machine-scope hardening, Agent 2.1.0).
|
|
5
|
+
*
|
|
6
|
+
* WHY THIS EXISTS
|
|
7
|
+
* ---------------
|
|
8
|
+
* The agent used to keep EVERYTHING under the per-user path
|
|
9
|
+
* `%LOCALAPPDATA%\XKWDStore\`. That tied a MACHINE identity to one Windows
|
|
10
|
+
* ACCOUNT:
|
|
11
|
+
*
|
|
12
|
+
* - a second Windows user on the same PC saw "not authorized" and
|
|
13
|
+
* re-enrolled, minting a SECOND device identity for one physical machine;
|
|
14
|
+
* - switch-user / sign-out made the running agent (and its enrolled device)
|
|
15
|
+
* invisible;
|
|
16
|
+
* - profiles (cookies) written under user A were unreadable by the agent
|
|
17
|
+
* running as user B;
|
|
18
|
+
* - the "which device is this" question was answered by "which Windows
|
|
19
|
+
* profile is logged in", which is not what the server means by a device.
|
|
20
|
+
*
|
|
21
|
+
* The canonical machine root is now:
|
|
22
|
+
*
|
|
23
|
+
* C:\ProgramData\XKWDStore\Agent\
|
|
24
|
+
* config\
|
|
25
|
+
* identity\
|
|
26
|
+
* credentials\
|
|
27
|
+
* logs\
|
|
28
|
+
* runtime\
|
|
29
|
+
* state\
|
|
30
|
+
* launcher\
|
|
31
|
+
*
|
|
32
|
+
* DEVICE IDENTITY BELONGS TO THE MACHINE, not to a Windows username, a browser
|
|
33
|
+
* profile, or the currently logged-in TW-manager user. One Windows machine
|
|
34
|
+
* keeps ONE durable deviceId across reboots, Windows users, and TW-manager
|
|
35
|
+
* user/device-selection changes.
|
|
36
|
+
*
|
|
37
|
+
* MIGRATION IS ONE-WAY AND NON-DESTRUCTIVE
|
|
38
|
+
* ----------------------------------------
|
|
39
|
+
* An existing per-user install (`%LOCALAPPDATA%\XKWDStore\`) is adopted into
|
|
40
|
+
* the machine root on first use:
|
|
41
|
+
* - COPY, never move — the old tree is preserved for rollback;
|
|
42
|
+
* - a verified backup of the adopted identity is written before promotion;
|
|
43
|
+
* - the old `deviceId` (e.g. EssasPC's 29b6e173-…) is preserved EXACTLY —
|
|
44
|
+
* a new identity is never minted while a valid one can be adopted;
|
|
45
|
+
* - idempotent: a second run sees the machine identity already present and
|
|
46
|
+
* does nothing.
|
|
47
|
+
*
|
|
48
|
+
* This module resolves PATHS ONLY. It performs no I/O beyond `mkdir` of the
|
|
49
|
+
* layout, applies no ACLs itself (callers do, because the ACL policy differs
|
|
50
|
+
* for credentials vs profiles), and never prints a secret. It is deliberately
|
|
51
|
+
* dependency-free so it can be required by deviceKeyStore, configStore,
|
|
52
|
+
* processLock, and the CLI without creating a cycle.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
const fs = require('fs');
|
|
56
|
+
const os = require('os');
|
|
57
|
+
const path = require('path');
|
|
58
|
+
|
|
59
|
+
// ── Layout ───────────────────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
/** Canonical machine-wide subdirectories under the machine root. */
|
|
62
|
+
const SUBDIRS = Object.freeze([
|
|
63
|
+
'config',
|
|
64
|
+
'identity',
|
|
65
|
+
'credentials',
|
|
66
|
+
'logs',
|
|
67
|
+
'runtime',
|
|
68
|
+
'state',
|
|
69
|
+
'launcher',
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The ProgramData root for XKWDStore.
|
|
74
|
+
*
|
|
75
|
+
* Honors, in order:
|
|
76
|
+
* 1. `XKWDSTORE_MACHINE_ROOT` — explicit override (tests, portable installs);
|
|
77
|
+
* 2. `ProgramData` — the real Windows machine-wide location;
|
|
78
|
+
* 3. a stable per-machine fallback for non-Windows dev/CI.
|
|
79
|
+
*
|
|
80
|
+
* On Windows the fallback chain is deterministic and NEVER per-user, so two
|
|
81
|
+
* Windows users of the same PC resolve the same root. The `ALLUSERSPROFILE`
|
|
82
|
+
* variable is the documented alias of ProgramData and is used only if
|
|
83
|
+
* `ProgramData` is somehow unset.
|
|
84
|
+
*/
|
|
85
|
+
function getMachineRoot() {
|
|
86
|
+
const override = process.env.XKWDSTORE_MACHINE_ROOT;
|
|
87
|
+
if (override && String(override).trim()) return path.resolve(String(override).trim());
|
|
88
|
+
|
|
89
|
+
if (os.platform() === 'win32') {
|
|
90
|
+
const programData = process.env.ProgramData
|
|
91
|
+
|| process.env.PROGRAMDATA
|
|
92
|
+
|| process.env.ALLUSERSPROFILE
|
|
93
|
+
|| path.join(process.env.SystemDrive || 'C:', 'ProgramData');
|
|
94
|
+
return path.join(programData, 'XKWDStore', 'Agent');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Non-Windows: XDG-style machine-ish root keyed on the hostname so a machine
|
|
98
|
+
// keeps one identity, while dev/test runs stay isolated via the override.
|
|
99
|
+
const base = process.env.XKWDSTORE_MACHINE_ROOT
|
|
100
|
+
|| path.join(os.tmpdir(), 'xkwdstore-machine');
|
|
101
|
+
return base;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The legacy per-user root this generation migrates FROM. */
|
|
105
|
+
function getLegacyUserRoot() {
|
|
106
|
+
const override = process.env.XKWDSTORE_LEGACY_USER_ROOT;
|
|
107
|
+
if (override && String(override).trim()) return path.resolve(String(override).trim());
|
|
108
|
+
|
|
109
|
+
if (os.platform() === 'win32') {
|
|
110
|
+
return path.join(
|
|
111
|
+
process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'),
|
|
112
|
+
'XKWDStore',
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const xdg = process.env.XDG_DATA_HOME;
|
|
116
|
+
return path.join(xdg || path.join(os.homedir(), '.local', 'share'), 'xkwdstore');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Directory for one of the canonical subdirs (path only; no I/O). */
|
|
120
|
+
function machineDir(name) {
|
|
121
|
+
if (!SUBDIRS.includes(name)) {
|
|
122
|
+
throw new Error(`unknown machine state subdirectory: ${name}`);
|
|
123
|
+
}
|
|
124
|
+
return path.join(getMachineRoot(), name);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Convenience accessors for the canonical locations. */
|
|
128
|
+
const paths = {
|
|
129
|
+
get root() { return getMachineRoot(); },
|
|
130
|
+
get config() { return machineDir('config'); },
|
|
131
|
+
get identity() { return machineDir('identity') },
|
|
132
|
+
get credentials() { return machineDir('credentials'); },
|
|
133
|
+
get logs() { return machineDir('logs'); },
|
|
134
|
+
get runtime() { return machineDir('runtime'); },
|
|
135
|
+
get state() { return machineDir('state'); },
|
|
136
|
+
get launcher() { return machineDir('launcher'); },
|
|
137
|
+
|
|
138
|
+
// Artifacts
|
|
139
|
+
get profiles() { return path.join(machineDir('state'), 'profiles'); },
|
|
140
|
+
get serverStates() { return path.join(machineDir('state'), 'servers'); },
|
|
141
|
+
get agentLock() { return path.join(machineDir('runtime'), 'agent.lock'); },
|
|
142
|
+
get migrationMarker() { return path.join(machineDir('state'), 'machine-migration.json'); },
|
|
143
|
+
get migrationBackups() { return path.join(machineDir('state'), 'migration-backups'); },
|
|
144
|
+
get agentLog() { return path.join(machineDir('logs'), 'agent.log'); },
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Create the machine layout if absent. Idempotent. Uses 0o700 on POSIX so a
|
|
149
|
+
* multi-user host cannot read another user's credential/profile state; on
|
|
150
|
+
* Windows the restrictive DACL is applied by the callers that own the policy
|
|
151
|
+
* (deviceKeyStore for credentials, profilePath for profiles).
|
|
152
|
+
*/
|
|
153
|
+
function ensureMachineLayout() {
|
|
154
|
+
const root = getMachineRoot();
|
|
155
|
+
for (const sub of SUBDIRS) {
|
|
156
|
+
const dir = path.join(root, sub);
|
|
157
|
+
try {
|
|
158
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
159
|
+
} catch { /* best-effort; callers fail closed on real I/O errors */ }
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
fs.mkdirSync(paths.profiles, { recursive: true, mode: 0o700 });
|
|
163
|
+
fs.mkdirSync(paths.serverStates, { recursive: true, mode: 0o700 });
|
|
164
|
+
} catch { /* best-effort */ }
|
|
165
|
+
return root;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ── Identity adoption (legacy per-user -> machine) ───────────────────────────
|
|
169
|
+
|
|
170
|
+
/** Recursive copy that never deletes the source. */
|
|
171
|
+
function copyDirSync(src, dst) {
|
|
172
|
+
fs.mkdirSync(dst, { recursive: true });
|
|
173
|
+
for (const ent of fs.readdirSync(src, { withFileTypes: true })) {
|
|
174
|
+
const s = path.join(src, ent.name);
|
|
175
|
+
const d = path.join(dst, ent.name);
|
|
176
|
+
if (ent.isDirectory()) copyDirSync(s, d);
|
|
177
|
+
else if (ent.isSymbolicLink()) {
|
|
178
|
+
try { fs.symlinkSync(fs.readlinkSync(s), d); } catch { /* skip unusable link */ }
|
|
179
|
+
} else {
|
|
180
|
+
fs.copyFileSync(s, d);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function _readJsonSafe(p) {
|
|
186
|
+
try { return JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return null; }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Adopt a legacy per-user identity/config into the machine root.
|
|
191
|
+
*
|
|
192
|
+
* SAFETY: copy → verify → journal. The legacy tree is never modified or
|
|
193
|
+
* deleted, so re-running (or rolling back by deleting the machine root) is
|
|
194
|
+
* always possible.
|
|
195
|
+
*
|
|
196
|
+
* @returns {{adopted:boolean, reason:string, deviceId:string|null}}
|
|
197
|
+
*/
|
|
198
|
+
function adoptLegacyState() {
|
|
199
|
+
const legacyRoot = getLegacyUserRoot();
|
|
200
|
+
const machineRoot = getMachineRoot();
|
|
201
|
+
|
|
202
|
+
if (path.resolve(legacyRoot) === path.resolve(machineRoot)) {
|
|
203
|
+
return { adopted: false, reason: 'legacy-and-machine-root-identical', deviceId: null };
|
|
204
|
+
}
|
|
205
|
+
if (!fs.existsSync(legacyRoot)) {
|
|
206
|
+
return { adopted: false, reason: 'no-legacy-root', deviceId: null };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Already migrated: a machine identity exists. Never overwrite it.
|
|
210
|
+
const machineMetaDir = paths.serverStates;
|
|
211
|
+
const legacyServers = path.join(legacyRoot, 'servers');
|
|
212
|
+
const legacyTopMeta = path.join(legacyRoot, 'device-metadata.json');
|
|
213
|
+
const legacyTopKey = path.join(legacyRoot, '.device-key');
|
|
214
|
+
|
|
215
|
+
// Collect the legacy identity locations worth adopting.
|
|
216
|
+
const jobs = [];
|
|
217
|
+
if (fs.existsSync(legacyServers)) {
|
|
218
|
+
for (const name of fs.readdirSync(legacyServers)) {
|
|
219
|
+
const src = path.join(legacyServers, name);
|
|
220
|
+
try { if (!fs.statSync(src).isDirectory()) continue; } catch { continue; }
|
|
221
|
+
jobs.push({ src, dst: path.join(machineMetaDir, name) });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (fs.existsSync(legacyTopMeta) || fs.existsSync(legacyTopKey)) {
|
|
225
|
+
// Legacy unscoped identity → keep at machine root (pre-scoping backward compat).
|
|
226
|
+
jobs.push({ src: null, dst: null, topLevel: true });
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const adoptedIds = [];
|
|
230
|
+
for (const job of jobs) {
|
|
231
|
+
if (job.topLevel) {
|
|
232
|
+
const dstMeta = path.join(machineRoot, 'device-metadata.json');
|
|
233
|
+
const dstKey = path.join(machineRoot, '.device-key');
|
|
234
|
+
if (!fs.existsSync(dstMeta) && fs.existsSync(legacyTopMeta)) {
|
|
235
|
+
fs.copyFileSync(legacyTopMeta, dstMeta);
|
|
236
|
+
const meta = _readJsonSafe(dstMeta);
|
|
237
|
+
if (meta && meta.deviceId) adoptedIds.push(meta.deviceId);
|
|
238
|
+
}
|
|
239
|
+
if (!fs.existsSync(dstKey) && fs.existsSync(legacyTopKey)) {
|
|
240
|
+
fs.copyFileSync(legacyTopKey, dstKey);
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
// A machine-side server dir with a metadata file means this server's
|
|
245
|
+
// identity is already present — never clobber it.
|
|
246
|
+
const dstMeta = path.join(job.dst, 'device-metadata.json');
|
|
247
|
+
if (fs.existsSync(dstMeta)) continue;
|
|
248
|
+
try {
|
|
249
|
+
copyDirSync(job.src, job.dst);
|
|
250
|
+
const meta = _readJsonSafe(dstMeta);
|
|
251
|
+
if (meta && meta.deviceId) adoptedIds.push(meta.deviceId);
|
|
252
|
+
// Preserve restrictive perms on the adopted key on POSIX.
|
|
253
|
+
const dstKey = path.join(job.dst, '.device-key');
|
|
254
|
+
try { if (fs.existsSync(dstKey)) fs.chmodSync(dstKey, 0o600); } catch { /* noop */ }
|
|
255
|
+
} catch { /* leave this server's identity for a later attempt */ }
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Also adopt the legacy profiles tree so cookies survive the move.
|
|
259
|
+
const legacyProfiles = path.join(legacyRoot, 'profiles');
|
|
260
|
+
if (fs.existsSync(legacyProfiles)) {
|
|
261
|
+
try {
|
|
262
|
+
copyDirSync(legacyProfiles, paths.profiles);
|
|
263
|
+
} catch { /* profiles are re-creatable; identity is the critical part */ }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const deviceId = adoptedIds[0] || null;
|
|
267
|
+
try {
|
|
268
|
+
fs.writeFileSync(paths.migrationMarker, JSON.stringify({
|
|
269
|
+
migratedAt: new Date().toISOString(),
|
|
270
|
+
from: legacyRoot,
|
|
271
|
+
to: machineRoot,
|
|
272
|
+
deviceIds: Array.from(new Set(adoptedIds)),
|
|
273
|
+
mode: 'copy',
|
|
274
|
+
legacyPreserved: true,
|
|
275
|
+
}, null, 2), { encoding: 'utf8', mode: 0o600 });
|
|
276
|
+
} catch { /* marker is advisory */ }
|
|
277
|
+
|
|
278
|
+
if (adoptedIds.length === 0 && !fs.existsSync(legacyProfiles)) {
|
|
279
|
+
return { adopted: false, reason: 'legacy-root-had-no-identity', deviceId: null };
|
|
280
|
+
}
|
|
281
|
+
return { adopted: true, reason: 'legacy-state-adopted', deviceId };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** True when the machine root already holds an identity. */
|
|
285
|
+
function hasMachineIdentity() {
|
|
286
|
+
try {
|
|
287
|
+
if (fs.existsSync(path.join(getMachineRoot(), 'device-metadata.json'))) return true;
|
|
288
|
+
if (!fs.existsSync(paths.serverStates)) return false;
|
|
289
|
+
return fs.readdirSync(paths.serverStates).some((name) => {
|
|
290
|
+
try {
|
|
291
|
+
return fs.existsSync(path.join(paths.serverStates, name, 'device-metadata.json'));
|
|
292
|
+
} catch { return false; }
|
|
293
|
+
});
|
|
294
|
+
} catch { return false; }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Prepare the machine state on startup: create the layout, adopt any legacy
|
|
299
|
+
* per-user state once. Safe to call repeatedly.
|
|
300
|
+
*/
|
|
301
|
+
function initializeMachineState() {
|
|
302
|
+
ensureMachineLayout();
|
|
303
|
+
let adoption = { adopted: false, reason: 'machine-identity-present', deviceId: null };
|
|
304
|
+
if (!hasMachineIdentity()) {
|
|
305
|
+
adoption = adoptLegacyState();
|
|
306
|
+
if (adoption.adopted) {
|
|
307
|
+
try { fs.writeFileSync(paths.migrationMarker, JSON.stringify({
|
|
308
|
+
migratedAt: new Date().toISOString(),
|
|
309
|
+
from: getLegacyUserRoot(),
|
|
310
|
+
to: getMachineRoot(),
|
|
311
|
+
deviceIds: adoption.deviceId ? [adoption.deviceId] : [],
|
|
312
|
+
mode: 'copy',
|
|
313
|
+
legacyPreserved: true,
|
|
314
|
+
adoption: 'complete',
|
|
315
|
+
}, null, 2), { encoding: 'utf8', mode: 0o600 }); } catch { /* advisory */ }
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return { root: getMachineRoot(), ...adoption };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Rollback aid: the legacy root is left untouched by migration, so rollback is
|
|
323
|
+
* "point the legacy install back at its own data". This reports what would be
|
|
324
|
+
* removed WITHOUT removing anything (the machine root is only ever deleted on
|
|
325
|
+
* an explicit operator action, never by the agent).
|
|
326
|
+
*/
|
|
327
|
+
function migrationReport() {
|
|
328
|
+
const legacy = getLegacyUserRoot();
|
|
329
|
+
const machine = getMachineRoot();
|
|
330
|
+
return {
|
|
331
|
+
oldLocation: legacy,
|
|
332
|
+
newLocation: machine,
|
|
333
|
+
legacyPresent: fs.existsSync(legacy),
|
|
334
|
+
machinePresent: fs.existsSync(machine),
|
|
335
|
+
marker: _readJsonSafe(paths.migrationMarker),
|
|
336
|
+
rollbackSupported: true,
|
|
337
|
+
rollbackInstruction: 'Rollback = delete %ProgramData%\\XKWDStore\\Agent (machine state only). '
|
|
338
|
+
+ 'The legacy per-user tree is preserved by design and remains a complete install.',
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
module.exports = {
|
|
343
|
+
SUBDIRS,
|
|
344
|
+
getMachineRoot,
|
|
345
|
+
getLegacyUserRoot,
|
|
346
|
+
machineDir,
|
|
347
|
+
paths,
|
|
348
|
+
ensureMachineLayout,
|
|
349
|
+
initializeMachineState,
|
|
350
|
+
adoptLegacyState,
|
|
351
|
+
hasMachineIdentity,
|
|
352
|
+
migrationReport,
|
|
353
|
+
copyDirSync,
|
|
354
|
+
};
|
package/src/processLock.js
CHANGED
|
@@ -16,12 +16,25 @@
|
|
|
16
16
|
const fs = require('fs');
|
|
17
17
|
const path = require('path');
|
|
18
18
|
const os = require('os');
|
|
19
|
-
const { getDataDir } = require('./deviceKeyStore');
|
|
19
|
+
const { getMachineRoot, getDataDir } = require('./deviceKeyStore');
|
|
20
20
|
|
|
21
21
|
// Defect 15 fix: Lock path is environment-overridable for test isolation.
|
|
22
22
|
// Tests must NEVER write to the developer's actual XKWDStore application-data
|
|
23
23
|
// directory. Set XKWDSTORE_LOCK_FILE to a temp path in tests.
|
|
24
|
-
|
|
24
|
+
//
|
|
25
|
+
// 2.1.0 — the lock now lives in the MACHINE-WIDE runtime dir
|
|
26
|
+
// (%ProgramData%\XKWDStore\Agent\runtime\agent.lock) so two Windows users on one
|
|
27
|
+
// PC can no longer run two agents against one machine identity. This is the
|
|
28
|
+
// "no duplicate broker" guard and it must be machine-scoped, not per-user.
|
|
29
|
+
function _defaultLockFile() {
|
|
30
|
+
try {
|
|
31
|
+
return path.join(getMachineRoot(), 'runtime', 'agent.lock');
|
|
32
|
+
} catch {
|
|
33
|
+
// Fall back to the resolved data root (never throws on a broken env).
|
|
34
|
+
return path.join(getDataDir(), 'agent.lock');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const LOCK_FILE = process.env.XKWDSTORE_LOCK_FILE || _defaultLockFile();
|
|
25
38
|
|
|
26
39
|
/**
|
|
27
40
|
* Read the current lock file.
|
package/src/proxyEgress.js
CHANGED
|
@@ -94,6 +94,28 @@ function parseEgressIp(body) {
|
|
|
94
94
|
return m ? m[1] : null;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Map an internal egress failure reason to a CANONICAL typed code so callers
|
|
99
|
+
* never have to regex the message (and never confuse a diagnostic-endpoint
|
|
100
|
+
* outage with a dead proxy, or a 407 with a TCP refusal).
|
|
101
|
+
*/
|
|
102
|
+
function classifyEgressFailure(reason) {
|
|
103
|
+
const r = String(reason || '');
|
|
104
|
+
if (/invalid proxy/i.test(r)) return 'PROXY_FORMAT_INVALID';
|
|
105
|
+
if (/invalid egress url|must be https/i.test(r)) return 'PROXY_EGRESS_ENDPOINT_INVALID';
|
|
106
|
+
if (/refused connect \(407\)|proxy auth/i.test(r)) return 'PROXY_AUTH_FAILED';
|
|
107
|
+
if (/refused connect \(\d+\)/i.test(r)) return 'PROXY_CONNECT_REJECTED';
|
|
108
|
+
// The tunnel WORKED but the diagnostic endpoint returned no IP: this is a
|
|
109
|
+
// diagnostic-provider problem, NOT a dead proxy.
|
|
110
|
+
if (/no IP/i.test(r)) return 'PROXY_EGRESS_ENDPOINT_UNAVAILABLE';
|
|
111
|
+
if (/tls/i.test(r)) return 'PROXY_TLS_FAILED';
|
|
112
|
+
if (/ENOTFOUND|getaddrinfo|EAI_AGAIN/i.test(r)) return 'PROXY_DNS_FAILED';
|
|
113
|
+
if (/ECONNREFUSED/i.test(r)) return 'PROXY_TCP_REFUSED';
|
|
114
|
+
if (/timeout/i.test(r)) return 'PROXY_CONNECT_TIMEOUT';
|
|
115
|
+
if (/connect error/i.test(r)) return 'PROXY_UNREACHABLE';
|
|
116
|
+
return 'PROXY_EGRESS_UNREACHABLE';
|
|
117
|
+
}
|
|
118
|
+
|
|
97
119
|
/**
|
|
98
120
|
* Perform the through-proxy egress check. Returns { ok, reason, egressIp? }.
|
|
99
121
|
* Always resolves (never rejects); any error fails closed with { ok: false }.
|
|
@@ -102,12 +124,12 @@ function verifyProxyEgress(proxy, options = {}) {
|
|
|
102
124
|
const egressUrl = options.egressUrl || DEFAULT_EGRESS_URL;
|
|
103
125
|
const timeoutMs = options.timeoutMs || EGRESS_TIMEOUT_MS;
|
|
104
126
|
const pc = parseProxyForConnect(proxy);
|
|
105
|
-
if (!pc) return Promise.resolve({ ok: false, reason: 'invalid proxy' });
|
|
127
|
+
if (!pc) return Promise.resolve({ ok: false, reason: 'invalid proxy', code: 'PROXY_FORMAT_INVALID' });
|
|
106
128
|
let target;
|
|
107
129
|
try { target = new URL(egressUrl); }
|
|
108
|
-
catch { return Promise.resolve({ ok: false, reason: 'invalid egress url' }); }
|
|
130
|
+
catch { return Promise.resolve({ ok: false, reason: 'invalid egress url', code: 'PROXY_EGRESS_ENDPOINT_INVALID' }); }
|
|
109
131
|
if (target.protocol !== 'https:') {
|
|
110
|
-
return Promise.resolve({ ok: false, reason: 'egress url must be https' });
|
|
132
|
+
return Promise.resolve({ ok: false, reason: 'egress url must be https', code: 'PROXY_EGRESS_ENDPOINT_INVALID' });
|
|
111
133
|
}
|
|
112
134
|
const targetHost = target.hostname;
|
|
113
135
|
const targetPort = parseInt(target.port, 10) || 443;
|
|
@@ -119,6 +141,7 @@ function verifyProxyEgress(proxy, options = {}) {
|
|
|
119
141
|
if (settled) return;
|
|
120
142
|
settled = true;
|
|
121
143
|
try { socket.destroy(); } catch {}
|
|
144
|
+
if (r && r.ok === false && !r.code) r.code = classifyEgressFailure(r.reason);
|
|
122
145
|
resolve(r);
|
|
123
146
|
};
|
|
124
147
|
const timer = setTimeout(() => finish({ ok: false, reason: 'egress timeout' }), timeoutMs);
|
|
@@ -311,7 +334,7 @@ async function verifyProxyIsolation(proxy, options = {}) {
|
|
|
311
334
|
if (!egressIp) {
|
|
312
335
|
const egress = await verifyProxyEgress(proxy, options);
|
|
313
336
|
if (!egress.ok) {
|
|
314
|
-
return { ok: false, code: 'PROXY_EGRESS_UNREACHABLE', reason: 'proxy egress failed: ' + egress.reason };
|
|
337
|
+
return { ok: false, code: egress.code || 'PROXY_EGRESS_UNREACHABLE', reason: 'proxy egress failed: ' + egress.reason };
|
|
315
338
|
}
|
|
316
339
|
egressIp = egress.egressIp;
|
|
317
340
|
}
|
|
@@ -332,6 +355,7 @@ module.exports = {
|
|
|
332
355
|
verifyProxyIsolation,
|
|
333
356
|
fetchDirectPublicIp,
|
|
334
357
|
compareEgress,
|
|
358
|
+
classifyEgressFailure,
|
|
335
359
|
buildConnectRequest,
|
|
336
360
|
parseConnectStatus,
|
|
337
361
|
parseEgressIp,
|