status_openvpn.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
	Copyright (C) 2014-2015 Deciso B.V.
5 6 7 8
	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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

	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.
*/
31
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32
require_once("openvpn.inc");
33
require_once("services.inc");
34
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
35 36 37 38

function kill_client($port, $remipp) {
	global $g;

39
	$tcpsrv = "unix:///var/etc/openvpn/{$port}.sock";
Ad Schellevis's avatar
Ad Schellevis committed
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
	$errval;
	$errstr;

	/* 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);

			$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;
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

$pgtitle = array(gettext("Status"), gettext("OpenVPN"));
$shortcut_section = "openvpn";

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
	$vpnid = 0;
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
	if (isset($_POST['action']) && $_POST['action'] == 'kill') {
			$port  = escapeshellarg($_POST['port']);
			$remipp  = escapeshellarg($_POST['remipp']);
			if (!empty($port) and !empty($remipp)) {
					$retval = kill_client($port, $remipp);
					echo htmlentities("|{$port}|{$remipp}|{$retval}|");
			} else {
					echo gettext("invalid input");
			}
			exit;
	}
}

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

include("head.inc"); ?>

98 99

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

Ad Schellevis's avatar
Ad Schellevis committed
102 103
<script type="text/javascript">
//<![CDATA[
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
$( document ).ready(function() {
	// link kill buttons
	$(".act_kill_client").click(function(){
		var port = $(this).attr("data-client-port");
		var ip = $(this).attr("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");
	});

	// minimize all buttons, some pf the buttons come from the shared service
	// functions, which outputs large buttons.
	$(".btn").each(function(){
		$(this).addClass("btn-xs");
	});

});
Ad Schellevis's avatar
Ad Schellevis committed
125 126
//]]>
</script>
127 128 129 130
<section class="page-content-main">
	<div class="container-fluid">
        <div class="row">
            <section class="col-xs-12">
131
	            <header class="content-box-head container-fluid"> <h3><?=gettext("OpenVPN Status");?></h3>
132 133 134
					</header>
						<div class="content-box-main col-xs-12">
							<form action="status_openvpn.php" method="get" name="iform">
135 136 137 138
										<div class="table-responsive">
										<table class="table table-striped">
											<?php $i = 0; ?>
											<?php foreach ($servers as $server): ?>
139
											<tr>
140 141 142
												<td colspan="8" class="listtopic">
													<b><?=$server['name'];?> <?=gettext("Client connections"); ?></b>
												</td>
143
											</tr>
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
											<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>
164
													<a	data-client-port="<?=$server['mgmt'];?>"
165 166 167 168 169 170 171 172 173 174 175 176 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
															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>
													</a>
												</td>
											</tr>
											<?php endforeach; ?>
											<tr>
												<td colspan="2">
													<?php $ssvc = find_service_by_openvpn_vpnid($server['vpnid']); ?>
													<?= 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>
															<?php echo 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; ?>
																	<tfoot>
																	<tr>
																		<td colspan="6"><?= gettext("An IP address followed by C indicates a host currently connected through the VPN.") ?></td>
																	</tr>
																	</tfoot>
																</tbody>
															</table>
														</div>
												</td>
											</tr>
										<?php endif; ?>
										<?php $i++; ?>
										<?php endforeach; ?>
222

223 224 225 226 227 228
										<?php if (!empty($sk_servers)) { ?>
											<tr>
												<td colspan="8" class="listtopic">
													<b><?=gettext("Peer to Peer Server Instance Statistics"); ?></b>
												</td>
											</tr>
229
										<tr>
230 231 232 233 234 235 236 237
											<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>
238
										</tr>
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
										<?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>
																<?php $ssvc = find_service_by_openvpn_vpnid($sk_server['vpnid']); ?>
																<?= get_service_status_icon($ssvc, false, true); ?>
																<?= get_service_control_links($ssvc, true); ?>
															</div>
														</td>
													</tr>
256
										<?php endforeach; ?>
257 258
										<?php
										} ?>
259

260
										<?php if (!empty($clients)) { ?>
261 262
										<tr>
											<tr>
263 264 265
												<td colspan="8" class="listtopic">
													<b><?=gettext("Client Instance Statistics"); ?><b>
												</td>
266 267
											</tr>
											<tr>
268 269 270 271 272 273 274 275
												<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>
276
											</tr>
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
											<?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>
														<?php $ssvc = find_service_by_openvpn_vpnid($client['vpnid']); ?>
														<?= get_service_status_icon($ssvc, false, true); ?>
														<?= get_service_control_links($ssvc, true); ?>
													</div>
												</td>
											</tr>
											<?php endforeach; ?>
295
										</table>
296 297 298 299 300 301 302
										</div>
										<?php
										}
										if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))) {
											echo gettext("No OpenVPN instance defined");
										}
										?>
303 304
					    </form>
				    </div>
305
          </section>
306
	</div>
307
		</div>
308 309
</section>

310 311

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