@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,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-account fingerprint generator.
|
|
5
|
+
*
|
|
6
|
+
* Generates deterministic-but-unique fingerprint profiles from an accountId,
|
|
7
|
+
* so each of the 100+ accounts gets a distinct, internally-consistent
|
|
8
|
+
* fingerprint (WebGL vendor/renderer, canvas noise seed, hardware concurrency,
|
|
9
|
+
* device memory, screen resolution, etc.) instead of all sharing the same one.
|
|
10
|
+
*
|
|
11
|
+
* The fingerprint is deterministic: the same accountId always yields the same
|
|
12
|
+
* profile, so cookies/sessions remain consistent across restarts.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const crypto = require('crypto');
|
|
16
|
+
|
|
17
|
+
// ── Realistic GPU profiles (vendor, renderer) ────────────────────────────────
|
|
18
|
+
// Using common real-world GPU strings so accounts don't stand out.
|
|
19
|
+
// Larger pool (16 entries) so 100+ accounts spread across more buckets.
|
|
20
|
+
const GPU_PROFILES = [
|
|
21
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) UHD Graphics 630 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
22
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) UHD Graphics 770 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
23
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) Iris(R) Xe Graphics Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
24
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) HD Graphics 4000 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
25
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) HD Graphics 4600 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
26
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) HD Graphics 530 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
27
|
+
{ vendor: 'Google Inc. (Intel)', renderer: 'ANGLE (Intel, Intel(R) HD Graphics 620 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
28
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce GTX 1060 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
29
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce GTX 1650 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
30
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce GTX 1660 Ti Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
31
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce RTX 2060 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
32
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce RTX 3060 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
33
|
+
{ vendor: 'Google Inc. (NVIDIA)', renderer: 'ANGLE (NVIDIA, NVIDIA GeForce RTX 4060 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
34
|
+
{ vendor: 'Google Inc. (AMD)', renderer: 'ANGLE (AMD, AMD Radeon(TM) Graphics Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
35
|
+
{ vendor: 'Google Inc. (AMD)', renderer: 'ANGLE (AMD, AMD Radeon RX 580 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
36
|
+
{ vendor: 'Google Inc. (AMD)', renderer: 'ANGLE (AMD, AMD Radeon(TM) Vega 8 Graphics Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
37
|
+
{ vendor: 'Google Inc. (AMD)', renderer: 'ANGLE (AMD, AMD Radeon RX 6600 Direct3D11 vs_5_0 ps_5_0, D3D11)' },
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
// ── Screen resolutions (desktop) ─────────────────────────────────────────────
|
|
41
|
+
const SCREEN_RESOLUTIONS = [
|
|
42
|
+
{ width: 1920, height: 1080 },
|
|
43
|
+
{ width: 2560, height: 1440 },
|
|
44
|
+
{ width: 1536, height: 864 },
|
|
45
|
+
{ width: 1440, height: 900 },
|
|
46
|
+
{ width: 1366, height: 768 },
|
|
47
|
+
{ width: 1280, height: 720 },
|
|
48
|
+
{ width: 1600, height: 900 },
|
|
49
|
+
{ width: 1680, height: 1050 },
|
|
50
|
+
{ width: 1360, height: 768 },
|
|
51
|
+
{ width: 1280, height: 800 },
|
|
52
|
+
{ width: 1280, height: 1024 },
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
// ── Hardware concurrency values ──────────────────────────────────────────────
|
|
56
|
+
const HARDWARE_CONCURRENCIES = [4, 4, 6, 6, 8, 8, 8, 12, 12, 16, 16, 20];
|
|
57
|
+
|
|
58
|
+
// ── Device memory values (GB) ────────────────────────────────────────────────
|
|
59
|
+
const DEVICE_MEMORIES = [4, 4, 8, 8, 8, 16, 16, 32];
|
|
60
|
+
|
|
61
|
+
// ── Locale → languages mapping ───────────────────────────────────────────────
|
|
62
|
+
const LOCALE_LANGUAGES = {
|
|
63
|
+
'en-US': ['en-US', 'en'],
|
|
64
|
+
'en-GB': ['en-GB', 'en'],
|
|
65
|
+
'ar-SA': ['ar-SA', 'ar', 'en-US', 'en'],
|
|
66
|
+
'ar': ['ar', 'en-US', 'en'],
|
|
67
|
+
'fr-FR': ['fr-FR', 'fr', 'en-US', 'en'],
|
|
68
|
+
'de-DE': ['de-DE', 'de', 'en-US', 'en'],
|
|
69
|
+
'es-ES': ['es-ES', 'es', 'en-US', 'en'],
|
|
70
|
+
'tr-TR': ['tr-TR', 'tr', 'en-US', 'en'],
|
|
71
|
+
'ru-RU': ['ru-RU', 'ru', 'en-US', 'en'],
|
|
72
|
+
'pt-BR': ['pt-BR', 'pt', 'en-US', 'en'],
|
|
73
|
+
'id-ID': ['id-ID', 'id', 'en-US', 'en'],
|
|
74
|
+
'ja-JP': ['ja-JP', 'ja', 'en-US', 'en'],
|
|
75
|
+
'ko-KR': ['ko-KR', 'ko', 'en-US', 'en'],
|
|
76
|
+
'zh-CN': ['zh-CN', 'zh', 'en-US', 'en'],
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// ── Locale → timezone mapping ────────────────────────────────────────────────
|
|
80
|
+
const LOCALE_TIMEZONES = {
|
|
81
|
+
'en-US': 'America/New_York',
|
|
82
|
+
'en-GB': 'Europe/London',
|
|
83
|
+
'ar-SA': 'Asia/Riyadh',
|
|
84
|
+
'ar': 'Asia/Riyadh',
|
|
85
|
+
'fr-FR': 'Europe/Paris',
|
|
86
|
+
'de-DE': 'Europe/Berlin',
|
|
87
|
+
'es-ES': 'Europe/Madrid',
|
|
88
|
+
'tr-TR': 'Europe/Istanbul',
|
|
89
|
+
'ru-RU': 'Europe/Moscow',
|
|
90
|
+
'pt-BR': 'America/Sao_Paulo',
|
|
91
|
+
'id-ID': 'Asia/Jakarta',
|
|
92
|
+
'ja-JP': 'Asia/Tokyo',
|
|
93
|
+
'ko-KR': 'Asia/Seoul',
|
|
94
|
+
'zh-CN': 'Asia/Shanghai',
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// ── Chrome User-Agent strings (realistic, per-account) ──────────────────────
|
|
98
|
+
// Chrome on Windows is the most common browser — using it makes accounts
|
|
99
|
+
// blend in with real users. {ver} is replaced with a recent Chrome version.
|
|
100
|
+
const CHROME_UAS = [
|
|
101
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
102
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
103
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
104
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
105
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
106
|
+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36',
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
// Recent Chrome versions (rotated per-account for diversity)
|
|
110
|
+
const CHROME_VERSIONS = ['140', '141', '142', '143', '144', '145', '146', '147', '148', '149'];
|
|
111
|
+
|
|
112
|
+
// ── Network connection profiles (effectiveType, downlink, rtt) ───────────────
|
|
113
|
+
const CONNECTION_PROFILES = [
|
|
114
|
+
{ effectiveType: '4g', downlink: 10, rtt: 50 },
|
|
115
|
+
{ effectiveType: '4g', downlink: 8.5, rtt: 100 },
|
|
116
|
+
{ effectiveType: '4g', downlink: 5.6, rtt: 150 },
|
|
117
|
+
{ effectiveType: '4g', downlink: 15, rtt: 30 },
|
|
118
|
+
{ effectiveType: '4g', downlink: 7.2, rtt: 80 },
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
// ── Battery profiles (level, charging) ───────────────────────────────────────
|
|
122
|
+
const BATTERY_PROFILES = [
|
|
123
|
+
{ level: 0.85, charging: true },
|
|
124
|
+
{ level: 0.62, charging: false },
|
|
125
|
+
{ level: 0.41, charging: false },
|
|
126
|
+
{ level: 0.93, charging: true },
|
|
127
|
+
{ level: 0.27, charging: false },
|
|
128
|
+
{ level: 0.78, charging: true },
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
// ── Media device counts (microphones, cameras, speakers) ─────────────────────
|
|
132
|
+
const MEDIA_DEVICE_COUNTS = [
|
|
133
|
+
{ audioinput: 1, audiooutput: 2, videoinput: 1 },
|
|
134
|
+
{ audioinput: 1, audiooutput: 1, videoinput: 0 },
|
|
135
|
+
{ audioinput: 2, audiooutput: 2, videoinput: 1 },
|
|
136
|
+
{ audioinput: 1, audiooutput: 1, videoinput: 1 },
|
|
137
|
+
{ audioinput: 0, audiooutput: 1, videoinput: 0 },
|
|
138
|
+
{ audioinput: 1, audiooutput: 2, videoinput: 0 },
|
|
139
|
+
{ audioinput: 2, audiooutput: 2, videoinput: 2 },
|
|
140
|
+
{ audioinput: 1, audiooutput: 2, videoinput: 2 },
|
|
141
|
+
];
|
|
142
|
+
|
|
143
|
+
// ── Deterministic PRNG from accountId ────────────────────────────────────────
|
|
144
|
+
function seededRandom(accountId) {
|
|
145
|
+
const hash = crypto.createHash('sha256').update(String(accountId)).digest();
|
|
146
|
+
let seed = hash.readUInt32BE(0);
|
|
147
|
+
return function () {
|
|
148
|
+
// xorshift32
|
|
149
|
+
seed ^= seed << 13;
|
|
150
|
+
seed ^= seed >>> 17;
|
|
151
|
+
seed ^= seed << 5;
|
|
152
|
+
return ((seed >>> 0) / 0xFFFFFFFF);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function pick(rng, arr) {
|
|
157
|
+
return arr[Math.floor(rng() * arr.length)];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generate a full fingerprint profile for an account.
|
|
162
|
+
* @param {string} accountId - The account ID (deterministic seed)
|
|
163
|
+
* @param {object} options - { mobileView, locale, userAgent }
|
|
164
|
+
* @returns {object} Fingerprint profile
|
|
165
|
+
*/
|
|
166
|
+
function generateFingerprint(accountId, options = {}) {
|
|
167
|
+
const rng = seededRandom(accountId);
|
|
168
|
+
|
|
169
|
+
const gpu = pick(rng, GPU_PROFILES);
|
|
170
|
+
const screen = options.mobileView
|
|
171
|
+
? { width: 390, height: 844 }
|
|
172
|
+
: pick(rng, SCREEN_RESOLUTIONS);
|
|
173
|
+
const hardwareConcurrency = pick(rng, HARDWARE_CONCURRENCIES);
|
|
174
|
+
const deviceMemory = pick(rng, DEVICE_MEMORIES);
|
|
175
|
+
|
|
176
|
+
const locale = options.locale || 'en-US';
|
|
177
|
+
const languages = LOCALE_LANGUAGES[locale] || ['en-US', 'en'];
|
|
178
|
+
const timezone = options.timezone || LOCALE_TIMEZONES[locale] || 'America/New_York';
|
|
179
|
+
|
|
180
|
+
// Per-account Chrome User-Agent — use a recent Chrome version
|
|
181
|
+
const chromeVersion = options.chromeVersion || pick(rng, CHROME_VERSIONS);
|
|
182
|
+
const uaTemplate = pick(rng, CHROME_UAS);
|
|
183
|
+
const userAgent = options.userAgent || uaTemplate.replace(/\{ver\}/g, chromeVersion);
|
|
184
|
+
|
|
185
|
+
// Canvas noise seed — a small random offset applied to pixel data
|
|
186
|
+
const canvasNoiseSeed = Math.floor(rng() * 1000);
|
|
187
|
+
|
|
188
|
+
// AudioContext noise — tiny float perturbation
|
|
189
|
+
const audioNoiseSeed = rng() * 0.0001;
|
|
190
|
+
|
|
191
|
+
// Network connection profile
|
|
192
|
+
const connection = pick(rng, CONNECTION_PROFILES);
|
|
193
|
+
|
|
194
|
+
// Battery profile
|
|
195
|
+
const battery = pick(rng, BATTERY_PROFILES);
|
|
196
|
+
|
|
197
|
+
// Media devices
|
|
198
|
+
const mediaDevices = pick(rng, MEDIA_DEVICE_COUNTS);
|
|
199
|
+
|
|
200
|
+
// Performance.now() jitter (microseconds)
|
|
201
|
+
const perfJitter = Math.floor(rng() * 100) + 10;
|
|
202
|
+
|
|
203
|
+
// devicePixelRatio — realistic Windows values, deterministic per account.
|
|
204
|
+
// Hardcoding 1 across all accounts was itself a fingerprint signal.
|
|
205
|
+
const DEVICE_PIXEL_RATIOS = [1, 1, 1.25, 1.25, 1.5, 1.5, 1.75, 2];
|
|
206
|
+
const devicePixelRatio = options.devicePixelRatio || pick(rng, DEVICE_PIXEL_RATIOS);
|
|
207
|
+
|
|
208
|
+
// Platform based on UA
|
|
209
|
+
const ua = userAgent.toLowerCase();
|
|
210
|
+
let platform = 'Win32';
|
|
211
|
+
let userAgentDataPlatform = 'Windows';
|
|
212
|
+
let oscpu = ''; // Chrome doesn't have navigator.oscpu (Firefox-only)
|
|
213
|
+
if (ua.includes('mac')) {
|
|
214
|
+
platform = 'MacIntel';
|
|
215
|
+
userAgentDataPlatform = 'macOS';
|
|
216
|
+
oscpu = '';
|
|
217
|
+
} else if (ua.includes('linux')) {
|
|
218
|
+
platform = 'Linux x86_64';
|
|
219
|
+
userAgentDataPlatform = 'Linux';
|
|
220
|
+
oscpu = '';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Chrome doesn't have navigator.buildID (Firefox-only)
|
|
224
|
+
const buildID = '';
|
|
225
|
+
|
|
226
|
+
// Chrome Sec-CH-UA client hints (used for anti-detection)
|
|
227
|
+
const secChUa = `"Chromium";v="${chromeVersion}", "Google Chrome";v="${chromeVersion}", "Not.A/Brand";v="99"`;
|
|
228
|
+
const secChUaMobile = '?0';
|
|
229
|
+
const secChUaPlatform = `"${userAgentDataPlatform}"`;
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
gpu,
|
|
233
|
+
screen,
|
|
234
|
+
hardwareConcurrency,
|
|
235
|
+
deviceMemory,
|
|
236
|
+
languages,
|
|
237
|
+
locale,
|
|
238
|
+
timezone,
|
|
239
|
+
userAgent,
|
|
240
|
+
platform,
|
|
241
|
+
userAgentDataPlatform,
|
|
242
|
+
oscpu,
|
|
243
|
+
buildID,
|
|
244
|
+
canvasNoiseSeed,
|
|
245
|
+
audioNoiseSeed,
|
|
246
|
+
connection,
|
|
247
|
+
battery,
|
|
248
|
+
mediaDevices,
|
|
249
|
+
perfJitter,
|
|
250
|
+
devicePixelRatio,
|
|
251
|
+
// Chrome-specific fields
|
|
252
|
+
secChUa,
|
|
253
|
+
secChUaMobile,
|
|
254
|
+
secChUaPlatform,
|
|
255
|
+
chromeVersion,
|
|
256
|
+
isChrome: true,
|
|
257
|
+
// Mobile vs desktop
|
|
258
|
+
isMobile: !!options.mobileView,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
module.exports = { generateFingerprint };
|
|
@@ -0,0 +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,
|
|
126
|
+
};
|
|
@@ -0,0 +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
|
+
|
|
85
|
+
module.exports = { bind, boundStatusMessage, verifyCommandLease, sessionsReport };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Phase 11 — Per-user local process lock.
|
|
5
|
+
*
|
|
6
|
+
* Prevents two terminal processes from silently operating the same device
|
|
7
|
+
* simultaneously. Uses a lock file under the standard runtime directory.
|
|
8
|
+
*
|
|
9
|
+
* The lock file contains:
|
|
10
|
+
* { pid, startedAt, deviceId, deviceName }
|
|
11
|
+
*
|
|
12
|
+
* When another instance is running, shows a clear message.
|
|
13
|
+
* Supports `--replace` for safe takeover.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const os = require('os');
|
|
19
|
+
const { getDataDir } = require('./deviceKeyStore');
|
|
20
|
+
|
|
21
|
+
// Defect 15 fix: Lock path is environment-overridable for test isolation.
|
|
22
|
+
// Tests must NEVER write to the developer's actual XKWDStore application-data
|
|
23
|
+
// directory. Set XKWDSTORE_LOCK_FILE to a temp path in tests.
|
|
24
|
+
const LOCK_FILE = process.env.XKWDSTORE_LOCK_FILE || path.join(getDataDir(), 'agent.lock');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Read the current lock file.
|
|
28
|
+
* Returns null if no lock exists or the lock is stale (PID not running).
|
|
29
|
+
*/
|
|
30
|
+
function readLock() {
|
|
31
|
+
if (!fs.existsSync(LOCK_FILE)) return null;
|
|
32
|
+
try {
|
|
33
|
+
const lock = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8'));
|
|
34
|
+
if (!lock.pid) return null;
|
|
35
|
+
|
|
36
|
+
// Check if the PID is still running
|
|
37
|
+
if (!_isProcessRunning(lock.pid)) {
|
|
38
|
+
// Stale lock — clean up
|
|
39
|
+
try { fs.unlinkSync(LOCK_FILE); } catch {}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return lock;
|
|
44
|
+
} catch {
|
|
45
|
+
// Corrupt lock file — clean up
|
|
46
|
+
try { fs.unlinkSync(LOCK_FILE); } catch {}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Acquire the process lock. Fails if another instance is running.
|
|
53
|
+
*
|
|
54
|
+
* @param {object} opts — { deviceId, deviceName }
|
|
55
|
+
* @returns {boolean} true if acquired, false if another instance is running
|
|
56
|
+
*/
|
|
57
|
+
function acquireLock(opts) {
|
|
58
|
+
// Ensure data dir exists
|
|
59
|
+
const dir = path.dirname(LOCK_FILE);
|
|
60
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
61
|
+
|
|
62
|
+
const existing = readLock();
|
|
63
|
+
if (existing) {
|
|
64
|
+
return {
|
|
65
|
+
acquired: false,
|
|
66
|
+
existing,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const lock = {
|
|
71
|
+
pid: process.pid,
|
|
72
|
+
startedAt: new Date().toISOString(),
|
|
73
|
+
deviceId: (opts && opts.deviceId) || null,
|
|
74
|
+
deviceName: (opts && opts.deviceName) || null,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
fs.writeFileSync(LOCK_FILE, JSON.stringify(lock, null, 2), {
|
|
78
|
+
encoding: 'utf8',
|
|
79
|
+
mode: 0o600,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return { acquired: true };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Request graceful shutdown of the existing process.
|
|
87
|
+
* Sends SIGTERM to the existing PID and waits up to 10 seconds.
|
|
88
|
+
*
|
|
89
|
+
* @param {object} existingLock
|
|
90
|
+
* @returns {boolean} true if the existing process stopped
|
|
91
|
+
*/
|
|
92
|
+
async function requestTakeover(existingLock) {
|
|
93
|
+
if (!existingLock || !existingLock.pid) return true;
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
process.kill(existingLock.pid, 'SIGTERM');
|
|
97
|
+
} catch {
|
|
98
|
+
// Process may have already exited
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Wait up to 10 seconds for the process to exit
|
|
103
|
+
for (let i = 0; i < 100; i++) {
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
105
|
+
if (!_isProcessRunning(existingLock.pid)) {
|
|
106
|
+
// Clean up the lock file
|
|
107
|
+
try { fs.unlinkSync(LOCK_FILE); } catch {}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Release the process lock (only if we own it).
|
|
117
|
+
*/
|
|
118
|
+
function releaseLock() {
|
|
119
|
+
try {
|
|
120
|
+
if (!fs.existsSync(LOCK_FILE)) return;
|
|
121
|
+
const lock = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8'));
|
|
122
|
+
if (lock.pid === process.pid) {
|
|
123
|
+
fs.unlinkSync(LOCK_FILE);
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
// Best effort
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Check if a process with the given PID is running.
|
|
132
|
+
*/
|
|
133
|
+
function _isProcessRunning(pid) {
|
|
134
|
+
try {
|
|
135
|
+
// Sending signal 0 checks if the process exists without actually sending a signal
|
|
136
|
+
process.kill(pid, 0);
|
|
137
|
+
return true;
|
|
138
|
+
} catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports = {
|
|
144
|
+
readLock,
|
|
145
|
+
acquireLock,
|
|
146
|
+
requestTakeover,
|
|
147
|
+
releaseLock,
|
|
148
|
+
LOCK_FILE,
|
|
149
|
+
};
|