Skip to content

Commit e01db24

Browse files
authored
Refactor (#1)
- Migrated the authentication page from XML to a fully custom PHP implementation for better control and flexibility - Service is now started on installation and persisted via an RC file for automatic restart after system reboot - Added support for start/stop/restart actions via the pfSense service shortcuts
1 parent e6ac589 commit e01db24

File tree

15 files changed

+439
-197
lines changed

15 files changed

+439
-197
lines changed

.github/workflows/release.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
branches:
8+
- main
9+
pull_request:
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || github.actor_id }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
release:
17+
runs-on: ubuntu-22.04
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Build
23+
id: build
24+
uses: vmactions/freebsd-vm@v1
25+
with:
26+
usesh: true
27+
copyback: true
28+
release: "15.0"
29+
prepare: |
30+
pkg install -y git go124 ca_root_nss poudriere
31+
git clone -b devel --depth 1 --single-branch https://github.com/pfsense/FreeBSD-ports.git /usr/ports
32+
run: |
33+
set -ex
34+
cd pfSense-pkg-NetBird
35+
make
36+
make package
37+
cd ../netbird
38+
make makesum
39+
make package
40+
41+
- name: Upload pfSense package artifact
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: pfSense-package
45+
path: pfSense-pkg-NetBird/work/pkg/pfSense-pkg-NetBird-*.pkg
46+
retention-days: 3
47+
48+
- name: Upload FreeBSD package artifact
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: FreeBSD-package
52+
path: netbird/work/pkg/netbird-*.pkg
53+
retention-days: 3
54+
55+
- name: Create Release
56+
if: startsWith(github.ref, 'refs/tags/')
57+
uses: actions/create-release@v1
58+
with:
59+
tag_name: ${{ github.ref_name }}
60+
release_name: Release ${{ github.ref_name }}
61+
draft: false
62+
prerelease: false
63+
env:
64+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65+
66+
- name: Upload packages to release page
67+
if: startsWith(github.ref, 'refs/tags/')
68+
uses: softprops/action-gh-release@v1
69+
with:
70+
files: |
71+
pfSense-pkg-NetBird/work/pkg/pfSense-pkg-NetBird-*.pkg
72+
netbird/work/pkg/netbird-*.pkg
73+
env:
74+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

assets/netbird.png

-126 KB
Loading

netbird/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
PORTNAME= netbird
2+
DISTVERSIONPREFIX= v
3+
DISTVERSION= 0.49.0
4+
CATEGORIES= security net net-vpn
5+
6+
MAINTAINER= [email protected]
7+
COMMENT= Peer-to-peer VPN that seamlessly connects your devices
8+
WWW= https://netbird.io/
9+
10+
LICENSE= BSD3CLAUSE
11+
LICENSE_FILE= ${WRKSRC}/LICENSE
12+
13+
NOT_FOR_ARCHS= i386
14+
NOT_FOR_ARCHS_REASON= "no 32-bit builds supported"
15+
16+
RUN_DEPENDS= ca_root_nss>0:security/ca_root_nss
17+
18+
USES= go:modules
19+
USE_RC_SUBR= netbird
20+
21+
GO_MODULE= github.com/netbirdio/netbird
22+
GO_TARGET= ./client:netbird
23+
GO_BUILDFLAGS= -tags freebsd -o ${PORTNAME} -ldflags \
24+
"-s -w -X github.com/netbirdio/netbird/version.version=${DISTVERSION}"
25+
26+
WRKSRC= ${WRKDIR}/netbird-${DISTVERSION}
27+
28+
PLIST_FILES= bin/netbird
29+
30+
.include <bsd.port.mk>

netbird/files/netbird.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
#
3+
# PROVIDE: netbird
4+
# REQUIRE: SERVERS
5+
# KEYWORD: shutdown
6+
#
7+
8+
. /etc/rc.subr
9+
10+
name="netbird"
11+
netbird_env="IS_DAEMON=1"
12+
pidfile="/var/run/${name}.pid"
13+
command="/usr/sbin/daemon"
14+
daemon_args="-P ${pidfile} -r -t \"${name}: daemon\""
15+
command_args="${daemon_args} /usr/local/bin/netbird service run --config /var/db/netbird/config.json --log-level info --daemon-addr unix:///var/run/netbird.sock --log-file /var/log/netbird/client.log"
16+
17+
run_rc_command "$1"
18+

netbird/pkg-descr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
NetBird is an open-source WireGuard-based overlay network combined with
2+
Zero Trust Network Access, providing secure and reliable connectivity
3+
to internal resources.
4+
5+
Key features:
6+
- Zero-config VPN: Easily create secure connections between devices without
7+
manual network setup.
8+
- Built on WireGuard: Leverages WireGuard's high-performance encryption for
9+
fast and secure communication.
10+
- Self-hosted or Cloud-managed: Users can deploy their own NetBird management
11+
server or use NetBird Cloud for centralized control.
12+
- Access Control & Routing: Fine-grained access control policies and automatic
13+
network routing simplify connectivity.
14+
- This FreeBSD port provides the NetBird client daemon and CLI tools, allowing
15+
FreeBSD systems to join a NetBird mesh network and securely communicate with
16+
other peers.
17+
18+
For more details, visit: https://netbird.io

netbird/pkg-message

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{ type: install
3+
message: <<EOM
4+
At this time this code is new, unvetted, possibly buggy, and should be
5+
considered "experimental". It might contain security issues. We gladly
6+
welcome your testing and bug reports, but do keep in mind that this code
7+
is new, so some caution should be exercised at the moment for using it
8+
in mission critical environments.
9+
EOM
10+
}
11+
]

pfSense-pkg-NetBird/files/usr/local/pkg/netbird.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@
3434
<name>NetBird</name>
3535
<section>VPN</section>
3636
<configfile>netbird.xml</configfile>
37-
<url>/pkg_edit.php?xml=netbird/netbird_auth.xml</url>
37+
<url>/netbird_auth.php</url>
3838
</menu>
3939
<menu>
4040
<name>NetBird</name>
4141
<section>Status</section>
4242
<configfile>netbird.xml</configfile>
4343
<url>/netbird_status.php</url>
4444
</menu>
45+
<service>
46+
<name>netbird</name>
47+
<rcfile>netbird.sh</rcfile>
48+
<executable>netbird</executable>
49+
<description>NetBird secure overlay network</description>
50+
</service>
4551
<tabs>
4652
<tab>
4753
<text>Authentication</text>
48-
<url>pkg_edit.php?xml=netbird/netbird_auth.xml</url>
54+
<url>netbird_auth.php</url>
4955
</tab>
5056
<tab>
5157
<text>Settings</text>
@@ -250,6 +256,12 @@
250256
]]>
251257
</note>
252258

259+
<custom_php_install_command>
260+
<![CDATA[netbird_install();]]>
261+
</custom_php_install_command>
262+
<custom_php_pre_deinstall_command>
263+
<![CDATA[netbird_deinstall();]]>
264+
</custom_php_pre_deinstall_command>
253265
<custom_php_after_head_command>
254266
<![CDATA[netbird_display_connection_info();]]>
255267
</custom_php_after_head_command>

pfSense-pkg-NetBird/files/usr/local/pkg/netbird/netbird.inc

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,56 @@
2020
*/
2121

2222
require_once('service-utils.inc');
23+
require_once("config.inc");
24+
require_once("util.inc");
2325

2426
define('NETBIRD_BIN', '/usr/local/bin/netbird');
27+
define('NETBIRD_CONFIG', '/var/db/netbird/config.json');
2528
define('PKG_BIN', '/usr/sbin/pkg');
2629

2730

2831
function netbird_resync_config()
2932
{
30-
if (netbird_is_connected()) {
31-
if (!netbird_disconnect()) {
32-
return;
33-
}
34-
}
35-
33+
$json = file_get_contents(NETBIRD_CONFIG);
34+
$config = json_decode($json, true);
3635

37-
$cmd = [NETBIRD_BIN, 'up'];
36+
if (!is_array($config)) {
37+
log_error("Invalid netbird configuration");
38+
return;
39+
}
3840

3941
if (!empty($_POST['wireguardport'])) {
40-
$cmd[] = '--wireguard-port=' . escapeshellarg((int)$_POST['wireguardport']);
41-
}
42-
if (!empty($_POST['loglevel'])) {
43-
$cmd[] = '--log-level=' . escapeshellarg($_POST['loglevel']);
42+
$config['WgPort'] = (int)$_POST['wireguardport'];
4443
}
4544

46-
$options = [
47-
'enablessh' => ['--allow-server-ssh', true],
48-
'blockinboundconn' => ['--block-inbound', true],
49-
'allowfirewallconfig' => ['--disable-firewall', false],
50-
'enabledns' => ['--disable-dns', false],
51-
'accesslan' => ['--block-lan-access', false],
52-
'allowclientroutes' => ['--disable-client-routes', false],
53-
'allowserverroutes' => ['--disable-server-routes', false],
54-
'enablerosenpass' => ['--enable-rosenpass', true],
55-
'rosenpasspermissive' => ['--rosenpass-permissive', true],
45+
$config_map = [
46+
'enablessh' => ['ServerSSHAllowed', true],
47+
'blockinboundconn' => ['BlockInbound', true],
48+
'allowfirewallconfig' => ['DisableFirewall', false],
49+
'enabledns' => ['DisableDNS', false],
50+
'accesslan' => ['BlockLANAccess', false],
51+
'allowclientroutes' => ['DisableClientRoutes', false],
52+
'allowserverroutes' => ['DisableServerRoutes', false],
53+
'enablerosenpass' => ['RosenpassEnabled', true],
54+
'rosenpasspermissive' => ['RosenpassPermissive', true],
5655
];
5756

58-
foreach ($options as $key => [$flag, $enabled_value]) {
59-
$is_checked = ($_POST[$key] ?? '') === 'on';
60-
$cmd[] = $flag . '=' . ($is_checked === $enabled_value ? 'true' : 'false');
57+
foreach ($config_map as $post_key => [$json_key, $enabled_val]) {
58+
$checked = ($_POST[$post_key] ?? '') === 'on';
59+
$config[$json_key] = ($checked === $enabled_val);
6160
}
6261

63-
exec(implode(' ', $cmd));
62+
file_put_contents(NETBIRD_CONFIG, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
63+
64+
65+
if (netbird_is_connected()) {
66+
if (!netbird_disconnect()) {
67+
return;
68+
}
69+
70+
$cmd = implode(' ', [NETBIRD_BIN, 'up']);
71+
exec($cmd);
72+
}
6473
}
6574

6675

@@ -114,7 +123,7 @@ function netbird_display_connection_info(): void
114123
$type = 'danger';
115124
$closable = false;
116125
} elseif (!netbird_is_connected()) {
117-
$message = gettext('NetBird is not connected. Refresh or check the NetBird status page.');
126+
$message = gettext('NetBird is not connected.');
118127
$type = 'warning';
119128
$closable = false;
120129
} else {
@@ -125,4 +134,38 @@ function netbird_display_connection_info(): void
125134

126135
print_info_box($message, $type, $closable ? 'close' : false);
127136
}
137+
138+
function netbird_write_rcfile() {
139+
$rc['file'] = 'netbird.sh';
140+
$rc['start'] .= "/usr/local/bin/netbird service start\n\t";
141+
$rc['stop'] .= "/usr/local/bin/netbird service stop\n\t";
142+
$rc['restart'] .= "/usr/local/bin/netbird service restart\n\t";
143+
write_rcfile($rc);
144+
}
145+
146+
function netbird_install()
147+
{
148+
netbird_write_rcfile();
149+
150+
if (!netbird_is_running()){
151+
$cmd = implode(' ', [NETBIRD_BIN, 'service', 'start']);
152+
exec($cmd);
153+
}
154+
}
155+
156+
function netbird_deinstall()
157+
{
158+
global $config;
159+
160+
if (netbird_is_running()) {
161+
stop_service("netbird");
162+
}
163+
164+
unlink_if_exists(NETBIRD_CONFIG);
165+
166+
if (isset($config['installedpackages']['netbird'])) {
167+
unset($config['installedpackages']['netbird']);
168+
write_config("Removed netbird configuration");
169+
}
170+
}
128171
?>

pfSense-pkg-NetBird/files/usr/local/pkg/netbird/netbird_auth.inc

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)