Project

General

Profile

Download (13.6 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
                        $retval |= filter_configure();
116
                        $savemsg = get_std_save_message($retval);
117

    
118
                        if (stristr($retval, "error") <> true)
119
	                        $savemsg = get_std_save_message($retval);
120
                        else
121
       	                	$savemsg = $retval;
122
			
123
			$output_form = $default_shaper_message;
124

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

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

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

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

    
265
			$retval = 0;
266
			$retval = filter_configure();
267
			$savemsg = get_std_save_message($retval);
268
			
269
			if (stristr($retval, "error") <> true)
270
					$savemsg = get_std_save_message($retval);
271
			else
272
					$savemsg = $retval;
273

    
274
 		/* reset rrd queues */
275
                system("rm -f /var/db/rrd/*queuedrops.rrd");
276
                system("rm -f /var/db/rrd/*queues.rrd");
277
			enable_rrd_graphing();
278

    
279
            unlink($d_shaperconfdirty_path);
280
			
281
			if ($queue) {
282
				$output_form .= $queue->build_form();
283
				$dontshow = false;
284
			}
285
			else {
286
				$output_form .= $default_shaper_message;
287
				$dontshow = true;
288
			}
289

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

    
310
if ($queue) {
311
                        if ($queue->GetEnabled())
312
                                $can_enable = true;
313
                        else
314
                                $can_enable = false;
315
                        if ($queue->CanHaveChilds() && $can_enable) { 
316
                                if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "")
317
                                        $can_add = false;
318
                                else
319
                                        $can_add = true;
320
                        } else
321
                                $can_add = false;
322
}
323

    
324
$tree = "<ul class=\"tree\" >";
325
if (is_array($altq_list_queues)) {
326
        foreach ($altq_list_queues as $tmpaltq) {
327
                $tree .= $tmpaltq->build_tree();
328
        }
329
$tree .=  get_interface_list_to_show();
330
}
331
$tree .= "</ul>";
332

    
333
if (!$dontshow || $newqueue) {
334

    
335
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
336
$output_form .= "Queue Actions";
337
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
338

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

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

    
387
//$pgtitle = "Firewall: Shaper: By Interface View";
388

    
389
include("head.inc");
390
?>
391

    
392
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
393
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
394
<script type="text/javascript" src="./tree/tree.js"></script>
395
<?php
396
if ($queue) {
397
        echo "<script type=\"text/javascript\">";
398
        echo $queue->build_javascript();
399
        echo "</script>";
400
}
401
echo $newjavascript;
402

    
403
include("fbegin.inc"); 
404
?>
405
<div id="inputerrors"></div>
406
<?php if ($input_errors) print_input_errors($input_errors); ?>
407

    
408
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
409

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

    
450
		      </td></tr>
451
                    </table>
452
		</div>
453
	  </td>
454
	</tr>
455
</table>
456
            </form>
457
<?php include("fend.inc"); 
458
?>
459
</body>
460
</html>
(55-55/206)