Project

General

Profile

Download (6.19 KB) Statistics
| Branch: | Tag: | Revision:
1 14f84bc9 Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_pftop.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 402c98a2 Reid Linnemann
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10
 *
11 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14 c5d81585 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
16 c5d81585 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22 fd9ebcd5 Stephen Beaver
 */
23 14f84bc9 Scott Ullrich
24
##|+PRIV
25 7997ed44 Renato Botelho
##|*IDENT=page-diagnostics-system-pftop
26
##|*NAME=Diagnostics: pfTop
27
##|*DESCR=Allows access to the 'Diagnostics: pfTop' page
28 1af5edbf Stephen Beaver
##|*MATCH=diag_pftop.php*
29 14f84bc9 Scott Ullrich
##|-PRIV
30
31 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
32 14f84bc9 Scott Ullrich
33 0da0d43e Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("pfTop"));
34 10085279 luckman212
$pftop = "/usr/local/sbin/pftop";
35 14f84bc9 Scott Ullrich
36 c07a2b86 jim-p
$sorttypes = array('age', 'bytes', 'dest', 'dport', 'exp', 'none', 'pkt', 'sport', 'src');
37 c668eaed Warren Baker
$viewtypes = array('default', 'label', 'long', 'queue', 'rules', 'size', 'speed', 'state', 'time');
38 38de3c9d Warren Baker
$viewall = array('queue', 'label', 'rules');
39
$numstates = array('50', '100', '200', '500', '1000', 'all');
40 c668eaed Warren Baker
41 5f601060 Phil Davis
if ($_REQUEST['getactivity']) {
42
	if ($_REQUEST['sorttype'] && in_array($_REQUEST['sorttype'], $sorttypes) &&
43
	    $_REQUEST['viewtype'] && in_array($_REQUEST['viewtype'], $viewtypes) &&
44
	    $_REQUEST['states'] && in_array($_REQUEST['states'], $numstates)) {
45 c668eaed Warren Baker
		$viewtype = escapeshellarg($_REQUEST['viewtype']);
46 ea031bde Warren Baker
		if (in_array($_REQUEST['viewtype'], $viewall)) {
47
			$sorttype = "";
48
			$numstate = "-a";
49
		} else {
50
			$sorttype = "-o " . escapeshellarg($_REQUEST['sorttype']);
51
			$numstate = ($_REQUEST['states'] == "all" ? "-a" : escapeshellarg($_REQUEST['states']));
52
		}
53 c668eaed Warren Baker
	} else {
54 19f7146a bcyrill
		$sorttype = "bytes";
55
		$viewtype = "default";
56 38de3c9d Warren Baker
		$numstate = "100";
57 c668eaed Warren Baker
	}
58 10085279 luckman212
	if ($_REQUEST['filter'] != "") {
59
		$filter = "-f " . escapeshellarg($_REQUEST['filter']);
60
	} else {
61
		$filter = "";
62
	}
63
	$text = shell_exec("$pftop {$filter} -b {$sorttype} -w 135 -v {$viewtype} {$numstate}");
64
	if (empty($text)) {
65
		echo "Invalid filter, check syntax";
66
	} else {
67 0d186018 jim-p
		echo trim(htmlentities($text));
68 10085279 luckman212
	}
69 14f84bc9 Scott Ullrich
	exit;
70
}
71
72
include("head.inc");
73
74 5f601060 Phil Davis
if ($_REQUEST['sorttype'] && in_array($_REQUEST['sorttype'], $sorttypes) &&
75
    $_REQUEST['viewtype'] && in_array($_REQUEST['viewtype'], $viewtypes) &&
76
    $_REQUEST['states'] && in_array($_REQUEST['states'], $numstates)) {
77 ea031bde Warren Baker
	$viewtype = escapeshellarg($_REQUEST['viewtype']);
78
	if (in_array($_REQUEST['viewtype'], $viewall)) {
79
		$sorttype = "";
80
		$numstate = "-a";
81
	} else {
82
		$sorttype = "-o " . escapeshellarg($_REQUEST['sorttype']);
83
		$numstate = ($_REQUEST['states'] == "all" ? "-a" : escapeshellarg($_REQUEST['states']));
84
	}
85 c668eaed Warren Baker
} else {
86 19f7146a bcyrill
	$sorttype = "bytes";
87
	$viewtype = "default";
88 38de3c9d Warren Baker
	$numstate = "100";
89 c668eaed Warren Baker
}
90 10085279 luckman212
if ($_REQUEST['filter'] != "") {
91
	$filter = "-f " . escapeshellarg($_REQUEST['filter']);
92
} else {
93
	$filter = "";
94
}
95 14f84bc9 Scott Ullrich
96 947141fd Phil Davis
if ($input_errors) {
97 45d6ada5 Sjon Hortensius
	print_input_errors($input_errors);
98 947141fd Phil Davis
}
99 45d6ada5 Sjon Hortensius
100
$form = new Form(false);
101
$form->addGlobal(new Form_Input(
102
	'getactivity',
103
	null,
104
	'hidden',
105
	'yes'
106
));
107
$section = new Form_Section('pfTop Configuration');
108
109
$validViews = array(
110 179377b0 robjarsen
	'default' => gettext('default'),
111
	'label' => gettext('label'),
112 ff30e319 bruno
	'long' => gettext('long'),
113 179377b0 robjarsen
	'queue' => gettext('queue'),
114
	'rules' => gettext('rules'),
115 ff30e319 bruno
	'size' => gettext('size'),
116 179377b0 robjarsen
	'speed' => gettext('speed'),
117
	'state' => gettext('state'),
118 ff30e319 bruno
	'time' => gettext('time'),
119 45d6ada5 Sjon Hortensius
);
120
$section->addInput(new Form_Select(
121
	'viewtype',
122
	'View',
123
	$viewtype,
124 ff30e319 bruno
	$validViews
125 45d6ada5 Sjon Hortensius
));
126
127 10085279 luckman212
$section->addInput(new Form_Input(
128
	'filter',
129
	'Filter expression',
130
	'text',
131
	$_REQUEST['filter'],
132
	['placeholder' => 'e.g. tcp, ip6 or dst net 208.123.73.0/24']
133
))->setHelp('<em>click for filter help</em>%1$s' .
134
	'<code>[proto &lt;ip|ip6|ah|carp|esp|icmp|ipv6-icmp|pfsync|tcp|udp&gt;]</code><br />' .
135
	'<code>[src|dst|gw] [host|net|port] &lt;host/network/port&gt;</code><br />' .
136
	'<code>[in|out]</code><br /><br />' .
137
	'These are the most common selectors. Some expressions can be combined using "and" / "or". ' .
138
	'See %2$s for more detailed expression syntax.%3$s',
139
	'<span class="infoblock"><br />',
140
	'<a target="_blank" href="https://www.freebsd.org/cgi/man.cgi?query=pftop#STATE_FILTERING">pftop(8)</a>',
141
	'</span></p>'
142
);
143
144 45d6ada5 Sjon Hortensius
$section->addInput(new Form_Select(
145
	'sorttype',
146
	'Sort by',
147
	$sorttype,
148
	array(
149 ff30e319 bruno
		'none' => gettext('None'),
150
		'age' => gettext('Age'),
151
		'bytes' => gettext('Bytes'),
152
		'dest' => gettext('Destination Address'),
153
		'dport' => gettext('Destination Port'),
154
		'exp' => gettext('Expiry'),
155
		'pkt' => gettext('Packet'),
156
		'sport' => gettext('Source Port'),
157
		'src' => gettext('Source Address'),
158 45d6ada5 Sjon Hortensius
	)
159
));
160
161 10085279 luckman212
$validStates = array(50, 100, 200, 500, 1000, 'all');
162 45d6ada5 Sjon Hortensius
$section->addInput(new Form_Select(
163
	'states',
164
	'Maximum # of States',
165
	$numstate,
166
	array_combine($validStates, $validStates)
167
));
168
169
$form->add($section);
170
print $form;
171 14f84bc9 Scott Ullrich
?>
172 45d6ada5 Sjon Hortensius
173 8fd9052f Colin Fleming
<script type="text/javascript">
174
//<![CDATA[
175 c668eaed Warren Baker
	function getpftopactivity() {
176 45d6ada5 Sjon Hortensius
		$.ajax(
177 1af5edbf Stephen Beaver
			'/diag_pftop.php',
178 14f84bc9 Scott Ullrich
			{
179 45d6ada5 Sjon Hortensius
				method: 'post',
180
				data: $(document.forms[0]).serialize(),
181
				dataType: "html",
182
				success: function (data) {
183
					$('#xhrOutput').html(data);
184
				},
185
			}
186
		);
187 14f84bc9 Scott Ullrich
	}
188 45d6ada5 Sjon Hortensius
189 947141fd Phil Davis
	events.push(function() {
190 45d6ada5 Sjon Hortensius
		setInterval('getpftopactivity()', 2500);
191
		getpftopactivity();
192
	});
193 8fd9052f Colin Fleming
//]]>
194 14f84bc9 Scott Ullrich
</script>
195 00271265 NewEraCracker
196 45d6ada5 Sjon Hortensius
<div class="panel panel-default">
197 f17594c7 Sjon Hortensius
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Output')?></h2></div>
198 45d6ada5 Sjon Hortensius
	<div class="panel panel-body">
199
		<pre id="xhrOutput"><?=gettext("Gathering pfTOP activity, please wait...")?></pre>
200 14f84bc9 Scott Ullrich
	</div>
201 99f089be Colin Fleming
</div>
202 45d6ada5 Sjon Hortensius
203 8fd9052f Colin Fleming
<script type="text/javascript">
204
//<![CDATA[
205 947141fd Phil Davis
events.push(function() {
206
	$('#viewtype').on('change', function() {
207
		if (['queue', 'label', 'rules'].indexOf($(this).val()) > -1) {
208 253102fd Viktor G
			$("#filter, #sorttype, #sorttypediv, #statesdiv, #states").parents('.form-group').hide();
209 947141fd Phil Davis
		} else {
210 253102fd Viktor G
			$("#filter, #sorttype, #sorttypediv, #statesdiv, #states").parents('.form-group').show();
211 947141fd Phil Davis
		}
212 45d6ada5 Sjon Hortensius
	});
213 e2654541 jim-p
	$('#filter').on('keypress keyup', function(event) {
214
		var keyPressed = event.keyCode || event.which;
215
		if (keyPressed === 13) {
216
			event.preventDefault();
217
			return false;
218
		}
219
	});
220 c668eaed Warren Baker
});
221 8fd9052f Colin Fleming
//]]>
222 c668eaed Warren Baker
</script>
223 c10cb196 Stephen Beaver
<?php include("foot.inc");