mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-13 16:14:40 +00:00
VPN/OpenVPN - add ovpn_status.py script and configd action to fetch connected clients, refactor legacy backend code while here (https://github.com/opnsense/core/issues/6312)
Eventually the old gui code should be replaced as well, but this is an easy to release step in between offering nearly the same output (p2p's presentation is aligned with server in stead of client) with code we are able to reuse for the openvpn aliases.
This commit is contained in:
parent
a37a922ade
commit
bb1aa66802
@ -1150,223 +1150,35 @@ function openvpn_configure_do($verbose = false, $interface = '', $carp_event = f
|
||||
service_log("done.\n", $verbose);
|
||||
}
|
||||
|
||||
function openvpn_get_active_servers($type = 'multipoint')
|
||||
|
||||
function openvpn_config()
|
||||
{
|
||||
global $config;
|
||||
|
||||
$servers = array();
|
||||
|
||||
if (!empty($config['openvpn']['openvpn-server'])) {
|
||||
foreach ($config['openvpn']['openvpn-server'] as $settings) {
|
||||
if (empty($settings) || isset($settings['disable'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$prot = $settings['protocol'];
|
||||
$port = $settings['local_port'];
|
||||
|
||||
$server = array();
|
||||
$server['port'] = ($settings['local_port']) ? $settings['local_port'] : 1194;
|
||||
$server['mode'] = $settings['mode'];
|
||||
if ($settings['description']) {
|
||||
$server['name'] = "{$settings['description']} {$prot}:{$port}";
|
||||
} else {
|
||||
$server['name'] = "Server {$prot}:{$port}";
|
||||
}
|
||||
$server['conns'] = array();
|
||||
$server['vpnid'] = $settings['vpnid'];
|
||||
$server['mgmt'] = "server{$server['vpnid']}";
|
||||
$socket = "unix:///var/etc/openvpn/{$server['mgmt']}.sock";
|
||||
list($tn, $sm) = explode('/', $settings['tunnel_network']);
|
||||
|
||||
if ((($server['mode'] == "p2p_shared_key") || ($sm >= 30) ) && ($type == "p2p")) {
|
||||
$servers[] = openvpn_get_client_status($server, $socket);
|
||||
} elseif (($server['mode'] != "p2p_shared_key") && ($type == "multipoint") && ($sm < 30)) {
|
||||
$servers[] = openvpn_get_server_status($server, $socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $servers;
|
||||
}
|
||||
|
||||
function openvpn_get_server_status($server, $socket)
|
||||
{
|
||||
$errval = 0;
|
||||
$errstr = '';
|
||||
$fp = @stream_socket_client($socket, $errval, $errstr, 1);
|
||||
if ($fp) {
|
||||
stream_set_timeout($fp, 1);
|
||||
|
||||
/* send our status request */
|
||||
fputs($fp, "status 3\n");
|
||||
|
||||
/* recv all response lines */
|
||||
while (!feof($fp)) {
|
||||
/* read the next line */
|
||||
$line = fgets($fp, 1024);
|
||||
$info = stream_get_meta_data($fp);
|
||||
if ($info['timed_out']) {
|
||||
break;
|
||||
}
|
||||
/* parse header list line */
|
||||
if (strstr($line, "HEADER")) {
|
||||
continue;
|
||||
}
|
||||
/* parse end of output line */
|
||||
if (strstr($line, "END") || strstr($line, "ERROR")) {
|
||||
break;
|
||||
}
|
||||
/* parse client list line */
|
||||
if (strstr($line, "CLIENT_LIST")) {
|
||||
$list = explode("\t", $line);
|
||||
$conn = array();
|
||||
$conn['common_name'] = $list[1];
|
||||
$conn['remote_host'] = $list[2];
|
||||
$conn['virtual_addr'] = $list[3];
|
||||
$conn['bytes_recv'] = $list[5];
|
||||
$conn['bytes_sent'] = $list[6];
|
||||
$conn['connect_time'] = date('Y-m-d H:i:s', $list[8]);
|
||||
$server['conns'][] = $conn;
|
||||
}
|
||||
/* parse routing table lines */
|
||||
if (strstr($line, "ROUTING_TABLE")) {
|
||||
$list = explode("\t", $line);
|
||||
$conn = array();
|
||||
$conn['virtual_addr'] = $list[1];
|
||||
$conn['common_name'] = $list[2];
|
||||
$conn['remote_host'] = $list[3];
|
||||
$conn['last_time'] = $list[4];
|
||||
$server['routes'][] = $conn;
|
||||
}
|
||||
}
|
||||
/* cleanup */
|
||||
fclose($fp);
|
||||
} else {
|
||||
$conn = array();
|
||||
$conn['common_name'] = '[error]'; // kind of a marker value now
|
||||
$conn['remote_host'] = gettext('Unable to contact daemon');
|
||||
$conn['virtual_addr'] = gettext('Service not running?');
|
||||
$conn['bytes_recv'] = 0;
|
||||
$conn['bytes_sent'] = 0;
|
||||
$conn['connect_time'] = 0;
|
||||
$server['conns'][] = $conn;
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
|
||||
function openvpn_get_active_clients()
|
||||
{
|
||||
global $config;
|
||||
|
||||
$clients = array();
|
||||
|
||||
if (!empty($config['openvpn']['openvpn-client'])) {
|
||||
foreach ($config['openvpn']['openvpn-client'] as $settings) {
|
||||
if (empty($settings) || isset($settings['disable'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$prot = $settings['protocol'];
|
||||
$port = ($settings['local_port']) ? ":{$settings['local_port']}" : "";
|
||||
|
||||
$client = array();
|
||||
$client['port'] = $settings['local_port'];
|
||||
if ($settings['description']) {
|
||||
$client['name'] = "{$settings['description']} {$prot}{$port}";
|
||||
} else {
|
||||
$client['name'] = "Client {$prot}{$port}";
|
||||
}
|
||||
|
||||
$client['vpnid'] = $settings['vpnid'];
|
||||
$client['mgmt'] = "client{$client['vpnid']}";
|
||||
$socket = "unix:///var/etc/openvpn/{$client['mgmt']}.sock";
|
||||
$client['status'] = 'down';
|
||||
$clients[] = openvpn_get_client_status($client, $socket);
|
||||
}
|
||||
}
|
||||
|
||||
return $clients;
|
||||
}
|
||||
|
||||
function openvpn_get_client_status($client, $socket)
|
||||
{
|
||||
$errval = 0;
|
||||
$errstr = '';
|
||||
$fp = @stream_socket_client($socket, $errval, $errstr, 1);
|
||||
if ($fp) {
|
||||
stream_set_timeout($fp, 1);
|
||||
/* send our status request */
|
||||
fputs($fp, "state all\n");
|
||||
|
||||
/* recv all response lines */
|
||||
while (!feof($fp)) {
|
||||
/* read the next line */
|
||||
$line = fgets($fp, 1024);
|
||||
|
||||
$info = stream_get_meta_data($fp);
|
||||
if ($info['timed_out']) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Get the client state */
|
||||
$list = explode(",", $line);
|
||||
if (count($list) > 1) {
|
||||
$client['connect_time'] = date('Y-m-d H:i:s', $list[0]);
|
||||
}
|
||||
if (strstr($line, 'CONNECTED')) {
|
||||
$client['status'] = 'up';
|
||||
$client['virtual_addr'] = $list[3];
|
||||
$client['remote_host'] = $list[4];
|
||||
} elseif (strstr($line, 'CONNECTING')) {
|
||||
$client['status'] = 'connecting';
|
||||
} elseif (strstr($line, "ASSIGN_IP")) {
|
||||
$client['status'] = "waiting";
|
||||
$client['virtual_addr'] = $list[3];
|
||||
} elseif (strstr($line, "RECONNECTING")) {
|
||||
$client['status'] = "reconnecting";
|
||||
$client['status'] .= "; " . $list[2];
|
||||
} elseif (strstr($line, "END") || strstr($line, "ERROR")) {
|
||||
/* parse end of output line */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If up, get read/write stats */
|
||||
if (strcmp($client['status'], "up") == 0) {
|
||||
fputs($fp, "status 2\n");
|
||||
/* recv all response lines */
|
||||
while (!feof($fp)) {
|
||||
/* read the next line */
|
||||
$line = fgets($fp, 1024);
|
||||
|
||||
$info = stream_get_meta_data($fp);
|
||||
if ($info['timed_out']) {
|
||||
break;
|
||||
$result = [];
|
||||
foreach (['openvpn-server', 'openvpn-client'] as $section) {
|
||||
$result[$section] = [];
|
||||
if (!empty($config['openvpn'][$section])) {
|
||||
foreach ($config['openvpn'][$section] as $settings) {
|
||||
if (empty($settings) || isset($settings['disable'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list = explode(",", $line);
|
||||
if (strstr($line, "TCP/UDP read bytes")) {
|
||||
$client['bytes_recv'] = $list[1];
|
||||
} elseif (strstr($line, "TCP/UDP write bytes")) {
|
||||
$client['bytes_sent'] = $list[1];
|
||||
} elseif (strstr($line, "END")) {
|
||||
/* parse end of output line */
|
||||
break;
|
||||
$server = [];
|
||||
$default_port = ($section == 'openvpn-server') ? 1194 : '';
|
||||
$server['port'] = ($settings['local_port']) ? $settings['local_port'] : $default_port;
|
||||
$server['mode'] = $settings['mode'];
|
||||
if (empty($settings['description'])) {
|
||||
$settings['description'] = ($section == 'openvpn-server') ? 'Server' : 'Client';
|
||||
}
|
||||
$server['name'] = "{$settings['description']} {$settings['protocol']}:{$settings['local_port']}";
|
||||
$server['vpnid'] = $settings['vpnid'];
|
||||
$result[$section][] = $server;
|
||||
}
|
||||
}
|
||||
fclose($fp);
|
||||
} else {
|
||||
$client['remote_host'] = gettext('Unable to contact daemon');
|
||||
$client['virtual_addr'] = gettext('Service not running?');
|
||||
$client['bytes_recv'] = 0;
|
||||
$client['bytes_sent'] = 0;
|
||||
$client['connect_time'] = 0;
|
||||
}
|
||||
return $client;
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
function openvpn_create_dirs()
|
||||
{
|
||||
@mkdir('/var/etc/openvpn-csc', 0750);
|
||||
|
||||
138
src/opnsense/scripts/openvpn/ovpn_status.py
Executable file
138
src/opnsense/scripts/openvpn/ovpn_status.py
Executable file
@ -0,0 +1,138 @@
|
||||
#!/usr/local/bin/python3
|
||||
|
||||
"""
|
||||
Copyright (c) 2023 Ad Schellevis <ad@opnsense.org>
|
||||
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.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import glob
|
||||
import socket
|
||||
import re
|
||||
import os
|
||||
import ujson
|
||||
socket.setdefaulttimeout(5)
|
||||
|
||||
|
||||
def ovpn_cmd(filename, cmd):
|
||||
try:
|
||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
sock.connect(filename)
|
||||
except socket.error:
|
||||
return None
|
||||
|
||||
sock.send(('%s\n'%cmd).encode())
|
||||
buffer = ''
|
||||
while True:
|
||||
try:
|
||||
buffer += sock.recv(65536).decode()
|
||||
except socket.timeout:
|
||||
break
|
||||
eob = buffer[-20:]
|
||||
if eob.find('END') > -1 or eob.find('ERROR') > -1:
|
||||
break
|
||||
sock.close()
|
||||
return buffer
|
||||
|
||||
|
||||
def ovpn_status(filename):
|
||||
response = {}
|
||||
buffer = ovpn_cmd(filename, 'status 3')
|
||||
if buffer is None:
|
||||
return {'status': 'failed'}
|
||||
|
||||
header_def = []
|
||||
target_struct = None
|
||||
for line in buffer.split('\n'):
|
||||
if line.startswith('TCP/UDP'):
|
||||
line = line.split(',')
|
||||
response[line[0][8:].replace(' ', '_')] = line[1].strip()
|
||||
continue
|
||||
line = line.split('\t')
|
||||
if line[0] == 'HEADER':
|
||||
header_def = []
|
||||
for item in line[1:]:
|
||||
header_def.append(re.sub('[\ \(\)]', '_', item.lower().strip()))
|
||||
target_struct = header_def.pop(0).lower() if len(header_def) > 0 else None
|
||||
response['status'] = 'ok'
|
||||
elif target_struct is not None and line[0] in ['CLIENT_LIST', 'ROUTING_TABLE']:
|
||||
line = line[1:]
|
||||
record = {}
|
||||
for i in range(len(header_def)):
|
||||
record[header_def[i]] = line[i].strip()
|
||||
if target_struct not in response:
|
||||
response[target_struct] = []
|
||||
response[target_struct].append(record)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def ovpn_state(filename):
|
||||
response = {'status': 'failed'}
|
||||
buffer = ovpn_cmd(filename, 'state')
|
||||
if buffer is None:
|
||||
return response
|
||||
|
||||
for line in buffer.split('\n'):
|
||||
tmp = line.split(',')
|
||||
if len(tmp) > 2 and tmp[0].isdigit():
|
||||
response['timestamp'] = int(tmp[0])
|
||||
response['status'] = tmp[1].lower()
|
||||
response['virtual_addr'] = tmp[3] if len(tmp) > 3 else ""
|
||||
response['remote_host'] = tmp[4] if len(tmp) > 4 else ""
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def main(params):
|
||||
response = {}
|
||||
for filename in glob.glob('/var/etc/openvpn/*.sock'):
|
||||
bname = os.path.basename(filename)[:-5]
|
||||
this_id = bname[6:]
|
||||
if bname.startswith('server') and 'server' in params.options:
|
||||
if 'server' not in response:
|
||||
response['server'] = {}
|
||||
response['server'][this_id] = ovpn_status(filename)
|
||||
if 'status' not in response['server'][this_id]:
|
||||
# p2p mode, no client_list or routing_table
|
||||
response['server'][this_id].update(ovpn_state(filename))
|
||||
elif bname.startswith('client') and 'client' in params.options:
|
||||
if 'client' not in response:
|
||||
response['client'] = {}
|
||||
response['client'][this_id] = ovpn_state(filename)
|
||||
if response['client'][this_id]['status'] != 'failed':
|
||||
response['client'][this_id].update(ovpn_status(filename))
|
||||
|
||||
return response
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--options',
|
||||
help='request status from client,server (comma separated)',
|
||||
type=lambda x: x.split(','),
|
||||
default='server'
|
||||
)
|
||||
print(ujson.dumps(main(parser.parse_args())))
|
||||
5
src/opnsense/service/conf/actions.d/actions_openvpn.conf
Normal file
5
src/opnsense/service/conf/actions.d/actions_openvpn.conf
Normal file
@ -0,0 +1,5 @@
|
||||
[status]
|
||||
command:/usr/local/opnsense/scripts/openvpn/ovpn_status.py
|
||||
parameters: --option %s
|
||||
type:script_output
|
||||
message:Query OpenVPN status (%s)
|
||||
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2014-2023 Deciso B.V.
|
||||
* Copyright (C) 2019 Franco Fichtner <franco@opnsense.org>
|
||||
* Copyright (C) 2014-2015 Deciso B.V.
|
||||
* Copyright (C) 2010 Jim Pingle <jimp@pfsense.org>
|
||||
* Copyright (C) 2008 Shrew Soft Inc. <mgrooms@shrew.net>
|
||||
* Copyright (C) 2005 Scott Ullrich <sullrich@gmail.com>
|
||||
@ -91,12 +91,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
}
|
||||
}
|
||||
|
||||
$servers = openvpn_get_active_servers();
|
||||
legacy_html_escape_form_data($servers);
|
||||
$sk_servers = openvpn_get_active_servers("p2p");
|
||||
legacy_html_escape_form_data($sk_servers);
|
||||
$clients = openvpn_get_active_clients();
|
||||
legacy_html_escape_form_data($clients);
|
||||
$openvpn_status = json_decode(configd_run('openvpn status client,server'), true) ?? [];
|
||||
$openvpn_cfg = openvpn_config();
|
||||
legacy_html_escape_form_data($openvpn_status);
|
||||
legacy_html_escape_form_data($openvpn_cfg);
|
||||
|
||||
include("head.inc"); ?>
|
||||
|
||||
@ -136,7 +134,7 @@ include("head.inc"); ?>
|
||||
<div class="row">
|
||||
<section class="col-xs-12">
|
||||
<!-- XXX unused? <form method="get" name="iform">-->
|
||||
<?php foreach ($servers as $i => $server): ?>
|
||||
<?php foreach ($openvpn_cfg['openvpn-server'] as $i => $server): ?>
|
||||
<div class="tab-content content-box __mb">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
@ -158,27 +156,42 @@ include("head.inc"); ?>
|
||||
<td><strong><?= gettext('Connected Since') ?></strong></td>
|
||||
<td><strong><?= gettext('Bytes Sent') ?></strong></td>
|
||||
<td><strong><?= gettext('Bytes Received') ?></strong></td>
|
||||
<td></td>
|
||||
<td><strong><?= gettext('Status') ?></strong></td>
|
||||
</tr>
|
||||
<?php if (empty($server['conns'])): ?>
|
||||
<?php if (empty($openvpn_status['server'][$server['vpnid']]) || empty($openvpn_status['server'][$server['vpnid']]['client_list'])): ?>
|
||||
<?php if (!empty($openvpn_status['server'][$server['vpnid']]) && isset($openvpn_status['server'][$server['vpnid']]['write_bytes'])):?>
|
||||
<?php $conn = $openvpn_status['server'][$server['vpnid']];?>
|
||||
<tr>
|
||||
<td colspan="7"><?= gettext('No OpenVPN clients are connected to this instance.') ?></td>
|
||||
</tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($server['conns'] as $conn): ?>
|
||||
<tr id="<?= html_safe("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><?= $conn['common_name'] ?? '' ?></td>
|
||||
<td><?= $conn['real_address'] ?? '' ?></td>
|
||||
<td><?= $conn['virtual_addr'] ?? '' ?></td>
|
||||
<td><?= date('Y-m-d H:i:s', $conn['timestamp']) ?></td>
|
||||
<td><?= format_bytes($conn['bytes_sent']) ?></td>
|
||||
<td><?= format_bytes($conn['bytes_recv']) ?></td>
|
||||
<td><?= format_bytes($conn['bytes_received']) ?></td>
|
||||
<td><?= $conn['status'];?> </td>
|
||||
</tr>
|
||||
<?php else:?>
|
||||
<tr>
|
||||
<td colspan="7">
|
||||
<?= gettext('No OpenVPN clients are connected to this instance.') ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endif ?>
|
||||
<?php else: ?>
|
||||
<?php foreach ($openvpn_status['server'][$server['vpnid']]['client_list'] as $conn): ?>
|
||||
<tr>
|
||||
<td><?= $conn['common_name'] ?></td>
|
||||
<td><?= $conn['real_address'] ?></td>
|
||||
<td><?= $conn['virtual_address'] ?></td>
|
||||
<td><?= $conn['connected_since'] ?></td>
|
||||
<td><?= format_bytes($conn['bytes_sent']) ?></td>
|
||||
<td><?= format_bytes($conn['bytes_received']) ?></td>
|
||||
<td>
|
||||
<?php if (count($server['conns']) != 1 || $conn['common_name'] != '[error]'): ?>
|
||||
<button data-client-port="<?= $server['mgmt']; ?>"
|
||||
data-client-ip="<?= $conn['remote_host']; ?>"
|
||||
<?php if ($conn['common_name'] != '[error]'): ?>
|
||||
<button data-client-port="server<?= $server['vpnid']; ?>"
|
||||
data-client-ip="<?= $conn['real_address']; ?>"
|
||||
data-common_name="<?= $conn['common_name']; ?>"
|
||||
title="<?= gettext("Kill client connection from") . " " . $conn['remote_host']; ?>"
|
||||
title="<?= gettext("Kill client connection from") . " " . $conn['real_address']; ?>"
|
||||
class="act_kill_client btn btn-default">
|
||||
<i class="fa fa-times fa-fw"></i>
|
||||
</button>
|
||||
@ -187,7 +200,7 @@ include("head.inc"); ?>
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($server['routes'])): ?>
|
||||
<?php if (!empty($openvpn_status['server'][$server['vpnid']]) && !empty($openvpn_status['server'][$server['vpnid']]['routing_table'])): ?>
|
||||
<tr>
|
||||
<td colspan="7">
|
||||
<span style="cursor:pointer;" class="act_show_routes" id="showroutes_<?= $i ?>">
|
||||
@ -203,12 +216,12 @@ include("head.inc"); ?>
|
||||
<td><strong><?= gettext('Last Used') ?></strong></td>
|
||||
<td colspan="3">
|
||||
</tr>
|
||||
<?php foreach ($server['routes'] as $conn): ?>
|
||||
<?php foreach ($openvpn_status['server'][$server['vpnid']]['routing_table'] as $conn): ?>
|
||||
<tr style="display:none;" data-for="showroutes_<?= $i ?>" id="<?= html_safe("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>
|
||||
<td><?= $conn['real_address'] ?></td>
|
||||
<td><?= $conn['virtual_address'] ?></td>
|
||||
<td><?= $conn['last_ref'] ?></td>
|
||||
<td colspan="3">
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
@ -222,48 +235,8 @@ include("head.inc"); ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach ?>
|
||||
<?php if (!empty($sk_servers)): ?>
|
||||
<div class="tab-content content-box __mb">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td colspan="8"><strong><?= gettext('Peer to Peer Server Instance Statistics') ?></strong></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong><?= gettext('Name') ?></strong></td>
|
||||
<td><strong><?= gettext('Remote Host') ?></strong></td>
|
||||
<td><strong><?= gettext('Virtual Addr') ?></strong></td>
|
||||
<td><strong><?= gettext('Connected Since') ?></strong></td>
|
||||
<td><strong><?= gettext('Bytes Sent') ?></strong></td>
|
||||
<td><strong><?= gettext('Bytes Received') ?></strong></td>
|
||||
<td><strong><?= gettext('Status') ?></strong></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<?php foreach ($sk_servers as $sk_server): ?>
|
||||
<tr id="<?= html_safe("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 = service_by_name('openvpn', array('id' => $sk_server['vpnid'])); ?>
|
||||
<?= service_control_icon($ssvc, true); ?>
|
||||
<?= service_control_links($ssvc, true); ?>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($clients)): ?>
|
||||
|
||||
<?php if (!empty($openvpn_cfg['openvpn-client'])): ?>
|
||||
<div class="tab-content content-box __mb">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
@ -281,15 +254,16 @@ include("head.inc"); ?>
|
||||
<td><strong><?= gettext('Status') ?></strong></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<?php foreach ($clients as $client): ?>
|
||||
<?php foreach ($openvpn_cfg['openvpn-client'] as $client): ?>
|
||||
<?php $conn = $openvpn_status['client'][$client['vpnid']] ?? [];?>
|
||||
<tr id="<?= html_safe("r:{$client['port']}:{$client['vpnid']}") ?>">
|
||||
<td><?= $client['name'] ?></td>
|
||||
<td><?= $client['remote_host'] ?></td>
|
||||
<td><?= $client['virtual_addr'] ?></td>
|
||||
<td><?= $client['connect_time'] ?></td>
|
||||
<td><?= format_bytes($client['bytes_sent']) ?></td>
|
||||
<td><?= format_bytes($client['bytes_recv']) ?></td>
|
||||
<td><?= $client['status'] ?></td>
|
||||
<td><?= $conn['remote_host'] ?? ''?></td>
|
||||
<td><?= $conn['virtual_addr'] ?? '' ?></td>
|
||||
<td><?= !empty($conn) ? date('Y-m-d H:i:s', $conn['timestamp']) : '' ?></td>
|
||||
<td><?= format_bytes($conn['write_bytes'] ?? '0') ?></td>
|
||||
<td><?= format_bytes($conn['read_bytes'] ?? '0') ?></td>
|
||||
<td><?= !empty($conn) ? $conn['status'] : '' ?></td>
|
||||
<td>
|
||||
<div>
|
||||
<?php $ssvc = service_by_name('openvpn', array('id' => $client['vpnid'])); ?>
|
||||
@ -304,7 +278,7 @@ include("head.inc"); ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php if (empty($clients) && empty($servers) && empty($sk_servers)): ?>
|
||||
<?php if (empty($openvpn_cfg['openvpn-server']) && empty($openvpn_cfg['openvpn-client'])): ?>
|
||||
<div class="tab-content content-box __mb">
|
||||
<div class="table-responsive">
|
||||
<table class="table-responsive table table-striped">
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2014-2016 Deciso B.V.
|
||||
* Copyright (C) 2014-2023 Deciso B.V.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -29,10 +29,16 @@
|
||||
require_once("guiconfig.inc");
|
||||
require_once("plugins.inc.d/openvpn.inc");
|
||||
|
||||
$servers = openvpn_get_active_servers();
|
||||
$sk_servers = openvpn_get_active_servers("p2p");
|
||||
$clients = openvpn_get_active_clients();
|
||||
|
||||
$openvpn_status = json_decode(configd_run('openvpn status client,server'), true) ?? [];
|
||||
$openvpn_cfg = openvpn_config();
|
||||
foreach ($openvpn_cfg as $section => &$ovpncfg) {
|
||||
foreach ($ovpncfg as &$item) {
|
||||
$opt = ($section == 'openvpn-server') ? 'server' : 'client';
|
||||
if (!empty($openvpn_status[$opt][$item['vpnid']])) {
|
||||
$item = array_merge($openvpn_status[$opt][$item['vpnid']], $item);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<script>
|
||||
$("#dashboard_container").on("WidgetsReady", function() {
|
||||
@ -49,7 +55,7 @@ $clients = openvpn_get_active_clients();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
foreach ($servers as $server) :?>
|
||||
foreach ($openvpn_cfg['openvpn-server'] as $server) :?>
|
||||
<table class="table table-striped table-condensed">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -65,57 +71,39 @@ $clients = openvpn_get_active_clients();
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($server['conns'] as $conn) :?>
|
||||
if (!empty($server['client_list'])):
|
||||
foreach ($server['client_list'] as $conn) :?>
|
||||
<tr>
|
||||
<td><?=$conn['common_name'];?><br/><?=$conn['connect_time'];?></td>
|
||||
<td><?=$conn['remote_host'];?><br/><?=$conn['virtual_addr'];?></td>
|
||||
<td><?=$conn['common_name'] ?? '';?><br/><?=$conn['connected_since'] ?? '';?></td>
|
||||
<td><?=$conn['real_address'] ?? '';?><br/><?=$conn['virtual_address'] ?? '';?></td>
|
||||
<td>
|
||||
<span class="fa fa-times fa-fw act_kill_client" data-client-port="<?=$server['mgmt'];?>"
|
||||
data-client-ip="<?=$conn['remote_host'];?>"
|
||||
<span class="fa fa-times fa-fw act_kill_client" data-client-port="server<?=$server['vpnid'];?>"
|
||||
data-client-ip="<?=$conn['real_address'];?>"
|
||||
style='cursor:pointer;'
|
||||
title='Kill client connection from <?=$conn['remote_host']; ?>'>
|
||||
title='Kill client connection from <?=$conn['real_address']; ?>'>
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
endforeach; ?>
|
||||
endforeach;
|
||||
elseif (!empty($server['timestamp'])):?>
|
||||
<tr>
|
||||
<td><?=date('Y-m-d H:i:s', $server['timestamp']);?></td>
|
||||
<td><?=$server['remote_host'];?><br/><?=$server['virtual_addr'];?></td>
|
||||
<td>
|
||||
<span class='fa fa-exchange fa-fw <?=$server['status'] == "CONNECTED" ? "text-success" : "text-danger" ;?>'></span>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
endif;?>
|
||||
</tbody>
|
||||
</table>
|
||||
<br/>
|
||||
<?php
|
||||
endforeach; ?>
|
||||
|
||||
<?php
|
||||
if (!empty($sk_servers)):?>
|
||||
<table class="table table-striped table-condensed">
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="3"><?= gettext('Peer to Peer Server Instance Statistics') ?></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?= gettext('Name/Time') ?></th>
|
||||
<th><?= gettext('Remote/Virtual IP') ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($sk_servers as $sk_server) :?>
|
||||
<tr>
|
||||
<td><?=$sk_server['name'];?><br/><?=$sk_server['connect_time'];?></td>
|
||||
<td><?=$sk_server['remote_host'];?><br/><?=$sk_server['virtual_addr'];?></td>
|
||||
<td>
|
||||
<span class='fa fa-exchange fa-fw <?=$sk_server['status'] == "up" ? "text-success" : "text-danger";?>'></span>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<br/>
|
||||
<?php
|
||||
endif; ?>
|
||||
<?php
|
||||
if (!empty($clients)) {?>
|
||||
if (!empty($openvpn_cfg['openvpn-server'])) {?>
|
||||
<table class="table table-striped table-condensed">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -130,12 +118,12 @@ endif; ?>
|
||||
<tbody>
|
||||
|
||||
<?php
|
||||
foreach ($clients as $client) :?>
|
||||
foreach ($openvpn_cfg['openvpn-client'] as $client) :?>
|
||||
<tr>
|
||||
<td><?=$client['name'];?><br/><?=$client['connect_time'];?></td>
|
||||
<td><?=$client['name'];?><br/><?=date('Y-m-d H:i:s', $server['timestamp']);?></td>
|
||||
<td><?=$client['remote_host'];?><br/><?=$client['virtual_addr'];?></td>
|
||||
<td>
|
||||
<span class='fa fa-exchange fa-fw <?=$client['status'] == "up" ? "text-success" : "text-danger" ;?>'></span>
|
||||
<span class='fa fa-exchange fa-fw <?=$client['status'] == "connected" ? "text-success" : "text-danger" ;?>'></span>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
@ -146,11 +134,7 @@ endif; ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
if ($DisplayNote) {
|
||||
echo "<br /><b>".gettext('NOTE:')."</b> ".gettext("You need to bind each OpenVPN client to enable its management daemon: use 'Local port' setting in the OpenVPN client screen");
|
||||
}
|
||||
|
||||
if ((empty($clients)) && (empty($servers)) && (empty($sk_servers))): ?>
|
||||
if (empty($openvpn_cfg['openvpn-client']) && empty($openvpn_cfg['openvpn-server'])): ?>
|
||||
<table class="table table-striped table-condensed">
|
||||
<tr>
|
||||
<td><?= gettext('No OpenVPN instance defined or enabled.') ?></td>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user