This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.php
109 lines (96 loc) · 3.27 KB
/
index.php
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
<?php
require_once __DIR__ . '/lib/SSH.php';
require_once __DIR__ . '/lib/Iptables.php';
$ssh = new SSH('192.168.20.127');
$ssh->setUsername('root');
$ssh->setPassword('root');
$iptables = new Iptables($ssh);
$iptables->setOnFly(FALSE);
$flashes = array();
$editDialogDisplayed = FALSE;
$editDialogAction = './index.php';
function buildQueryFromRule(\stdClass $rule, $table, $chain)
{
$rule = clone $rule;
$parameters = array();
$parameters['protocol'] = $rule->protocol;
$parameters['in'] = $rule->in;
$parameters['out'] = $rule->out;
$parameters['source'] = $rule->source;
$parameters['destination'] = $rule->destination;
$parameters['target'] = $rule->target;
if (preg_match('~--(d|s)port ([0-9:]+)~i', $rule->additional, $matches)) {
$parameters[$matches[1] . 'port'] = $matches[2];
$rule->additional = str_replace($matches[0], '', $rule->additional);
}
$parameters['additional'] = trim($rule->additional);
$parameters['table'] = $table;
$parameters['chain'] = $chain;
return http_build_query($parameters);
}
function buildRuleFromQuery(array $array)
{
$rule = new \stdClass();
$rule->in = isset($array['in']) ? $array['in'] : '';
$rule->out = isset($array['out']) ? $array['out'] : '';
$rule->source = isset($array['source']) ? $array['source'] : '';
$rule->destination = isset($array['destination']) ? $array['destination'] : '';
$rule->protocol = isset($array['protocol']) ? $array['protocol'] : '';
$rule->additional = isset($array['additional']) ? trim($array['additional']) : '';
if (isset($array['dport']) && $array['dport']) {
$rule->additional .= ' --dport ' . $array['dport'];
}
if (isset($array['sport']) && $array['sport']) {
$rule->additional .= ' --sport ' . $array['sport'];
}
$rule->target = isset($array['target']) ? $array['target'] : '';
return $rule;
}
if (isset($_GET['reload'])) {
session_start();
unset($_SESSION[Iptables::$sessionCacheKey]);
header('Location: index.php');
}
if (isset($_GET['export'])) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="iptables-export.conf"');
echo $iptables->export();
exit;
}
if (isset($_GET['import'])) {
if (!isset($_FILES['file']) || $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE) {
$flashes['danger'][] = 'No file selected';
} else {
$content = file_get_contents($_FILES['file']['tmp_name']);
$output = $iptables->import($content);
if ($output) {
$flashes['danger'][] = $output;
} else {
$flashes['success'][] = 'File successfully imported';
}
header('Location: index.php');
}
}
if (isset($_GET['remove'])) {
$rule = buildRuleFromQuery($_GET);
$iptables->remove($rule, $_GET['table'], $_GET['chain']);
}
if (isset($_GET['edit'])) {
$editDialogDisplayed = TRUE;
$rule = buildRuleFromQuery($_GET);
$editDialogAction .= '?edit&' . buildQueryFromRule($rule, $_GET['table'], $_GET['chain']);
if (isset($_POST['submit'])) {
$iptables->remove($rule, $_GET['table'], $_GET['chain']);
$newRule = buildRuleFromQuery($_POST);
$iptables->add($newRule, $_POST['table'], $_POST['chain']);
}
}
if (isset($_GET['add'])) {
$editDialogDisplayed = TRUE;
$editDialogAction .= '?add';
if (isset($_POST['submit'])) {
$rule = buildRuleFromQuery($_POST);
$iptables->add($rule, $_POST['table'], $_POST['chain']);
}
}
include __DIR__ . '/template.phtml';