Commit 68477040 authored by Ad Schellevis's avatar Ad Schellevis

(legacy) refactor diag_testport.php

parent 708a14bc
<?php <?php
/* /*
Copyright (C) 2014 Deciso B.V. Copyright (C) 2016 Deciso B.V.
Copyright (C) 2013 Jim Pingle <jimp@pfsense.org> Copyright (C) 2013 Jim Pingle <jimp@pfsense.org>
Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>. Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
require_once("guiconfig.inc"); require_once("guiconfig.inc");
require_once("system.inc"); require_once("system.inc");
require_once("interfaces.inc"); require_once("interfaces.inc");
define('NC_TIMEOUT', 10); $cmd_output = false;
$do_testport = false; if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// set form defaults
if ($_POST || $_REQUEST['host']) { $pconfig = array();
unset($input_errors); $pconfig['ipprotocol'] = 'ipv4';
$pconfig['host'] = null;
/* input validation */ $pconfig['port'] = null;
$reqdfields = explode(" ", "host port"); $pconfig['showtext'] = null;
$reqdfieldsn = array(gettext("Host"),gettext("Port")); $pconfig['sourceip'] = null;
do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors); } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pconfig = $_POST;
if (!is_ipaddr($_REQUEST['host']) && !is_hostname($_REQUEST['host'])) { $input_errors = array();
$input_errors[] = gettext("Please enter a valid IP or hostname.");
} /* input validation */
$reqdfields = explode(" ", "host port");
if (!is_port($_REQUEST['port'])) { $reqdfieldsn = array(gettext("Host"),gettext("Port"));
$input_errors[] = gettext("Please enter a valid port number."); do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);
}
if (!is_ipaddr($pconfig['host']) && !is_hostname($pconfig['host'])) {
if (($_REQUEST['srcport'] != "") && (!is_numeric($_REQUEST['srcport']) || !is_port($_REQUEST['srcport']))) { $input_errors[] = gettext("Please enter a valid IP or hostname.");
$input_errors[] = gettext("Please enter a valid source port number, or leave the field blank."); }
}
if (!is_port($pconfig['port'])) {
if (is_ipaddrv4($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv6")) { $input_errors[] = gettext("Please enter a valid port number.");
$input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6."); }
}
if (is_ipaddrv6($_REQUEST['host']) && ($_REQUEST['ipprotocol'] == "ipv4")) { if (($pconfig['srcport'] != "") && (!is_numeric($pconfig['srcport']) || !is_port($pconfig['srcport']))) {
$input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4."); $input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
} }
if (!$input_errors) { if (is_ipaddrv4($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv6")) {
$do_testport = true; $input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6.");
$timeout = NC_TIMEOUT; }
} if (is_ipaddrv6($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv4")) {
$input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4.");
/* 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']; if (count($input_errors) == 0) {
$port = $_REQUEST['port']; $nc_args = "-w 10" ;
$srcport = $_REQUEST['srcport']; if (empty($pconfig['showtext'])) {
$showtext = isset($_REQUEST['showtext']); $nc_args .= " -z ";
$ipprotocol = $_REQUEST['ipprotocol']; }
if (!empty($pconfig['srcport'])) {
$nc_args .= " -p " . escapeshellarg($pconfig['srcport']) . " ";
}
switch ($pconfig['ipprotocol']) {
case "ipv4":
$ifaddr = ($pconfig['sourceip'] == "any") ? "" : get_interface_ip($pconfig['sourceip']);
$nc_args .= " -4";
break;
case "ipv6":
$ifaddr = (is_linklocal($pconfig['sourceip']) ? $pconfig['sourceip'] : get_interface_ipv6($pconfig['sourceip']));
$nc_args .= " -6";
break;
}
if (!empty($ifaddr)) {
$nc_args .= " -s " . escapeshellarg($ifaddr) . " ";
$scope = get_ll_scope($ifaddr);
if (!empty($scope) && !strstr($host, "%")) {
$host .= "%{$scope}";
}
}
$cmd_action = "/usr/bin/nc {$nc_args} " . escapeshellarg($pconfig['host']) . " " . escapeshellarg($pconfig['port']) . " 2>&1";
$process = proc_open($cmd_action, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);
if (is_resource($process)) {
$cmd_output = stream_get_contents($pipes[1]);
$cmd_output .= stream_get_contents($pipes[2]);
}
}
} }
legacy_html_escape_form_data($pconfig);
include("head.inc"); ?> include("head.inc"); ?>
<body> <body>
<?php include("fbegin.inc"); ?> <?php include("fbegin.inc"); ?>
<section class="page-content-main"> <section class="page-content-main">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<section class="col-xs-12">
<section class="col-xs-12"> <div id="message" style="" class="alert alert-warning" role="alert">
<?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."); ?>
<?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 />
<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."); ?>
<?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."); ?> </div>
<br /><br /><br /> <div class="content-box">
<div class="content-box-main ">
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?> <?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
<form method="post" name="iform" id="iform">
<div class="content-box"> <div class="table-responsive">
<table class="table table-striped">
<header class="content-box-head container-fluid"> <thead>
<h3><?=gettext("Test Port"); ?></h3> <tr>
</header> <td width="22%"><strong><?=gettext("Test Port"); ?></strong></td>
<td width="78%" align="right">
<div class="content-box-main "> <small><?=gettext("full help"); ?> </small>
<form action="<?=$_SERVER['REQUEST_URI'];?>" method="post" name="iform" id="iform"> <i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
<div class="table-responsive"> &nbsp;
<table class="table table-striped __nomb"> </td>
<tbody> </tr>
<tr> </thead>
<td><?=gettext("Host"); ?></td> <tbody>
<td><input name="host" type="text" class="form-control" id="host" value="<?=htmlspecialchars($host);?>" /></td> <tr>
</tr> <td><a id="help_for_ipprotocol" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("IP Protocol"); ?></td>
<tr> <td>
<td><?= gettext("Port"); ?></td> <select name="ipprotocol" class="form-control">
<td><input name="port" type="text" class="form-control" id="port" size="10" value="<?=htmlspecialchars($port);?>" /></td> <option value="ipv4" <?= $pconfig['ipprotocol'] == "ipv4" ? "selected=\"selected\"" : ""; ?>>
</tr> <?=gettext("IPv4");?>
<tr> </option>
<td><?= gettext("Source Port"); ?></td> <option value="ipv6" <?= $pconfig['ipprotocol'] == "ipv6" ? "selected=\"selected\"" : ""; ?>>
<td><input name="srcport" type="text" class="form-control" id="srcport" size="10" value="<?=htmlspecialchars($srcport);?>" /> <?=gettext("IPv6");?>
<p class="text-muted"><em><small><?php echo gettext("This should typically be left blank."); ?></small></em></p> </option>
</td> </select>
</tr> <div class="hidden" for="help_for_ipprotocol">
<tr> <?=gettext("If you force IPv4 or IPv6 and use a hostname that does not contain a result using that protocol, <br />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><?= gettext("Show Remote Text"); ?></td> </div>
<td><input name="showtext" type="checkbox" id="showtext" <?php if ($showtext) echo "checked=\"checked\"" ?> /> </td>
<p class="text-muted"><em><small><?php echo gettext("Shows the text given by the server when connecting to the port. Will take 10+ seconds to display if checked."); ?></small></em></p> </tr>
</td> <tr>
</tr> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Host"); ?></td>
<tr> <td>
<td><?=gettext("Source Address"); ?></td> <input name="host" type="text" value="<?=$pconfig['host'];?>" />
<td><select name="sourceip" class="form-control"> </td>
<option value=""><?= gettext('Any') ?></option> </tr>
<?php $sourceips = get_possible_traffic_source_addresses(true); <tr>
foreach ($sourceips as $sip): <td><i class="fa fa-info-circle text-muted"></i> <?= gettext("Port"); ?></td>
$selected = ""; <td>
if (!link_interface_to_bridge($sip['value']) && ($sip['value'] == $sourceip)) <input name="port" type="text" value="<?=$pconfig['port'];?>" />
$selected = "selected=\"selected\""; </td>
?> </tr>
<option value="<?=$sip['value'];?>" <?=$selected;?>> <tr>
<?=htmlspecialchars($sip['name']);?> <td><a id="help_for_srcport" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?= gettext("Source Port"); ?></td>
</option> <td>
<?php endforeach; ?> <input name="srcport" type="text" value="<?=$pconfig['srcport'];?>" />
</select> <div class="hidden" for="help_for_srcport">
</td> <?=gettext("This should typically be left blank."); ?>
</tr> </div>
<tr> </td>
<td><?=gettext("IP Protocol"); ?></td> </tr>
<td> <tr>
<select name="ipprotocol" class="form-control"> <td><a id="help_for_showtext" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?= gettext("Show Remote Text"); ?></td>
<option value="any" <?php if ("any" == $ipprotocol) echo "selected=\"selected\""; ?>> <td>
<?= gettext('Any') ?> <input name="showtext" type="checkbox" id="showtext" <?= !empty($pconfig['showtext']) ? "checked=\"checked\"" : "";?> />
</option> <div class="hidden" for="help_for_showtext">
<option value="ipv4" <?php if ($ipprotocol == "ipv4") echo "selected=\"selected\""; ?>> <?=gettext("Shows the text given by the server when connecting to the port. Will take 10+ seconds to display if checked."); ?>
<?=gettext("IPv4");?> </div>
</option> </td>
<option value="ipv6" <?php if ($ipprotocol == "ipv6") echo "selected=\"selected\""; ?>> </tr>
<?=gettext("IPv6");?> <tr>
</option> <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Source Address"); ?></td>
</select> <td>
<p class="text-muted"><em><small><?php echo gettext("If you force IPv4 or IPv6 and use a hostname that does not contain a result using that protocol, <br />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."); ?></small></em></p> <select name="sourceip" class="selectpicker" data-size="5" data-live-search="true">
</td> <option value=""><?= gettext('Any') ?></option>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Test"); ?>" /></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</div>
</section>
<?php if ($do_testport): ?>
<section class="col-xs-12">
<script type="text/javascript">
//<![CDATA[
window.onload=function(){
document.getElementById("testportCaptured").wrap='off';
}
//]]>
</script>
<div class="content-box">
<header class="content-box-head container-fluid">
<h3><?=gettext("Port Test Results"); ?></h3>
</header>
<div class="content-box-main col-xs-12">
<pre>
<?php <?php
foreach (get_possible_traffic_source_addresses(true) as $sip):?>
<option value="<?=$sip['value'];?>" <?=!link_interface_to_bridge($sip['value']) && ($sip['value'] == $sourceip) ? "selected=\"selected\"" : "";?>>
<?=htmlspecialchars($sip['name']);?>
</option>
<?php
endforeach;?>
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Test"); ?>" /></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</div>
</section>
<?php
if ($cmd_output !== false): ?>
<section class="col-xs-12">
<div class="content-box">
<header class="content-box-head container-fluid">
<h3><?=gettext("Port Test Results"); ?></h3>
</header>
<div class="content-box-main col-xs-12">
<?php
if (empty($cmd_output) && !empty($pconfig['showtext'])):?>
<pre><?= gettext("No output received, or connection failed. Try with \"Show Remote Text\" unchecked first.");?></pre>
<?php
elseif (empty($cmd_output)):?>
<pre><?=gettext("Connection failed (Refused/Timeout)");?></pre>
<?php
else:?>
<pre><?=$cmd_output;?></pre>
<?php
endif;?>
$result = ""; </div>
$nc_base_cmd = "/usr/bin/nc"; </div>
$nc_args = "-w " . escapeshellarg($timeout); </section>
if (!$showtext) <?php
$nc_args .= " -z "; endif;?>
if (!empty($srcport)) </div>
$nc_args .= " -p " . escapeshellarg($srcport) . " "; </div>
/* 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":
$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_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);
}
}
?>
</pre>
</div>
</div>
</section>
<?php endif; ?>
</div>
</div>
</section> </section>
<?php include('foot.inc'); ?> <?php include('foot.inc'); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment