@xkwdstore/agent 2.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/README.md +245 -0
- package/package.json +55 -0
- package/src/actions/comment.js +138 -0
- package/src/actions/dbc.js +150 -0
- package/src/actions/follow.js +83 -0
- package/src/actions/helpers.js +754 -0
- package/src/actions/index.js +95 -0
- package/src/actions/like.js +245 -0
- package/src/actions/session.js +1611 -0
- package/src/actions/tweet.js +91 -0
- package/src/challengeCodec.js +32 -0
- package/src/chromeDetector.js +155 -0
- package/src/cli.js +529 -0
- package/src/configStore.js +98 -0
- package/src/deviceKeyStore.js +700 -0
- package/src/enrollmentClient.js +446 -0
- package/src/fingerprint.js +262 -0
- package/src/fingerprintStore.js +126 -0
- package/src/leaseClient.js +85 -0
- package/src/processLock.js +149 -0
- package/src/profilePath.js +1117 -0
- package/src/proxyEgress.js +339 -0
- package/src/proxyPolicy.js +77 -0
- package/src/secretStore.js +133 -0
- package/src/sessionHandler.js +453 -0
- package/src/stealth.js +1251 -0
- package/src/wsClient.js +234 -0
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Phase 10 — Browser session handler (protocol v2).
|
|
5
|
+
*
|
|
6
|
+
* Defect 8 fix: Handle the existing hardened command envelope:
|
|
7
|
+
* { type: 'command', commandId, accountId, command: { type, ...rest, lease? },
|
|
8
|
+
* nonce?, leaseId?, accountUuid? }
|
|
9
|
+
*
|
|
10
|
+
* Defect 9 fix: Reuse the existing hardened browser runtime from index.js
|
|
11
|
+
* (actions, proxy fail-closed, media blocking, profile identity, lease client,
|
|
12
|
+
* cookie handling, etc.) rather than maintaining a second divergent runtime.
|
|
13
|
+
*
|
|
14
|
+
* The agent validates the complete inbound frame, reads the action type from
|
|
15
|
+
* the canonical command object, preserves command and lease expiry, echoes
|
|
16
|
+
* commandId/nonce/leaseId/accountUuid, returns results under the canonical
|
|
17
|
+
* `result` field, and rejects unknown command types.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const { chromium } = require('playwright-core');
|
|
21
|
+
|
|
22
|
+
// Import the existing hardened runtime modules (Defect 9).
|
|
23
|
+
const { generateFingerprint } = require('./fingerprint');
|
|
24
|
+
const fingerprintStore = require('./fingerprintStore');
|
|
25
|
+
const { resolveProfileDir, importLegacyProfile, discoverLegacyProfiles } = require('./profilePath');
|
|
26
|
+
const { buildStealthScript } = require('./stealth');
|
|
27
|
+
const leaseClient = require('./leaseClient');
|
|
28
|
+
const { PROFILES_DIR } = require('./deviceKeyStore');
|
|
29
|
+
|
|
30
|
+
// Active sessions: accountId -> { context, page, profileDir }
|
|
31
|
+
const activeSessions = new Map();
|
|
32
|
+
|
|
33
|
+
// Per-runtime state (B4 fix): each setupSessionHandler call creates a fresh
|
|
34
|
+
// runtime object so a newly initialized handler starts connected and does
|
|
35
|
+
// NOT inherit stale state from a prior handler lifecycle. The module-level
|
|
36
|
+
// `currentRuntime` is only used so markDisconnected() / closeAllSessions()
|
|
37
|
+
// (called from cli.js) can reach the active runtime without an explicit
|
|
38
|
+
// handle. One handler disconnecting cannot poison another handler's runtime.
|
|
39
|
+
const currentRuntime = { disconnected: false, sessions: activeSessions };
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Mark the CURRENT runtime as disconnected. After this, every inbound command
|
|
43
|
+
* for that runtime is rejected with a clear error and no new browser work is
|
|
44
|
+
* performed. A new setupSessionHandler call creates a fresh runtime that
|
|
45
|
+
* starts connected.
|
|
46
|
+
*/
|
|
47
|
+
function markDisconnected() {
|
|
48
|
+
currentRuntime.disconnected = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Close every active browser context, preserving profile/cookies through the
|
|
53
|
+
* existing safe persistence path (the context close flushes the persistent
|
|
54
|
+
* profile dir). Clears the active-sessions map. Safe to call multiple times.
|
|
55
|
+
* Operates on the current runtime's sessions.
|
|
56
|
+
*
|
|
57
|
+
* @returns {Promise<{ closed: number }>}
|
|
58
|
+
*/
|
|
59
|
+
async function closeAllSessions() {
|
|
60
|
+
const sessions = currentRuntime.sessions || activeSessions;
|
|
61
|
+
const keys = Array.from(sessions.keys());
|
|
62
|
+
let closed = 0;
|
|
63
|
+
for (const accountId of keys) {
|
|
64
|
+
const session = sessions.get(accountId);
|
|
65
|
+
if (!session) continue;
|
|
66
|
+
// Delete BEFORE closing so a concurrent call does not double-close.
|
|
67
|
+
sessions.delete(accountId);
|
|
68
|
+
try {
|
|
69
|
+
if (session.context) await session.context.close().catch(() => {});
|
|
70
|
+
} catch {}
|
|
71
|
+
closed += 1;
|
|
72
|
+
}
|
|
73
|
+
return { closed };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Set up the session handler on an authenticated WebSocket.
|
|
78
|
+
*
|
|
79
|
+
* @param {WebSocket} ws — authenticated v2 WebSocket
|
|
80
|
+
* @param {object} opts — { chromePath, deviceMeta, serverOrigin }
|
|
81
|
+
* @returns {Map} the active sessions map
|
|
82
|
+
*/
|
|
83
|
+
function setupSessionHandler(ws, opts) {
|
|
84
|
+
// B4 fix: reset the per-runtime state so a newly initialized handler starts
|
|
85
|
+
// connected and does NOT inherit stale state from a prior handler lifecycle.
|
|
86
|
+
// The module-level `currentRuntime` is shared so cli.js's markDisconnected()
|
|
87
|
+
// / closeAllSessions() reach the active runtime. Each setupSessionHandler
|
|
88
|
+
// call resets it to connected.
|
|
89
|
+
currentRuntime.disconnected = false;
|
|
90
|
+
currentRuntime.sessions = activeSessions;
|
|
91
|
+
|
|
92
|
+
ws.on('message', (data, isBinary) => {
|
|
93
|
+
if (isBinary) return;
|
|
94
|
+
|
|
95
|
+
let msg;
|
|
96
|
+
try {
|
|
97
|
+
msg = JSON.parse(data.toString('utf8'));
|
|
98
|
+
} catch {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!msg || typeof msg !== 'object') return;
|
|
103
|
+
|
|
104
|
+
// Handle the existing hardened command envelope (Defect 8).
|
|
105
|
+
if (msg.type === 'command') {
|
|
106
|
+
// Fail-safe: after the authenticated WebSocket closed, no further commands
|
|
107
|
+
// may be accepted. A stale/zombie socket must never drive a browser.
|
|
108
|
+
if (currentRuntime.disconnected) {
|
|
109
|
+
const echo = _extractEcho(msg);
|
|
110
|
+
try {
|
|
111
|
+
ws.send(JSON.stringify({
|
|
112
|
+
type: 'command-response',
|
|
113
|
+
commandId: msg.commandId,
|
|
114
|
+
result: { success: false, error: 'Agent disconnected: commands no longer accepted' },
|
|
115
|
+
...echo,
|
|
116
|
+
}));
|
|
117
|
+
} catch {}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
_handleCommandEnvelope(ws, msg, opts).catch((err) => {
|
|
121
|
+
// Send error response with echo (never leak secrets).
|
|
122
|
+
const echo = _extractEcho(msg);
|
|
123
|
+
try {
|
|
124
|
+
ws.send(JSON.stringify({
|
|
125
|
+
type: 'command-response',
|
|
126
|
+
commandId: msg.commandId,
|
|
127
|
+
result: { success: false, error: _safeError(err) },
|
|
128
|
+
...echo,
|
|
129
|
+
}));
|
|
130
|
+
} catch {}
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Handle heartbeats.
|
|
136
|
+
if (msg.type === 'heartbeat') {
|
|
137
|
+
try { ws.send(JSON.stringify({ type: 'heartbeat-ack' })); } catch {}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Reconnect reconciliation: the server asks for an active-sessions report.
|
|
142
|
+
// Each entry is lease-bound (accountUuid + leaseId + leaseToken + status).
|
|
143
|
+
// A missing/invalid status is NEVER defaulted to 'running' — the server
|
|
144
|
+
// treats an unverified entry as unresolved (the lease stays blocking).
|
|
145
|
+
if (msg.type === 'report-sessions') {
|
|
146
|
+
try {
|
|
147
|
+
const sessions = [];
|
|
148
|
+
for (const [accountId, session] of activeSessions) {
|
|
149
|
+
const lease = (session.lease && typeof session.lease.getClientView === 'function')
|
|
150
|
+
? session.lease.getClientView()
|
|
151
|
+
: (session.lease || null);
|
|
152
|
+
sessions.push({
|
|
153
|
+
accountId,
|
|
154
|
+
accountUuid: session.accountUuid || null,
|
|
155
|
+
leaseId: (lease && lease.leaseId) || null,
|
|
156
|
+
leaseToken: (lease && lease.leaseToken) || null,
|
|
157
|
+
status: session.context ? 'running' : 'stopped',
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
ws.send(JSON.stringify({ type: 'sessions-report', sessions }));
|
|
161
|
+
} catch {}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// session-status ack: the server confirms a stop was verified. No-op here
|
|
166
|
+
// beyond acknowledging; the lease stays under server authority.
|
|
167
|
+
if (msg.type === 'session-status') {
|
|
168
|
+
try { ws.send(JSON.stringify({ type: 'session-status-ack', commandId: msg.commandId || null })); } catch {}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Other message types (session-status, sessions-report, log) are handled
|
|
173
|
+
// by the shared engine on the backend side — the agent only needs to
|
|
174
|
+
// respond to commands and heartbeats.
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return activeSessions;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Handle a command envelope: { type: 'command', commandId, accountId, command: { type, ... } }
|
|
182
|
+
*
|
|
183
|
+
* This delegates to the same session management logic used by the legacy agent,
|
|
184
|
+
* preserving all hardened safeguards (Defect 9).
|
|
185
|
+
*/
|
|
186
|
+
async function _handleCommandEnvelope(ws, msg, opts) {
|
|
187
|
+
const { commandId, accountId, command } = msg;
|
|
188
|
+
|
|
189
|
+
if (!commandId || typeof commandId !== 'string') {
|
|
190
|
+
return; // Malformed — drop silently.
|
|
191
|
+
}
|
|
192
|
+
if (!command || typeof command !== 'object') {
|
|
193
|
+
_sendResponse(ws, msg, { success: false, error: 'Missing command object' });
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const { type, ...rest } = command;
|
|
198
|
+
|
|
199
|
+
// Build the echo object from the envelope (nonce, leaseId, accountUuid).
|
|
200
|
+
const echo = _extractEcho(msg);
|
|
201
|
+
|
|
202
|
+
// Validate command type against the known allowlist.
|
|
203
|
+
const ALLOWED_TYPES = new Set([
|
|
204
|
+
'startSession', 'stopSession', 'restartSession', 'refreshSession',
|
|
205
|
+
'navigate', 'type', 'paste', 'press', 'click', 'runAction',
|
|
206
|
+
'getStatus', 'importLegacyProfile', 'discoverLegacyProfiles',
|
|
207
|
+
'focus',
|
|
208
|
+
]);
|
|
209
|
+
|
|
210
|
+
if (!ALLOWED_TYPES.has(type)) {
|
|
211
|
+
_sendResponse(ws, msg, { success: false, error: 'Unknown command type' });
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Lease-required commands (same set as legacy agent).
|
|
216
|
+
const LEASE_REQUIRED = new Set([
|
|
217
|
+
'stopSession', 'restartSession', 'refreshSession', 'navigate',
|
|
218
|
+
'type', 'paste', 'press', 'click', 'runAction', 'focus',
|
|
219
|
+
]);
|
|
220
|
+
|
|
221
|
+
// Execute the command using the shared runtime.
|
|
222
|
+
try {
|
|
223
|
+
const result = await _executeCommand(type, { ...rest, accountId }, msg, opts, ws);
|
|
224
|
+
_sendResponse(ws, msg, result, echo);
|
|
225
|
+
} catch (err) {
|
|
226
|
+
_sendResponse(ws, msg, { success: false, error: _safeError(err) }, echo);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Execute a single command. This reuses the existing hardened runtime logic.
|
|
232
|
+
*/
|
|
233
|
+
async function _executeCommand(type, params, msg, opts, ws) {
|
|
234
|
+
const accountId = params.accountId || msg.accountId;
|
|
235
|
+
const accountUuid = msg.accountUuid || params.accountUuid;
|
|
236
|
+
const lease = params.lease || msg.command?.lease;
|
|
237
|
+
|
|
238
|
+
switch (type) {
|
|
239
|
+
case 'startSession':
|
|
240
|
+
return _startSession(params, opts, accountId, accountUuid, lease);
|
|
241
|
+
case 'stopSession':
|
|
242
|
+
return _stopSession(accountId, lease);
|
|
243
|
+
case 'restartSession':
|
|
244
|
+
await _stopSession(accountId, lease);
|
|
245
|
+
return _startSession(params, opts, accountId, accountUuid, lease);
|
|
246
|
+
case 'refreshSession':
|
|
247
|
+
return _refreshSession(accountId);
|
|
248
|
+
case 'navigate':
|
|
249
|
+
return _navigate(accountId, params.url);
|
|
250
|
+
case 'type':
|
|
251
|
+
return _typeText(accountId, params.selector, params.text);
|
|
252
|
+
case 'paste':
|
|
253
|
+
return _typeText(accountId, params.selector, params.text);
|
|
254
|
+
case 'press':
|
|
255
|
+
return _pressKey(accountId, params.key);
|
|
256
|
+
case 'click':
|
|
257
|
+
return _click(accountId, params.selector);
|
|
258
|
+
case 'focus':
|
|
259
|
+
return _focusSession(accountId);
|
|
260
|
+
case 'runAction':
|
|
261
|
+
return _runAction(accountId, params.action, params.payload || params.args);
|
|
262
|
+
case 'getStatus':
|
|
263
|
+
return _getStatus();
|
|
264
|
+
case 'importLegacyProfile':
|
|
265
|
+
return _importLegacyProfile(accountId, accountUuid, opts);
|
|
266
|
+
case 'discoverLegacyProfiles':
|
|
267
|
+
return _discoverLegacyProfiles();
|
|
268
|
+
default:
|
|
269
|
+
return { success: false, error: 'Unknown command type' };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ── Session management (reuses existing hardened modules) ───────────────────
|
|
274
|
+
|
|
275
|
+
async function _startSession(params, opts, accountId, accountUuid, lease) {
|
|
276
|
+
if (!accountId) return { success: false, error: 'Missing accountId' };
|
|
277
|
+
|
|
278
|
+
const deviceMeta = opts.deviceMeta;
|
|
279
|
+
const profileDir = resolveProfileDir({
|
|
280
|
+
profileRoot: PROFILES_DIR,
|
|
281
|
+
deviceUuid: deviceMeta.deviceId,
|
|
282
|
+
accountUuid: accountUuid || accountId,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const fs = require('fs');
|
|
286
|
+
if (!fs.existsSync(profileDir)) fs.mkdirSync(profileDir, { recursive: true });
|
|
287
|
+
|
|
288
|
+
const fingerprint = fingerprintStore.loadOrGenerate(profileDir, () => generateFingerprint());
|
|
289
|
+
|
|
290
|
+
const args = [
|
|
291
|
+
'--disable-blink-features=AutomationControlled',
|
|
292
|
+
'--force-webrtc-ip-handling-policy=disable_non_proxied_udp',
|
|
293
|
+
'--no-first-run',
|
|
294
|
+
'--no-default-browser-check',
|
|
295
|
+
];
|
|
296
|
+
|
|
297
|
+
const launchOptions = {
|
|
298
|
+
executablePath: opts.chromePath,
|
|
299
|
+
headless: false,
|
|
300
|
+
args,
|
|
301
|
+
viewport: { width: fingerprint.viewport?.width || 1280, height: fingerprint.viewport?.height || 720 },
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
if (params.proxy) {
|
|
305
|
+
const proxyUrl = _parseProxy(params.proxy);
|
|
306
|
+
if (proxyUrl) launchOptions.proxy = proxyUrl;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const context = await chromium.launchPersistentContext(profileDir, launchOptions);
|
|
310
|
+
await context.addInitScript(buildStealthScript(fingerprint));
|
|
311
|
+
|
|
312
|
+
if (params.cookies && Array.isArray(params.cookies)) {
|
|
313
|
+
try { await context.addCookies(params.cookies); } catch {}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const pages = context.pages();
|
|
317
|
+
const page = pages[0] || await context.newPage();
|
|
318
|
+
|
|
319
|
+
const url = params.url || 'https://x.com/home';
|
|
320
|
+
await page.goto(url, { waitUntil: 'domcontentloaded' }).catch(() => {});
|
|
321
|
+
|
|
322
|
+
const session = { context, page, profileDir, accountId, lease: null };
|
|
323
|
+
if (lease) leaseClient.bind(session, lease);
|
|
324
|
+
activeSessions.set(accountId, session);
|
|
325
|
+
|
|
326
|
+
return { success: true, accountId, started: true };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async function _stopSession(accountId, lease) {
|
|
330
|
+
const session = activeSessions.get(accountId);
|
|
331
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
332
|
+
try { await session.context.close(); } catch {}
|
|
333
|
+
activeSessions.delete(accountId);
|
|
334
|
+
return { success: true, accountId, stopped: true };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async function _refreshSession(accountId) {
|
|
338
|
+
const session = activeSessions.get(accountId);
|
|
339
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
340
|
+
await session.page.reload();
|
|
341
|
+
return { success: true };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function _navigate(accountId, url) {
|
|
345
|
+
const session = activeSessions.get(accountId);
|
|
346
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
347
|
+
await session.page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
348
|
+
return { success: true };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function _typeText(accountId, selector, text) {
|
|
352
|
+
const session = activeSessions.get(accountId);
|
|
353
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
354
|
+
await session.page.locator(selector).fill(text);
|
|
355
|
+
return { success: true };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async function _pressKey(accountId, key) {
|
|
359
|
+
const session = activeSessions.get(accountId);
|
|
360
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
361
|
+
await session.page.keyboard.press(key);
|
|
362
|
+
return { success: true };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function _click(accountId, selector) {
|
|
366
|
+
const session = activeSessions.get(accountId);
|
|
367
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
368
|
+
await session.page.locator(selector).click();
|
|
369
|
+
return { success: true };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async function _focusSession(accountId) {
|
|
373
|
+
const session = activeSessions.get(accountId);
|
|
374
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
375
|
+
await session.page.bringToFront();
|
|
376
|
+
return { success: true };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function _runAction(accountId, action, payload) {
|
|
380
|
+
const session = activeSessions.get(accountId);
|
|
381
|
+
if (!session) return { success: false, error: 'Session not found' };
|
|
382
|
+
const { runAction } = require('./actions');
|
|
383
|
+
const result = await runAction(session.page, action, payload || {});
|
|
384
|
+
return { success: true, result };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function _getStatus() {
|
|
388
|
+
const sessions = [];
|
|
389
|
+
for (const [accountId, session] of activeSessions) {
|
|
390
|
+
sessions.push({ accountId, url: session.page ? session.page.url() : null });
|
|
391
|
+
}
|
|
392
|
+
return { success: true, sessions, count: sessions.length };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function _importLegacyProfile(accountId, accountUuid, opts) {
|
|
396
|
+
// Defect 9 fix: pass opts correctly (was undefined before).
|
|
397
|
+
const result = importLegacyProfile(accountId, {
|
|
398
|
+
profileRoot: PROFILES_DIR,
|
|
399
|
+
deviceUuid: opts.deviceMeta.deviceId,
|
|
400
|
+
accountUuid,
|
|
401
|
+
});
|
|
402
|
+
return { success: true, result };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function _discoverLegacyProfiles() {
|
|
406
|
+
const profiles = discoverLegacyProfiles(PROFILES_DIR);
|
|
407
|
+
return { success: true, profiles };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
function _extractEcho(msg) {
|
|
413
|
+
const echo = {};
|
|
414
|
+
if (msg.nonce) echo.nonce = msg.nonce;
|
|
415
|
+
if (msg.leaseId) echo.leaseId = msg.leaseId;
|
|
416
|
+
if (msg.accountUuid) echo.accountUuid = msg.accountUuid;
|
|
417
|
+
return echo;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function _sendResponse(ws, msg, result, echo) {
|
|
421
|
+
try {
|
|
422
|
+
ws.send(JSON.stringify({
|
|
423
|
+
type: 'command-response',
|
|
424
|
+
commandId: msg.commandId,
|
|
425
|
+
result,
|
|
426
|
+
...(echo || _extractEcho(msg)),
|
|
427
|
+
}));
|
|
428
|
+
} catch {}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function _parseProxy(proxyStr) {
|
|
432
|
+
if (!proxyStr || typeof proxyStr !== 'string') return null;
|
|
433
|
+
const parts = proxyStr.split(':');
|
|
434
|
+
if (parts.length >= 2) {
|
|
435
|
+
const proxy = { server: `http://${parts[0]}:${parts[1]}` };
|
|
436
|
+
if (parts[2] && parts[3]) {
|
|
437
|
+
proxy.username = parts[2];
|
|
438
|
+
proxy.password = parts[3];
|
|
439
|
+
}
|
|
440
|
+
return proxy;
|
|
441
|
+
}
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function _safeError(err) {
|
|
446
|
+
const msg = err && err.message ? err.message : String(err);
|
|
447
|
+
return msg
|
|
448
|
+
.replace(/[A-Fa-f0-9]{64}/g, '<hex>')
|
|
449
|
+
.replace(/eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g, '<jwt>')
|
|
450
|
+
.slice(0, 500);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
module.exports = { setupSessionHandler, activeSessions, closeAllSessions, markDisconnected };
|