Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 1 addition & 77 deletions src/app/dashboard/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import fetch from 'node-fetch';
import fetchCookie from 'fetch-cookie';
import { CookieJar } from 'tough-cookie';

const jar = new CookieJar();
const fetchWithCookies = fetchCookie(fetch, jar);

const USERNAME = 'ubnt';
const PASSWORD = 'samitherover';
const baseStationIP = '192.168.0.2';

const hosts = ['192.168.0.2', '192.168.0.3', '192.168.0.55']; // Add more hosts here as needed

const ping = require('ping');
Expand Down Expand Up @@ -39,75 +28,10 @@ async function pingHosts(hosts: string[]): Promise<{ [key: string]: number }> {
return results;
}

// Authenticates with the base station
async function authenticate() {
const formData = new URLSearchParams();
formData.append('uri', '/index.cgi');
formData.append('username', USERNAME);
formData.append('password', PASSWORD);

const response = await fetchWithCookies(`http://${baseStationIP}/login.cgi`, {
method: 'POST',
body: formData,
redirect: 'manual',
});

if (response.status !== 302) {
throw new Error(`Authentication failed: ${response.status}`);
}
}

// Fetches status JSON from the base station
async function fetchStatus() {
const response = await fetchWithCookies(`http://${baseStationIP}/status.cgi`, {
method: 'GET',
redirect: 'manual',
});


if (response.status === 302) {
await authenticate();
throw new Error('Not authenticated (redirected to login)');
}

if (!response.ok) {
throw new Error(`Failed to fetch status: ${response.status}`);
}
return response.json();
}

export async function GET(request: Request) {
// Initialize response data with defaults
let uplinkCapacity = 0;
let downlinkCapacity = 0;
let uplinkThroughput = 0;
let downlinkThroughput = 0;
let baseStationError: string | null = null;

// Try to fetch base station data, but don't fail if it's unavailable
try {
const status : any = await fetchStatus();
uplinkCapacity = status.wireless?.txrate ?? 0;
downlinkCapacity = status.wireless?.rxrate ?? 0;
uplinkThroughput = status.wireless?.throughput?.tx ?? 0;
downlinkThroughput = status.wireless?.throughput?.rx ?? 0;
} catch (error: any) {
baseStationError = error.message;
console.warn('Failed to fetch base station data:', error.message);
}

// Always ping hosts, regardless of base station status
try {
const pings = await pingHosts(hosts);

return Response.json({
uplinkCapacity,
downlinkCapacity,
uplinkThroughput,
downlinkThroughput,
pings,
baseStationError, // Include error info in response
});
return Response.json({pings});
} catch (error: any) {
console.error('Failed to ping hosts:', error);
return Response.json(
Expand Down
Loading
Loading