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
read_altq_config();
49
/* 
50
 * The whole logic in these code maybe can be specified.
51
 * If you find a better way contact me :).
52
 */
53

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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