status_openvpn.php 12.1 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
    Copyright (C) 2014-2015 Deciso B.V.
    Copyright (C) 2010 Jim Pingle
    Copyright (C) 2008 Shrew Soft Inc.
    Copyright (C) 2005 Scott Ullrich, Colin Smith
    All rights reserved.
Ad Schellevis's avatar
Ad Schellevis committed
9

10 11
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
Ad Schellevis's avatar
Ad Schellevis committed
12

13 14
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
Ad Schellevis's avatar
Ad Schellevis committed
15

16 17 18
    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.
Ad Schellevis's avatar
Ad Schellevis committed
19

20 21 22 23 24 25 26 27 28 29
    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
30
*/
31

32
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
33
require_once("openvpn.inc");
34
require_once("services.inc");
35
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
36

37 38
function kill_client($port, $remipp)
{
39 40 41
    $tcpsrv = "unix:///var/etc/openvpn/{$port}.sock";
    $errval;
    $errstr;
Ad Schellevis's avatar
Ad Schellevis committed
42

43 44 45 46 47 48 49 50
    /* open a tcp connection to the management port of each server */
    $fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
    $killed = -1;
    if ($fp) {
        stream_set_timeout($fp, 1);
        fputs($fp, "kill {$remipp}\n");
        while (!feof($fp)) {
            $line = fgets($fp, 1024);
Ad Schellevis's avatar
Ad Schellevis committed
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
            $info = stream_get_meta_data($fp);
            if ($info['timed_out']) {
                break;
            }
            /* parse header list line */
            if (strpos($line, "INFO:") !== false) {
                continue;
            }
            if (strpos($line, "SUCCESS") !== false) {
                $killed = 0;
            }
            break;
        }
        fclose($fp);
    }
    return $killed;
Ad Schellevis's avatar
Ad Schellevis committed
68 69
}

70
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
71
  $vpnid = 0;
72
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
73 74 75 76 77 78 79 80 81 82 83
  if (isset($_POST['action']) && $_POST['action'] == 'kill') {
      $port  = $_POST['port'];
      $remipp  = $_POST['remipp'];
      if (!empty($port) && !empty($remipp)) {
          $retval = kill_client($port, $remipp);
          echo htmlentities("|{$port}|{$remipp}|{$retval}|");
      } else {
          echo gettext("invalid input");
      }
      exit;
  }
84 85
}

Ad Schellevis's avatar
Ad Schellevis committed
86
$servers = openvpn_get_active_servers();
87
legacy_html_escape_form_data($servers);
Ad Schellevis's avatar
Ad Schellevis committed
88
$sk_servers = openvpn_get_active_servers("p2p");
89
legacy_html_escape_form_data($sk_servers);
Ad Schellevis's avatar
Ad Schellevis committed
90
$clients = openvpn_get_active_clients();
91
legacy_html_escape_form_data($clients);
Ad Schellevis's avatar
Ad Schellevis committed
92 93 94

include("head.inc"); ?>

95 96

<body>
Ad Schellevis's avatar
Ad Schellevis committed
97
<?php include("fbegin.inc"); ?>
98

Ad Schellevis's avatar
Ad Schellevis committed
99 100
<script type="text/javascript">
//<![CDATA[
101
$( document ).ready(function() {
102 103 104 105 106 107 108 109 110 111 112 113 114
  // link kill buttons
  $(".act_kill_client").click(function(event){
    event.preventDefault();
    var port = $(this).data("client-port");
    var ip = $(this).data("client-ip");
    $.post(window.location, {action: 'kill', port:port,remipp:ip}, function(data) {
          location.reload();
    });
  });
  // link show/hide routes
  $(".act_show_routes").click(function(){
    $("*[for='" + $(this).attr('id') + "']").toggleClass("hidden show");
  });
115

116 117 118 119 120
  // minimize all buttons, some of the buttons come from the shared service
  // functions, which outputs large buttons.
  $(".btn").each(function(){
    $(this).addClass("btn-xs");
  });
121 122

});
Ad Schellevis's avatar
Ad Schellevis committed
123 124
//]]>
</script>
125
<section class="page-content-main">
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
  <div class="container-fluid">
    <div class="row">
      <section class="col-xs-12">
        <header class="content-box-head container-fluid"> <h3><?=gettext("OpenVPN Status");?></h3>
        </header>
        <div class="content-box-main col-xs-12">
          <form action="status_openvpn.php" method="get" name="iform">
            <div class="table-responsive">
              <table class="table table-striped">
<?php
                $i = 0;
                foreach ($servers as $server): ?>
                <tr>
                  <td colspan="8" class="listtopic">
                    <b><?=$server['name'];?> <?=gettext("Client connections"); ?></b>
                  </td>
                </tr>
                <tr>
                  <td><?=gettext("Common Name"); ?></td>
                  <td><?=gettext("Real Address"); ?></td>
                  <td><?=gettext("Virtual Address"); ?></td>
                  <td><?=gettext("Connected Since"); ?></td>
                  <td><?=gettext("Bytes Sent"); ?></td>
                  <td><?=gettext("Bytes Received"); ?></td>
                  <td></td>
                  <td></td>
                </tr>
<?php
                foreach ($server['conns'] as $conn): ?>
                <tr id="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>">
                  <td><?=$conn['common_name'];?></td>
                  <td><?=$conn['remote_host'];?></td>
                  <td><?=$conn['virtual_addr'];?></td>
                  <td><?=$conn['connect_time'];?></td>
                  <td><?=format_bytes($conn['bytes_sent']);?></td>
                  <td><?=format_bytes($conn['bytes_recv']);?></td>
                  <td></td>
                  <td>
                    <button  data-client-port="<?=$server['mgmt'];?>"
                        data-client-ip="<?=$conn['remote_host'];?>"
                        title="<?=gettext("Kill client connection from"). " ".  $conn['remote_host'] ; ?>"
                        class="act_kill_client btn btn-default">
                        <span class="glyphicon glyphicon-remove"></span>
                    </button>
                  </td>
                </tr>
<?php
                endforeach; ?>
                <tr>
                  <td colspan="2">
176
                    <?php $ssvc = find_service_by_name('openvpn', array('vpnid' => $server['vpnid'])); ?>
177 178 179 180 181 182 183 184 185 186 187 188 189 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                    <?= get_service_status_icon($ssvc, true, true); ?>
                    <?= get_service_control_links($ssvc, true); ?>
                  </td>
                  <td colspan="6">&nbsp;</td>
                </tr>
<?php
                  if (isset($server['routes']) && count($server['routes'])): ?>
                <tr>
                  <td colspan="8">
                    <button class="btn btn-default act_show_routes" type="button" id="showroutes_<?=$i?>"><i class="fa fa-info"></i>
                      <?=gettext("Show/Hide Routing Table"); ?>
                    </button>
                    <div  class="hidden"  for="showroutes_<?=$i?>">
                      <small>
                        <?=$server['name'];?> <?=gettext("Routing Table"); ?>
                      </small>
                      <table class="table table-striped table-bordered">
                        <thead>
                          <tr>
                            <th><?=gettext("Common Name"); ?></th>
                            <th><?=gettext("Real Address"); ?></th>
                            <th><?=gettext("Target Network"); ?></th>
                            <th><?=gettext("Last Used"); ?></th>
                          </tr>
                        </thead>
                        <tbody>
<?php
                          foreach ($server['routes'] as $conn): ?>
                          <tr id="<?php echo "r:{$server['mgmt']}:{$conn['remote_host']}"; ?>">
                            <td><?=$conn['common_name'];?></td>
                            <td><?=$conn['remote_host'];?></td>
                            <td><?=$conn['virtual_addr'];?></td>
                            <td><?=$conn['last_time'];?></td>
                          </tr>
<?php
                          endforeach; ?>
                        </tbody>
                        <tfoot>
                          <tr>
                            <td colspan="6"><?= gettext("An IP address followed by C indicates a host currently connected through the VPN.") ?></td>
                          </tr>
                        </tfoot>
                      </table>
                    </div>
                  </td>
                </tr>
<?php
                  endif;
                  $i++;
                endforeach;
              if (!empty($sk_servers)): ?>
                <tr>
                  <td colspan="8" class="listtopic">
                    <b><?=gettext("Peer to Peer Server Instance Statistics"); ?></b>
                  </td>
                </tr>
                <tr>
                  <td><?=gettext("Name"); ?></td>
                  <td><?=gettext("Remote Host"); ?></td>
                  <td><?=gettext("Virtual Addr"); ?></td>
                  <td><?=gettext("Connected Since"); ?></td>
                  <td><?=gettext("Bytes Sent"); ?></td>
                  <td><?=gettext("Bytes Received"); ?></td>
                  <td><?=gettext("Status"); ?></td>
                  <td></td>
                </tr>
<?php
                foreach ($sk_servers as $sk_server): ?>
                <tr id="<?php echo "r:{$sk_server['port']}:{$sk_server['vpnid']}"; ?>">
                  <td><?=$sk_server['name'];?></td>
                  <td><?=$sk_server['remote_host'];?></td>
                  <td><?=$sk_server['virtual_addr'];?></td>
                  <td><?=$sk_server['connect_time'];?></td>
                  <td><?=format_bytes($sk_server['bytes_sent']);?></td>
                  <td><?=format_bytes($sk_server['bytes_recv']);?></td>
                  <td><?=$sk_server['status'];?></td>
                  <td>
                    <div>
255
                      <?php $ssvc = find_service_by_name('openvpn', array('vpnid' => $sk_server['vpnid'])); ?>
256 257 258 259 260 261 262 263
                      <?= get_service_status_icon($ssvc, false, true); ?>
                      <?= get_service_control_links($ssvc, true); ?>
                    </div>
                  </td>
                </tr>
<?php
                endforeach;
              endif; ?>
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
<?php
              if (!empty($clients)):?>
              <tr>
                <tr>
                  <td colspan="8" class="listtopic">
                    <b><?=gettext("Client Instance Statistics"); ?><b>
                  </td>
                </tr>
                <tr>
                  <td><?=gettext("Name"); ?></td>
                  <td><?=gettext("Connected Since"); ?></td>
                  <td><?=gettext("Virtual Addr"); ?></td>
                  <td><?=gettext("Remote Host"); ?></td>
                  <td><?=gettext("Bytes Sent"); ?></td>
                  <td><?=gettext("Bytes Rcvd"); ?></td>
                  <td><?=gettext("Status"); ?></td>
                  <td></td>
                </tr>
<?php
                foreach ($clients as $client): ?>
                <tr id="<?php echo "r:{$client['port']}:{$client['vpnid']}"; ?>">
                  <td><?=$client['name'];?></td>
                  <td><?=$client['connect_time'];?></td>
                  <td><?=$client['virtual_addr'];?></td>
                  <td><?=$client['remote_host'];?></td>
                  <td><?=format_bytes($client['bytes_sent']);?></td>
                  <td><?=format_bytes($client['bytes_recv']);?></td>
                  <td><?=$client['status'];?></td>
                  <td>
                    <div>
295
                      <?php $ssvc = find_service_by_name('openvpn', array('vpnid' => $client['vpnid'])); ?>
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                      <?= get_service_status_icon($ssvc, false, true); ?>
                      <?= get_service_control_links($ssvc, true); ?>
                    </div>
                  </td>
                </tr>
<?php
                endforeach; ?>
              </table>
            </div>
<?php
            endif;
            if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
              echo gettext("No OpenVPN instance defined");
            }?>
          </form>
        </div>
      </section>
    </div>
  </div>
315 316
</section>

317 318

<?php include("foot.inc"); ?>