summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSander van Leeuwen <sander@kraagjes.nl>2015-04-22 22:52:30 +0200
committerSander van Leeuwen <sander@kraagjes.nl>2015-04-22 22:52:30 +0200
commit7aac34133e50f907212cddde1b4a38a4d726c785 (patch)
tree2d0877d5e172c4f03c6ce20d8c1084e438352434
parent8c10899b42768a9bc9c642eda86837af6f1c52f4 (diff)
parentdfd6f17907c073fbc575f9ba9866db385b30dae6 (diff)
downloadpfsense-7aac34133e50f907212cddde1b4a38a4d726c785.zip
pfsense-7aac34133e50f907212cddde1b4a38a4d726c785.tar.gz
Merge pull request #99 from sbeaver-netgate/diag_testport
Diag testport
-rw-r--r--usr/local/www/diag_testport.php361
1 files changed, 185 insertions, 176 deletions
diff --git a/usr/local/www/diag_testport.php b/usr/local/www/diag_testport.php
index 393c757..50fe3e5 100644
--- a/usr/local/www/diag_testport.php
+++ b/usr/local/www/diag_testport.php
@@ -2,7 +2,7 @@
/*
diag_testport.php
Copyright (C) 2013 Jim P (jimp@pfsense.org)
- Copyright (C) 2013-2015 Electric Sheep Fencing, LP
+
All rights reserved.
Portions based on diag_ping.php
@@ -34,7 +34,7 @@
/*
pfSense_BUILDER_BINARIES: /usr/bin/nc
- pfSense_MODULE: routing
+ pfSense_MODULE: routing
*/
##|+PRIV
@@ -44,6 +44,9 @@
##|*MATCH=diag_testport.php*
##|-PRIV
+// Calling netcat and parsing hte results has been moved to the if ($_POST) section so that the results are known
+// before we draw the form and any resulting error messages will allear in the correct place
+
$allowautocomplete = true;
$pgtitle = array(gettext("Diagnostics"), gettext("Test Port"));
@@ -51,6 +54,18 @@ require("guiconfig.inc");
define('NC_TIMEOUT', 10);
$do_testport = false;
+$retval = 1;
+
+function create_sourceaddresslist() {
+ $list = array('any' => 'Any');
+
+ $sourceips = get_possible_traffic_source_addresses(true);
+
+ foreach ($sourceips as $sipvalue => $sipname)
+ $list[$sipname[value]] = $sipname[name];
+
+ return($list);
+}
if ($_POST || $_REQUEST['host']) {
unset($input_errors);
@@ -83,7 +98,7 @@ if ($_POST || $_REQUEST['host']) {
$do_testport = true;
$timeout = NC_TIMEOUT;
}
-
+
/* Save these request vars even if there were input errors. Then the fields are refilled for the user to correct. */
$host = $_REQUEST['host'];
$sourceip = $_REQUEST['sourceip'];
@@ -91,193 +106,187 @@ if ($_POST || $_REQUEST['host']) {
$srcport = $_REQUEST['srcport'];
$showtext = isset($_REQUEST['showtext']);
$ipprotocol = $_REQUEST['ipprotocol'];
-}
-include("head.inc"); ?>
-<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
-<?php include("fbegin.inc"); ?>
-<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag test port">
-<tr><td>
-<?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."); ?>
-<br /><br />
-<?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."); ?>
-<br /><br /><br />
-<?php if ($input_errors) print_input_errors($input_errors); ?>
- <form action="diag_testport.php" method="post" name="iform" id="iform">
- <table width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
- <tr>
- <td colspan="2" valign="top" class="listtopic"><?=gettext("Test Port"); ?></td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncellreq"><?=gettext("Host"); ?></td>
- <td width="78%" class="vtable">
- <?=$mandfldhtml;?>
- <input name="host" type="text" class="formfld unknown" id="host" size="20" value="<?=htmlspecialchars($host);?>" /></td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncellreq"><?= gettext("Port"); ?></td>
- <td width="78%" class="vtable">
- <input name="port" type="text" class="formfld unknown" id="port" size="10" value="<?=htmlspecialchars($port);?>" />
- </td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncell"><?= gettext("Source Port"); ?></td>
- <td width="78%" class="vtable">
- <input name="srcport" type="text" class="formfld unknown" id="srcport" size="10" value="<?=htmlspecialchars($srcport);?>" />
- <br /><br /><?php echo gettext("This should typically be left blank."); ?>
- </td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncell"><?= gettext("Show Remote Text"); ?></td>
- <td width="78%" class="vtable">
- <input name="showtext" type="checkbox" id="showtext" <?php if ($showtext) echo "checked=\"checked\"" ?> />
- <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."); ?>
- </td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncell"><?=gettext("Source Address"); ?></td>
- <td width="78%" class="vtable">
- <select name="sourceip" class="formselect">
- <option value="">Any</option>
- <?php $sourceips = get_possible_traffic_source_addresses(true);
- foreach ($sourceips as $sipvalue => $sipname):
- $selected = "";
- if (!link_interface_to_bridge($sipvalue) && ($sipvalue == $sourceip))
- $selected = "selected=\"selected\"";
- ?>
- <option value="<?=$sipvalue;?>" <?=$selected;?>>
- <?=htmlspecialchars($sipname);?>
- </option>
- <?php endforeach; ?>
- </select>
- </td>
- </tr>
- <tr>
- <td width="22%" valign="top" class="vncell"><?=gettext("IP Protocol"); ?></td>
- <td width="78%" class="vtable">
- <select name="ipprotocol" class="formfld">
- <option value="any" <?php if ("any" == $ipprotocol) echo "selected=\"selected\""; ?>>
- Any
- </option>
- <option value="ipv4" <?php if ($ipprotocol == "ipv4") echo "selected=\"selected\""; ?>>
- <?=gettext("IPv4");?>
- </option>
- <option value="ipv6" <?php if ($ipprotocol == "ipv6") echo "selected=\"selected\""; ?>>
- <?=gettext("IPv6");?>
- </option>
- </select>
- <br /><br />
- <?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."); ?>
- </td>
- </tr>
- <tr>
- <td width="22%" valign="top">&nbsp;</td>
- <td width="78%">
- <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Test"); ?>" />
- </td>
- </tr>
- <tr>
- <td valign="top" colspan="2">
- <?php if ($do_testport) {
- echo "<font face=\"terminal\" size=\"2\">";
- echo "<strong>" . gettext("Port Test Results") . ":</strong><br />";
- ?>
- <script type="text/javascript">
+ if ( $do_testport ) {
+?>
+ <script type="text/javascript">
//<![CDATA[
window.onload=function(){
document.getElementById("testportCaptured").wrap='off';
}
//]]>
- </script>
- <?php
- echo "<textarea id=\"testportCaptured\" style=\"width:98%\" name=\"code\" rows=\"15\" cols=\"66\" readonly=\"readonly\">";
- $result = "";
- $nc_base_cmd = "/usr/bin/nc";
- $nc_args = "-w " . escapeshellarg($timeout);
- if (!$showtext)
- $nc_args .= " -z ";
- if (!empty($srcport))
- $nc_args .= " -p " . escapeshellarg($srcport) . " ";
-
- /* Attempt to determine the interface address, if possible. Else try both. */
- if (is_ipaddrv4($host)) {
- $ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip);
- $nc_args .= " -4";
- } elseif (is_ipaddrv6($host)) {
- if ($sourceip == "any")
- $ifaddr = "";
- else if (is_linklocal($sourceip))
- $ifaddr = $sourceip;
- else
- $ifaddr = get_interface_ipv6($sourceip);
- $nc_args .= " -6";
- } else {
+ </script>
+<?php
+ $result = "";
+ $ncoutput = "";
+ $nc_base_cmd = '/usr/bin/nc';
+ $nc_args = "-w " . escapeshellarg($timeout);
+ if (!$showtext)
+ $nc_args .= ' -z ';
+ if (!empty($srcport))
+ $nc_args .= ' -p ' . escapeshellarg($srcport) . ' ';
+
+ /* Attempt to determine the interface address, if possible. Else try both. */
+ if (is_ipaddrv4($host)) {
+ $ifaddr = ($sourceip == "any") ? "" : get_interface_ip($sourceip);
+ $nc_args .= ' -4';
+ } elseif (is_ipaddrv6($host)) {
+ if ($sourceip == "any")
+ $ifaddr = '';
+ else if (is_linklocal($sourceip))
+ $ifaddr = $sourceip;
+ else
+ $ifaddr = get_interface_ipv6($sourceip);
+ $nc_args .= ' -6';
+ } else {
+ switch ($ipprotocol) {
+ case "ipv4":
+ $ifaddr = get_interface_ip($sourceip);
+ $nc_ipproto = ' -4';
+ break;
+ case "ipv6":
+ $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
+ $nc_ipproto = ' -6';
+ break;
+ case "any":
+ $ifaddr = get_interface_ip($sourceip);
+ $nc_ipproto = (!empty($ifaddr)) ? ' -4' : '';
+ if (empty($ifaddr)) {
+ $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
+ $nc_ipproto = (!empty($ifaddr)) ? ' -6' : '';
+ }
+ break;
+ }
+ /* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
+ if (!empty($ifaddr)) {
+ $nc_args .= $nc_ipproto;
+ } elseif ($sourceip == "any") {
switch ($ipprotocol) {
case "ipv4":
- $ifaddr = get_interface_ip($sourceip);
- $nc_ipproto = " -4";
+ $nc_ipproto = ' -4';
break;
case "ipv6":
- $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
- $nc_ipproto = " -6";
- break;
- case "any":
- $ifaddr = get_interface_ip($sourceip);
- $nc_ipproto = (!empty($ifaddr)) ? " -4" : "";
- if (empty($ifaddr)) {
- $ifaddr = (is_linklocal($sourceip) ? $sourceip : get_interface_ipv6($sourceip));
- $nc_ipproto = (!empty($ifaddr)) ? " -6" : "";
- }
+ $nc_ipproto = ' -6';
break;
}
- /* Netcat doesn't like it if we try to connect using a certain type of IP without specifying the family. */
- if (!empty($ifaddr)) {
- $nc_args .= $nc_ipproto;
- } elseif ($sourceip == "any") {
- switch ($ipprotocol) {
- case "ipv4":
- $nc_ipproto = " -4";
- break;
- case "ipv6":
- $nc_ipproto = " -6";
- break;
- }
- $nc_args .= $nc_ipproto;
- }
- }
- /* Only add on the interface IP if we managed to find one. */
- if (!empty($ifaddr)) {
- $nc_args .= " -s " . escapeshellarg($ifaddr) . " ";
- $scope = get_ll_scope($ifaddr);
- if (!empty($scope) && !strstr($host, "%"))
- $host .= "%{$scope}";
+ $nc_args .= $nc_ipproto;
}
+ }
+ /* Only add on the interface IP if we managed to find one. */
+ if (!empty($ifaddr)) {
+ $nc_args .= ' -s ' . escapeshellarg($ifaddr) . ' ';
+ $scope = get_ll_scope($ifaddr);
+ if (!empty($scope) && !strstr($host, "%"))
+ $host .= "%{$scope}";
+ }
- $nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . " " . escapeshellarg($port) . " 2>&1";
- exec($nc_cmd, $result, $retval);
- //echo "NC CMD: {$nc_cmd}\n\n";
- if (empty($result)) {
- if ($showtext)
- echo gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first.");
- else
- echo gettext("Connection failed (Refused/Timeout)");
- } else {
- if (is_array($result)) {
- foreach ($result as $resline) {
- echo htmlspecialchars($resline) . "\n";
- }
- } else {
- echo htmlspecialchars($result);
+ $nc_cmd = "{$nc_base_cmd} {$nc_args} " . escapeshellarg($host) . ' ' . escapeshellarg($port) . ' 2>&1';
+ exec($nc_cmd, $result, $retval);
+ // echo "NC CMD: {$nc_cmd}\n\n";
+
+ if (!empty($result)) {
+ if (is_array($result)) {
+ foreach ($result as $resline) {
+ $ncoutput .= htmlspecialchars($resline) . "\n";
}
+ } else {
+ $ncoutput .= htmlspecialchars($result);
}
- echo '</textarea>&nbsp;</font>' ;
}
- ?>
- </td>
- </tr>
- </table>
-</form>
-</td></tr></table>
-<?php include("fend.inc"); ?>
-</body>
-</html>
+ }
+}
+
+include("head.inc");
+
+// Handle the display of all messages here wher the user can readily see them
+if ($input_errors)
+ print_input_errors($input_errors);
+else {
+ // New page
+ if(empty($result) && $retval != 0 && !$showtext) {
+ print('<div class="alert alert-warning" role="alert">' . '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.' . '</div>');
+ }
+
+ // Good host & port
+ if($retval == 0 && $do_testport == 1) {
+ if(!$showtext)
+ print('<div class="alert alert-success" role="alert">'.gettext("Port test to host: " . $host . " Port: " . $port . " successful").'</div>');
+ else
+ print('<div class="alert alert-success" role="alert">'.gettext("Port test to host: " . $host . " Port: " . $port . " successful") . '. Any text received from teh host will be shown below the form.' . '</div>');
+ }
+
+ // netcat exit value != 0
+ if($retval != 0 && !empty($result))
+ if($showtext)
+ print('<div class="alert alert-danger" role="alert">'.gettext('No output received, or connection failed. Try with "Show Remote Text" unchecked first.').'</div>');
+ else
+ print('<div class="alert alert-danger" role="alert">'.gettext('Connection failed.').'</div>');
+}
+
+require('classes/Form.class.php');
+
+$form = new Form(new Form_Button(
+ 'Submit',
+ gettext('Test')
+));
+
+$section = new Form_Section('Test Port');
+
+$section->addInput(new Form_Input(
+ 'host',
+ 'Hostname',
+ 'text',
+ htmlspecialchars($host),
+ ['placeholder' => 'Hostname to look up.']
+));
+
+$section->addInput(new Form_Input(
+ 'port',
+ 'Port',
+ 'text',
+ htmlspecialchars($port),
+ ['placeholder' => 'Port to test.']
+));
+
+$section->addInput(new Form_Input(
+ 'srcport',
+ 'Source Port',
+ 'text',
+ htmlspecialchars($srcport),
+ ['placeholder' => 'Typically left blank.']
+));
+
+$section->addInput(new Form_Checkbox(
+ 'showtext',
+ 'Show Remote Text',
+ '',
+ $showtext
+))->setHelp(gettext("Shows the text given by the server when connecting to the port. If checked it will take 10+ seconds to display in a panel below this form."));
+
+$section->addInput(new Form_Select(
+ 'sourceip',
+ 'Source Address',
+ $pconfig['source'],
+ create_sourceaddresslist()
+))->setHelp('Select source address for the trace');
+
+$section->addInput(new Form_Select(
+ 'ipproto',
+ 'IP Protocol',
+ $pconfig['protocol'],
+ array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
+))->setHelp(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."));
+
+$form->add($section);
+print $form;
+
+if($ncoutput && !empty($result) && $showtext && $retval == 0)
+ {
+ print("<div class=\"panel panel-default\">" . "<div class=\"panel-heading\">Received Remote Text</div>" . "<div class=\"panel-body\">");
+ print(nl2br($ncoutput));
+ print("</dev></div>");
+ }
+
+include("foot.inc"); ?>
OpenPOWER on IntegriCloud