Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1 c73fec1f jim-p
<?php
2
/*
3
	diag_testport.php
4
	Copyright (C) 2013 Jim P (jimp@pfsense.org)
5 ed2d1343 Renato Botelho
        Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6 c73fec1f jim-p
	All rights reserved.
7
8
	Portions based on diag_ping.php
9
	part of m0n0wall (http://m0n0.ch/wall)
10
	Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15
16
	1. Redistributions of source code must retain the above copyright notice,
17
	this list of conditions and the following disclaimer.
18
19
	2. Redistributions in binary form must reproduce the above copyright
20
	notice, this list of conditions and the following disclaimer in the
21
	documentation and/or other materials provided with the distribution.
22
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34
35
/*
36
	pfSense_BUILDER_BINARIES:	/usr/bin/nc
37
	pfSense_MODULE:	routing
38
*/
39
40
##|+PRIV
41
##|*IDENT=page-diagnostics-testport
42
##|*NAME=Diagnostics: Test Port
43
##|*DESCR=Allow access to the 'Diagnostics: Test Port' page.
44
##|*MATCH=diag_testport.php*
45
##|-PRIV
46
47
$allowautocomplete = true;
48
49
$pgtitle = array(gettext("Diagnostics"), gettext("Test Port"));
50
require("guiconfig.inc");
51
52
define('NC_TIMEOUT', 10);
53
54
if ($_POST || $_REQUEST['host']) {
55
	unset($input_errors);
56
	unset($do_testport);
57
58
	/* input validation */
59
	$reqdfields = explode(" ", "host port");
60
	$reqdfieldsn = array(gettext("Host"),gettext("Port"));
61 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
62 c73fec1f jim-p
63
	if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) {
64 d8b8937d jim-p
		$input_errors[] = gettext("Please enter a valid IP or hostname.");
65 c73fec1f jim-p
	}
66
67
	if (!is_port($_REQUEST['port'])) {
68 d8b8937d jim-p
		$input_errors[] = gettext("Please enter a valid port number.");
69 c73fec1f jim-p
	}
70
71 46d3f6a6 jim-p
	if (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport'])) {
72 d8b8937d jim-p
		$input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
73 c73fec1f jim-p
	}
74
75
	if (is_ipaddrv4($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv6")) {
76
		$input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6.");
77
	}
78
	if (is_ipaddrv6($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv4")) {
79
		$input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4.");
80
	}
81
82
	if (!$input_errors) {
83
		$do_testport = true;
84
		$host = $_REQUEST['host'];
85 6aa305d2 jim-p
		$sourceip = $_REQUEST['sourceip'];
86 c73fec1f jim-p
		$port = $_REQUEST['port'];
87
		$srcport = $_REQUEST['srcport'];
88
		$showtext = isset($_REQUEST['showtext']);
89
		$ipprotocol = $_REQUEST['ipprotocol'];
90
		$timeout = NC_TIMEOUT;
91
	}
92
}
93
if (!isset($do_testport)) {
94
	$do_testport = false;
95
	$host = '';
96
	$port = '';
97
	$srcport = '';
98
	unset($showtext);
99
}
100
101
include("head.inc"); ?>
102
<body link="#000000" vlink="#000000" alink="#000000">
103 a3381369 Colin Fleming
<?php include("fbegin.inc"); ?>
104 2231fe29 Colin Fleming
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag test port">
105 c73fec1f jim-p
<tr><td>
106
<?php echo gettext("This page allows you to perform a simple TCP connection test to determine if a host is up and accepting connections on a given port. This test does not function for UDP since there is no way to reliably determine if a UDP port accepts connections in this manner."); ?>
107 8cd558b6 ayvis
<br /><br />
108 d8b8937d jim-p
<?php echo gettext("No data is transmitted to the remote host during this test, it will only attempt to open a connection and optionally display the data sent back from the server."); ?>
109 8cd558b6 ayvis
<br /><br /><br />
110 c73fec1f jim-p
<?php if ($input_errors) print_input_errors($input_errors); ?>
111
	<form action="diag_testport.php" method="post" name="iform" id="iform">
112 2231fe29 Colin Fleming
	<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
113 c73fec1f jim-p
		<tr>
114
			<td colspan="2" valign="top" class="listtopic"><?=gettext("Test Port"); ?></td>
115
		</tr>
116
		<tr>
117
			<td width="22%" valign="top" class="vncellreq"><?=gettext("Host"); ?></td>
118
			<td width="78%" class="vtable">
119
			<?=$mandfldhtml;?>
120 2231fe29 Colin Fleming
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>" /></td>
121 c73fec1f jim-p
		</tr>
122
		<tr>
123
			<td width="22%" valign="top" class="vncellreq"><?= gettext("Port"); ?></td>
124
			<td width="78%" class="vtable">
125 2231fe29 Colin Fleming
				<input name="port" type="text" class="formfld" id="port" size="10" value="<?=htmlspecialchars($port);?>" />
126 c73fec1f jim-p
			</td>
127
		</tr>
128
		<tr>
129
			<td width="22%" valign="top" class="vncell"><?= gettext("Source Port"); ?></td>
130
			<td width="78%" class="vtable">
131 2231fe29 Colin Fleming
				<input name="srcport" type="text" class="formfld" id="srcport" size="10" value="<?=htmlspecialchars($srcport);?>" />
132 8cd558b6 ayvis
				<br /><br /><?php echo gettext("This should typically be left blank."); ?>
133 c73fec1f jim-p
			</td>
134
		</tr>
135
		<tr>
136
			<td width="22%" valign="top" class="vncell"><?= gettext("Show Remote Text"); ?></td>
137
			<td width="78%" class="vtable">
138 2231fe29 Colin Fleming
				<input name="showtext" type="checkbox" id="showtext" <?php if ($showtext) echo "checked=\"checked\"" ?> />
139 8cd558b6 ayvis
				<br /><br /><?php echo gettext("Shows the text given by the server when connecting to the port. Will take 10+ seconds to display if checked."); ?>
140 c73fec1f jim-p
			</td>
141
		</tr>
142
		<tr>
143 6aa305d2 jim-p
			<td width="22%" valign="top" class="vncell"><?=gettext("Source Address"); ?></td>
144 c73fec1f jim-p
			<td width="78%" class="vtable">
145 6aa305d2 jim-p
				<select name="sourceip" class="formselect">
146
					<option value="">Any</option>
147 81448ffa jim-p
				<?php   $sourceips = get_possible_traffic_source_addresses(true);
148 6aa305d2 jim-p
					foreach ($sourceips as $sip):
149
						$selected = "";
150
						if (!link_interface_to_bridge($sip['value']) && ($sip['value'] == $sourceip))
151 2231fe29 Colin Fleming
							$selected = "selected=\"selected\"";
152 6aa305d2 jim-p
				?>
153
					<option value="<?=$sip['value'];?>" <?=$selected;?>>
154
						<?=htmlspecialchars($sip['name']);?>
155
					</option>
156
					<?php endforeach; ?>
157
				</select>
158 c73fec1f jim-p
			</td>
159
		</tr>
160
		<tr>
161
			<td width="22%" valign="top" class="vncell"><?=gettext("IP Protocol"); ?></td>
162
			<td width="78%" class="vtable">
163
			<select name="ipprotocol" class="formfld">
164 2231fe29 Colin Fleming
				<option value="any" <?php if ("any" == $ipprotocol) echo "selected=\"selected\""; ?>>
165 c73fec1f jim-p
					Any
166
				</option>
167 2231fe29 Colin Fleming
				<option value="ipv4" <?php if ($ipprotocol == "ipv4") echo "selected=\"selected\""; ?>>
168 c73fec1f jim-p
					<?=gettext("IPv4");?>
169
				</option>
170 2231fe29 Colin Fleming
				<option value="ipv6" <?php if ($ipprotocol == "ipv6") echo "selected=\"selected\""; ?>>
171 c73fec1f jim-p
					<?=gettext("IPv6");?>
172
				</option>
173
			</select>
174 8cd558b6 ayvis
			<br /><br />
175 c73fec1f jim-p
			<?php echo gettext("If you force IPv4 or IPv6 and use a hostname that does not contain a result using that protocol, it will result in an error. For example if you force IPv4 and use a hostname that only returns an AAAA IPv6 IP address, it will not work."); ?>
176
			</td>
177
		</tr>
178
		<tr>
179
			<td width="22%" valign="top">&nbsp;</td>
180
			<td width="78%">
181 2231fe29 Colin Fleming
				<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Test"); ?>" />
182 c73fec1f jim-p
			</td>
183
		</tr>
184
		<tr>
185
		<td valign="top" colspan="2">
186 a3381369 Colin Fleming
		<?php if ($do_testport) {
187 2231fe29 Colin Fleming
			echo "<font face=\"terminal\" size=\"2\">";
188 8cd558b6 ayvis
			echo "<strong>" . gettext("Port Test Results") . ":</strong><br />";
189 2231fe29 Colin Fleming
		?>
190
			<script type="text/javascript">
191
			//<![CDATA[
192
			window.onload=function(){
193
				document.getElementById("testportCaptured").wrap='off';
194
			}
195
			//]]>
196
			</script>
197
		<?php
198
			echo "<textarea id=\"testportCaptured\" style=\"width:98%\" name=\"code\" rows=\"15\" cols=\"66\" readonly=\"readonly\">";
199 c73fec1f jim-p
			$result = "";
200
			$nc_base_cmd = "/usr/bin/nc";
201 46d3f6a6 jim-p
			$nc_args = "-w " . escapeshellarg($timeout);
202 c73fec1f jim-p
			if (!$showtext)
203
				$nc_args .= " -z ";
204
			if (!empty($srcport))
205 46d3f6a6 jim-p
				$nc_args .= " -p " . escapeshellarg($srcport) . " ";
206 c73fec1f jim-p
207
			/* Attempt to determine the interface address, if possible. Else try both. */
208
			if (is_ipaddrv4($host)) {
209 6aa305d2 jim-p
				$ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip);
210 c73fec1f jim-p
				$nc_args .= " -4";
211
			} elseif (is_ipaddrv6($host)) {
212 d318da67 Renato Botelho
				if ($sourceip == "any")
213
					$ifaddr = "";
214
				else if (is_linklocal($sourceip))
215
					$ifaddr = $sourceip;
216
				else
217
					$ifaddr = get_interface_ipv6($sourceip);
218 c73fec1f jim-p
				$nc_args .= " -6";
219
			} else {
220
				switch ($ipprotocol) {
221
					case "ipv4":
222 6aa305d2 jim-p
						$ifaddr = get_interface_ip($sourceip);
223 c73fec1f jim-p
						$nc_ipproto = " -4";
224
						break;
225
					case "ipv6":
226 d318da67 Renato Botelho
						$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
227 c73fec1f jim-p
						$nc_ipproto = " -6";
228
						break;
229
					case "any":
230 6aa305d2 jim-p
						$ifaddr = get_interface_ip($sourceip);
231 c73fec1f jim-p
						$nc_ipproto = (!empty($ifaddr)) ? " -4" : "";
232
						if (empty($ifaddr)) {
233 d318da67 Renato Botelho
							$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
234 c73fec1f jim-p
							$nc_ipproto = (!empty($ifaddr)) ? " -6" : "";
235
						}
236
						break;
237
				}
238
				/* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
239
				if (!empty($ifaddr)) {
240
					$nc_args .= $nc_ipproto;
241 6aa305d2 jim-p
				} elseif ($sourceip == "any") {
242 c73fec1f jim-p
					switch ($ipprotocol) {
243
						case "ipv4":
244
							$nc_ipproto = " -4";
245
							break;
246
						case "ipv6":
247
							$nc_ipproto = " -6";
248
							break;
249
					}
250
					$nc_args .= $nc_ipproto;
251
				}
252
			}
253
			/* Only add on the interface IP if we managed to find one. */
254 d318da67 Renato Botelho
			if (!empty($ifaddr)) {
255 6aa305d2 jim-p
				$nc_args .= " -s " . escapeshellarg($ifaddr) . " ";
256 d318da67 Renato Botelho
				$scope = get_ll_scope($ifaddr);
257
				if (!empty($scope) && !strstr($host, "%"))
258
					$host .= "%{$scope}";
259
			}
260 c73fec1f jim-p
261
			$nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . " " . escapeshellarg($port) . " 2>&1";
262
			exec($nc_cmd, $result, $retval);
263
			//echo "NC CMD: {$nc_cmd}\n\n";
264
			if (empty($result)) {
265
				if ($showtext)
266
					echo gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first.");
267
				else
268
					echo gettext("Connection failed (Refused/Timeout)");
269
			} else {
270
				if (is_array($result)) {
271
					foreach ($result as $resline) {
272
						echo htmlspecialchars($resline) . "\n";
273
					}
274
				} else {
275
					echo htmlspecialchars($result);
276
				}
277
			}
278 2231fe29 Colin Fleming
			echo '</textarea>&nbsp;</font>' ;
279 c73fec1f jim-p
		}
280
		?>
281
		</td>
282
		</tr>
283
	</table>
284
</form>
285
</td></tr></table>
286
<?php include("fend.inc"); ?>
287 2231fe29 Colin Fleming
</body>
288
</html>