Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_testport.php
4
	Copyright (C) 2013 Jim P (jimp@pfsense.org)
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	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
$do_testport = false;
54

    
55
if ($_POST || $_REQUEST['host']) {
56
	unset($input_errors);
57

    
58
	/* input validation */
59
	$reqdfields = explode(" ", "host port");
60
	$reqdfieldsn = array(gettext("Host"),gettext("Port"));
61
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
62

    
63
	if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) {
64
		$input_errors[] = gettext("Please enter a valid IP or hostname.");
65
	}
66

    
67
	if (!is_port($_REQUEST['port'])) {
68
		$input_errors[] = gettext("Please enter a valid port number.");
69
	}
70

    
71
	if (($_REQUEST['srcport'] != "") && (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport']))) {
72
		$input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
73
	}
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
		$timeout = NC_TIMEOUT;
85
	}
86
	
87
	/* Save these request vars even if there were input errors. Then the fields are refilled for the user to correct. */
88
	$host = $_REQUEST['host'];
89
	$sourceip = $_REQUEST['sourceip'];
90
	$port = $_REQUEST['port'];
91
	$srcport = $_REQUEST['srcport'];
92
	$showtext = isset($_REQUEST['showtext']);
93
	$ipprotocol = $_REQUEST['ipprotocol'];
94
}
95

    
96
include("head.inc"); ?>
97
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
98
<?php include("fbegin.inc"); ?>
99
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag test port">
100
<tr><td>
101
<?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."); ?>
102
<br /><br />
103
<?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."); ?>
104
<br /><br /><br />
105
<?php if ($input_errors) print_input_errors($input_errors); ?>
106
	<form action="diag_testport.php" method="post" name="iform" id="iform">
107
	<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
108
		<tr>
109
			<td colspan="2" valign="top" class="listtopic"><?=gettext("Test Port"); ?></td>
110
		</tr>
111
		<tr>
112
			<td width="22%" valign="top" class="vncellreq"><?=gettext("Host"); ?></td>
113
			<td width="78%" class="vtable">
114
			<?=$mandfldhtml;?>
115
			<input name="host" type="text" class="formfld unknown" id="host" size="20" value="<?=htmlspecialchars($host);?>" /></td>
116
		</tr>
117
		<tr>
118
			<td width="22%" valign="top" class="vncellreq"><?= gettext("Port"); ?></td>
119
			<td width="78%" class="vtable">
120
				<input name="port" type="text" class="formfld unknown" id="port" size="10" value="<?=htmlspecialchars($port);?>" />
121
			</td>
122
		</tr>
123
		<tr>
124
			<td width="22%" valign="top" class="vncell"><?= gettext("Source Port"); ?></td>
125
			<td width="78%" class="vtable">
126
				<input name="srcport" type="text" class="formfld unknown" id="srcport" size="10" value="<?=htmlspecialchars($srcport);?>" />
127
				<br /><br /><?php echo gettext("This should typically be left blank."); ?>
128
			</td>
129
		</tr>
130
		<tr>
131
			<td width="22%" valign="top" class="vncell"><?= gettext("Show Remote Text"); ?></td>
132
			<td width="78%" class="vtable">
133
				<input name="showtext" type="checkbox" id="showtext" <?php if ($showtext) echo "checked=\"checked\"" ?> />
134
				<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."); ?>
135
			</td>
136
		</tr>
137
		<tr>
138
			<td width="22%" valign="top" class="vncell"><?=gettext("Source Address"); ?></td>
139
			<td width="78%" class="vtable">
140
				<select name="sourceip" class="formselect">
141
					<option value="">Any</option>
142
				<?php   $sourceips = get_possible_traffic_source_addresses(true);
143
					foreach ($sourceips as $sip):
144
						$selected = "";
145
						if (!link_interface_to_bridge($sip['value']) && ($sip['value'] == $sourceip))
146
							$selected = "selected=\"selected\"";
147
				?>
148
					<option value="<?=$sip['value'];?>" <?=$selected;?>>
149
						<?=htmlspecialchars($sip['name']);?>
150
					</option>
151
					<?php endforeach; ?>
152
				</select>
153
			</td>
154
		</tr>
155
		<tr>
156
			<td width="22%" valign="top" class="vncell"><?=gettext("IP Protocol"); ?></td>
157
			<td width="78%" class="vtable">
158
			<select name="ipprotocol" class="formfld">
159
				<option value="any" <?php if ("any" == $ipprotocol) echo "selected=\"selected\""; ?>>
160
					Any
161
				</option>
162
				<option value="ipv4" <?php if ($ipprotocol == "ipv4") echo "selected=\"selected\""; ?>>
163
					<?=gettext("IPv4");?>
164
				</option>
165
				<option value="ipv6" <?php if ($ipprotocol == "ipv6") echo "selected=\"selected\""; ?>>
166
					<?=gettext("IPv6");?>
167
				</option>
168
			</select>
169
			<br /><br />
170
			<?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."); ?>
171
			</td>
172
		</tr>
173
		<tr>
174
			<td width="22%" valign="top">&nbsp;</td>
175
			<td width="78%">
176
				<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Test"); ?>" />
177
			</td>
178
		</tr>
179
		<tr>
180
		<td valign="top" colspan="2">
181
		<?php if ($do_testport) {
182
			echo "<font face=\"terminal\" size=\"2\">";
183
			echo "<strong>" . gettext("Port Test Results") . ":</strong><br />";
184
		?>
185
			<script type="text/javascript">
186
			//<![CDATA[
187
			window.onload=function(){
188
				document.getElementById("testportCaptured").wrap='off';
189
			}
190
			//]]>
191
			</script>
192
		<?php
193
			echo "<textarea id=\"testportCaptured\" style=\"width:98%\" name=\"code\" rows=\"15\" cols=\"66\" readonly=\"readonly\">";
194
			$result = "";
195
			$nc_base_cmd = "/usr/bin/nc";
196
			$nc_args = "-w " . escapeshellarg($timeout);
197
			if (!$showtext)
198
				$nc_args .= " -z ";
199
			if (!empty($srcport))
200
				$nc_args .= " -p " . escapeshellarg($srcport) . " ";
201

    
202
			/* Attempt to determine the interface address, if possible. Else try both. */
203
			if (is_ipaddrv4($host)) {
204
				if ($sourceip == "any") {
205
					$ifaddr = "";
206
				} else {
207
					if (is_ipaddr($sourceip)) {
208
						$ifaddr = $sourceip;
209
					} else {
210
						$ifaddr = get_interface_ip($sourceip);
211
					}
212
				}
213
				$nc_args .= " -4";
214
			} elseif (is_ipaddrv6($host)) {
215
				if ($sourceip == "any")
216
					$ifaddr = "";
217
				else if (is_linklocal($sourceip))
218
					$ifaddr = $sourceip;
219
				else
220
					$ifaddr = get_interface_ipv6($sourceip);
221
				$nc_args .= " -6";
222
			} else {
223
				switch ($ipprotocol) {
224
					case "ipv4":
225
						$ifaddr = get_interface_ip($sourceip);
226
						$nc_ipproto = " -4";
227
						break;
228
					case "ipv6":
229
						$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
230
						$nc_ipproto = " -6";
231
						break;
232
					case "any":
233
						$ifaddr = get_interface_ip($sourceip);
234
						$nc_ipproto = (!empty($ifaddr)) ? " -4" : "";
235
						if (empty($ifaddr)) {
236
							$ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
237
							$nc_ipproto = (!empty($ifaddr)) ? " -6" : "";
238
						}
239
						break;
240
				}
241
				/* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
242
				if (!empty($ifaddr)) {
243
					$nc_args .= $nc_ipproto;
244
				} elseif ($sourceip == "any") {
245
					switch ($ipprotocol) {
246
						case "ipv4":
247
							$nc_ipproto = " -4";
248
							break;
249
						case "ipv6":
250
							$nc_ipproto = " -6";
251
							break;
252
					}
253
					$nc_args .= $nc_ipproto;
254
				}
255
			}
256
			/* Only add on the interface IP if we managed to find one. */
257
			if (!empty($ifaddr)) {
258
				$nc_args .= " -s " . escapeshellarg($ifaddr) . " ";
259
				$scope = get_ll_scope($ifaddr);
260
				if (!empty($scope) && !strstr($host, "%"))
261
					$host .= "%{$scope}";
262
			}
263

    
264
			$nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . " " . escapeshellarg($port) . " 2>&1";
265
			exec($nc_cmd, $result, $retval);
266
			//echo "NC CMD: {$nc_cmd}\n\n";
267
			if (empty($result)) {
268
				if ($showtext)
269
					echo gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first.");
270
				else
271
					echo gettext("Connection failed (Refused/Timeout)");
272
			} else {
273
				if (is_array($result)) {
274
					foreach ($result as $resline) {
275
						echo htmlspecialchars($resline) . "\n";
276
					}
277
				} else {
278
					echo htmlspecialchars($result);
279
				}
280
			}
281
			echo '</textarea>&nbsp;</font>' ;
282
		}
283
		?>
284
		</td>
285
		</tr>
286
	</table>
287
</form>
288
</td></tr></table>
289
<?php include("fend.inc"); ?>
290
</body>
291
</html>
(51-51/251)