144 lines
4.6 KiB
JavaScript
144 lines
4.6 KiB
JavaScript
import Gio from 'gi://Gio';
|
|
import GLib from 'gi://GLib';
|
|
import GObject from 'gi://GObject';
|
|
import NM from 'gi://NM';
|
|
import St from 'gi://St';
|
|
|
|
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
|
|
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
|
|
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
|
|
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
|
|
|
|
const UNIT = 'netbird-hooks.service';
|
|
const GREEN = '#A3BE8C';
|
|
const RED = '#BF616A';
|
|
|
|
const Indicator = GObject.registerClass(
|
|
class Indicator extends PanelMenu.Button {
|
|
_init(extension, settings) {
|
|
super._init(0.0, 'NetBird Hooks');
|
|
this._extension = extension;
|
|
this._settings = settings;
|
|
this._icon = new St.Icon({icon_name: 'network-vpn-symbolic',
|
|
style_class: 'system-status-icon', style: `color: ${RED};`});
|
|
this.add_child(this._icon);
|
|
|
|
const settingsItem = new PopupMenu.PopupMenuItem('NetBird settings');
|
|
settingsItem.connect('activate', () => {
|
|
try {
|
|
this._extension.openPreferences();
|
|
} catch (e) {
|
|
logError(e, 'netbird-hooks openPreferences');
|
|
}
|
|
});
|
|
this.menu.addMenuItem(settingsItem);
|
|
|
|
this._client = NM.Client.new(null);
|
|
this._client.connectObject(
|
|
'notify::active-connections', () => this._refresh(),
|
|
'notify::connectivity', () => this._refresh(),
|
|
this);
|
|
this._timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 15,
|
|
() => {
|
|
this._refresh();
|
|
return GLib.SOURCE_CONTINUE;
|
|
});
|
|
this._refresh();
|
|
}
|
|
|
|
_refresh() {
|
|
if (this._pending)
|
|
return;
|
|
this._pending = true;
|
|
try {
|
|
const proc = Gio.Subprocess.new(['netbird', 'status'],
|
|
Gio.SubprocessFlags.STDOUT_PIPE |
|
|
Gio.SubprocessFlags.STDERR_SILENCE);
|
|
proc.communicate_utf8_async(null, null, (p, res) => {
|
|
this._pending = false;
|
|
let connected = false;
|
|
try {
|
|
const [, out] = p.communicate_utf8_finish(res);
|
|
connected = p.get_exit_status() === 0 &&
|
|
/Management: Connected/i.test(out ?? '');
|
|
} catch {
|
|
connected = false;
|
|
}
|
|
this._setConnected(connected);
|
|
});
|
|
} catch {
|
|
this._pending = false;
|
|
this._setConnected(false);
|
|
}
|
|
}
|
|
|
|
_setConnected(connected) {
|
|
this.visible = this._settings.get_boolean('show-indicator');
|
|
this._icon.style = `color: ${connected ? GREEN : RED};`;
|
|
}
|
|
|
|
destroy() {
|
|
if (this._timer) {
|
|
GLib.source_remove(this._timer);
|
|
this._timer = null;
|
|
}
|
|
this._client.disconnectObject(this);
|
|
super.destroy();
|
|
}
|
|
});
|
|
|
|
export default class NetbirdHooksExtension extends Extension {
|
|
enable() {
|
|
this._settings = this.getSettings();
|
|
this._unitPath = GLib.build_filenamev([GLib.get_user_config_dir(),
|
|
'systemd', 'user', UNIT]);
|
|
this._writeUnit();
|
|
this._systemctl(['daemon-reload']);
|
|
this._systemctl(['enable', '--now', UNIT]);
|
|
this._indicator = new Indicator(this, this._settings);
|
|
Main.panel.addToStatusArea(this.uuid, this._indicator);
|
|
}
|
|
|
|
disable() {
|
|
this._indicator.destroy();
|
|
this._indicator = null;
|
|
this._systemctl(['disable', '--now', UNIT]);
|
|
try {
|
|
Gio.File.new_for_path(this._unitPath).delete(null);
|
|
} catch {
|
|
}
|
|
this._systemctl(['daemon-reload']);
|
|
this._settings = null;
|
|
}
|
|
|
|
_writeUnit() {
|
|
GLib.mkdir_with_parents(GLib.path_get_dirname(this._unitPath), 0o755);
|
|
const contents = [
|
|
'[Unit]',
|
|
'Description=NetBird open-wifi enforcement watcher',
|
|
'After=network-online.target',
|
|
'',
|
|
'[Service]',
|
|
'Type=simple',
|
|
`ExecStart=/bin/sh ${this.path}/netbird-policy.sh`,
|
|
'Restart=on-failure',
|
|
'RestartSec=5',
|
|
'',
|
|
'[Install]',
|
|
'WantedBy=default.target',
|
|
'',
|
|
].join('\n');
|
|
GLib.file_set_contents(this._unitPath, contents);
|
|
}
|
|
|
|
_systemctl(args) {
|
|
try {
|
|
const proc = Gio.Subprocess.new(['systemctl', '--user', ...args],
|
|
Gio.SubprocessFlags.NONE);
|
|
proc.wait_check(null);
|
|
} catch (e) {
|
|
logError(e, 'netbird-hooks systemctl');
|
|
}
|
|
}
|
|
}
|