-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
538 lines (495 loc) · 20 KB
/
index.html
File metadata and controls
538 lines (495 loc) · 20 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
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
<!DOCTYPE html>
<html>
<head>
<title>EC2 Control Panel</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background: linear-gradient(135deg, #232526 0%, #ff9966 100%);
min-height: 100vh;
margin: 0;
font-family: 'Segoe UI', Arial, sans-serif;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(30, 30, 40, 0.45);
border-radius: 22px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.25);
padding: 2.5rem 2rem 2rem 2rem;
max-width: 420px;
width: 100%;
text-align: center;
color: #fff;
backdrop-filter: blur(12px);
border: 1.5px solid rgba(255, 255, 255, 0.18);
}
h2 {
margin-bottom: 1.5rem;
color: #ff9966;
letter-spacing: 1px;
font-weight: 800;
font-family: 'Segoe UI', 'Fira Mono', 'Consolas', monospace;
text-shadow: 0 2px 8px #23252644;
}
label {
display: block;
margin: 1.2rem 0 0.5rem 0;
font-size: 1.1rem;
color: #ff5e62;
font-weight: 600;
letter-spacing: 0.5px;
}
select {
width: 90%;
padding: 0.6rem;
border-radius: 8px;
border: none;
margin-bottom: 0.8rem;
font-size: 1rem;
background: rgba(255,255,255,0.85);
color: #ff5e62;
font-weight: 700;
box-shadow: 0 2px 8px rgba(255, 94, 98, 0.08);
outline: none;
transition: box-shadow 0.2s, background 0.2s;
}
select:focus {
box-shadow: 0 0 0 2px #ff9966;
background: #fff;
}
button {
background: linear-gradient(90deg, #ff9966 0%, #ff5e62 100%);
color: #fff;
border: none;
border-radius: 8px;
padding: 0.7rem 1.5rem;
margin: 0.5rem 0.3rem;
font-size: 1rem;
font-weight: 700;
cursor: pointer;
box-shadow: 0 2px 8px rgba(255, 94, 98, 0.12);
transition: background 0.2s, transform 0.2s;
letter-spacing: 0.5px;
}
button:hover {
background: linear-gradient(90deg, #ff5e62 0%, #ff9966 100%);
transform: translateY(-2px) scale(1.04);
}
#result {
margin-top: 1.5rem;
background: rgba(255, 255, 255, 0.10);
border-radius: 8px;
padding: 1rem;
color: #fff;
font-size: 1.05rem;
min-height: 2.2rem;
word-break: break-all;
font-family: 'Fira Mono', 'Consolas', monospace;
letter-spacing: 0.5px;
}
.spinner-overlay {
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(255, 153, 102, 0.12);
z-index: 1000;
display: none;
align-items: center;
justify-content: center;
}
.spinner {
border: 6px solid #fff;
border-top: 6px solid #ff5e62;
border-radius: 50%;
width: 48px;
height: 48px;
animation: spin 1s linear infinite;
margin: auto;
}
@keyframes spin {
0% { transform: rotate(0deg);}
100% { transform: rotate(360deg);}
}
#instance-group {
display: none;
}
@media (max-width: 500px) {
.container {
padding: 1.2rem 0.5rem 1rem 0.5rem;
max-width: 98vw;
}
h2 {
font-size: 1.2rem;
}
button {
width: 90%;
margin: 0.5rem 0;
}
}
/* DevOps accent bar */
.accent-bar {
width: 60px;
height: 6px;
border-radius: 3px;
margin: 0 auto 1.5rem auto;
background: linear-gradient(90deg, #ff9966 0%, #ff5e62 100%);
box-shadow: 0 2px 8px #ff5e6244;
}
/* Sign out button */
.user-controls {
position: absolute;
top: 15px;
right: 15px;
display: flex;
align-items: center;
}
.sign-out-btn {
background: rgba(255, 255, 255, 0.2);
color: #fff;
border: none;
border-radius: 6px;
padding: 6px 12px;
font-size: 0.9rem;
cursor: pointer;
transition: background 0.2s, transform 0.2s;
}
.sign-out-btn:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
}
@media (max-width: 500px) {
.user-controls {
top: 10px;
right: 10px;
}
.sign-out-btn {
font-size: 0.8rem;
padding: 4px 8px;
}
}
</style>
</head>
<body>
<div class="spinner-overlay" id="spinner-overlay">
<div class="spinner"></div>
</div>
<div class="user-controls">
<button class="sign-out-btn" onclick="signOut()">Sign Out</button>
</div>
<div class="container">
<div class="accent-bar"></div>
<h2>EC2 Instance Control</h2>
<label for="region">Region:</label>
<select id="region">
<option value="">Select region</option>
</select>
<div id="instance-group" style="display:none;">
<label for="instance">Instance:</label>
<select id="instance"></select>
</div>
<div>
<button onclick="startInstance()">Start</button>
<button onclick="stopInstance()">Stop</button>
<button onclick="getIPs()">Get IPs</button>
</div>
<div id="result"></div>
</div>
<script>
const apiBase = "YOUR_API_ENDPOINT";
let idToken = null;
let tokenExpiration = null;
// Get authentication token from localStorage
function getAuthToken() {
const token = localStorage.getItem('id_token');
const expiry = localStorage.getItem('token_expiry');
// Check if token is expired
if (token && expiry) {
const expiryTime = parseInt(expiry);
// If token expires in less than 5 minutes, consider it expired
if (Date.now() >= expiryTime - (5 * 60 * 1000)) {
console.log("Token expired or about to expire, clearing authentication");
localStorage.removeItem('id_token');
localStorage.removeItem('access_token');
localStorage.removeItem('token_expiry');
return null;
}
}
return token;
}
function showSpinner(show) {
document.getElementById("spinner-overlay").style.display = show ? "flex" : "none";
}
// Check if the user is authenticated
function checkAuthentication() {
idToken = getAuthToken();
if (!idToken) {
document.getElementById("result").innerText = "Authentication required. Redirecting...";
// Redirect to Cognito login
window.location.href = "https://YOUR_COGNITO_LOGIN_URL/login?" +
"client_id=CLIENT_ID&" +
"response_type=token&" +
"scope=email+openid+profile&" +
"redirect_uri=https://YOUR_URL/callback.html";
return false;
}
return true;
}
async function fetchRegions() {
if (!checkAuthentication()) return;
showSpinner(true);
const sel = document.getElementById("region");
sel.innerHTML = '<option value="">Select region</option>';
document.getElementById("instance-group").style.display = "none";
document.getElementById("result").innerText = "";
try {
const res = await fetch(apiBase + "/regions", {
headers: {
"Authorization": `Bearer ${idToken}`
}
});
const rawData = await res.text();
// Parse the response based on what we received
let regions = [];
try {
const data = JSON.parse(rawData);
if (Array.isArray(data)) {
regions = data;
} else if (data && data.body) {
if (typeof data.body === 'string') {
regions = JSON.parse(data.body);
} else if (Array.isArray(data.body)) {
regions = data.body;
}
}
} catch (e) {
// Silent error handling
}
// Sort regions alphabetically
regions.sort();
regions.forEach(r => {
const opt = document.createElement("option");
opt.value = r;
opt.text = r;
sel.appendChild(opt);
});
} catch (e) {
sel.innerHTML = '<option value="">Error loading regions</option>';
document.getElementById("result").innerText = "Error connecting to API. Please check your authentication.";
}
showSpinner(false);
}
async function fetchInstances(region) {
if (!checkAuthentication()) return;
const sel = document.getElementById("instance");
const group = document.getElementById("instance-group");
sel.innerHTML = "<option>Loading...</option>";
group.style.display = "none";
if (!region) {
sel.innerHTML = "";
return;
}
showSpinner(true);
try {
const res = await fetch(apiBase + "/instances?region=" + encodeURIComponent(region), {
headers: {
"Authorization": `Bearer ${idToken}`
}
});
const rawData = await res.text();
// Parse the response based on what we received
let instances = [];
try {
const data = JSON.parse(rawData);
if (Array.isArray(data)) {
instances = data;
} else if (data && data.body) {
if (typeof data.body === 'string') {
instances = JSON.parse(data.body);
} else if (Array.isArray(data.body)) {
instances = data.body;
}
}
} catch (e) {
// Silent error handling
}
sel.innerHTML = "";
if (instances.length === 0) {
sel.innerHTML = "<option>No instances found</option>";
} else {
instances.forEach(i => {
const opt = document.createElement("option");
opt.value = i.InstanceId;
opt.text = `${i.InstanceId} - ${i.InstanceType} ${i.Name ? `(${i.Name})` : ''} [${i.State}]`;
sel.appendChild(opt);
});
}
group.style.display = "block";
} catch (e) {
sel.innerHTML = "<option>Error loading instances</option>";
document.getElementById("result").innerText = "Error connecting to API. Please check your authentication.";
}
showSpinner(false);
}
async function startInstance() {
if (!checkAuthentication()) return;
showSpinner(true);
document.getElementById("result").innerText = "Starting instance...";
const region = document.getElementById("region").value;
const instance_id = document.getElementById("instance").value;
if (!region || !instance_id) {
document.getElementById("result").innerText = "Please select region and instance.";
showSpinner(false);
return;
}
try {
const res = await fetch(apiBase + "/start", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${idToken}`
},
body: JSON.stringify({region, instance_id})
});
const data = await res.json();
let resultText = data.message || "Unknown response";
// Add IP information if available
if (data.public_ip || data.private_ip) {
resultText += `\nPublic IP: ${data.public_ip || "N/A"}`;
resultText += `\nPrivate IP: ${data.private_ip || "N/A"}`;
}
document.getElementById("result").innerText = resultText;
} catch (e) {
document.getElementById("result").innerText = "Error starting instance. Please check your authentication.";
}
showSpinner(false);
}
async function stopInstance() {
if (!checkAuthentication()) return;
showSpinner(true);
document.getElementById("result").innerText = "Stopping instance...";
const region = document.getElementById("region").value;
const instance_id = document.getElementById("instance").value;
if (!region || !instance_id) {
document.getElementById("result").innerText = "Please select region and instance.";
showSpinner(false);
return;
}
try {
const res = await fetch(apiBase + "/stop", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${idToken}`
},
body: JSON.stringify({region, instance_id})
});
const data = await res.json();
document.getElementById("result").innerText = data.message || data.error || "Unknown response";
} catch (e) {
document.getElementById("result").innerText = "Error stopping instance. Please check your authentication.";
}
showSpinner(false);
}
async function getIPs() {
if (!checkAuthentication()) return;
showSpinner(true);
document.getElementById("result").innerText = "Fetching IP addresses...";
const region = document.getElementById("region").value;
const instance_id = document.getElementById("instance").value;
if (!region || !instance_id) {
document.getElementById("result").innerText = "Please select region and instance.";
showSpinner(false);
return;
}
const payload = {region, instance_id};
console.log("Sending payload to /getip:", payload);
try {
const res = await fetch(apiBase + "/getip", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${idToken}`
},
body: JSON.stringify(payload)
});
if (!res.ok) {
const errorText = await res.text();
console.error(`Error response (${res.status}):`, errorText);
document.getElementById("result").innerText =
`Error fetching IPs: Server returned ${res.status} ${res.statusText}`;
showSpinner(false);
return;
}
const responseText = await res.text();
console.log("Raw response:", responseText);
let data;
try {
data = JSON.parse(responseText);
} catch (parseError) {
console.error("JSON parse error:", parseError);
document.getElementById("result").innerText =
"Error: Invalid response format from server";
showSpinner(false);
return;
}
if (data.public_ip || data.private_ip || data.state) {
let resultText = "";
if (data.state) {
resultText += `Instance State: ${data.state}\n`;
}
resultText += `Public IP: ${data.public_ip || "N/A"}\n`;
resultText += `Private IP: ${data.private_ip || "N/A"}`;
document.getElementById("result").innerText = resultText;
} else if (data.error) {
document.getElementById("result").innerText = `Error: ${data.error}`;
} else {
document.getElementById("result").innerText = "No IP information available";
}
} catch (e) {
console.error("Fetch error:", e);
document.getElementById("result").innerText =
`Error fetching IPs: ${e.message || "Unknown error"}`;
}
showSpinner(false);
}
// Sign out function
function signOut() {
// Clear local storage tokens
localStorage.removeItem('id_token');
localStorage.removeItem('access_token');
localStorage.removeItem('token_expiry');
// Clear cookies
document.cookie = "CognitoIdentityServiceProvider.idToken=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; secure; domain=" + window.location.hostname;
document.cookie = "CognitoIdentityServiceProvider.accessToken=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; secure; domain=" + window.location.hostname;
// Reset token variables
idToken = null;
tokenExpiration = null;
// Show message
document.getElementById("result").innerText = "You have been signed out. Redirecting...";
// Redirect to Cognito logout endpoint
setTimeout(function() {
window.location.href = "https://COGNITO_LOGOUT_URL/logout?" +
"client_id=CLIENT_ID&" +
"logout_uri=" + encodeURIComponent(window.location.origin);
}, 1000);
}
document.getElementById("region").addEventListener("change", function() {
const region = this.value;
if (region) {
fetchInstances(region);
} else {
document.getElementById("instance-group").style.display = "none";
document.getElementById("instance").innerHTML = "";
}
});
window.onload = function() {
// Check authentication before loading
if (checkAuthentication()) {
fetchRegions();
}
};
</script>
</body>
</html>