Project

General

Profile

Download (14.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    firewall_shaper.php
5
    Copyright (C) 2004, 2005 Scott Ullrich
6
    All rights reserved.
7

    
8
    Originally part of m0n0wall (http://m0n0.ch/wall)
9
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
    All rights reserved.
11

    
12
    Redistribution and use in source and binary forms, with or without
13
    modification, are permitted provided that the following conditions are met:
14

    
15
    1. Redistributions of source code must retain the above copyright notice,
16
       this list of conditions and the following disclaimer.
17

    
18
    2. Redistributions in binary form must reproduce the above copyright
19
       notice, this list of conditions and the following disclaimer in the
20
       documentation and/or other materials provided with the distribution.
21

    
22
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
    POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
require("guiconfig.inc");
35

    
36
/* redirect to wizard if shaper isn't already configured */
37
if(isset($config['shaper']['enable'])) {
38
	$pconfig['enable'] = TRUE;
39
} else {
40
	if(!is_array($config['shaper']['queue']))
41
		Header("Location: wizard.php?xml=traffic_shaper_wizard.xml");
42
}
43

    
44
if (!is_array($config['shaper']['rule'])) {
45
	$config['shaper']['rule'] = array();
46
}
47

    
48
if (!is_array($config['shaper']['queue'])) {
49
	$config['shaper']['queue'] = array();
50
}
51

    
52
$a_shaper = &$config['shaper']['rule'];
53
$a_queue = &$config['shaper']['queue'];
54

    
55
function wipe_magic () {
56
  global $config;
57

    
58
  /* wipe previous */
59
  unset($config['shaper']['queue']);
60
  unset($config['shaper']['rule']);
61
  $config['shaper']['enable'] = FALSE;
62
}
63

    
64
if ($_POST['remove'] or $_GET['remove']) {
65
  wipe_magic();
66
  $savemsg = '<p><span class="red"><strong>Note: The traffic shaper has been disabled.</strong></span><strong><br>';
67
  touch($d_shaperconfdirty_path);
68
  unset($config['shaper']['enable']);
69
  write_config();
70
  filter_configure();
71
  Header("Location: index.php");
72
  exit;
73
}
74

    
75
if ($_POST) {
76

    
77
	if ($_POST['submit']) {
78
		$pconfig = $_POST;
79
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
80
		write_config();
81
	}
82

    
83
	if ($_POST['apply'] || $_POST['submit']) {
84
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
85
		write_config();		
86
		$retval = 0;
87
		$savemsg = get_std_save_message($retval);
88
		/* Setup pf rules since the user may have changed the optimization value */
89
		config_lock();
90
		$retval = filter_configure();
91
		config_unlock();
92
		if(stristr($retval, "error") <> true)
93
		    $savemsg = get_std_save_message($retval);
94
		else
95
		    $savemsg = $retval;
96

    
97
	        if(file_exists($d_shaperconfdirty_path))
98
	          unlink($d_shaperconfdirty_path);
99
	}
100
}
101

    
102
if (isset($_POST['del_x'])) {
103
        /* delete selected rules */
104
        if (is_array($_POST['rule']) && count($_POST['rule'])) {
105
                foreach ($_POST['rule'] as $rulei) {
106
                        unset($a_shaper[$rulei]);
107
                }
108
                write_config();
109
                touch($d_natconfdirty_path);
110
                header("Location: firewall_shaper.php");
111
                exit;
112
        }
113
}
114

    
115

    
116
if ($_GET['act'] == "down") {
117
	if ($a_shaper[$_GET['id']] && $a_shaper[$_GET['id']+1]) {
118
		$tmp = $a_shaper[$_GET['id']+1];
119
		$a_shaper[$_GET['id']+1] = $a_shaper[$_GET['id']];
120
		$a_shaper[$_GET['id']] = $tmp;
121
		write_config();
122
		touch($d_shaperconfdirty_path);
123
		header("Location: firewall_shaper.php");
124
		exit;
125
	}
126
} else if ($_GET['act'] == "up") {
127
	if (($_GET['id'] > 0) && $a_shaper[$_GET['id']]) {
128
		$tmp = $a_shaper[$_GET['id']-1];
129
		$a_shaper[$_GET['id']-1] = $a_shaper[$_GET['id']];
130
		$a_shaper[$_GET['id']] = $tmp;
131
		write_config();
132
		touch($d_shaperconfdirty_path);
133
		header("Location: firewall_shaper.php");
134
		exit;
135
	}
136
} else if ($_GET['act'] == "toggle") {
137
	if ($a_shaper[$_GET['id']]) {
138
		$a_shaper[$_GET['id']]['disabled'] = !isset($a_shaper[$_GET['id']]['disabled']);
139
		write_config();
140
		touch($d_shaperconfdirty_path);
141
		header("Location: firewall_shaper.php");
142
		exit;
143
	}
144
} else {
145
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
146
	   so we use .x/.y to fine move button clicks instead... */
147
	unset($movebtn);
148
	foreach ($_POST as $pn => $pd) {
149
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
150
			$movebtn = $matches[1];
151
			break;
152
		}
153
	}
154
	/* move selected rules before this rule */
155
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
156
		$a_shaper_new = array();
157

    
158
		/* copy all rules < $movebtn and not selected */
159
		for ($i = 0; $i < $movebtn; $i++) {
160
			if (!in_array($i, $_POST['rule']))
161
				$a_shaper_new[] = $a_shaper[$i];
162
		}
163

    
164
		/* copy all selected rules */
165
		for ($i = 0; $i < count($a_shaper); $i++) {
166
			if ($i == $movebtn)
167
				continue;
168
			if (in_array($i, $_POST['rule']))
169
				$a_shaper_new[] = $a_shaper[$i];
170
		}
171

    
172
		/* copy $movebtn rule */
173
		if ($movebtn < count($a_shaper))
174
			$a_shaper_new[] = $a_shaper[$movebtn];
175

    
176
		/* copy all rules > $movebtn and not selected */
177
		for ($i = $movebtn+1; $i < count($a_shaper); $i++) {
178
			if (!in_array($i, $_POST['rule']))
179
				$a_shaper_new[] = $a_shaper[$i];
180
		}
181

    
182
		$a_shaper = $a_shaper_new;
183
		write_config();
184
		touch($d_shaperconfdirty_path);
185
		header("Location: firewall_shaper.php");
186
		exit;
187
	}
188
}
189

    
190
$pgtitle = "Firewall: Shaper: Rules";
191
include("head.inc");
192

    
193
?>
194

    
195
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
196
<?php include("fbegin.inc"); ?>
197
<p class="pgtitle"><?=$pgtitle?></p>
198
<form action="firewall_shaper.php" method="post" name="iform">
199
<script type="text/javascript" language="javascript" src="row_toggle.js">
200
</script>
201
<?php if ($savemsg) print_info_box($savemsg); ?>
202
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
203
<?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>
204
<?php endif; ?>
205
<table width="100%" border="0" cellpadding="0" cellspacing="0">
206
  <tr><td>
207
<?php
208
	$tab_array = array();
209
	$tab_array[0] = array("Rules", true, "firewall_shaper.php");
210
	$tab_array[1] = array("Queues", false, "firewall_shaper_queues.php");
211
	$tab_array[2] = array("EZ Shaper wizard", false, "wizard.php?xml=traffic_shaper_wizard.xml");
212
	display_top_tabs($tab_array);
213
?>  
214
  </td></tr>
215
  <tr>
216
    <td>
217
	<div id="mainarea">
218
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
219
                <tr>
220
                  <td class="vtable"><p>
221
                      <input name="enable" type="checkbox" id="enable" value="yes" <?php if ($pconfig['enable'] == "yes") echo "checked";?>>
222
                      <strong>Enable traffic shaper<br>
223
                      </strong></p></td>
224
                </tr>
225
                <tr>
226
                  <td> 
227
		  <input name="submit" type="submit" class="formbtn" value="Save"> 
228
		  <input name="remove" type="submit" class="formbtn" id="remove" value="Remove Wizard"> 
229
                  </td>
230
		  
231
                </tr>
232
              </table>
233
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
234
                      <tr id="frheader">
235
		        <td width="3%" class="list">&nbsp;</td>
236
                        <td width="3%" class="list">&nbsp;</td>
237
                        <td width="5%" class="listhdrrns">If</td>
238
                        <td width="5%" class="listhdrrns">Proto</td>
239
                        <td width="20%" class="listhdrr">Source</td>
240
                        <td width="20%" class="listhdrr">Destination</td>
241
                        <td width="15%" class="listhdrrns">Target</td>
242
                        <td width="25%" class="listhdr">Description</td>
243
                        <td width="10%" class="list"></td>
244
                      </tr>
245
                      <?php $nrules = $i = 0; foreach ($a_shaper as $shaperent): ?>
246
                      <tr valign="top" id="fr<?=$nrules;?>">
247
                        <td class="listt"><input type="checkbox" id="frc<?=$nrules;?>" name="rule[]" value="<?=$i;?>" onClick="fr_bgcolor('<?=$nrules;?>')" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
248
                        <td class="listt" align="center"></td>
249
                        <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
250
                          <?php
251
				  $dis = "";
252
				  if (isset($shaperent['disabled'])) {
253
				  	$dis = "_d";
254
					$textss = "<span class=\"gray\">";
255
					$textse = "</span>";
256
				  } else {
257
				  	$textss = $textse = "";
258
				  }
259
				  $iflabels = array('lan' => 'LAN', 'wan' => 'WAN', 'pptp' => 'PPTP');
260
				  for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
261
				  	$iflabels['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
262
				  echo $textss . htmlspecialchars($iflabels[$shaperent['in-interface']]) . "->" . htmlspecialchars($iflabels[$shaperent['out-interface']]);
263

    
264
				  echo "<br>";
265
				  echo "<a href=\"?act=toggle&id={$i}\">";
266
				  if ($shaperent['direction'] == "in")
267
				  	echo "<img src=\"./themes/".$g['theme']."/images/icons/icon_in{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
268
				  if ($shaperent['direction'] == "out")
269
				  	echo "<img src=\"./themes/".$g['theme']."/images/icons/icon_out{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
270

    
271
				  echo "</a>" . $textse;;
272
				  ?>
273
                        </td>
274
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
275
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
276
                        </td>
277
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['source'])); ?>
278
						<?php if ($shaperent['source']['port']): ?><br>
279
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
280
						<?php endif; ?><?=$textse;?>
281
                        </td>
282
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['destination'])); ?>
283
						<?php if ($shaperent['destination']['port']): ?><br>
284
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['port'])); ?>
285
						<?php endif; ?><?=$textse;?>
286
                        </td>
287
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?>
288
                          <?php
289
							if (isset($shaperent['outqueue']) && isset($shaperent['inqueue'])) {
290
								$desc = htmlspecialchars($shaperent['outqueue']);
291
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['outqueue']}\">{$desc}</a>";
292
								$desc = htmlspecialchars($shaperent['inqueue']);
293
							    echo "/<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['inqueue']}\">{$desc}</a>";
294
							}
295
						  ?><?=$textse;?>
296
                        </td>
297
                        <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><font color="white">
298
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
299
                          &nbsp; </td>
300
                        <td valign="middle" nowrap class="list"> <a href="firewall_shaper_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="edit rule" width="17" height="17" border="0"></a>
301
                          <?php if ($i > 0): ?>
302
                          <a href="firewall_shaper.php?act=up&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up.gif" title="move up" width="17" height="17" border="0"></a>
303
                          <?php else: ?>
304
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up_d.gif" width="17" height="17" border="0">
305
                          <?php endif; ?>
306
			  <input name="move_<?=$i;?>" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="move selected rules before this rule" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"><br>
307
			  
308
			  <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="delete selected mappings" onclick="return confirm('Do you really want to delete the selected mappings?')">
309
			  
310
                          <?php if (isset($a_shaper[$i+1])): ?>
311
                          <a href="firewall_shaper.php?act=down&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down.gif" title="move down" width="17" height="17" border="0"></a>
312
                          <?php else: ?>
313
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down_d.gif" width="17" height="17" border="0">
314
                          <?php endif; ?>
315
                          <a href="firewall_shaper_edit.php?dup=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add a new rule based on this one" width="17" height="17" border="0"></a>
316
                        </td>
317
                      </tr>
318
                      <?php $nrules++; $i++; endforeach; ?>
319
                      <tr>
320
                        <td class="list" colspan="8"></td>
321
                        <td class="list"> <a href="firewall_shaper_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
322
                      </tr>
323
		      <tr>
324
		    <td colspan="8"><p><span class="red"><strong>Note:</strong></span><strong><br>
325
                    </strong>The first rule that matches a packet will be executed.<br>
326
                    The following match patterns are not shown in the list above:
327
                    IP packet length, TCP flags.<br>
328
                    You can check the results of your queues at <a href="status_queues.php">Status:Queues</a>.</td>
329
		    </tr>
330
                    </table>
331
		</div>
332
	  </td>
333
	</tr>
334
</table>
335
            </form>
336
<?php include("fend.inc"); ?>
337
</body>
338
</html>
(56-56/175)