Project

General

Profile

Download (13.4 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
require("guiconfig.inc");
32

    
33
if($_GET['reset'] <> "") {
34
        mwexec("killall -9 pfctl php");
35
	exit;
36
}
37

    
38
$pgtitle = array("Firewall","Traffic Shaper");
39

    
40
read_altq_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['interface'])
50
                $interface = trim($_GET['interface']);
51
        if ($_GET['action'])
52
                $action = $_GET['action'];
53
}
54
if ($_POST) {
55
	if ($_POST['name'])
56
        	$qname = trim($_POST['name']);
57
        if ($_POST['interface'])
58
                $interface = trim($_POST['interface']);
59
	if ($_POST['parentqueue'])
60
		$parentqueue = trim($_POST['parentqueue']);
61
}
62

    
63
if ($interface) {
64
	$altq = $altq_list_queues[$interface];
65
	if ($altq) {
66
		$queue =& $altq->find_queue($interface, $qname);
67
	} else $addnewaltq = true;
68
}
69

    
70
$dontshow = false;
71
$newqueue = false;
72
$output_form = "";
73

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

    
108
                        config_lock();
109
                        $retval = filter_configure();
110
                        config_unlock();
111

    
112
                        if (stristr($retval, "error") <> true)
113
	                        $savemsg = get_std_save_message($retval);
114
                        else
115
       	                	$savemsg = $retval;
116
			
117
			$output_form = $default_shaper_message;
118

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

    
146
			if ($q) {
147
				$q->SetInterface($interface);
148
				$output_form .= $q->build_form();
149
				$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
150
				$output_form .= " value=\"".$qname."\">";
151
				$newjavascript = $q->build_javascript();
152
                unset($q);
153
				$newqueue = true;
154
			}
155
		break;
156
		case "show":
157
			if ($queue)  
158
                        $output_form .= $queue->build_form();
159
			else
160
					$input_errors[] = "Queue not found!";
161
		break;
162
		case "enable":
163
			if ($queue) {
164
					$queue->SetEnabled("on");
165
					$output_form .= $queue->build_form();
166
					write_config();
167
					touch($d_shaperconfdirty_path);
168
			} else
169
					$input_errors[] = "Queue not found!";
170
		break;
171
		case "disable":
172
			if ($queue) {
173
					$queue->SetEnabled("");
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
		default:
181
			$output_form .= "<p class=\"pgtitle\">" . $default_shaper_msg."</p>";
182
			$dontshow = true;
183
			break;
184
	}
185
} else if ($_POST) {
186
	unset($input_errors);
187

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

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

    
259
			$retval = 0;
260
			$savemsg = get_std_save_message($retval);
261
			
262
			config_lock();
263
			$retval = filter_configure();
264
			config_unlock();
265
			
266
			if (stristr($retval, "error") <> true)
267
					$savemsg = get_std_save_message($retval);
268
			else
269
					$savemsg = $retval;
270

    
271
 		/* reset rrd queues */
272
                system("rm -f /var/db/rrd/*queuedrops.rrd");
273
                system("rm -f /var/db/rrd/*queues.rrd");
274
			enable_rrd_graphing();
275

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

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

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

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

    
330
if (!$dontshow || $newqueue) {
331

    
332
$output_form .= "<tr><td width=\"22%\" valign=\"top\" class=\"vncellreq\">";
333
$output_form .= "Queue Actions";
334
$output_form .= "</td><td valign=\"top\" class=\"vncellreq\" width=\"78%\">";
335

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

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

    
384
//$pgtitle = "Firewall: Shaper: By Interface View";
385

    
386
include("head.inc");
387
?>
388

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

    
400
include("fbegin.inc"); 
401
?>
402
<div id="inputerrors"></div>
403
<?php if ($input_errors) print_input_errors($input_errors); ?>
404

    
405
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
406

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

    
445
		      </td></tr>
446
                    </table>
447
		</div>
448
	  </td>
449
	</tr>
450
</table>
451
            </form>
452
<?php include("fend.inc"); 
453
?>
454
</body>
455
</html>
(55-55/193)