Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1 585a8faf Ermal Luçi
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper.php
5
	Copyright (C) 2004, 2005 Scott Ullrich
6
	Copyright (C) 2008 Ermal Lu?i
7
	All rights reserved.
8
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
require("guiconfig.inc");
32
33
if($_GET['reset'] <> "") {
34
        mwexec("killall -9 pfctl php");
35
	exit;
36
}
37
38 6d0c07ea Ermal Luçi
$pgtitle = array("Firewall","Traffic Shaper", "Limiter");
39 585a8faf Ermal Luçi
40
read_dummynet_config();
41
/* 
42
 * The whole logic in these code maybe can be specified.
43
 * If you find a better way contact me :).
44
 */
45
46
if ($_GET) {
47
	if ($_GET['queue'])
48
        	$qname = trim($_GET['queue']);
49
        if ($_GET['pipe'])
50
                $pipe = trim($_GET['pipe']);
51
        if ($_GET['action'])
52
                $action = $_GET['action'];
53
}
54
if ($_POST) {
55
	if ($_POST['name'])
56
        	$qname = trim($_POST['name']);
57
        if ($_POST['pipe'])
58 d3e5e7df Ermal Luçi
        	$pipe = trim($_POST['pipe']);
59
	else
60
		$pipe = trim($_POST['name']);
61 585a8faf Ermal Luçi
	if ($_POST['parentqueue'])
62
		$parentqueue = trim($_POST['parentqueue']);
63
}
64
65
if ($pipe) {
66
	$dnpipe = $dummynet_pipe_list[$pipe];
67
	if ($dnpipe) {
68
		$queue =& $dnpipe->find_queue($pipe, $qname);
69
	} else $addnewpipe = true;
70
}
71
72
$dontshow = false;
73
$newqueue = false;
74
$output_form = "";
75
76
if ($_GET) {
77
	switch ($action) {
78
	case "delete":
79
			if ($queue) {
80
				$queue->delete_queue();
81
				write_config();
82
				touch($d_shaperconfdirty_path);
83
			}
84
			header("Location: firewall_shaper_vinterface.php");
85
			exit;
86
		break;
87
	case "resetall":
88
			foreach ($dummynet_pipe_list as $dn)
89 d3e5e7df Ermal Luçi
				$dn->delete_queue();
90 585a8faf Ermal Luçi
			unset($dummynet_pipe_list);
91
			$dummynet_pipe_list = array();
92
			unset($config['dnshaper']['queue']);
93
			unset($queue);
94
			unset($pipe);
95
			$can_add = false;
96
			$can_enable = false;
97
			$dontshow = true;
98
			foreach ($config['filter']['rule'] as $key => $rule) {
99
				if (isset($rule['dnpipe']))
100
					unset($config['filter']['rule'][$key]['dnpipe']);
101
				if (isset($rule['pdnpipe']))
102
					unset($config['filter']['rule'][$key]['pdnpipe']);
103
			}
104
			write_config();
105
			
106
			$retval = 0;
107
                        $savemsg = get_std_save_message($retval);
108
109
                        config_lock();
110
                        $retval = filter_configure();
111
                        config_unlock();
112
113
                        if (stristr($retval, "error") <> true)
114
	                        $savemsg = get_std_save_message($retval);
115
                        else
116
       	                	$savemsg = $retval;
117
			
118
			$output_form = $dn_default_shaper_message;
119
120
		break;
121
	case "add":
122
		if ($dnpipe) {
123 d3e5e7df Ermal Luçi
			$q = new dnqueue_class();
124
			$q->SetPipe($pipe);
125
			$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
126
			$output_form .= " value=\"".$pipe."\">";
127 585a8faf Ermal Luçi
		} else if ($addnewpipe) {
128
			$q = new dnpipe_class();
129
			$q->SetQname($pipe);
130
		} else 
131
			$input_errors[] = "Could not create new queue/discipline!";
132
133
			if ($q) {
134
				$output_form .= $q->build_form();
135
                unset($q);
136
				$newqueue = true;
137
			}
138
		break;
139 d3e5e7df Ermal Luçi
	case "show":
140
		if ($queue)  
141
                       $output_form .= $queue->build_form();
142
		else
143
			$input_errors[] = "Queue not found!";
144 585a8faf Ermal Luçi
		break;
145 d3e5e7df Ermal Luçi
	case "enable":
146
		if ($queue) {
147
				$queue->SetEnabled("on");
148
				$output_form .= $queue->build_form();
149
				write_config();
150
				touch($d_shaperconfdirty_path);
151
		} else
152
				$input_errors[] = "Queue not found!";
153 585a8faf Ermal Luçi
		break;
154 d3e5e7df Ermal Luçi
	case "disable":
155
		if ($queue) {
156
				$queue->SetEnabled("");
157
				$output_form .= $queue->build_form();
158
				write_config();
159
				touch($d_shaperconfdirty_path);
160
		} else
161
				$input_errors[] = "Queue not found!";
162
		break;
163
	default:
164
		$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg."</p>";
165
		$dontshow = true;
166 585a8faf Ermal Luçi
		break;
167
	}
168
} else if ($_POST) {
169
	unset($input_errors);
170
171
	if ($addnewpipe) {
172
		$dnpipe =& new dnpipe_class();
173
		
174
		$dnpipe->ReadConfig($_POST);
175
		$dnpipe->validate_input($_POST, &$input_errors);
176
		if (!$input_errors) {
177
			unset($tmppath);
178
			$tmppath[] = $dnpipe->GetQname();
179
			$dnpipe->SetLink(&$tmppath);	
180
			$dnpipe->wconfig();
181
			write_config();
182
			touch($d_shaperconfdirty_path);
183
			$can_enable = true;
184 d3e5e7df Ermal Luçi
       		     	$can_add = true;
185 585a8faf Ermal Luçi
		}
186 d3e5e7df Ermal Luçi
		read_dummynet_config();
187 585a8faf Ermal Luçi
		$output_form .= $dnpipe->build_form();
188
189
	} else if ($parentqueue) { /* Add a new queue */
190
		if ($dnpipe) {
191
			$tmppath =& $dnpipe->GetLink();
192
			array_push($tmppath, $qname);
193
			$tmp =& $dnpipe->add_queue($pipe, $_POST, $tmppath, &$input_errors);
194
			if (!$input_errors) {
195
				array_pop($tmppath);
196
				$tmp->wconfig();
197
				write_config();
198
				$can_enable = true;
199 d3e5e7df Ermal Luçi
                		$can_add = false;
200 585a8faf Ermal Luçi
				touch($d_shaperconfdirty_path);
201
				$can_enable = true;
202
			}
203 d3e5e7df Ermal Luçi
			read_dummynet_config();
204 585a8faf Ermal Luçi
			$output_form .= $tmp->build_form();			
205
		} else
206
			$input_errors[] = "Could not add new queue.";
207
	} else if ($_POST['apply']) {
208
			write_config();
209
210
			$retval = 0;
211
			$savemsg = get_std_save_message($retval);
212
			
213
			config_lock();
214
			$retval = filter_configure();
215
			config_unlock();
216
			
217
			if (stristr($retval, "error") <> true)
218
					$savemsg = get_std_save_message($retval);
219
			else
220
					$savemsg = $retval;
221
222 d3e5e7df Ermal Luçi
 		/* XXX: TODO Make dummynet pretty graphs */ 
223
		//	enable_rrd_graphing();
224 585a8faf Ermal Luçi
225
            unlink($d_shaperconfdirty_path);
226
			
227
			if ($queue) {
228
				$output_form .= $queue->build_form();
229
				$dontshow = false;
230
			}
231
			else {
232
				$output_form .= $dn_default_shaper_message;
233
				$dontshow = true;
234
			}
235
236
	} else if ($queue) {
237
                $queue->validate_input($_POST, &$input_errors);
238
                if (!$input_errors) {
239 d3e5e7df Ermal Luçi
                            	$queue->update_dn_data($_POST);
240
                            	$queue->wconfig();
241
				write_config();
242 585a8faf Ermal Luçi
				touch($d_shaperconfdirty_path);
243
				$dontshow = false;
244
                } 
245 d3e5e7df Ermal Luçi
		read_dummynet_config();
246
		$output_form .= $queue->build_form();
247 585a8faf Ermal Luçi
	} else  {
248
		$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg."</p>";
249
		$dontshow = true;
250
	}
251
} else {
252
	$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg."</p>";
253
	$dontshow = true;
254
}
255
256
if ($queue) {
257
                        if ($queue->GetEnabled())
258
                                $can_enable = true;
259
                        else
260
                                $can_enable = false;
261
                        if ($queue->CanHaveChilds()) { 
262 d3e5e7df Ermal Luçi
                       		$can_add = true;
263 585a8faf Ermal Luçi
                        } else
264
                                $can_add = false;
265
}
266
267
$tree = "<ul class=\"tree\" >";
268
if (is_array($dummynet_pipe_list)) {
269
        foreach ($dummynet_pipe_list as $tmpdn) {
270
                $tree .= $tmpdn->build_tree();
271
        }
272
}
273
$tree .= "</ul>";
274
275
if (!$dontshow || $newqueue) {
276
277
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
278
$output_form .= "Queue Actions";
279
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
280
281
$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
282
if ($can_add || $addnewaltq) {
283
	$output_form .= "<a href=\"firewall_shaper_vinterface.php?pipe=";
284
	$output_form .= $pipe; 
285
	if ($queue) {
286
		$output_form .= "&queue=" . $queue->GetQname();
287
	}
288
	$output_form .= "&action=add\">";
289
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"add\" value=\"Add new queue\">";
290
	$output_form .= "</a>";
291
}
292 d3e5e7df Ermal Luçi
$output_form .= "<a href=\"firewall_shaper_vinterface.php?pipe=";
293
$output_form .= $pipe . "&queue=";
294
if ($queue) {
295
	$output_form .= "&queue=" . $queue->GetQname();
296
}
297
$output_form .= "&action=delete\">";
298
$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
299
if ($queue)
300
	$output_form .= " value=\"Delete this queue\">";
301
else
302
	$output_form .= " value=\"Delete virtual interface\">";
303
$output_form .= "</a>";  
304 585a8faf Ermal Luçi
$output_form .= "</td></tr>";
305
$output_form .= "</div>";
306
} 
307
else 
308
	$output_form .= "</div>";
309
310
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
311
if (!$dontshow) {
312
if ($queue || $dnpipe || $newqueue) {
313
	$output .= "<tr><td valign=\"top\" class=\"vncellreq\"><br>";
314
	$output .= "Enable/Disable";
315
	$output .= "</td><td class=\"vncellreq\">";
316
	$output .= " <input type=\"checkbox\" id=\"enabled\" name=\"enabled\"";
317
	if ($queue)
318
		if ($queue->GetEnabled() <> "")
319
       			$output .=  " CHECKED";
320
	$output .= " ><span class=\"vexpl\"> Enable/Disable queue and its childs</span>";
321
	$output .= "</td></tr>";
322
}
323
}
324
$output .= $output_form;
325
326
include("head.inc");
327
?>
328
329
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
330
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
331
<script type="text/javascript" src="./tree/tree.js"></script>
332
<?php
333
include("fbegin.inc"); 
334
?>
335
<div id="inputerrors"></div>
336
<?php if ($input_errors) print_input_errors($input_errors); ?>
337
338
<form action="firewall_shaper_vinterface.php" method="post" id="iform" name="iform">
339
340
<?php if ($savemsg) print_info_box($savemsg); ?>
341
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
342
<?php print_info_box_np("The traffic shaper configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
343
<?php endif; ?>
344
<table width="100%" border="0" cellpadding="0" cellspacing="0">
345
  <tr><td>
346
<?php
347
	$tab_array = array();
348
	$tab_array[0] = array("By Interface", false, "firewall_shaper.php");
349
	$tab_array[1] = array("By Queue", false, "firewall_shaper_queues.php");
350 6d0c07ea Ermal Luçi
	$tab_array[2] = array("Limiter", true, "firewall_shaper_vinterface.php");
351 585a8faf Ermal Luçi
	$tab_array[3] = array("Wizards", false, "firewall_shaper_wizards.php");
352
	display_top_tabs($tab_array);
353
?>
354
  </td></tr>
355
  <tr>
356
    <td>
357
	<div id="mainarea">
358
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
359
<?php if (count($dummynet_pipe_list) > 0): ?>
360
                        <tr class="tabcont"><td width="25%" align="left">
361
                        </td><td width="75%"> </td></tr>
362
<? endif; ?>
363
			<tr>
364
			<td width="25%" valign="top" algin="left">
365
			<?php
366
				echo $tree; 
367
			?>
368 d3e5e7df Ermal Luçi
			<br/><br/>
369 585a8faf Ermal Luçi
			<a href="firewall_shaper_vinterface.php?pipe=new&action=add">
370 1fa12d94 Ermal Luçi
			<input type="button" id="newpipe" name="newpipe" value="Create limiter"></a><br/>
371 585a8faf Ermal Luçi
			</td>
372
			<td width="75%" valign="top" align="center">
373
			<table>
374
			<?
375
				echo $output;
376
			?>	
377
			</table>
378
379
		      </td></tr>
380
                    </table>
381
		</div>
382
	  </td>
383
	</tr>
384
</table>
385
            </form>
386
<?php include("fend.inc"); 
387
?>
388
</body>
389
</html>