@xkwdstore/agent 2.0.2 → 2.0.3
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 +251 -253
- package/package.json +54 -54
- package/src/actions/comment.js +137 -137
- package/src/actions/dbc.js +149 -149
- package/src/actions/follow.js +82 -82
- package/src/actions/helpers.js +753 -753
- package/src/actions/index.js +94 -94
- package/src/actions/like.js +244 -244
- package/src/actions/session.js +1610 -1610
- package/src/actions/tweet.js +90 -90
- package/src/challengeCodec.js +31 -31
- package/src/chromeDetector.js +154 -154
- package/src/cli.js +664 -528
- package/src/configStore.js +97 -97
- package/src/deviceKeyStore.js +947 -700
- package/src/enrollmentClient.js +588 -581
- package/src/fingerprint.js +261 -261
- package/src/fingerprintStore.js +125 -125
- package/src/leaseClient.js +84 -84
- package/src/processLock.js +148 -148
- package/src/profilePath.js +1116 -1116
- package/src/proxyEgress.js +338 -338
- package/src/proxyPolicy.js +76 -76
- package/src/secretStore.js +132 -132
- package/src/sessionHandler.js +618 -452
- package/src/stealth.js +1250 -1250
- package/src/wsClient.js +233 -233
package/src/fingerprintStore.js
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Phase 4 — persistent browser fingerprints.
|
|
5
|
-
*
|
|
6
|
-
* A fingerprint is generated once per account (deterministic from the account
|
|
7
|
-
* id) and then persisted to disk inside the account's namespaced profile dir.
|
|
8
|
-
* On every subsequent launch the persisted fingerprint is loaded as-is instead
|
|
9
|
-
* of being regenerated, so an account keeps the SAME fingerprint across agent
|
|
10
|
-
* upgrades even if the generation algorithm changes (new GPU/screen pools,
|
|
11
|
-
* tweaked PRNG, etc.). This is what makes fingerprints "stable across upgrades".
|
|
12
|
-
*
|
|
13
|
-
* The persisted blob carries a SCHEMA_VERSION. If a future agent ships a
|
|
14
|
-
* fingerprint format the persisted blob does not satisfy (major schema bump),
|
|
15
|
-
* the store regenerates and re-persists — never silently mixing an old shape
|
|
16
|
-
* with new stealth code that expects new fields.
|
|
17
|
-
*
|
|
18
|
-
* Operator overrides (userAgent, locale, timezone, mobileView) are NOT part of
|
|
19
|
-
* the persisted identity: they are applied on top of the loaded base on every
|
|
20
|
-
* launch, so an operator changing the account's configured timezone still
|
|
21
|
-
* takes effect immediately while the randomized hardware fingerprint stays
|
|
22
|
-
* stable. Critically, the timezone override is what carries the configured
|
|
23
|
-
* timezone all the way into the browser (Playwright timezoneId) — see index.js.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const fs = require('fs');
|
|
27
|
-
const path = require('path');
|
|
28
|
-
|
|
29
|
-
const SCHEMA_VERSION = 1;
|
|
30
|
-
const FINGERPRINT_FILE = 'fingerprint.json';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Fields that define the stable hardware/device identity. These are persisted
|
|
34
|
-
* and reloaded verbatim so an account keeps the same hardware fingerprint
|
|
35
|
-
* forever, even across agent upgrades that change the generation pools/PRNG.
|
|
36
|
-
*
|
|
37
|
-
* Deliberately NOT persisted (kept live per-launch):
|
|
38
|
-
* - userAgent / chromeVersion / secChUa — must track the actually-installed
|
|
39
|
-
* Chrome version; a stale UA is itself a detection signal.
|
|
40
|
-
* - locale / timezone / languages — operator-tunable per the account config;
|
|
41
|
-
* a change must take effect immediately (and the timezone must reach the
|
|
42
|
-
* browser).
|
|
43
|
-
* - isMobile / mobileView screen — per-launch session choice.
|
|
44
|
-
*/
|
|
45
|
-
const PERSISTED_FIELDS = [
|
|
46
|
-
'gpu', 'screen', 'hardwareConcurrency', 'deviceMemory',
|
|
47
|
-
'canvasNoiseSeed', 'audioNoiseSeed', 'connection', 'battery',
|
|
48
|
-
'mediaDevices', 'perfJitter', 'devicePixelRatio',
|
|
49
|
-
];
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Load the persisted fingerprint for a profile dir, or generate + persist one.
|
|
53
|
-
*
|
|
54
|
-
* @param {object} args
|
|
55
|
-
* @param {string} args.profileDir namespaced profile directory
|
|
56
|
-
* @param {string} args.accountId account id (generation seed)
|
|
57
|
-
* @param {object} args.options operator overrides (mobileView, locale,
|
|
58
|
-
* userAgent, timezone, chromeVersion)
|
|
59
|
-
* @param {function} args.generator generateFingerprint(accountId, options)
|
|
60
|
-
* @returns {object} the effective fingerprint to apply this launch
|
|
61
|
-
*/
|
|
62
|
-
function loadOrGenerate({ profileDir, accountId, options = {}, generator }) {
|
|
63
|
-
const filePath = path.join(profileDir, FINGERPRINT_FILE);
|
|
64
|
-
const base = readPersisted(filePath);
|
|
65
|
-
|
|
66
|
-
if (!base) {
|
|
67
|
-
// First launch for this profile: generate the stable base and persist it.
|
|
68
|
-
const generated = generator(accountId, options);
|
|
69
|
-
persist(filePath, generated);
|
|
70
|
-
return generated;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Schema drift → regenerate the stable base (keep the operator overrides
|
|
74
|
-
// from `options` applied live by re-running the generator with the same
|
|
75
|
-
// seed, then persisting).
|
|
76
|
-
if (base.__schema !== SCHEMA_VERSION) {
|
|
77
|
-
const regenerated = generator(accountId, options);
|
|
78
|
-
persist(filePath, regenerated);
|
|
79
|
-
return regenerated;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Reload the persisted base and apply live operator overrides on top.
|
|
83
|
-
return applyOverrides(base, options, generator, accountId);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function readPersisted(filePath) {
|
|
87
|
-
try {
|
|
88
|
-
if (!fs.existsSync(filePath)) return null;
|
|
89
|
-
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
90
|
-
if (!data || typeof data !== 'object') return null;
|
|
91
|
-
return data;
|
|
92
|
-
} catch { return null; }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function persist(filePath, fingerprint) {
|
|
96
|
-
try {
|
|
97
|
-
const blob = { __schema: SCHEMA_VERSION };
|
|
98
|
-
for (const f of PERSISTED_FIELDS) blob[f] = fingerprint[f];
|
|
99
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
100
|
-
fs.writeFileSync(filePath, JSON.stringify(blob, null, 2), 'utf-8');
|
|
101
|
-
} catch { /* best-effort; non-fatal */ }
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Rebuild a full fingerprint object for this launch: generate a fresh,
|
|
106
|
-
* internally-consistent fingerprint (deterministic from accountId + the live
|
|
107
|
-
* operator options), then overlay the persisted hardware-identity fields from
|
|
108
|
-
* disk so the device fingerprint stays stable across upgrades. Derived fields
|
|
109
|
-
* (platform, secChUa, userAgent) are NOT overwritten — they come from the live
|
|
110
|
-
* generation and stay consistent with the installed Chrome version / locale.
|
|
111
|
-
*/
|
|
112
|
-
function applyOverrides(base, options, generator, accountId) {
|
|
113
|
-
const fresh = generator(accountId, options);
|
|
114
|
-
for (const f of PERSISTED_FIELDS) {
|
|
115
|
-
if (f === 'screen' && options.mobileView) continue; // live mobile screen wins
|
|
116
|
-
if (base[f] !== undefined) fresh[f] = base[f];
|
|
117
|
-
}
|
|
118
|
-
return fresh;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
module.exports = {
|
|
122
|
-
SCHEMA_VERSION,
|
|
123
|
-
FINGERPRINT_FILE,
|
|
124
|
-
PERSISTED_FIELDS,
|
|
125
|
-
loadOrGenerate,
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Phase 4 — persistent browser fingerprints.
|
|
5
|
+
*
|
|
6
|
+
* A fingerprint is generated once per account (deterministic from the account
|
|
7
|
+
* id) and then persisted to disk inside the account's namespaced profile dir.
|
|
8
|
+
* On every subsequent launch the persisted fingerprint is loaded as-is instead
|
|
9
|
+
* of being regenerated, so an account keeps the SAME fingerprint across agent
|
|
10
|
+
* upgrades even if the generation algorithm changes (new GPU/screen pools,
|
|
11
|
+
* tweaked PRNG, etc.). This is what makes fingerprints "stable across upgrades".
|
|
12
|
+
*
|
|
13
|
+
* The persisted blob carries a SCHEMA_VERSION. If a future agent ships a
|
|
14
|
+
* fingerprint format the persisted blob does not satisfy (major schema bump),
|
|
15
|
+
* the store regenerates and re-persists — never silently mixing an old shape
|
|
16
|
+
* with new stealth code that expects new fields.
|
|
17
|
+
*
|
|
18
|
+
* Operator overrides (userAgent, locale, timezone, mobileView) are NOT part of
|
|
19
|
+
* the persisted identity: they are applied on top of the loaded base on every
|
|
20
|
+
* launch, so an operator changing the account's configured timezone still
|
|
21
|
+
* takes effect immediately while the randomized hardware fingerprint stays
|
|
22
|
+
* stable. Critically, the timezone override is what carries the configured
|
|
23
|
+
* timezone all the way into the browser (Playwright timezoneId) — see index.js.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const fs = require('fs');
|
|
27
|
+
const path = require('path');
|
|
28
|
+
|
|
29
|
+
const SCHEMA_VERSION = 1;
|
|
30
|
+
const FINGERPRINT_FILE = 'fingerprint.json';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Fields that define the stable hardware/device identity. These are persisted
|
|
34
|
+
* and reloaded verbatim so an account keeps the same hardware fingerprint
|
|
35
|
+
* forever, even across agent upgrades that change the generation pools/PRNG.
|
|
36
|
+
*
|
|
37
|
+
* Deliberately NOT persisted (kept live per-launch):
|
|
38
|
+
* - userAgent / chromeVersion / secChUa — must track the actually-installed
|
|
39
|
+
* Chrome version; a stale UA is itself a detection signal.
|
|
40
|
+
* - locale / timezone / languages — operator-tunable per the account config;
|
|
41
|
+
* a change must take effect immediately (and the timezone must reach the
|
|
42
|
+
* browser).
|
|
43
|
+
* - isMobile / mobileView screen — per-launch session choice.
|
|
44
|
+
*/
|
|
45
|
+
const PERSISTED_FIELDS = [
|
|
46
|
+
'gpu', 'screen', 'hardwareConcurrency', 'deviceMemory',
|
|
47
|
+
'canvasNoiseSeed', 'audioNoiseSeed', 'connection', 'battery',
|
|
48
|
+
'mediaDevices', 'perfJitter', 'devicePixelRatio',
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Load the persisted fingerprint for a profile dir, or generate + persist one.
|
|
53
|
+
*
|
|
54
|
+
* @param {object} args
|
|
55
|
+
* @param {string} args.profileDir namespaced profile directory
|
|
56
|
+
* @param {string} args.accountId account id (generation seed)
|
|
57
|
+
* @param {object} args.options operator overrides (mobileView, locale,
|
|
58
|
+
* userAgent, timezone, chromeVersion)
|
|
59
|
+
* @param {function} args.generator generateFingerprint(accountId, options)
|
|
60
|
+
* @returns {object} the effective fingerprint to apply this launch
|
|
61
|
+
*/
|
|
62
|
+
function loadOrGenerate({ profileDir, accountId, options = {}, generator }) {
|
|
63
|
+
const filePath = path.join(profileDir, FINGERPRINT_FILE);
|
|
64
|
+
const base = readPersisted(filePath);
|
|
65
|
+
|
|
66
|
+
if (!base) {
|
|
67
|
+
// First launch for this profile: generate the stable base and persist it.
|
|
68
|
+
const generated = generator(accountId, options);
|
|
69
|
+
persist(filePath, generated);
|
|
70
|
+
return generated;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Schema drift → regenerate the stable base (keep the operator overrides
|
|
74
|
+
// from `options` applied live by re-running the generator with the same
|
|
75
|
+
// seed, then persisting).
|
|
76
|
+
if (base.__schema !== SCHEMA_VERSION) {
|
|
77
|
+
const regenerated = generator(accountId, options);
|
|
78
|
+
persist(filePath, regenerated);
|
|
79
|
+
return regenerated;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Reload the persisted base and apply live operator overrides on top.
|
|
83
|
+
return applyOverrides(base, options, generator, accountId);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function readPersisted(filePath) {
|
|
87
|
+
try {
|
|
88
|
+
if (!fs.existsSync(filePath)) return null;
|
|
89
|
+
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
90
|
+
if (!data || typeof data !== 'object') return null;
|
|
91
|
+
return data;
|
|
92
|
+
} catch { return null; }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function persist(filePath, fingerprint) {
|
|
96
|
+
try {
|
|
97
|
+
const blob = { __schema: SCHEMA_VERSION };
|
|
98
|
+
for (const f of PERSISTED_FIELDS) blob[f] = fingerprint[f];
|
|
99
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
100
|
+
fs.writeFileSync(filePath, JSON.stringify(blob, null, 2), 'utf-8');
|
|
101
|
+
} catch { /* best-effort; non-fatal */ }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Rebuild a full fingerprint object for this launch: generate a fresh,
|
|
106
|
+
* internally-consistent fingerprint (deterministic from accountId + the live
|
|
107
|
+
* operator options), then overlay the persisted hardware-identity fields from
|
|
108
|
+
* disk so the device fingerprint stays stable across upgrades. Derived fields
|
|
109
|
+
* (platform, secChUa, userAgent) are NOT overwritten — they come from the live
|
|
110
|
+
* generation and stay consistent with the installed Chrome version / locale.
|
|
111
|
+
*/
|
|
112
|
+
function applyOverrides(base, options, generator, accountId) {
|
|
113
|
+
const fresh = generator(accountId, options);
|
|
114
|
+
for (const f of PERSISTED_FIELDS) {
|
|
115
|
+
if (f === 'screen' && options.mobileView) continue; // live mobile screen wins
|
|
116
|
+
if (base[f] !== undefined) fresh[f] = base[f];
|
|
117
|
+
}
|
|
118
|
+
return fresh;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
module.exports = {
|
|
122
|
+
SCHEMA_VERSION,
|
|
123
|
+
FINGERPRINT_FILE,
|
|
124
|
+
PERSISTED_FIELDS,
|
|
125
|
+
loadOrGenerate,
|
|
126
126
|
};
|
package/src/leaseClient.js
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Agent-side lease credential binding (Phase 3).
|
|
5
|
-
*
|
|
6
|
-
* The plaintext `leaseToken` is stored ONLY in the active in-memory session
|
|
7
|
-
* object — it is never written to disk, never logged, and discarded when the
|
|
8
|
-
* session stops. It is echoed back to the server in the start ack, session
|
|
9
|
-
* status, stop ack, and session-error messages so the server can verify (via a
|
|
10
|
-
* constant-time hash compare in security/leaseStore) that the holder is
|
|
11
|
-
* authoritative. A command that does not bind to the active session's lease is
|
|
12
|
-
* rejected.
|
|
13
|
-
*
|
|
14
|
-
* This module is pure and has no Playwright/WS dependencies so it can be unit
|
|
15
|
-
* tested without a browser.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Attach a lease credential to an active session object.
|
|
20
|
-
* Returns true if bound, false if the credential is incomplete.
|
|
21
|
-
*/
|
|
22
|
-
function bind(session, lease) {
|
|
23
|
-
if (!session || !lease || !lease.leaseId || !lease.leaseToken) return false;
|
|
24
|
-
session.lease = {
|
|
25
|
-
leaseId: String(lease.leaseId),
|
|
26
|
-
leaseToken: String(lease.leaseToken),
|
|
27
|
-
accountUuid: lease.accountUuid ? String(lease.accountUuid) : null,
|
|
28
|
-
deviceId: lease.deviceId ? String(lease.deviceId) : null,
|
|
29
|
-
};
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Build a session-status message that includes the lease credential when the
|
|
35
|
-
* session is bound. `extra` fields (e.g. { error }) are merged in.
|
|
36
|
-
*/
|
|
37
|
-
function boundStatusMessage(session, accountId, status, extra) {
|
|
38
|
-
const base = { type: 'session-status', accountId: String(accountId), status };
|
|
39
|
-
if (session && session.lease && session.lease.leaseId && session.lease.leaseToken) {
|
|
40
|
-
base.accountUuid = session.lease.accountUuid;
|
|
41
|
-
base.leaseId = session.lease.leaseId;
|
|
42
|
-
base.leaseToken = session.lease.leaseToken;
|
|
43
|
-
}
|
|
44
|
-
return extra ? { ...base, ...extra } : base;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Verify that an inbound command is bound to the active session's lease.
|
|
49
|
-
* Returns { ok: true } or { ok: false, code }.
|
|
50
|
-
*/
|
|
51
|
-
function verifyCommandLease(session, command) {
|
|
52
|
-
if (!command || !command.lease) return { ok: false, code: 'LEASE_CREDENTIAL_REQUIRED' };
|
|
53
|
-
const lease = command.lease;
|
|
54
|
-
if (!lease.leaseId || !lease.leaseToken) return { ok: false, code: 'LEASE_CREDENTIAL_REQUIRED' };
|
|
55
|
-
if (!session || !session.lease) return { ok: false, code: 'NO_ACTIVE_LEASE' };
|
|
56
|
-
if (session.lease.leaseId !== String(lease.leaseId)) return { ok: false, code: 'LEASE_MISMATCH' };
|
|
57
|
-
if (session.lease.leaseToken !== String(lease.leaseToken)) return { ok: false, code: 'LEASE_TOKEN_INVALID' };
|
|
58
|
-
if (lease.expiresAt && Number.isFinite(Date.parse(lease.expiresAt)) && Date.parse(lease.expiresAt) < Date.now()) {
|
|
59
|
-
return { ok: false, code: 'LEASE_EXPIRED' };
|
|
60
|
-
}
|
|
61
|
-
return { ok: true };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Build the reconnect sessions report: one entry per bound active session,
|
|
66
|
-
* each carrying its lease credential so the server can reconcile.
|
|
67
|
-
*/
|
|
68
|
-
function sessionsReport(activeSessions) {
|
|
69
|
-
const out = [];
|
|
70
|
-
if (!activeSessions || typeof activeSessions[Symbol.iterator] !== 'function') return out;
|
|
71
|
-
for (const [accountId, session] of activeSessions) {
|
|
72
|
-
if (session && session.lease && session.lease.leaseId && session.lease.leaseToken) {
|
|
73
|
-
out.push({
|
|
74
|
-
accountId: String(accountId),
|
|
75
|
-
accountUuid: session.lease.accountUuid,
|
|
76
|
-
leaseId: session.lease.leaseId,
|
|
77
|
-
leaseToken: session.lease.leaseToken,
|
|
78
|
-
status: 'running',
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return out;
|
|
83
|
-
}
|
|
84
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Agent-side lease credential binding (Phase 3).
|
|
5
|
+
*
|
|
6
|
+
* The plaintext `leaseToken` is stored ONLY in the active in-memory session
|
|
7
|
+
* object — it is never written to disk, never logged, and discarded when the
|
|
8
|
+
* session stops. It is echoed back to the server in the start ack, session
|
|
9
|
+
* status, stop ack, and session-error messages so the server can verify (via a
|
|
10
|
+
* constant-time hash compare in security/leaseStore) that the holder is
|
|
11
|
+
* authoritative. A command that does not bind to the active session's lease is
|
|
12
|
+
* rejected.
|
|
13
|
+
*
|
|
14
|
+
* This module is pure and has no Playwright/WS dependencies so it can be unit
|
|
15
|
+
* tested without a browser.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Attach a lease credential to an active session object.
|
|
20
|
+
* Returns true if bound, false if the credential is incomplete.
|
|
21
|
+
*/
|
|
22
|
+
function bind(session, lease) {
|
|
23
|
+
if (!session || !lease || !lease.leaseId || !lease.leaseToken) return false;
|
|
24
|
+
session.lease = {
|
|
25
|
+
leaseId: String(lease.leaseId),
|
|
26
|
+
leaseToken: String(lease.leaseToken),
|
|
27
|
+
accountUuid: lease.accountUuid ? String(lease.accountUuid) : null,
|
|
28
|
+
deviceId: lease.deviceId ? String(lease.deviceId) : null,
|
|
29
|
+
};
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Build a session-status message that includes the lease credential when the
|
|
35
|
+
* session is bound. `extra` fields (e.g. { error }) are merged in.
|
|
36
|
+
*/
|
|
37
|
+
function boundStatusMessage(session, accountId, status, extra) {
|
|
38
|
+
const base = { type: 'session-status', accountId: String(accountId), status };
|
|
39
|
+
if (session && session.lease && session.lease.leaseId && session.lease.leaseToken) {
|
|
40
|
+
base.accountUuid = session.lease.accountUuid;
|
|
41
|
+
base.leaseId = session.lease.leaseId;
|
|
42
|
+
base.leaseToken = session.lease.leaseToken;
|
|
43
|
+
}
|
|
44
|
+
return extra ? { ...base, ...extra } : base;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Verify that an inbound command is bound to the active session's lease.
|
|
49
|
+
* Returns { ok: true } or { ok: false, code }.
|
|
50
|
+
*/
|
|
51
|
+
function verifyCommandLease(session, command) {
|
|
52
|
+
if (!command || !command.lease) return { ok: false, code: 'LEASE_CREDENTIAL_REQUIRED' };
|
|
53
|
+
const lease = command.lease;
|
|
54
|
+
if (!lease.leaseId || !lease.leaseToken) return { ok: false, code: 'LEASE_CREDENTIAL_REQUIRED' };
|
|
55
|
+
if (!session || !session.lease) return { ok: false, code: 'NO_ACTIVE_LEASE' };
|
|
56
|
+
if (session.lease.leaseId !== String(lease.leaseId)) return { ok: false, code: 'LEASE_MISMATCH' };
|
|
57
|
+
if (session.lease.leaseToken !== String(lease.leaseToken)) return { ok: false, code: 'LEASE_TOKEN_INVALID' };
|
|
58
|
+
if (lease.expiresAt && Number.isFinite(Date.parse(lease.expiresAt)) && Date.parse(lease.expiresAt) < Date.now()) {
|
|
59
|
+
return { ok: false, code: 'LEASE_EXPIRED' };
|
|
60
|
+
}
|
|
61
|
+
return { ok: true };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build the reconnect sessions report: one entry per bound active session,
|
|
66
|
+
* each carrying its lease credential so the server can reconcile.
|
|
67
|
+
*/
|
|
68
|
+
function sessionsReport(activeSessions) {
|
|
69
|
+
const out = [];
|
|
70
|
+
if (!activeSessions || typeof activeSessions[Symbol.iterator] !== 'function') return out;
|
|
71
|
+
for (const [accountId, session] of activeSessions) {
|
|
72
|
+
if (session && session.lease && session.lease.leaseId && session.lease.leaseToken) {
|
|
73
|
+
out.push({
|
|
74
|
+
accountId: String(accountId),
|
|
75
|
+
accountUuid: session.lease.accountUuid,
|
|
76
|
+
leaseId: session.lease.leaseId,
|
|
77
|
+
leaseToken: session.lease.leaseToken,
|
|
78
|
+
status: 'running',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
85
|
module.exports = { bind, boundStatusMessage, verifyCommandLease, sessionsReport };
|