2020-04-20 11:53:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'includes/status_messages.php';
|
|
|
|
require_once 'config.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage WireGuard configuration
|
|
|
|
*/
|
|
|
|
function DisplayWireGuardConfig()
|
|
|
|
{
|
|
|
|
$status = new StatusMessages();
|
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['savewgettings'])) {
|
2020-08-25 21:11:27 +00:00
|
|
|
# Todo: validate input
|
2020-04-20 11:53:46 +00:00
|
|
|
if (isset($_POST['authUser'])) {
|
2020-08-25 21:11:27 +00:00
|
|
|
$peer_id = strip_tags(trim($_POST'peer_id']));
|
2020-04-20 11:53:46 +00:00
|
|
|
}
|
2020-08-25 21:11:27 +00:00
|
|
|
if (isset($_POST['wg_endpoint'])) {
|
|
|
|
$wg_endpoint = strip_tags(trim($_POST['wg_endpoint']));
|
2020-04-20 11:53:46 +00:00
|
|
|
}
|
2020-08-25 21:11:27 +00:00
|
|
|
if (isset($_POST['wg_allowedips'])) {
|
|
|
|
$wg_allowedips = strip_tags(trim($_POST['wg_allowedips']));
|
|
|
|
}
|
|
|
|
if (isset($_POST['wg_pkeepalive'])) {
|
|
|
|
$wg_pkeepalive = strip_tags(trim($_POST['wg_pkeepalive']));
|
|
|
|
}
|
|
|
|
if (isset($_POST['wg_peerpubkey'])) {
|
|
|
|
$wg_endpoint = strip_tags(trim($_POST['wg_peerpubkey']));
|
|
|
|
}
|
|
|
|
file_put_contents("/tmp/wgdata", $config);
|
|
|
|
system('sudo cp /tmp/wgdata '.RASPI_WIREGUARD_CONFIG, $return);
|
|
|
|
|
|
|
|
if ($return == 0) {
|
|
|
|
$status->addMessage('Wireguard configuration updated successfully', 'success');
|
|
|
|
} else {
|
|
|
|
$status->addMessage('Wireguard configuration failed to be updated.', 'danger');
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:53:46 +00:00
|
|
|
} elseif (isset($_POST['startwg'])) {
|
|
|
|
$status->addMessage('Attempting to start WireGuard', 'info');
|
2020-04-22 09:01:31 +00:00
|
|
|
exec('sudo /usr/bin/wg-quick up wg0', $return);
|
2020-04-20 11:53:46 +00:00
|
|
|
foreach ($return as $line) {
|
|
|
|
$status->addMessage($line, 'info');
|
|
|
|
}
|
|
|
|
} elseif (isset($_POST['stopwg'])) {
|
|
|
|
$status->addMessage('Attempting to stop WireGuard', 'info');
|
2020-04-22 09:01:31 +00:00
|
|
|
exec('sudo /usr/bin/wg-quick down wg0', $return);
|
2020-04-20 11:53:46 +00:00
|
|
|
foreach ($return as $line) {
|
|
|
|
$status->addMessage($line, 'info');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 09:01:31 +00:00
|
|
|
exec('pidof wg-crypt-wg0 | wc -l', $wgstatus);
|
2020-04-20 11:53:46 +00:00
|
|
|
|
|
|
|
$serviceStatus = $wgstatus[0] == 0 ? "down" : "up";
|
2020-04-22 09:01:31 +00:00
|
|
|
$wg_state = ($wgstatus[0] > 0);
|
2020-04-20 11:53:46 +00:00
|
|
|
|
|
|
|
echo renderTemplate(
|
|
|
|
"wireguard", compact(
|
|
|
|
"status",
|
2020-04-22 09:01:31 +00:00
|
|
|
"wg_state",
|
2020-08-25 21:11:27 +00:00
|
|
|
"serviceStatus",
|
|
|
|
"endpoint_enable",
|
|
|
|
"peer_id",
|
|
|
|
"wg_endpoint",
|
|
|
|
"wg_allowedips",
|
|
|
|
"wg_pkeepalive",
|
|
|
|
"wg_peerpubkey"
|
2020-04-20 11:53:46 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|