-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
603 lines (523 loc) · 21.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import WebSocket from 'ws';
import { performance } from 'perf_hooks';
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent'; // Fix import
import "dotenv/config";
// Constants
const AUTH_TOKEN = process.env.AUTH_TOKEN;
const CHECK_INTERVAL = 5 * 60 * 1;
const API_BASE_URL = 'https://app.despeed.net/v1/api'; // Make sure v1 is included
const API_ENDPOINTS = {
eligibility: '/speedtest-eligibility',
points: '/points'
};
// Proxy configuration (optional)
const PROXY_CONFIG = (process.env.PROXY_HOST && process.env.PROXY_PORT) ? {
host: process.env.PROXY_HOST,
port: process.env.PROXY_PORT
} : null;
// Create proxy agent only if proxy is configured
const proxyAgent = PROXY_CONFIG ?
new HttpsProxyAgent(`http://${PROXY_CONFIG.host}:${PROXY_CONFIG.port}`) :
null;
// Configure axios with conditional proxy
const axiosConfig = PROXY_CONFIG ? {
proxy: {
host: PROXY_CONFIG.host,
port: PROXY_CONFIG.port,
protocol: 'http'
},
httpsAgent: proxyAgent
} : {};
// Add retry configuration
const RETRY_CONFIG = {
retries: 3,
retryDelay: 5000,
retryCondition: (error) => {
return axios.isAxiosError(error) && (
error.code === 'ECONNRESET' ||
error.code === 'ETIMEDOUT' ||
error.response?.status >= 500
);
}
};
// Update axios instance with retry
const api = axios.create({
baseURL: API_BASE_URL,
...axiosConfig,
headers: {
'Authorization': `Bearer ${AUTH_TOKEN}`,
'Content-Type': 'application/json',
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'identity', // Force plain text encoding
'Accept-Language': 'en-US,en;q=0.9',
'Referer': 'https://app.despeed.net/dashboard',
'Origin': 'https://app.despeed.net',
'sec-ch-ua': '"Brave";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
},
// Important: Enable credentials to send cookies
withCredentials: true,
timeout: 30000,
responseType: 'json', // Ensure JSON response
decompress: false, // Disable automatic decompression
proxy: false, // Disable default proxy handling
retry: RETRY_CONFIG.retries,
retryDelay: RETRY_CONFIG.retryDelay
});
// Add retry interceptor
api.interceptors.response.use(undefined, async (err) => {
const config = err.config;
if (!config || !RETRY_CONFIG.retryCondition(err) || !config.retry) {
return Promise.reject(err);
}
config.retry -= 1;
config.retryCount = (config.retryCount || 0) + 1;
console.log(`Retrying request (${config.retryCount}/${RETRY_CONFIG.retries})...`);
await new Promise(resolve => setTimeout(resolve, RETRY_CONFIG.retryDelay));
return api(config);
});
// Add cookie handling
const COOKIES = {
refreshToken: process.env.AUTH_TOKEN,
'connect.sid': process.env.CONNECT_SID
};
// Add request interceptor to include cookies
api.interceptors.request.use(config => {
config.headers.Cookie = Object.entries(COOKIES)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
return config;
});
// Add response interceptor
api.interceptors.response.use(
response => response,
error => {
console.error('API Error:', {
url: error.config?.url,
method: error.config?.method,
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
message: error.message
});
return Promise.reject(error);
}
);
// Generate UUID for client session ID
const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
// Constants
const CLIENT_INFO = {
client_library_name: "ndt7-js",
client_library_version: "0.0.6"
};
// Constants for WebSocket
const WS_PROTOCOL = 'net.measurementlab.ndt.v7';
const WS_OPTIONS = {
handshakeTimeout: 60000, // Increase from 30000 to 60000
maxPayload: 104857600,
perMessageDeflate: false,
skipUTF8Validation: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'Upgrade',
'Upgrade': 'websocket',
'Sec-WebSocket-Version': '13',
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits'
},
// Only add proxy if configured
...(PROXY_CONFIG && {
agent: proxyAgent,
proxy: `http://${PROXY_CONFIG.host}:${PROXY_CONFIG.port}`
})
};
// Main speed test function
const measureSpeed = async () => {
console.log('Starting speed test...');
try {
const serverInfo = await discoverServers();
console.log('Using test server:', serverInfo);
console.log('\nStarting download test...');
const downloadSpeed = await runDownloadTest(serverInfo['///ndt/v7/download']);
console.log(`Download Speed: ${downloadSpeed.toFixed(2)} Mbps`);
console.log('\nStarting upload test...');
const uploadSpeed = await runUploadTest(serverInfo['///ndt/v7/upload']);
console.log(`Upload Speed: ${uploadSpeed.toFixed(2)} Mbps`);
return {
download: downloadSpeed,
upload: uploadSpeed
};
} catch (err) {
console.error('Error during speed test:', err);
throw err;
}
};
// Discover test servers
const discoverServers = async () => {
const metadata = {
client_name: "speed-measurementlab-net-1",
client_session_id: generateUUID(),
...CLIENT_INFO
};
const params = new URLSearchParams(metadata);
const url = `https://locate.measurementlab.net/v2/nearest/ndt/ndt7?${params}`;
try {
const response = await axios.get(url);
const data = response.data;
if (!data.results?.length) {
throw new Error('No test servers available');
}
const server = data.results[0];
return {
'///ndt/v7/download': server.urls['wss:///ndt/v7/download'],
'///ndt/v7/upload': server.urls['wss:///ndt/v7/upload']
};
} catch (error) {
console.error('Error discovering servers:', error.message);
throw error;
}
};
// Modified runDownloadTest function
const runDownloadTest = (url) => {
return new Promise((resolve, reject) => {
let retryCount = 0;
const MAX_RETRIES = 3;
const RETRY_DELAY = 5000;
function attemptConnection() {
console.log(`Attempting download connection (attempt ${retryCount + 1}/${MAX_RETRIES})...`);
console.log(`Using server: ${new URL(url).hostname}`);
// Update connection logging to be conditional on proxy configuration
if (PROXY_CONFIG) {
console.log(`Attempting download connection through proxy ${PROXY_CONFIG.host}:${PROXY_CONFIG.port}...`);
} else {
console.log('Attempting direct download connection...');
}
const wsOptions = {
...WS_OPTIONS,
headers: {
...WS_OPTIONS.headers,
'Host': new URL(url).host,
'Origin': 'https://app.despeed.net'
}
};
// Only add agent if proxy is configured
if (proxyAgent) {
wsOptions.agent = proxyAgent;
}
// Create WebSocket with direct protocol string
const ws = new WebSocket(url, 'net.measurementlab.ndt.v7', wsOptions);
let hasStarted = false;
let totalBytes = 0;
const startTime = performance.now();
// Connection timeout handler
const connectionTimeout = setTimeout(() => {
if (!hasStarted && ws.readyState !== WebSocket.OPEN) {
console.log('Connection taking too long, attempting retry...');
ws.terminate();
}
}, WS_OPTIONS.handshakeTimeout);
ws.on('open', () => {
console.log('WebSocket connection opened for download test');
hasStarted = true;
clearTimeout(connectionTimeout);
});
// Modified error handler
ws.on('error', (err) => {
console.log(`WebSocket error (${retryCount + 1}/${MAX_RETRIES}):`, err.message);
clearTimeout(connectionTimeout);
handleError(err);
});
// Add protocol verification
ws.on('upgrade', (response) => {
const protocol = response.headers['sec-websocket-protocol'];
if (protocol !== WS_PROTOCOL) {
ws.terminate();
handleError(new Error(`Protocol mismatch. Expected: ${WS_PROTOCOL}, Got: ${protocol}`));
}
});
ws.on('message', (data) => {
if (typeof data === 'string') {
try {
const parsed = JSON.parse(data);
console.log('Download message:', parsed);
return;
} catch (e) {
console.log('Received string message:', data);
return;
}
}
totalBytes += data.length;
});
ws.on('close', () => {
if (!hasStarted) {
reject(new Error('Connection closed before test could start'));
return;
}
const duration = (performance.now() - startTime) / 1000;
const speedMbps = (totalBytes * 8) / (1000000 * duration);
console.log(`Download test completed: ${totalBytes} bytes in ${duration}s`);
resolve(speedMbps);
});
// Add timeout
setTimeout(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.close();
}
}, 10000);
}
function handleError(err) {
if (retryCount < MAX_RETRIES - 1) {
retryCount++;
console.log(`Retrying download test in ${RETRY_DELAY/1000} seconds... (${retryCount}/${MAX_RETRIES})`);
setTimeout(attemptConnection, RETRY_DELAY);
} else {
console.error('Max retries reached, giving up');
reject(err);
}
}
attemptConnection();
});
};
// Similarly update runUploadTest
const runUploadTest = (url) => {
return new Promise((resolve, reject) => {
const wsOptions = {
...WS_OPTIONS,
headers: {
...WS_OPTIONS.headers,
'Host': new URL(url).host,
'Origin': 'https://app.despeed.net',
'Sec-WebSocket-Protocol': WS_PROTOCOL
}
};
// Only add proxy if configured
if (PROXY_CONFIG) {
wsOptions.agent = proxyAgent;
console.log(`Attempting upload through proxy ${PROXY_CONFIG.host}:${PROXY_CONFIG.port}...`);
} else {
console.log('Attempting direct upload connection...');
}
let retryCount = 0;
const MAX_RETRIES = 3;
const RETRY_DELAY = 5000;
function attemptConnection() {
console.log(`Attempting upload connection (attempt ${retryCount + 1}/${MAX_RETRIES})...`);
// Create WebSocket instance first
const ws = new WebSocket(url, 'net.measurementlab.ndt.v7', wsOptions);
let hasStarted = false;
let connectionTimeout;
// Set connection timeout after ws is defined
connectionTimeout = setTimeout(() => {
if (!hasStarted) {
ws.terminate();
handleError(new Error('Connection timeout'));
}
}, wsOptions.timeout);
function handleError(err) {
clearTimeout(connectionTimeout);
if (retryCount < MAX_RETRIES - 1) {
retryCount++;
console.log(`Retrying upload test in ${RETRY_DELAY/1000} seconds...`);
setTimeout(attemptConnection, RETRY_DELAY);
} else {
reject(err);
}
}
let totalBytes = 0;
const startTime = performance.now();
const chunkSize = 16384;
ws.on('open', () => {
console.log('WebSocket connection opened for upload test');
hasStarted = true;
const sendChunk = () => {
if (ws.readyState === WebSocket.OPEN) {
const chunk = Buffer.alloc(chunkSize);
ws.send(chunk);
totalBytes += chunkSize;
}
};
const interval = setInterval(sendChunk, 0);
setTimeout(() => {
clearInterval(interval);
if (ws.readyState === WebSocket.OPEN) {
ws.close();
}
}, 10000);
});
ws.on('message', (data) => {
if (typeof data === 'string') {
try {
const parsed = JSON.parse(data);
console.log('Upload message:', parsed);
} catch (e) {
console.log('Received string message:', data);
}
}
});
ws.on('error', handleError);
ws.on('close', () => {
if (!hasStarted) {
reject(new Error('Connection closed before test could start'));
return;
}
const duration = (performance.now() - startTime) / 1000;
const speedMbps = (totalBytes * 8) / (1000000 * duration);
console.log(`Upload test completed: ${totalBytes} bytes in ${duration}s`);
resolve(speedMbps);
});
}
attemptConnection();
});
};
// Function to check eligibility (modified)
async function checkEligibility() {
try {
// First check profile to ensure authentication works
const profileResponse = await api.get('/auth/profile');
if (profileResponse.data && typeof profileResponse.data === 'object') {
console.log('Profile check successful');
} else {
console.error('Invalid profile response');
return false;
}
// Then check eligibility
const response = await api.post(API_ENDPOINTS.eligibility, {}, {
retry: RETRY_CONFIG.retries
});
if (response.data?.success) {
console.log('Eligibility response:', {
isEligible: response.data.data.isEligible,
nextTime: response.data.data.timing.nextTime,
lastTime: response.data.data.timing.lastTime,
completed: response.data.data.today.completed,
total: response.data.data.today.total
});
// Check if eligible based on time and completion status
const now = new Date();
const nextTime = new Date(response.data.data.timing.nextTime);
const isTimeEligible = now >= nextTime;
const isNotCompleted = response.data.data.today.completed < response.data.data.today.total;
if (isTimeEligible && isNotCompleted) {
console.log('Eligible for speed test - Time OK and not completed');
return true;
} else {
if (!isTimeEligible) {
console.log(`Not eligible yet - Next test at: ${nextTime.toISOString()}`);
}
if (!isNotCompleted) {
console.log(`Daily quota completed - ${response.data.data.today.completed}/${response.data.data.today.total}`);
}
return false;
}
} else {
console.error('Invalid eligibility response');
return false;
}
} catch (error) {
console.error('Error checking eligibility:', {
code: error.code,
message: error.message,
retryCount: error.config?.retryCount || 0
});
// Return cached nextCheckTime if available
if (nextCheckTime) {
console.log('Using cached next check time due to error');
return false;
}
// Default wait time on error
nextCheckTime = new Date(Date.now() + 5 * 60000).toISOString();
return false;
}
}
// Function to submit speed test results
async function submitResults(downloadSpeed, uploadSpeed) {
try {
const response = await api.post(API_ENDPOINTS.points, {
download_speed: downloadSpeed,
upload_speed: uploadSpeed,
latitude: 0,
longitude: 0
});
console.log('Results submitted successfully:', response.data);
return response.data;
} catch (error) {
if (error.response?.status === 401) {
console.error('Authentication failed. Please check your JWT token.');
process.exit(1); // Exit if unauthorized
}
throw error;
}
}
// Add cache for next check time
let nextCheckTime = null;
// Modified runSpeedTestLoop to use cached next check time
async function runSpeedTestLoop() {
while (true) {
try {
console.log('\n=== Starting new test cycle ===');
console.log(`Current time: ${new Date().toISOString()}`);
// If we have a cached next check time, use it
if (nextCheckTime && new Date() < new Date(nextCheckTime)) {
const now = new Date();
const waitTime = Math.max(new Date(nextCheckTime) - now + 1000, 60000);
const waitMinutes = Math.round(waitTime/60000);
console.log('\nStatus Summary (Cached):');
console.log('-------------------');
console.log(`Waiting Time: ${waitMinutes} minutes`);
console.log(`Next Check: ${nextCheckTime}`);
console.log('-------------------\n');
await new Promise(resolve => setTimeout(resolve, waitTime));
continue;
}
const eligibilityCheck = await checkEligibility();
if (eligibilityCheck) {
nextCheckTime = null; // Reset cache when eligible
console.log('Eligible for speed test. Starting...');
const results = await measureSpeed();
console.log('\nSubmitting results to Despeed...');
await submitResults(results.download, results.upload);
} else {
// Get and cache next check time
const response = await api.post(API_ENDPOINTS.eligibility, {});
if (response.data?.success) {
nextCheckTime = response.data.data.timing.nextTime;
const now = new Date();
const waitTime = Math.max(new Date(nextCheckTime) - now + 1000, 60000);
const waitMinutes = Math.round(waitTime/60000);
console.log('\nStatus Summary:');
console.log('-------------------');
console.log(`Tests Today: ${response.data.data.today.completed}/${response.data.data.today.total}`);
console.log(`Waiting Time: ${waitMinutes} minutes`);
console.log(`Next Check: ${nextCheckTime}`);
console.log('-------------------\n');
await new Promise(resolve => setTimeout(resolve, waitTime));
} else {
nextCheckTime = null; // Reset cache on error
await new Promise(resolve => setTimeout(resolve, CHECK_INTERVAL));
}
}
} catch (error) {
console.error('Error in test cycle:', error);
await new Promise(resolve => setTimeout(resolve, 60000));
}
}
}
// Start the continuous testing loop
console.log('Starting continuous speed test monitoring...');
runSpeedTestLoop().catch(console.error);
export { measureSpeed, discoverServers, runDownloadTest, runUploadTest };