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
  filter_configure();
63
}
64

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

    
76
if ($_POST) {
77

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

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

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

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

    
116

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

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

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

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

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

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

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

    
194
?>
195

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

    
265
				  echo "<br>";
266
				  echo "<a href=\"?act=toggle&id={$i}\">";
267
				  if ($shaperent['direction'] == "in")
268
				  	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\">";
269
				  if ($shaperent['direction'] == "out")
270
				  	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\">";
271

    
272
				  echo "</a>" . $textse;;
273
				  ?>
274
                        </td>
275
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
276
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
277
                        </td>
278
                        <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'])); ?>
279
						<?php if ($shaperent['source']['port']): ?><br>
280
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
281
						<?php endif; ?><?=$textse;?>
282
                        </td>
283
                        <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'])); ?>
284
						<?php if ($shaperent['destination']['port']): ?><br>
285
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['port'])); ?>
286
						<?php endif; ?><?=$textse;?>
287
                        </td>
288
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?>
289
                          <?php
290
							if (isset($shaperent['outqueue']) && isset($shaperent['inqueue'])) {
291
								$desc = htmlspecialchars($shaperent['outqueue']);
292
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['outqueue']}\">{$desc}</a>";
293
								$desc = htmlspecialchars($shaperent['inqueue']);
294
							    echo "/<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['inqueue']}\">{$desc}</a>";
295
							}
296
						  ?><?=$textse;?>
297
                        </td>
298
                        <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><font color="white">
299
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
300
                          &nbsp; </td>
301
                        <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>
302
                          <?php if ($i > 0): ?>
303
                          <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>
304
                          <?php else: ?>
305
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up_d.gif" width="17" height="17" border="0">
306
                          <?php endif; ?>
307
			  <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>
308
			  
309
			  <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?')">
310
			  
311
                          <?php if (isset($a_shaper[$i+1])): ?>
312
                          <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>
313
                          <?php else: ?>
314
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down_d.gif" width="17" height="17" border="0">
315
                          <?php endif; ?>
316
                          <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>
317
                        </td>
318
                      </tr>
319
                      <?php $nrules++; $i++; endforeach; ?>
320
                      <tr>
321
                        <td class="list" colspan="8"></td>
322
                        <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>
323
                      </tr>
324
		      <tr>
325
		    <td colspan="8"><p><span class="red"><strong>Note:</strong></span><strong><br>
326
                    </strong>The first rule that matches a packet will be executed.<br>
327
                    The following match patterns are not shown in the list above:
328
                    IP packet length, TCP flags.<br>
329
                    You can check the results of your queues at <a href="status_queues.php">Status:Queues</a>.</td>
330
		    </tr>
331
                    </table>
332
		</div>
333
	  </td>
334
	</tr>
335
</table>
336
            </form>
337
<?php include("fend.inc"); ?>
338
</body>
339
</html>
(48-48/161)