diag_testport.php 10.9 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    Copyright (C) 2016 Deciso B.V.
    Copyright (C) 2013 Jim Pingle <jimp@pfsense.org>
    Copyright (C) 2003-2005 Bob Zoller (bob@kludgebox.com) and Manuel Kasper <mk@neon1.net>.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    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
    POSSIBILITY OF SUCH DAMAGE.
Ad Schellevis's avatar
Ad Schellevis committed
29 30
*/

31
require_once("guiconfig.inc");
32
require_once("system.inc");
33
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
$cmd_output = false;
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // set form defaults
    $pconfig = array();
    $pconfig['ipprotocol'] = 'ipv4';
    $pconfig['host'] = null;
    $pconfig['port'] = null;
    $pconfig['showtext'] = null;
    $pconfig['sourceip'] = null;
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pconfig = $_POST;
    $input_errors = array();

    /* input validation */
    $reqdfields = explode(" ", "host port");
    $reqdfieldsn = array(gettext("Host"),gettext("Port"));
    do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);

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

    if (!is_port($pconfig['port'])) {
        $input_errors[] = gettext("Please enter a valid port number.");
    }

    if (($pconfig['srcport'] != "") && (!is_numeric($pconfig['srcport']) || !is_port($pconfig['srcport']))) {
        $input_errors[] = gettext("Please enter a valid source port number, or leave the field blank.");
    }

    if (is_ipaddrv4($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv6")) {
        $input_errors[] = gettext("You cannot connect to an IPv4 address using IPv6.");
    }
    if (is_ipaddrv6($pconfig['host']) && ($pconfig['ipprotocol'] == "ipv4")) {
        $input_errors[] = gettext("You cannot connect to an IPv6 address using IPv4.");
    }

    if (count($input_errors) == 0) {
        $nc_args = "-w 10" ;
        if (empty($pconfig['showtext'])) {
            $nc_args .= " -z ";
        }
        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]);
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
105 106
}

107
legacy_html_escape_form_data($pconfig);
Ad Schellevis's avatar
Ad Schellevis committed
108
include("head.inc"); ?>
109
<body>
Ad Schellevis's avatar
Ad Schellevis committed
110
<?php include("fbegin.inc"); ?>
111 112

<section class="page-content-main">
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  <div class="container-fluid">
    <div class="row">
      <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."); ?>
          <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."); ?>
        </div>
        <div class="content-box">
          <div class="content-box-main ">
            <?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
            <form method="post" name="iform" id="iform">
              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <td width="22%"><strong><?=gettext("Test Port"); ?></strong></td>
                      <td width="78%" align="right">
                        <small><?=gettext("full help"); ?> </small>
                        <i class="fa fa-toggle-off text-danger"  style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
                        &nbsp;
                      </td>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td><a id="help_for_ipprotocol" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("IP Protocol"); ?></td>
                      <td>
                        <select name="ipprotocol" class="form-control">
                          <option value="ipv4" <?= $pconfig['ipprotocol'] == "ipv4" ? "selected=\"selected\"" : ""; ?>>
                            <?=gettext("IPv4");?>
                          </option>
                          <option value="ipv6" <?= $pconfig['ipprotocol'] == "ipv6" ? "selected=\"selected\"" : ""; ?>>
                            <?=gettext("IPv6");?>
                          </option>
                        </select>
                        <div class="hidden" for="help_for_ipprotocol">
150
                          <?=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."); ?>
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Host"); ?></td>
                      <td>
                        <input name="host" type="text" value="<?=$pconfig['host'];?>" />
                      </td>
                    </tr>
                    <tr>
                      <td><i class="fa fa-info-circle text-muted"></i> <?= gettext("Port"); ?></td>
                      <td>
                        <input name="port" type="text" value="<?=$pconfig['port'];?>" />
                      </td>
                    </tr>
                    <tr>
                      <td><a id="help_for_srcport" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?= gettext("Source Port"); ?></td>
                      <td>
                        <input name="srcport" type="text" value="<?=$pconfig['srcport'];?>" />
                        <div class="hidden" for="help_for_srcport">
                          <?=gettext("This should typically be left blank."); ?>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td><a id="help_for_showtext" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?= gettext("Show Remote Text"); ?></td>
                      <td>
                        <input name="showtext" type="checkbox" id="showtext" <?= !empty($pconfig['showtext']) ? "checked=\"checked\"" : "";?> />
                        <div class="hidden" for="help_for_showtext">
                          <?=gettext("Shows the text given by the server when connecting to the port. Will take 10+ seconds to display if checked."); ?>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Source Address"); ?></td>
                      <td>
                          <select name="sourceip" class="selectpicker" data-size="5" data-live-search="true">
                            <option value=""><?= gettext('Any') ?></option>
189
<?php
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                          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;?>
229

230 231 232 233 234 235 236
          </div>
        </div>
      </section>
<?php
      endif;?>
    </div>
  </div>
237
</section>
238
<?php include('foot.inc'); ?>