-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (194 loc) · 8.05 KB
/
Copy pathindex.js
File metadata and controls
227 lines (194 loc) · 8.05 KB
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
import dotenv from "dotenv";
import axios from "axios";
import express from "express";
import cors from "cors";
dotenv.config({ path: "config/.env" });
// --- Debug: Check Loaded Environment Variables ---
console.log("Loaded env vars:", {
VT_API_KEY: process.env.VT_API_KEY ? "Present" : "Missing",
OPENWEATHER_API_KEY: process.env.OPENWEATHER_API_KEY ? "Present" : "Missing",
ABUSEIPDB_API_KEY: process.env.ABUSEIPDB_API_KEY ? "Present" : "Missing",
});
// --- Preloaded Keys (from .env) ---
const keys = [
process.env.VT_API_KEY && {
platform: "VirusTotal",
key: process.env.VT_API_KEY,
baseUrl: "https://www.virustotal.com/api/v3",
testEndpoint: "/ip_addresses/8.8.8.8",
},
process.env.OPENWEATHER_API_KEY && {
platform: "OpenWeatherMap",
key: process.env.OPENWEATHER_API_KEY,
baseUrl: "https://api.openweathermap.org/data/2.5",
testEndpoint: "/weather?q=London&appid={key}",
},
process.env.ABUSEIPDB_API_KEY && {
platform: "AbuseIPDB",
key: process.env.ABUSEIPDB_API_KEY,
baseUrl: "https://api.abuseipdb.com/api/v2",
testEndpoint: "/check?ipAddress=8.8.8.8",
},
].filter(Boolean);
// --- Debug: Check Loaded Keys ---
console.log(`Loaded ${keys.length} API keys:`, keys.map(k => k.platform));
// --- Startup Message ---
console.log(`[${new Date().toISOString()}] API Health Check Server running on localhost`);
// --- Track API Requests and Historical Data ---
let requestCount = 0; // Counter for API requests
let historicalResults = []; // Store historical health check results
// --- Health Check Logic ---
async function checkKeyStatus(entry) {
const startTime = performance.now();
let status, httpStatus, errorMessage;
try {
let response;
switch (entry.platform) {
case "VirusTotal":
response = await axios.get(`${entry.baseUrl}${entry.testEndpoint}`, {
headers: { "x-apikey": entry.key },
});
status = "OK";
httpStatus = response.status;
break;
case "OpenWeatherMap":
response = await axios.get(
`${entry.baseUrl}${entry.testEndpoint.replace("{key}", entry.key)}`,
{ headers: {} }
);
status = "OK";
httpStatus = response.status;
break;
case "AbuseIPDB":
response = await axios.get(`${entry.baseUrl}${entry.testEndpoint}`, {
headers: { Key: entry.key, Accept: "application/json" },
});
status = "OK";
httpStatus = response.status;
break;
default:
status = "Unknown Platform";
httpStatus = null;
}
} catch (err) {
httpStatus = err.response?.status || null;
if (httpStatus === 401 || httpStatus === 403) {
status = "Error: Authentication Failed";
errorMessage = err.response?.data?.error?.message || "Invalid or unauthorized API key";
} else if (httpStatus === 429) {
status = "Error: Rate Limit Exceeded";
errorMessage = err.response?.data?.error?.message || "Too many requests";
} else {
status = `Error: Request Failed`;
errorMessage = err.response?.data?.error?.message || err.message;
}
}
requestCount++; // Increment API request counter
const responseTime = Math.round(performance.now() - startTime);
const now = new Date().toISOString();
const result = {
platform: entry.platform,
status,
httpStatus: httpStatus || "N/A",
responseTime: responseTime, // Store as number, not string
errorMessage: errorMessage || null,
timestamp: now,
};
console.log(
`[${result.timestamp}] ${result.platform}: ${result.status} | HTTP: ${result.httpStatus} | Response Time: ${result.responseTime}ms${
result.errorMessage ? ` | Error: ${result.errorMessage}` : ""
}`
);
return result;
}
// --- Run Checks at Interval ---
let lastCheckTime = null;
let healthResults = [];
// async function runChecks() {
// const currentTime = performance.now();
// const intervalSinceLastCheck = lastCheckTime ? Math.round(currentTime - lastCheckTime) : "Initial";
// lastCheckTime = currentTime;
// console.log(`\n=== Checking API Keys (Interval: ${intervalSinceLastCheck}ms) ===`);
// const cycleStartTime = performance.now();
// const results = await Promise.all(keys.map(checkKeyStatus));
// healthResults = results; // Store latest results for /health endpoint
// historicalResults.push(...results); // Store in historical data
// // Trim historical data to last 30 days (optional, to manage memory)
// const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
// historicalResults = historicalResults.filter(r => new Date(r.timestamp) >= thirtyDaysAgo);
// const cycleTime = Math.round(performance.now() - cycleStartTime);
// console.log(`API Health Check completed in ${cycleTime}ms`);
// }
async function runChecks() {
const currentTime = performance.now();
const intervalSinceLastCheck = lastCheckTime ? Math.round(currentTime - lastCheckTime) : "Initial";
lastCheckTime = currentTime;
console.log(`\n=== Checking API Keys (Interval: ${intervalSinceLastCheck}ms) ===`);
const cycleStartTime = performance.now();
const results = await Promise.all(keys.map(checkKeyStatus));
healthResults = results; // Store latest results for /health endpoint
historicalResults.push(...results); // Store in historical data
// Trim to last 30 days
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
historicalResults = historicalResults.filter(r => new Date(r.timestamp) >= thirtyDaysAgo);
const cycleTime = Math.round(performance.now() - cycleStartTime);
console.log(`API Health Check completed in ${cycleTime}ms`);
// 🔥 Aggregated stats logging here
const avgResponseTime = healthResults.length > 0
? Math.round(healthResults.reduce((sum, r) => sum + r.responseTime, 0) / healthResults.length)
: 0;
const totalChecks = historicalResults.length;
const successfulChecks = historicalResults.filter(r => r.status === "OK").length;
const uptime = totalChecks > 0 ? ((successfulChecks / totalChecks) * 100).toFixed(2) : 0;
const apiRequests = requestCount;
console.log("\n--- Aggregated API Stats ---");
console.log(`Uptime: ${uptime}%`);
console.log(`Average Response Time: ${avgResponseTime} ms`);
console.log(`Total API Checks Today: ${apiRequests}`);
console.log("----------------------------");
}
// --- Express Server ---
const app = express();
app.use(cors());
app.use(express.json());
// Health endpoint
app.get('/health', (req, res) => {
// Convert responseTime back to string format for frontend compatibility
const formattedResults = healthResults.map(result => ({
...result,
responseTime: `${result.responseTime}ms`
}));
res.json(formattedResults);
});
// Stats endpoint for average response time, uptime, and API requests
app.get('/stats', (req, res) => {
// Calculate average response time (using numeric values)
const avgResponseTime = healthResults.length > 0
? Math.round(
healthResults.reduce((sum, r) => sum + r.responseTime, 0) / healthResults.length
)
: 0;
// Calculate uptime (percentage of successful checks in historical data)
const totalChecks = historicalResults.length;
const successfulChecks = historicalResults.filter(r => r.status === "OK").length;
const uptime = totalChecks > 0 ? ((successfulChecks / totalChecks) * 100).toFixed(2) : 0;
// Get API request count
const apiRequests = requestCount;
res.json({
avgResponseTime, // Return as number
uptime: parseFloat(uptime), // Return as number
apiRequests, // Return as number
});
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
const interval = setInterval(runChecks, 40000); // Every 40 seconds
runChecks(); // Run immediately on start
// Optional: Graceful shutdown
process.on("SIGINT", () => {
console.log("Stopping API checks...");
clearInterval(interval);
process.exit(0);
});