Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a plugin for Netbird #4531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions net/netbird/+POST_INSTALL.post
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
chmod +x /usr/local/etc/rc.syshook.d/carp/30-netbird
chmod +x /usr/local/opnsense/scripts/OPNsense/netbird/initialup.sh
/etc/rc.d/os-release start
10 changes: 10 additions & 0 deletions net/netbird/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PLUGIN_NAME= netbird
PLUGIN_VERSION= 0.8
PLUGIN_REVISION= 1
PLUGIN_DEPENDS= netbird
PLUGIN_COMMENT= Netbird plugin
PLUGIN_MAINTAINER= [email protected]
PLUGIN_WWW= https:/netbird.io
PLUGIN_DEVEL= no
PLUGIN_SUFFIX=
.include "../../Mk/plugins.mk"
1 change: 1 addition & 0 deletions net/netbird/pkg-descr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Netbird plugin for OPNSense
30 changes: 30 additions & 0 deletions net/netbird/src/etc/inc/plugins.inc.d/netbird.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

function netbird_enabled()
{
$mdlNetbird = new \OPNsense\netbird\Netbird();
return $mdlNetbird->general->Enabled->__toString() == 1;
}

function netbird_services()
{
$services = array();

if (!netbird_enabled()) {
return $services;
}

$services[] = array(
'description' => gettext('Netbird'),
'configd' => array(
'restart' => array('netbird restart'),
'start' => array('netbird start'),
'stop' => array('netbird stop'),
),
'name' => 'netbird',
'pidfile' => '/var/run/netbird.pid'
);

return $services;
}

52 changes: 52 additions & 0 deletions net/netbird/src/etc/rc.syshook.d/carp/30-netbird
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/local/bin/php
<?php


require_once('config.inc');
require_once('util.inc');
require_once('interfaces.inc');


$model = new \OPNsense\netbird\Netbird();
$enabled = $model->general->Enabled->__toString();


if(!$enabled) {
exit(0);
}

$carpif = $model->general->CarpIf->__toString();

if($carpif == '') {
exit(0);
}

$target_vhid = $model->general->VHID;
$subsystem = !empty($argv[1]) ? $argv[1] : '';
$type = !empty($argv[2]) ? $argv[2] : '';

if ($type != 'MASTER' && $type != 'BACKUP') {
exit(1);
}

if (!strstr($subsystem, '@')) {
exit(1);
}

list ($vhid, $iface) = explode('@', $subsystem);
$friendly = convert_real_interface_to_friendly_interface_name($iface);


if ($carpif != $friendly || $vhid != $target_vhid) {
exit(0);
}

switch ($type) {
case 'MASTER':
shell_exec('/usr/local/bin/netbird up');
break;
case 'BACKUP':
shell_exec('/usr/local/bin/netbird down');
break;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace OPNsense\netbird\Api;

use OPNsense\Base\ApiMutableModelControllerBase;

/**
* netbird settings controller
* @package OPNsense\netbird
*/
class InitialController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'netbird';
protected static $internalModelClass = 'OPNsense\netbird\Initial';

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
<?php

namespace OPNsense\netbird\Api;

use OPNsense\Base\ApiMutableServiceControllerBase;
use OPNsense\Core\Backend;
use OPNsense\Core\Config;
use OPNsense\netbird\Initial;
use OPNsense\netbird\Netbird;


/**
* Class ServiceController
* @package OPNsense\netbird
*/
class ServiceController extends ApiMutableServiceControllerBase
{
const NETBIRD_CONFIG_JSON = '/usr/local/etc/netbird/config.json';
protected static $internalServiceClass = '\OPNsense\netbird\Netbird';
protected static $internalServiceEnabled = 'general.Enabled';
protected static $internalServiceTemplate = 'OPNsense/netbird';
protected static $internalServiceName = 'netbird';

public function conStatusAction(): string
{
$backend = new Backend();
$bckResult = $backend->configdRun("netbird con-status");
if ($bckResult !== null) {
return nl2br(htmlspecialchars($bckResult));
}
return "Error retrieving connection status";
}

public function searchFilter($array, $value): bool
{
foreach ($array as $val) {
if (str_contains(strval($val), strtolower($value))) {
return true;
}
}
return false;
}

public function upDownStatusAction(): string
{
$backend = new Backend();
$bckResult = $backend->configdRun("netbird status");
if (!str_contains($bckResult, "is running")) {
return json_encode(array('updown' => "NOT RUNNING", 'status' => "Netbird is not running"));
}
$bckResult = $backend->configdRun("netbird short-con-status");
$txtStatus = nl2br(htmlspecialchars($bckResult));
$bckResult = $backend->configdRun("netbird con-status-json");
$status = json_decode($bckResult, true);
if (!$status['publicKey']) {
return json_encode(array('updown' => "DOWN", 'status' => $txtStatus));
}
return json_encode(array('updown' => "UP", 'status' => $txtStatus));
}

public function searchAction(): string
{
$request = $this->request;
$backend = new Backend();
$bckResult = $backend->configdRun("netbird status");
if (!str_contains($bckResult, "is running")) {
return json_encode(array('current' => 1, 'rowCount' => 0, 'total' => 0, 'rows' => array()));
}
$bckResult = $backend->configdRun("netbird con-status-json");
$status = json_decode($bckResult, true);
$itemsPerPage = $request->get('rowCount', 'int', -1);
$currentPage = $request->get('current', 'int', 1);
$sortBy = array('status');
$sortDescending = false;


$searchPhrase = strtolower($request->get('searchPhrase', 'string', ''));
if (!$status['peers']['details']) {
return json_encode(array('current' => 1, 'rowCount' => 0, 'total' => 0, 'rows' => array()));
}
$details = $status['peers']['details'];
$details = array_filter($details, function ($item) use ($searchPhrase) {
return $this->searchFilter($item, $searchPhrase);
});
$detailsFlat = array();
foreach ($details as $detail) {
$detailsFlat[] = $this->flattenOneLevel($detail);
}
if ($request->hasPost('sort') && is_array($request->get("sort")) && !empty($request->get("sort"))) {
$sortBy = array_keys($request->get("sort"));
if (!empty($sortBy) && $request->get("sort")[$sortBy[0]] == "desc") {
$sortDescending = true;
}

}
$sortValues = array();
foreach ($detailsFlat as $detail) {
$sortValues[] = $detail[$sortBy[0]];
}
array_multisort($sortValues, $sortDescending ? SORT_DESC : SORT_ASC, $detailsFlat);
$page = array_slice($detailsFlat, ($currentPage - 1) * $itemsPerPage, $itemsPerPage);
$page = $this->convertFieldsToDisplay($page);
$result = array('current' => $currentPage, 'rowCount' => count($page), 'total' => count($detailsFlat), 'rows' => $page);
return json_encode($result);
}

private function flattenOneLevel($array): array
{
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $subkey => $subvalue) {
if ($key == "routes") {
$result[$key] = implode("<br />", $value);
}
else {
$result[$key . "." . $subkey] = $subvalue;
}
}
} else {
$result[$key] = $value;
}
}
return $result;
}

public function setUpAction(): string
{
$backend = new Backend();
try {
return $backend->configdRun("netbird set-up");
} catch (\Exception $e) {
return "Error running netbird up" . "\n" . $e->getMessage();
}
}

public function initialUpAction(): string
{
$backend = new Backend();
$mdlInitial = new Initial();
$key = $mdlInitial->initial->setupkey->__toString();
$api = $mdlInitial->initial->mgmtservice->__toString();
$hostname = $mdlInitial->initial->hostname->__toString();
if ($hostname == "") {
$hostname = gethostname();
if(!$hostname){
$hostname = "OPNsense";
}else{
if(str_contains($hostname, ".")){
$hostname = explode(".", $hostname)[0];
}
}

$mdlInitial->initial->hostname = $hostname;
}
$mdlInitial->initial->setupkey = "00000000-0000-0000-0000-000000000000";
$mdlInitial->initial->initsure = 0;

$mdlInitial->serializeToConfig();
$cnf = Config::getInstance();
$cnf->save();

$bckresult = $backend->configdRun("netbird set-up-initial " . escapeshellarg($api) . " " . escapeshellarg($key) . " " . escapeshellarg($hostname));
return nl2br(htmlspecialchars($bckresult));
}

public function setDownAction(): string
{
$backend = new Backend();
try {
return $backend->configdRun("netbird set-down");
} catch (\Exception $e) {
return "Error running netbird down" . "\n" . $e->getMessage();
}
}

public function reloadAction()
{
$status = "failed";
if ($this->request->isPost()) {
try {
$mdlNetbird = new Netbird();
$backend = new Backend();
if (trim($backend->configdRun('template reload OPNsense/netbird')) == "OK") {
$status = "ok";
}

$enabled = $mdlNetbird->general->Enabled->__toString() == 1;
$carpEnabled = $mdlNetbird->general->CarpIf->__toString() != '';
$disableClientRoutes = $mdlNetbird->general->DisableClientRoutes->__toString() == 1;
$disableServerRoutes = $mdlNetbird->general->DisableServerRoutes->__toString() == 1;
$disableDNS = $mdlNetbird->general->DisableDNS->__toString() == 1;
$rpEnabled = $mdlNetbird->general->QuantumEnabled->__toString() == 1;
$rpPermissive = $mdlNetbird->general->QuantumPermissive->__toString() == 1;
$wgPort = $mdlNetbird->general->WgPort->__toString();
$netbirdConfigJson = file_get_contents(self::NETBIRD_CONFIG_JSON);
$netbirdConfig = json_decode($netbirdConfigJson, true);
$netbirdConfig["DisableAutoConnect"] = $carpEnabled;
$netbirdConfig["DisableClientRoutes"] = $disableClientRoutes;
$netbirdConfig["DisableServerRoutes"] = $disableServerRoutes;
$netbirdConfig["DisableDNS"] = $disableDNS;
$netbirdConfig["RosenpassEnabled"] = $rpEnabled;
$netbirdConfig["RosenpassPermissive"] = $rpPermissive;
$netbirdConfig["WgPort"] = intval($wgPort);
$netbirdConfigJson = json_encode($netbirdConfig);
file_put_contents(self::NETBIRD_CONFIG_JSON, $netbirdConfigJson);
$action = $enabled ? "restart" : "stop";
$backend->configdRun("netbird $action");
} catch (\Exception $e) {
$status = "failed";
syslog(LOG_ERR, "netbird: failed to reload configuration: " . $e->getMessage());
}
}
return array("status" => $status);
}

/**
* @param array $page
* @return array
*/
public function convertFieldsToDisplay(array $page): array
{
for ($i = 0; $i < count($page); $i++) {
$page[$i]['latency'] = round($page[$i]['latency'] / 1000000, 2) . " ms";
$received = $page[$i]['transferReceived'];
$rcvUnit = "KiB";
$received /= 1024;
if ($received > 1024) {
$received /= 1024;
$rcvUnit = "MiB";
}
if ($received > 1024) {
$received /= 1024;
$rcvUnit = "GiB";
}

$sent = $page[$i]['transferSent'];
$sentUnit = "KiB";
$sent /= 1024;
if ($sent > 1024) {
$sent /= 1024;
$sentUnit = "MiB";
}
if ($sent > 1024) {
$sent /= 1024;
$sentUnit = "GiB";
}
$page[$i]['transferReceived'] = round($received, 2) . " " . $rcvUnit;
$page[$i]['transferSent'] = round($sent, 2) . " " . $sentUnit;
$page[$i]['lastStatusUpdate'] = date("Y-m-d H:i:s", strtotime($page[$i]['lastStatusUpdate']));
$page[$i]['lastWireguardHandshake'] = date("Y-m-d H:i:s", strtotime($page[$i]['lastWireguardHandshake']));
foreach ($page[$i] as $key => $value) {
if ($value == "true") {
$page[$i][$key] = 1;
} elseif ($value == "false") {
$page[$i][$key] = 0;
}

}
}
return $page;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


namespace OPNsense\netbird\Api;

use OPNsense\Base\ApiMutableModelControllerBase;

/**
* netbird settings controller
* @package OPNsense\netbird
*/
class SettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelName = 'netbird';
protected static $internalModelClass = 'OPNsense\netbird\Netbird';
}
Loading