This commit is contained in:
devdatt 2025-12-27 09:31:55 +05:30
parent 2a518fdd8c
commit 0f11c4c0a3
3 changed files with 114 additions and 134 deletions

View File

@ -1,179 +1,149 @@
<?php include 'header.php' ?>
<?php
exec("sudo chmod 444 /sys/class/dmi/id/product_uuid");
$file = __DIR__ . '/firewall.json';
$rules = [];
$data = [
'80' => '',
'443' => '',
'1935' => '',
'1937' => ''
];
if (file_exists($file)) {
$json = file_get_contents($file);
$rules = json_decode($json, true) ?: [];
if (file_exists($jsonFile)) {
$stored = json_decode(file_get_contents($jsonFile), true);
if (is_array($stored)) {
$data = array_merge($data, $stored);
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$rules = [];
if (!empty($_POST['ip_version'])) {
foreach ($_POST['ip_version'] as $i => $v) {
$rules[] = [
'ip_version' => $_POST['ip_version'][$i] ?? '',
'ip_address' => $_POST['ip_address'][$i] ?? '',
'port' => $_POST['port'][$i] ?? '',
'protocol' => $_POST['protocol'][$i] ?? '',
'description' => $_POST['description'][$i] ?? ''
];
}
foreach ($data as $port => $val) {
$data[$port] = trim($_POST["port_$port"] ?? '');
}
file_put_contents($file, json_encode($rules, JSON_PRETTY_PRINT));
file_put_contents($jsonFile, json_encode($data, JSON_PRETTY_PRINT));
}
?>
<style>
body {
font-family: Arial, sans-serif;
font-family: system-ui, sans-serif;
background: #f5f7fa;
padding: 20px;
}
.container {
max-width: 1100px;
margin: auto;
max-width: 520px;
margin: 40px auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
padding: 24px;
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0, 0, 0, .08);
}
table {
h2 {
margin-bottom: 20px;
font-size: 20px;
}
.row {
margin-bottom: 16px;
}
label {
display: block;
font-weight: 600;
margin-bottom: 6px;
}
input[type=text] {
width: 100%;
border-collapse: collapse;
}
th,
td {
padding: 10px;
border-bottom: 1px solid #ddd;
border-radius: 6px;
border: 1px solid #ccc;
font-size: 14px;
}
th {
background: #f0f2f5;
input[type=text]:invalid {
border-color: #d33;
}
input,
select {
width: 100%;
padding: 6px;
small {
color: #666;
}
button {
padding: 6px 12px;
margin-top: 20px;
padding: 12px 18px;
border: none;
border-radius: 4px;
border-radius: 8px;
background: #2563eb;
color: #fff;
font-size: 15px;
cursor: pointer;
}
.btn-add {
background: #2e7d32;
color: #fff;
}
.btn-remove {
background: #c62828;
color: #fff;
}
.btn-save {
background: #1565c0;
color: #fff;
margin-top: 15px;
}
.actions {
text-align: right;
button:hover {
background: #1e4ed8;
}
</style>
<script>
function validateIPs(input) {
if (!input.value.trim()) return true;
const ips = input.value.split(',').map(i => i.trim());
const ipv4 =
/^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
const ipv6 =
/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::1|::)$/;
for (const ip of ips) {
if (!(ipv4.test(ip) || ipv6.test(ip))) {
return false;
}
}
return true;
}
function attachValidation() {
document.querySelectorAll('input[type=text]').forEach(inp => {
inp.addEventListener('input', () => {
inp.setCustomValidity(
validateIPs(inp) ? '' : 'Invalid IPv4 or IPv6 address'
);
});
});
}
window.onload = attachValidation;
</script>
<div class="containerindex">
<div class="grid">
<div class="card wide">
<h2>Allow Rules</h2>
<h2>Firewall Allowed IPs</h2>
<form method="post">
<table id="rulesTable">
<thead>
<tr>
<th>IP Address</th>
<th>Port</th>
<th>Protocol</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $port => $value): ?>
<div class="row">
<label>Port <?= htmlspecialchars($port) ?></label>
<input
type="text"
name="port_<?= $port ?>"
value="<?= htmlspecialchars($value) ?>"
placeholder="IPv4, IPv6 (comma separated)">
<small>Example: 192.168.1.10, 2001:db8::1</small>
</div>
<?php endforeach; ?>
<?php if ($rules): foreach ($rules as $r): ?>
<tr>
<td><input type="text" name="ip_address[]" value="<?= htmlspecialchars($r['ip_address']) ?>"></td>
<td><input type="number" name="port[]" value="<?= htmlspecialchars($r['port']) ?>"></td>
<td>
<select name="protocol[]">
<option value="tcp" <?= $r['protocol'] == 'tcp' ? 'selected' : '' ?>>TCP</option>
<option value="udp" <?= $r['protocol'] == 'udp' ? 'selected' : '' ?>>UDP</option>
<option value="any" <?= $r['protocol'] == 'any' ? 'selected' : '' ?>>ANY</option>
</select>
</td>
<td><input type="text" name="description[]" value="<?= htmlspecialchars($r['description']) ?>"></td>
<td class="actions">
<button type="button" class="btn-remove" onclick="removeRow(this)">Remove</button>
</td>
</tr>
<?php endforeach;
else: ?>
<tr>
<td><input type="text" name="ip_address[]" placeholder="192.168.1.0/24 or 2001:db8::/64"></td>
<td><input type="text" name="port[]" placeholder="1-65535 or any"></td>
<td>
<select name="protocol[]">
<option value="tcp">TCP</option>
<option value="udp">UDP</option>
<option value="any">ANY</option>
</select>
</td>
<td><input type="text" name="description[]"></td>
<td class="actions">
<button type="button" class="btn-remove" onclick="removeRow(this)">Remove</button>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<br>
<button type="button" class="btn-add" onclick="addRow()">Add Rule</button>
<br><br>
<button type="submit" class="btn-save">Save Rules</button>
<br><br>
<br><br>
<button type="submit">Save Rules</button>
</form>
<br><br>
</div>
</div>
</div>
<script>
function addRow() {
const tbody = document.querySelector('#rulesTable tbody');
const row = tbody.rows[0].cloneNode(true);
row.querySelectorAll('input').forEach(i => i.value = '');
row.querySelectorAll('select').forEach(s => s.selectedIndex = 0);
tbody.appendChild(row);
}
function removeRow(btn) {
const tbody = document.querySelector('#rulesTable tbody');
if (tbody.rows.length > 1) {
btn.closest('tr').remove();
}
}
</script>
<?php include 'footer.php' ?>

View File

@ -1,4 +1,5 @@
<?php
exec("sudo chmod 444 /sys/class/dmi/id/product_uuid");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
switch ($_POST['action']) {

View File

@ -407,11 +407,20 @@ sudo systemctl status nginx.service --no-pager
sudo chmod 777 -R /var/www
sudo chown -R www-data:www-data /var/www
sudo ufw allow proto udp to 224.0.0.0/4
sudo ufw route allow proto udp to 224.0.0.0/4
sudo ufw deny out to 239.255.254.254 port 39000 proto udp
sudo systemctl daemon-reload
sudo chmod 444 /sys/class/dmi/id/product_uuid
sudo systemctl disable systemd-networkd-wait-online.service
sudo systemctl mask systemd-networkd-wait-online.service
sudo ufw default allow outgoing
sudo ufw default deny incoming
sudo ufw allow 1935
sudo ufw allow 1937
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow proto udp to 224.0.0.0/4
sudo ufw route allow proto udp to 224.0.0.0/4
sudo ufw deny out to 239.255.254.254 port 39000 proto udp
sudo ufw allow from 172.16.111.112 to 172.16.111.111 port 8080
sudo ufw --force enable