forked from presswizards/cloudflare-waf-rules-wizard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy-status.php
More file actions
669 lines (594 loc) · 20.9 KB
/
proxy-status.php
File metadata and controls
669 lines (594 loc) · 20.9 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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
/**
* DNS Proxy Status page
*/
// Direct access protection
defined( 'CLOUDFLARE_API_KEY' ) || exit( 'No direct script access allowed' );
?>
<h2>Cloudflare DNS Proxy Status</h2>
<div id="ajax-message" class="notice hidden">
<p id="ajax-message-text"></p>
</div>
<div class="backup-controls">
<button id="create-backup-btn" class="button-primary">
<span class="btn-icon">💾</span> Create Backup
</button>
<button id="disable-all-btn" class="button-danger" disabled>
<span class="btn-icon">🚫</span> Disable All Proxy
</button>
<button id="restore-backup-btn" class="button-secondary">
<span class="btn-icon">⏮</span> Restore Backup
</button>
</div>
<div id="restore-modal" class="modal hidden">
<div class="modal-content">
<div class="modal-header">
<h3>Restore from Backup</h3>
<button class="modal-close">×</button>
</div>
<div class="modal-body">
<p>Select a backup to restore:</p>
<div id="backup-list" class="backup-list">
<p class="loading-message">Loading backups...</p>
</div>
</div>
</div>
</div>
<?php
$zones = pw_get_cloudflare_zones(
CLOUDFLARE_ACCOUNT_IDS,
CLOUDFLARE_API_KEY,
CLOUDFLARE_EMAIL
);
foreach ( $zones as $zone ) :
$zone_id = $zone['id'];
$dns_records = pw_get_zone_dns_records(
$zone_id,
CLOUDFLARE_API_KEY,
CLOUDFLARE_EMAIL
);
?>
<div class="zone-section" data-zone-id="<?php echo htmlspecialchars( $zone_id ); ?>">
<div class="zone-header">
<div class="zone-info">
<h3><?php echo htmlspecialchars( $zone['name'] ); ?></h3>
<p class="zone-id-display">Zone ID: <code><?php echo htmlspecialchars( $zone_id ); ?></code></p>
</div>
<div class="zone-controls">
<button class="zone-disable-btn button-danger-small" data-zone-id="<?php echo htmlspecialchars( $zone_id ); ?>" data-zone-name="<?php echo htmlspecialchars( $zone['name'] ); ?>" disabled>
<span class="btn-icon">🚫</span> Disable All
</button>
<button class="zone-restore-btn button-secondary-small" data-zone-id="<?php echo htmlspecialchars( $zone_id ); ?>" data-zone-name="<?php echo htmlspecialchars( $zone['name'] ); ?>">
<span class="btn-icon">⏮</span> Restore
</button>
</div>
</div>
<?php if ( empty( $dns_records ) ) : ?>
<p class="notice notice-warning">No proxiable DNS records found for this zone.</p>
<?php else : ?>
<table class="dns-records-table">
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>Content</th>
<th>Proxy Status</th>
</tr>
</thead>
<tbody>
<?php foreach ( $dns_records as $record ) : ?>
<tr data-zone-id="<?php echo htmlspecialchars( $zone_id ); ?>" data-record-id="<?php echo htmlspecialchars( $record['id'] ); ?>">
<td class="record-type">
<span class="type-badge type-<?php echo strtolower( $record['type'] ); ?>">
<?php echo htmlspecialchars( $record['type'] ); ?>
</span>
</td>
<td class="record-name"><?php echo htmlspecialchars( $record['name'] ); ?></td>
<td class="record-content"><?php echo htmlspecialchars( $record['content'] ); ?></td>
<td class="proxy-cell">
<button class="proxy-toggle-btn <?php echo $record['proxied'] ? 'proxied' : 'not-proxied'; ?>"
data-record-id="<?php echo htmlspecialchars( $record['id'] ); ?>"
data-zone-id="<?php echo htmlspecialchars( $zone_id ); ?>"
data-proxied="<?php echo $record['proxied'] ? 'true' : 'false'; ?>"
data-record-type="<?php echo htmlspecialchars( $record['type'] ); ?>"
data-record-name="<?php echo htmlspecialchars( $record['name'] ); ?>"
data-record-content="<?php echo htmlspecialchars( $record['content'] ); ?>">
<span class="toggle-status"><?php echo $record['proxied'] ? 'ON' : 'OFF'; ?></span>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php endforeach; ?>
<div class="help">
<h2>DNS Proxy Status</h2>
<p>This page shows all A and CNAME records for your zones. Click the toggle button to enable or disable Cloudflare proxy for individual records.</p>
<p><strong>Proxied (ON):</strong> Traffic is routed through Cloudflare's network, hiding your origin IP and enabling Cloudflare features like DDoS protection and caching.</p>
<p><strong>Not Proxied (OFF):</strong> DNS only mode - your origin server's IP is exposed and traffic goes directly to your server.</p>
<p><a target="_blank" href="https://developers.cloudflare.com/dns/manage-dns-records/reference/proxied-dns-records/">Developer Docs</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const toggleButtons = document.querySelectorAll('.proxy-toggle-btn');
const messageBox = document.getElementById('ajax-message');
const messageText = document.getElementById('ajax-message-text');
toggleButtons.forEach(button => {
button.addEventListener('click', function() {
const recordId = this.dataset.recordId;
const zoneId = this.dataset.zoneId;
const isProxied = this.dataset.proxied === 'true';
const recordName = this.dataset.recordName;
const recordType = this.dataset.recordType;
const recordContent = this.dataset.recordContent;
const newProxiedValue = !isProxied;
// Show confirmation
const action = newProxiedValue ? 'ENABLE' : 'DISABLE';
const warning = newProxiedValue
? 'This will route traffic through Cloudflare.'
: 'This will expose your origin server IP address.';
if (!confirm(`${action} proxy for ${recordName}?\n\n${warning}`)) {
return;
}
// Show loading state
this.classList.add('loading');
this.disabled = true;
const statusSpan = this.querySelector('.toggle-status');
const originalText = statusSpan.textContent;
statusSpan.textContent = '...';
// Prepare form data
const formData = new FormData();
formData.append('zone_id', zoneId);
formData.append('record_id', recordId);
formData.append('setting', 'dns_record_proxy');
formData.append('value', newProxiedValue ? 'true' : 'false');
formData.append('record_type', recordType);
formData.append('record_name', recordName);
formData.append('record_content', recordContent);
// Send AJAX request
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
this.classList.remove('loading');
this.disabled = false;
if (data.success) {
// Update button state
this.dataset.proxied = newProxiedValue ? 'true' : 'false';
this.className = 'proxy-toggle-btn ' + (newProxiedValue ? 'proxied' : 'not-proxied');
statusSpan.textContent = newProxiedValue ? 'ON' : 'OFF';
// Show success message
messageBox.className = 'notice notice-success';
messageText.textContent = data.message;
} else {
// Restore original state
statusSpan.textContent = originalText;
messageBox.className = 'notice notice-error';
messageText.textContent = data.message;
}
// Hide message after some time
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
this.classList.remove('loading');
this.disabled = false;
statusSpan.textContent = originalText;
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
});
});
// Backup and Restore functionality
const createBackupBtn = document.getElementById('create-backup-btn');
const disableAllBtn = document.getElementById('disable-all-btn');
const restoreBackupBtn = document.getElementById('restore-backup-btn');
const restoreModal = document.getElementById('restore-modal');
const modalClose = document.querySelector('.modal-close');
const backupList = document.getElementById('backup-list');
// Load backups and update UI state
function loadBackups() {
const formData = new FormData();
formData.append('setting', 'get_backups');
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update disable all button state
disableAllBtn.disabled = !data.can_disable_all;
if (data.can_disable_all) {
const timeRemaining = Math.round((data.latest_backup_time + 3600 - Math.floor(Date.now() / 1000)) / 60);
disableAllBtn.title = `Backup created recently (${timeRemaining} minutes remaining)`;
} else {
disableAllBtn.title = 'Create a backup first (valid for 1 hour)';
}
// Update zone-level button states
updateZoneButtonStates(data.can_disable_all);
// Populate backup list
if (data.backups.length === 0) {
backupList.innerHTML = '<p class="no-backups">No backups found</p>';
} else {
let html = '<div class="backup-items">';
data.backups.forEach(backup => {
const date = new Date(backup.timestamp * 1000);
const dateStr = date.toLocaleString();
html += `
<div class="backup-item">
<div class="backup-info">
<strong>${dateStr}</strong>
<span class="backup-zones">${backup.zones} zone(s)</span>
</div>
<button class="button-secondary restore-btn" data-filename="${backup.filename}">
Restore
</button>
</div>
`;
});
html += '</div>';
backupList.innerHTML = html;
// Add click handlers to restore buttons
document.querySelectorAll('.restore-btn').forEach(btn => {
btn.addEventListener('click', function() {
const filename = this.dataset.filename;
if (confirm(`Restore proxy settings from this backup?\n\nThis will update DNS records to match the backup state.`)) {
restoreBackup(filename);
}
});
});
}
}
})
.catch(error => {
console.error('Error loading backups:', error);
backupList.innerHTML = '<p class="error-message">Error loading backups</p>';
});
}
// Create backup
createBackupBtn.addEventListener('click', function() {
if (!confirm('Create a backup of current DNS proxy settings for all zones?')) {
return;
}
this.disabled = true;
this.textContent = 'Creating...';
const formData = new FormData();
formData.append('setting', 'create_backup');
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
this.disabled = false;
this.innerHTML = '<span class="btn-icon">💾</span> Create Backup';
if (data.success) {
messageBox.className = 'notice notice-success';
messageText.textContent = data.message;
loadBackups(); // Refresh backup list and button states
} else {
messageBox.className = 'notice notice-error';
messageText.textContent = data.message || 'Failed to create backup';
}
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
this.disabled = false;
this.innerHTML = '<span class="btn-icon">💾</span> Create Backup';
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
});
// Disable all proxy
disableAllBtn.addEventListener('click', function() {
if (!confirm('DISABLE proxy for ALL DNS records in ALL zones?\n\nThis will expose your origin server IP addresses.\n\nThis action requires a recent backup (within 1 hour).')) {
return;
}
this.disabled = true;
this.textContent = 'Disabling...';
const formData = new FormData();
formData.append('setting', 'disable_all');
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
this.innerHTML = '<span class="btn-icon">🚫</span> Disable All Proxy';
if (data.success) {
messageBox.className = 'notice notice-success';
messageText.textContent = data.message;
// Reload page to show updated status
setTimeout(() => {
location.reload();
}, 2000);
} else {
this.disabled = false;
messageBox.className = 'notice notice-error';
messageText.textContent = data.message || 'Failed to disable all';
}
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
this.disabled = false;
this.innerHTML = '<span class="btn-icon">🚫</span> Disable All Proxy';
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
});
// Show restore modal
restoreBackupBtn.addEventListener('click', function() {
restoreModal.classList.remove('hidden');
loadBackups();
});
// Close modal
modalClose.addEventListener('click', function() {
restoreModal.classList.add('hidden');
});
// Close modal on outside click
restoreModal.addEventListener('click', function(e) {
if (e.target === restoreModal) {
restoreModal.classList.add('hidden');
}
});
// Restore from backup
function restoreBackup(filename) {
const formData = new FormData();
formData.append('setting', 'restore_backup');
formData.append('filename', filename);
// Show loading in modal
backupList.innerHTML = '<p class="loading-message">Restoring...</p>';
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
messageBox.className = 'notice notice-success';
messageText.textContent = data.message;
restoreModal.classList.add('hidden');
// Reload page to show updated status
setTimeout(() => {
location.reload();
}, 2000);
} else {
messageBox.className = 'notice notice-error';
messageText.textContent = data.message || 'Failed to restore backup';
loadBackups(); // Reload backup list
}
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
loadBackups(); // Reload backup list
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
}
// Initial load of backups to set button states
loadBackups();
// Zone-level controls
const zoneDisableButtons = document.querySelectorAll('.zone-disable-btn');
const zoneRestoreButtons = document.querySelectorAll('.zone-restore-btn');
// Update zone button states based on backup availability
function updateZoneButtonStates(canDisableAll) {
zoneDisableButtons.forEach(btn => {
btn.disabled = !canDisableAll;
if (canDisableAll) {
btn.title = 'Disable all proxy for this zone';
} else {
btn.title = 'Create a backup first (valid for 1 hour)';
}
});
}
// Disable all for a specific zone
zoneDisableButtons.forEach(btn => {
btn.addEventListener('click', function() {
const zoneId = this.dataset.zoneId;
const zoneName = this.dataset.zoneName;
if (!confirm(`DISABLE proxy for ALL DNS records in ${zoneName}?\n\nThis will expose your origin server IP address for this zone.\n\nThis action requires a recent backup (within 1 hour).`)) {
return;
}
this.disabled = true;
const originalHtml = this.innerHTML;
this.textContent = 'Disabling...';
const formData = new FormData();
formData.append('setting', 'disable_zone');
formData.append('zone_id', zoneId);
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
this.innerHTML = originalHtml;
this.disabled = false;
if (data.success) {
messageBox.className = 'notice notice-success';
messageText.textContent = `${zoneName}: ${data.message}`;
// Update all toggles in this zone to OFF
const zoneSection = this.closest('.zone-section');
const toggleButtons = zoneSection.querySelectorAll('.proxy-toggle-btn');
toggleButtons.forEach(btn => {
if (btn.dataset.proxied === 'true') {
btn.dataset.proxied = 'false';
btn.classList.remove('proxied');
btn.classList.add('not-proxied');
const statusSpan = btn.querySelector('.toggle-status');
if (statusSpan) {
statusSpan.textContent = 'OFF';
}
}
});
} else {
messageBox.className = 'notice notice-error';
messageText.textContent = data.message || 'Failed to disable zone';
}
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
this.disabled = false;
this.innerHTML = originalHtml;
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
});
});
// Restore a specific zone from backup
zoneRestoreButtons.forEach(btn => {
btn.addEventListener('click', function() {
const zoneId = this.dataset.zoneId;
const zoneName = this.dataset.zoneName;
// Get available backups
const formData = new FormData();
formData.append('setting', 'get_backups');
fetch('ajax-handler.php', {
method: 'POST',
body: formData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
if (!data.success || data.backups.length === 0) {
alert('No backups available. Please create a backup first.');
return;
}
// Show backup selection modal
let backupOptions = data.backups.map((backup, index) => {
const date = new Date(backup.timestamp * 1000);
const dateStr = date.toLocaleString();
return `${index + 1}. ${dateStr} (${backup.zones} zones)`;
}).join('\n');
const selection = prompt(`Select backup to restore ${zoneName} from:\n\n${backupOptions}\n\nEnter backup number (1-${data.backups.length}):`);
if (selection === null) return; // User cancelled
const backupIndex = parseInt(selection) - 1;
if (isNaN(backupIndex) || backupIndex < 0 || backupIndex >= data.backups.length) {
alert('Invalid selection');
return;
}
const selectedBackup = data.backups[backupIndex];
if (!confirm(`Restore ${zoneName} from backup created on ${new Date(selectedBackup.timestamp * 1000).toLocaleString()}?`)) {
return;
}
// Perform restore
this.disabled = true;
const originalHtml = this.innerHTML;
this.textContent = 'Restoring...';
const restoreFormData = new FormData();
restoreFormData.append('setting', 'restore_zone');
restoreFormData.append('zone_id', zoneId);
restoreFormData.append('filename', selectedBackup.filename);
fetch('ajax-handler.php', {
method: 'POST',
body: restoreFormData,
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(restoreData => {
this.innerHTML = originalHtml;
this.disabled = false;
if (restoreData.success) {
messageBox.className = 'notice notice-success';
messageText.textContent = `${zoneName}: ${restoreData.message}`;
// Update all toggles in this zone based on restored state
const zoneSection = this.closest('.zone-section');
// Get the restored state from the response
if (restoreData.records) {
restoreData.records.forEach(record => {
const toggleBtn = zoneSection.querySelector(`button[data-record-id="${record.id}"]`);
if (toggleBtn) {
toggleBtn.dataset.proxied = record.proxied ? 'true' : 'false';
toggleBtn.classList.toggle('proxied', record.proxied);
toggleBtn.classList.toggle('not-proxied', !record.proxied);
const statusSpan = toggleBtn.querySelector('.toggle-status');
if (statusSpan) {
statusSpan.textContent = record.proxied ? 'ON' : 'OFF';
}
}
});
}
} else {
messageBox.className = 'notice notice-error';
messageText.textContent = restoreData.message || 'Failed to restore zone';
}
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
})
.catch(error => {
console.error('Error:', error);
this.disabled = false;
this.innerHTML = originalHtml;
messageBox.className = 'notice notice-error';
messageText.textContent = 'Network error occurred';
setTimeout(() => {
messageBox.className = 'notice hidden';
}, 7 * 1000);
});
})
.catch(error => {
console.error('Error fetching backups:', error);
alert('Error loading backups');
});
});
});
// Initial load of backups (which will also update zone button states)
loadBackups();
});
</script>