Project

General

Profile

Download (13.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
##|+PRIV
32
##|*IDENT=page-firewall-trafficshaper
33
##|*NAME=Firewall: Traffic Shaper page
34
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
35
##|*MATCH=firewall_shaper.php*
36
##|-PRIV
37

    
38

    
39
require("guiconfig.inc");
40

    
41
if($_GET['reset'] <> "") {
42
        mwexec("killall -9 pfctl php");
43
	exit;
44
}
45

    
46
$pgtitle = array("Firewall","Traffic Shaper");
47

    
48
$shaperIFlist = get_configured_interface_with_descr();
49
read_altq_config();
50
/* 
51
 * The whole logic in these code maybe can be specified.
52
 * If you find a better way contact me :).
53
 */
54

    
55
if ($_GET) {
56
	if ($_GET['queue'])
57
        	$qname = trim($_GET['queue']);
58
        if ($_GET['interface'])
59
                $interface = trim($_GET['interface']);
60
        if ($_GET['action'])
61
                $action = $_GET['action'];
62
}
63
if ($_POST) {
64
	if ($_POST['name'])
65
        	$qname = trim($_POST['name']);
66
        if ($_POST['interface'])
67
                $interface = trim($_POST['interface']);
68
	if ($_POST['parentqueue'])
69
		$parentqueue = trim($_POST['parentqueue']);
70
}
71

    
72
if ($interface) {
73
	$altq = $altq_list_queues[$interface];
74
	if ($altq) {
75
		$queue =& $altq->find_queue($interface, $qname);
76
	} else $addnewaltq = true;
77
}
78

    
79
$dontshow = false;
80
$newqueue = false;
81
$output_form = "";
82

    
83
if ($_GET) {
84
	switch ($action) {
85
	case "delete":
86
			if ($queue) {
87
				$queue->delete_queue();
88
				write_config();
89
				touch($d_shaperconfdirty_path);
90
			}
91
			header("Location: firewall_shaper.php");
92
			exit;
93
		break;
94
	case "resetall":
95
			foreach ($altq_list_queues as $altq)
96
				$altq->delete_all();
97
			unset($altq_list_queues);
98
			$altq_list_queues = array();
99
			$tree = "<ul class=\"tree\" >";
100
			$tree .= get_interface_list_to_show();
101
			$tree .= "</ul>";
102
			unset($config['shaper']['queue']);
103
			unset($queue);
104
			unset($altq);
105
			$can_add = false;
106
			$can_enable = false;
107
			$dontshow = true;
108
			foreach ($config['filter']['rule'] as $key => $rule) {
109
				if (isset($rule['wizard']) && $rule['wizard'] == "yes")
110
					unset($config['filter']['rule'][$key]);
111
			}
112
			write_config();
113
			
114
			$retval = 0;
115
                        $savemsg = get_std_save_message($retval);
116

    
117
                        config_lock();
118
                        $retval = filter_configure();
119
                        config_unlock();
120

    
121
                        if (stristr($retval, "error") <> true)
122
	                        $savemsg = get_std_save_message($retval);
123
                        else
124
       	                	$savemsg = $retval;
125
			
126
			$output_form = $default_shaper_message;
127

    
128
		break;
129
	case "add":
130
			/* XXX: Find better way because we shouldn't know about this */
131
		if ($altq) {
132
	                switch ($altq->GetScheduler()) {
133
         	        case "PRIQ":
134
                	        $q = new priq_queue();
135
                        	break;
136
			case "FAIRQ":
137
				$q = new fairq_queue();
138
				break;
139
                        case "HFSC":
140
                         	$q = new hfsc_queue();
141
                        	break;
142
                        case "CBQ":
143
                                $q = new cbq_queue();
144
                        	break;
145
                        default:
146
                                /* XXX: Happens when sched==NONE?! */
147
				$q = new altq_root_queue();
148
                        	break;
149
        		}
150
		} else if ($addnewaltq) {
151
			$q = new altq_root_queue();
152
		} else 
153
			$input_errors[] = "Could not create new queue/discipline!";
154

    
155
			if ($q) {
156
				$q->SetInterface($interface);
157
				$output_form .= $q->build_form();
158
				$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
159
				$output_form .= " value=\"".$qname."\">";
160
				$newjavascript = $q->build_javascript();
161
                unset($q);
162
				$newqueue = true;
163
			}
164
		break;
165
		case "show":
166
			if ($queue)  
167
                        $output_form .= $queue->build_form();
168
			else
169
					$input_errors[] = "Queue not found!";
170
		break;
171
		case "enable":
172
			if ($queue) {
173
					$queue->SetEnabled("on");
174
					$output_form .= $queue->build_form();
175
					write_config();
176
					touch($d_shaperconfdirty_path);
177
			} else
178
					$input_errors[] = "Queue not found!";
179
		break;
180
		case "disable":
181
			if ($queue) {
182
					$queue->SetEnabled("");
183
					$output_form .= $queue->build_form();
184
					write_config();
185
					touch($d_shaperconfdirty_path);
186
			} else
187
					$input_errors[] = "Queue not found!";
188
		break;
189
		default:
190
			$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
191
			$dontshow = true;
192
			break;
193
	}
194
} else if ($_POST) {
195
	unset($input_errors);
196

    
197
	if ($addnewaltq) {
198
		$altq =& new altq_root_queue();
199
		$altq->SetInterface($interface);
200
		
201
		switch ($altq->GetBwscale()) {
202
				case "Mb":
203
					$factor = 1000 * 1000;
204
					brak;
205
				case "Kb":
206
					$factor = 1000;
207
					break;
208
				case "b":
209
					$factor = 1;
210
					break;
211
				case "Gb":
212
					$factor = 1000 * 1000 * 1000;
213
					break;
214
				case "%": /* We don't use it for root_XXX queues. */
215
				default: /* XXX assume Kb by default. */
216
					$factor = 1000;
217
					break;
218
			} 
219
		$altq->SetAvailableBandwidth($altq->GetBandwidth() * $factor);
220
		$altq->ReadConfig($_POST);
221
		$altq->validate_input($_POST, &$input_errors);
222
		if (!$input_errors) {
223
			unset($tmppath);
224
			$tmppath[] = $altq->GetInterface();
225
			$altq->SetLink(&$tmppath);	
226
			$altq->wconfig();
227
			write_config();
228
			touch($d_shaperconfdirty_path);
229
			$can_enable = true;
230
                        $can_add = true;
231
		}
232
		read_altq_config();
233
		$output_form .= $altq->build_form();
234

    
235
	} else if ($parentqueue) { /* Add a new queue */
236
		$qtmp =& $altq->find_queue($interface, $parentqueue);
237
		if ($qtmp) {
238
			$tmppath =& $qtmp->GetLink();
239
			array_push($tmppath, $qname);
240
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, &$input_errors);
241
			if (!$input_errors) {
242
				array_pop($tmppath);
243
				$tmp->wconfig();
244
				$can_enable = true;
245
				if ($tmp->CanHaveChilds() && $can_enable) {
246
					if ($tmp->GetDefault() <> "")
247
                             			$can_add = false;
248
                        		else
249
                             			$can_add = true;
250
				} else
251
					$can_add = false;
252
				write_config();
253
				touch($d_shaperconfdirty_path);
254
				$can_enable = true;
255
				if ($altq->GetScheduler() != "PRIQ") /* XXX */
256
					if ($tmp->GetDefault() <> "")
257
                                                $can_add = false;
258
                                        else
259
                                                $can_add = true;
260
			}
261
			read_altq_config();
262
			$output_form .= $tmp->build_form();			
263
		} else
264
			$input_errors[] = "Could not add new queue.";
265
	} else if ($_POST['apply']) {
266
			write_config();
267

    
268
			$retval = 0;
269
			$savemsg = get_std_save_message($retval);
270
			
271
			config_lock();
272
			$retval = filter_configure();
273
			config_unlock();
274
			
275
			if (stristr($retval, "error") <> true)
276
					$savemsg = get_std_save_message($retval);
277
			else
278
					$savemsg = $retval;
279

    
280
 		/* reset rrd queues */
281
                system("rm -f /var/db/rrd/*queuedrops.rrd");
282
                system("rm -f /var/db/rrd/*queues.rrd");
283
			enable_rrd_graphing();
284

    
285
            unlink($d_shaperconfdirty_path);
286
			
287
			if ($queue) {
288
				$output_form .= $queue->build_form();
289
				$dontshow = false;
290
			}
291
			else {
292
				$output_form .= $default_shaper_message;
293
				$dontshow = true;
294
			}
295

    
296
	} else if ($queue) {
297
                $queue->validate_input($_POST, &$input_errors);
298
                if (!$input_errors) {
299
                            $queue->update_altq_queue_data($_POST);
300
                            $queue->wconfig();
301
							write_config();
302
				touch($d_shaperconfdirty_path);
303
				$dontshow = false;
304
                } 
305
		read_altq_config();
306
		$output_form .= $queue->build_form();
307
	} else  {
308
		$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
309
		$dontshow = true;
310
	}
311
} else {
312
	$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
313
	$dontshow = true;
314
}
315

    
316
if ($queue) {
317
                        if ($queue->GetEnabled())
318
                                $can_enable = true;
319
                        else
320
                                $can_enable = false;
321
                        if ($queue->CanHaveChilds() && $can_enable) { 
322
                                if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "")
323
                                        $can_add = false;
324
                                else
325
                                        $can_add = true;
326
                        } else
327
                                $can_add = false;
328
}
329

    
330
$tree = "<ul class=\"tree\" >";
331
if (is_array($altq_list_queues)) {
332
        foreach ($altq_list_queues as $tmpaltq) {
333
                $tree .= $tmpaltq->build_tree();
334
        }
335
$tree .=  get_interface_list_to_show();
336
}
337
$tree .= "</ul>";
338

    
339
if (!$dontshow || $newqueue) {
340

    
341
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
342
$output_form .= "Queue Actions";
343
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
344

    
345
$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
346
if ($can_add || $addnewaltq) {
347
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
348
	$output_form .= $interface; 
349
	if ($queue) {
350
		$output_form .= "&queue=" . $queue->GetQname();
351
	}
352
	$output_form .= "&action=add\">";
353
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"add\" value=\"Add new queue\">";
354
	$output_form .= "</a>";
355
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
356
	$output_form .= $interface . "&queue=";
357
	if ($queue) {
358
		$output_form .= "&queue=" . $queue->GetQname();
359
	}
360
	$output_form .= "&action=delete\">";
361
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
362
	if ($queue)
363
		$output_form .= " value=\"Delete this queue\">";
364
	else
365
		$output_form .= " value=\"Disable shaper on interface\">";
366
	$output_form .= "</a>";  
367
}
368
$output_form .= "</td></tr>";
369
$output_form .= "</div>";
370
} 
371
else 
372
	$output_form .= "</div>";
373

    
374
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
375
if (!$dontshow) {
376
if ($queue || $altq || $newqueue) {
377
	$output .= "<tr><td valign=\"top\" class=\"vncellreq\"><br>";
378
	$output .= "Enable/Disable";
379
	$output .= "</td><td class=\"vncellreq\">";
380
	$output .= " <input type=\"checkbox\" id=\"enabled\" name=\"enabled\"";
381
	if ($queue)
382
		if ($queue->GetEnabled())
383
       			$output .=  " CHECKED";
384
	else if ($altq)
385
		if ($altq->GetEnabled())
386
			$output .= " CHECKED";
387
	$output .= " ><span class=\"vexpl\"> Enable/Disable queue and its childs</span>";
388
	$output .= "</td></tr>";
389
}
390
}
391
$output .= $output_form;
392

    
393
//$pgtitle = "Firewall: Shaper: By Interface View";
394

    
395
include("head.inc");
396
?>
397

    
398
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
399
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
400
<script type="text/javascript" src="./tree/tree.js"></script>
401
<?php
402
if ($queue) {
403
        echo "<script type=\"text/javascript\">";
404
        echo $queue->build_javascript();
405
        echo "</script>";
406
}
407
echo $newjavascript;
408

    
409
include("fbegin.inc"); 
410
?>
411
<div id="inputerrors"></div>
412
<?php if ($input_errors) print_input_errors($input_errors); ?>
413

    
414
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
415

    
416
<?php if ($savemsg) print_info_box($savemsg); ?>
417
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
418
<?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>
419
<?php endif; ?>
420
<table width="100%" border="0" cellpadding="0" cellspacing="0">
421
  <tr><td>
422
<?php
423
	$tab_array = array();
424
	$tab_array[0] = array("By Interface", true, "firewall_shaper.php");
425
	$tab_array[1] = array("By Queue", false, "firewall_shaper_queues.php");
426
	$tab_array[2] = array("Limiter", false, "firewall_shaper_vinterface.php");
427
	$tab_array[3] = array("Layer7", false, "firewall_shaper_layer7.php");
428
	$tab_array[4] = array("Wizards", false, "firewall_shaper_wizards.php");
429
	display_top_tabs($tab_array);
430
?>
431
  </td></tr>
432
  <tr>
433
    <td>
434
	<div id="mainarea">
435
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
436
<?php if (count($altq_list_queues) > 0): ?>
437
                        <tr class="tabcont"><td width="25%" align="left">
438
                                <a href="firewall_shaper.php?action=resetall" >
439
                                        <input type="button" value="Remove Shaper" class="formbtn">
440
                                </a>
441
                        </td><td width="75%"> </td></tr>
442
<? endif; ?>
443
			<tr>
444
			<td width="25%" valign="top" algin="left">
445
			<?php
446
				echo $tree; 
447
			?>
448
			</td>
449
			<td width="75%" valign="top" align="center">
450
			<table>
451
			<?
452
				echo $output;
453
			?>	
454
			</table>
455

    
456
		      </td></tr>
457
                    </table>
458
		</div>
459
	  </td>
460
	</tr>
461
</table>
462
            </form>
463
<?php include("fend.inc"); 
464
?>
465
</body>
466
</html>
(56-56/207)