forked from itserver/speedtest-raspberry
101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
const speedTest = require('speedtest-net');
|
|
const axios = require('axios');
|
|
|
|
async function runSpeedTest(serverId) {
|
|
const options = {
|
|
serverId,
|
|
acceptLicense: true,
|
|
acceptGdpr: true,
|
|
};
|
|
|
|
try {
|
|
console.log(`Testing speed with server ID: ${serverId}, please wait...`);
|
|
const result = await speedTest(options);
|
|
|
|
// Format hasil
|
|
const output = {
|
|
timestamp: new Date().toISOString(),
|
|
ping: {
|
|
jitter: result.ping.jitter,
|
|
latency: result.ping.latency,
|
|
},
|
|
download: {
|
|
bandwidth: result.download.bandwidth,
|
|
bytes: result.download.bytes,
|
|
elapsed: result.download.elapsed,
|
|
},
|
|
upload: {
|
|
bandwidth: result.upload.bandwidth,
|
|
bytes: result.upload.bytes,
|
|
elapsed: result.upload.elapsed,
|
|
},
|
|
packetLoss: result.packetLoss,
|
|
isp: result.isp,
|
|
interface: {
|
|
internalIp: result.interface.internalIp,
|
|
name: result.interface.name,
|
|
macAddr: result.interface.macAddr,
|
|
isVpn: result.interface.isVpn,
|
|
externalIp: result.interface.externalIp,
|
|
},
|
|
server: {
|
|
id: result.server.id,
|
|
name: result.server.name,
|
|
location: result.server.location,
|
|
country: result.server.country,
|
|
host: result.server.host,
|
|
port: result.server.port,
|
|
ip: result.server.ip,
|
|
},
|
|
result: {
|
|
id: result.result.id,
|
|
url: result.result.url,
|
|
},
|
|
};
|
|
|
|
console.log('Test complete. Result:');
|
|
console.log(JSON.stringify(output, null, 2));
|
|
return output;
|
|
} catch (error) {
|
|
console.error(`Error running speed test with server ID: ${serverId}:`, error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function sendResultToServer(result) {
|
|
const url = 'https://raspberry.pandawa24jam.com/wbhkraspberry';
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
'author': 'vedeom',
|
|
};
|
|
|
|
try {
|
|
console.log('Sending result to server...');
|
|
const response = await axios.post(url, result, { headers });
|
|
console.log('Result successfully sent. Server response:');
|
|
console.log(response.data);
|
|
} catch (error) {
|
|
console.error('Failed to send result to server:', error.message);
|
|
}
|
|
}
|
|
|
|
(async () => {
|
|
try {
|
|
// Coba server pertama
|
|
const primaryServerId = 39296; // Gunadarma University
|
|
const result = await runSpeedTest(primaryServerId);
|
|
await sendResultToServer(result);
|
|
} catch (primaryError) {
|
|
console.warn('Primary server failed. Attempting backup server...');
|
|
|
|
// Jika server pertama gagal, coba server kedua
|
|
const backupServerId = 13039; // Server kedua
|
|
try {
|
|
const result = await runSpeedTest(backupServerId);
|
|
await sendResultToServer(result);
|
|
} catch (backupError) {
|
|
console.error('Backup server also failed. Please check your internet connection or server configurations.');
|
|
}
|
|
}
|
|
})();
|