Project

General

Profile

Download (9.39 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_testport.php
4

    
5
	Copyright (C) 2013 Jim P (jimp@pfsense.org)
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

    
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
	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 (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
		$host = $_REQUEST['host'];
85
		$interface = $_REQUEST['interface'];
86
		$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
<?php include("fbegin.inc"); ?>
104
<table width="100%" border="0" cellpadding="0" cellspacing="0">
105
<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
<br/><br/>
108
<?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
<br/><br/><br/>
110
<?php if ($input_errors) print_input_errors($input_errors); ?>
111
	<form action="diag_testport.php" method="post" name="iform" id="iform">
112
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
113
		<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
			<input name="host" type="text" class="formfld" id="host" size="20" value="<?=htmlspecialchars($host);?>"></td>
121
		</tr>
122
		<tr>
123
			<td width="22%" valign="top" class="vncellreq"><?= gettext("Port"); ?></td>
124
			<td width="78%" class="vtable">
125
				<input name="port" type="text" class="formfld" id="port" size="10" value="<?=htmlspecialchars($port);?>">
126
			</td>
127
		</tr>
128
		<tr>
129
			<td width="22%" valign="top" class="vncell"><?= gettext("Source Port"); ?></td>
130
			<td width="78%" class="vtable">
131
				<input name="srcport" type="text" class="formfld" id="srcport" size="10" value="<?=htmlspecialchars($srcport);?>">
132
				<br/><br/><?php echo gettext("This should typically be left blank."); ?>
133
			</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
				<input name="showtext" type="checkbox" id="showtext" <?php if ($showtext) echo "checked" ?>>
139
				</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
			</td>
141
		</tr>
142
		<tr>
143
			<td width="22%" valign="top" class="vncell"><?=gettext("Interface"); ?></td>
144
			<td width="78%" class="vtable">
145
			<select name="interface" class="formfld">
146
				<option value="any" <?php if ("any" == $interface) echo "selected"; ?>>
147
					Any
148
				</option>
149
			<?php $interfaces = get_configured_interface_with_descr();
150
			foreach ($interfaces as $iface => $ifacename): ?>
151
				<option value="<?=$iface;?>" <?php if (!link_interface_to_bridge($iface) && $iface == $interface) echo "selected"; ?>>
152
					<?=htmlspecialchars($ifacename);?>
153
				</option>
154
			<?php endforeach; ?>
155
			</select>
156
			</td>
157
		</tr>
158
		<tr>
159
			<td width="22%" valign="top" class="vncell"><?=gettext("IP Protocol"); ?></td>
160
			<td width="78%" class="vtable">
161
			<select name="ipprotocol" class="formfld">
162
				<option value="any" <?php if ("any" == $ipprotocol) echo "selected"; ?>>
163
					Any
164
				</option>
165
				<option value="ipv4" <?php if ($ipprotocol == "ipv4") echo "selected"; ?>>
166
					<?=gettext("IPv4");?>
167
				</option>
168
				<option value="ipv6" <?php if ($ipprotocol == "ipv6") echo "selected"; ?>>
169
					<?=gettext("IPv6");?>
170
				</option>
171
			</select>
172
			<br/><br/>
173
			<?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."); ?>
174
			</td>
175
		</tr>
176
		<tr>
177
			<td width="22%" valign="top">&nbsp;</td>
178
			<td width="78%">
179
				<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Test"); ?>">
180
			</td>
181
		</tr>
182
		<tr>
183
		<td valign="top" colspan="2">
184
		<?php if ($do_testport) {
185
			echo "<font face='terminal' size='2'>";
186
			echo "<strong>" . gettext("Port Test Results") . ":</strong><br>";
187
			echo '<pre>';
188
			$result = "";
189
			$nc_base_cmd = "/usr/bin/nc";
190
			$nc_args = "-w {$timeout}";
191
			if (!$showtext)
192
				$nc_args .= " -z ";
193
			if (!empty($srcport))
194
				$nc_args .= " -p {$srcport} ";
195

    
196
			/* Attempt to determine the interface address, if possible. Else try both. */
197
			if (is_ipaddrv4($host)) {
198
				$ifaddr = ($interface == "any") ? "" : get_interface_ip($interface);
199
				$nc_args .= " -4";
200
			} elseif (is_ipaddrv6($host)) {
201
				$ifaddr = ($interface == "any") ? "" : get_interface_ipv6($interface);
202
				$nc_args .= " -6";
203
			} else {
204
				switch ($ipprotocol) {
205
					case "ipv4":
206
						$ifaddr = get_interface_ip($interface);
207
						$nc_ipproto = " -4";
208
						break;
209
					case "ipv6":
210
						$ifaddr = get_interface_ipv6($interface);
211
						$nc_ipproto = " -6";
212
						break;
213
					case "any":
214
						$ifaddr = get_interface_ip($interface);
215
						$nc_ipproto = (!empty($ifaddr)) ? " -4" : "";
216
						if (empty($ifaddr)) {
217
							$ifaddr = get_interface_ipv6($interface);
218
							$nc_ipproto = (!empty($ifaddr)) ? " -6" : "";
219
						}
220
						break;
221
				}
222
				/* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
223
				if (!empty($ifaddr)) {
224
					$nc_args .= $nc_ipproto;
225
				} elseif ($interface == "any") {
226
					switch ($ipprotocol) {
227
						case "ipv4":
228
							$nc_ipproto = " -4";
229
							break;
230
						case "ipv6":
231
							$nc_ipproto = " -6";
232
							break;
233
					}
234
					$nc_args .= $nc_ipproto;
235
				}
236
			}
237
			/* Only add on the interface IP if we managed to find one. */
238
			if (!empty($ifaddr))
239
				$nc_args .= " -s {$ifaddr} ";
240

    
241
			$nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . " " . escapeshellarg($port) . " 2>&1";
242
			exec($nc_cmd, $result, $retval);
243
			//echo "NC CMD: {$nc_cmd}\n\n";
244
			if (empty($result)) {
245
				if ($showtext)
246
					echo gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first.");
247
				else
248
					echo gettext("Connection failed (Refused/Timeout)");
249
			} else {
250
				if (is_array($result)) {
251
					foreach ($result as $resline) {
252
						echo htmlspecialchars($resline) . "\n";
253
					}
254
				} else {
255
					echo htmlspecialchars($result);
256
				}
257
			}
258
			echo '</pre>' ;
259
		}
260
		?>
261
		</td>
262
		</tr>
263
	</table>
264
</form>
265
</td></tr></table>
266
<?php include("fend.inc"); ?>
(50-50/246)