Skip to content

Commit

Permalink
refactor: improvements and better readability
Browse files Browse the repository at this point in the history
* refactor: wip

* fix(metadata): remove settings-schema
  • Loading branch information
diegodario88 authored Mar 23, 2023
1 parent 651ecce commit 0b6a287
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 78 deletions.
11 changes: 4 additions & 7 deletions scripts/bridge-snx-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ set pass [lindex $argv 0]
spawn snx

expect "Please enter your password:" {
send "$pass\r\n"
send "$pass\r"
}

expect {
"SNX: Connection aborted" {
send_user "Login Failed: $pass\n"
}
send_user "Login Successful: $pass\n"
}
expect "SNX: Connection aborted" {
send_user "Login failed due to connection issues. Please ensure that you're connected to the internet and try again. If the problem persists, please contact support."
}
15 changes: 6 additions & 9 deletions snx-vpn-indicator@diegodario88.github.io/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ class Extension {
return this._networkManagerClient
.get_devices()
.map((device) => device.get_description().trim())
.some(
(description) =>
description === Util.getConstantByKey('SNX_DEVICE_NAME')
);
.some((description) => description === Util.CONSTANTS['SNX_DEVICE_NAME']);
}

/**
Expand All @@ -97,8 +94,7 @@ class Extension {
*/
handleOnAnyDeviceAdded(_, device) {
const description = device.get_description();
const shouldAvoid =
description !== Util.getConstantByKey('SNX_DEVICE_NAME');
const shouldAvoid = description !== Util.CONSTANTS['SNX_DEVICE_NAME'];

if (shouldAvoid) {
return;
Expand All @@ -115,14 +111,15 @@ class Extension {
*/
handleOnAnyDeviceRemoved(_, device) {
const description = device.get_description();
const shouldAvoid =
description !== Util.getConstantByKey('SNX_DEVICE_NAME');
const shouldAvoid = description !== Util.CONSTANTS['SNX_DEVICE_NAME'];

if (shouldAvoid) {
return;
}

this._indicator.hide();
const stateReasonCode = device.get_state_reason();
const reason = Util.NMDeviceStateReason[stateReasonCode];
this._indicator.hide(reason);
}
}

Expand Down
13 changes: 9 additions & 4 deletions snx-vpn-indicator@diegodario88.github.io/indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var SnxIndicator = GObject.registerClass(
super._init();

this._indicator = this._addIndicator();
this._indicator.icon_name = Util.getConstantByKey('ENABLED_VPN_ICON');
this._indicator.icon_name = Util.CONSTANTS['ENABLED_VPN_ICON'];
this._indicator.visible = hasTunsnxDevice;
this._snxToggle = new Toggle.SnxToggle(hasTunsnxDevice);
this.quickSettingsItems.push(this._snxToggle);
Expand Down Expand Up @@ -41,16 +41,21 @@ var SnxIndicator = GObject.registerClass(
}
}

hide() {
/**
* @param {string} reason
*/
hide(reason) {
log(`[SnxIndicator] hide: ${reason}`);
this._indicator.visible = false;
this._snxToggle.checked = false;
this._snxToggle.icon_name = Util.getConstantByKey('DISABLED_VPN_ICON');
this._snxToggle.icon_name = Util.CONSTANTS['DISABLED_VPN_ICON'];
this._snxToggle._removeSessionParameters();
}

show() {
this._indicator.visible = true;
this._snxToggle.checked = true;
this._snxToggle.icon_name = Util.getConstantByKey('ENABLED_VPN_ICON');
this._snxToggle.icon_name = Util.CONSTANTS['ENABLED_VPN_ICON'];
}
}
);
1 change: 0 additions & 1 deletion snx-vpn-indicator@diegodario88.github.io/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
"url": "https://github.com/diegodario88/snx-vpn-indicator",
"name": "SNX VPN Indicator",
"description": "This extension adds VPN functionality to the quickSettings by integrating the SSL Network Extender (SNX CLI) client",
"settings-schema": "org.gnome.shell.extensions.snx-indicator",
"gettext-domain": "snx-indicator"
}
62 changes: 29 additions & 33 deletions snx-vpn-indicator@diegodario88.github.io/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ var SnxToggle = GObject.registerClass(
};

if (Util.getGnomeShellVersion() > 43) {
config.title = Util.getConstantByKey('SNX_LABEL');
config.title = Util.CONSTANTS['SNX_LABEL'];
} else {
config.label = Util.getConstantByKey('SNX_LABEL');
config.label = Util.CONSTANTS['SNX_LABEL'];
}

super._init(config);

this.icon_name = hasTunsnxDevice
? Util.getConstantByKey('ENABLED_VPN_ICON')
: Util.getConstantByKey('DISABLED_VPN_ICON');
? Util.CONSTANTS['ENABLED_VPN_ICON']
: Util.CONSTANTS['DISABLED_VPN_ICON'];

this.previousCancellable = null;
this._mainItemsSection = new PopupMenu.PopupMenuSection();
this._separator = new PopupMenu.PopupSeparatorMenuItem('Connector');

this._popupSwitchMenuItem = new PopupMenu.PopupSwitchMenuItem(
Util.getConstantByKey('SNX_LABEL_EXTENDED'),
Util.CONSTANTS['SNX_LABEL_EXTENDED'],
this.checked
);

this._mainItemsSection.addMenuItem(this._popupSwitchMenuItem);
this.menu.setHeader(Util.getConstantByKey('ENABLED_VPN_ICON'), _('VPN'));
this.menu.setHeader(Util.CONSTANTS['ENABLED_VPN_ICON'], _('VPN'));
this.menu.addMenuItem(this._mainItemsSection);
this.menu.addMenuItem(this._separator);

Expand Down Expand Up @@ -66,6 +66,7 @@ var SnxToggle = GObject.registerClass(
* @param {string} loginResponse
*/
_addSessionParameters(loginResponse) {
this._removeSessionParameters();
const sessionParams = Util.parseSessionParameters(loginResponse);

sessionParams.forEach((session) =>
Expand Down Expand Up @@ -101,7 +102,7 @@ var SnxToggle = GObject.registerClass(
[
'zenity',
'--password',
'--title=SSL Network Extender VPN Authentication ',
'--title=SNX VPN Authentication ',
'--timeout=20'
],
null,
Expand All @@ -126,33 +127,30 @@ var SnxToggle = GObject.registerClass(
.trimEnd()
.trimStart();

if (
loginResponse.includes('Access denied') ||
loginResponse.includes('Authentication failed.')
) {
if (!loginResponse.includes('Session parameters:')) {
throw new Gio.IOErrorEnum({
code: Gio.IOErrorEnum.FAILED,
message: loginResponse
});
}

this.icon_name = Util.getConstantByKey('ENABLED_VPN_ICON');
this._addSessionParameters(loginResponse);

Util.vpnNotify(
Util.VPN_NOTIFY(
_('Successfully connected to VPN'),
Util.getConstantByKey('ENABLED_VPN_ICON')
Util.CONSTANTS['ENABLED_VPN_ICON']
);
} catch (error) {
logError(error);
if (error.code !== 14) {
Util.vpnNotify(
Util.VPN_NOTIFY(
_(error.message),
Util.getConstantByKey('NO_ROUTE_VPN_ICON')
Util.CONSTANTS['NO_ROUTE_VPN_ICON']
);
}

this.checked = false;
this.icon_name = Util.getConstantByKey('DISABLED_VPN_ICON');
this.icon_name = Util.CONSTANTS['DISABLED_VPN_ICON'];
}
}

Expand All @@ -161,21 +159,19 @@ var SnxToggle = GObject.registerClass(
* @param {Gio.Cancellable} cancellable
* @returns void
*/
_handleUncheckedAction(cancellable) {
Util.execCommunicate(['/usr/bin/snx', '-d'], null, cancellable)
.then((output) => {
this._removeSessionParameters();
Util.vpnNotify(
_(output),
Util.getConstantByKey('DISCONNECTED_VPN_ICON')
);
})
.catch((error) => {
Util.vpnNotify(
_(error.message),
Util.getConstantByKey('NO_ROUTE_VPN_ICON')
);
});
async _handleUncheckedAction(cancellable) {
try {
const output = await Util.execCommunicate(
['/usr/bin/snx', '-d'],
null,
cancellable
);

Util.VPN_NOTIFY(_(output), Util.CONSTANTS['DISCONNECTED_VPN_ICON']);
} catch (error) {
logError(error);
Util.VPN_NOTIFY(_(error.message), Util.CONSTANTS['NO_ROUTE_VPN_ICON']);
}
}

async _toggleMode() {
Expand All @@ -184,7 +180,7 @@ var SnxToggle = GObject.registerClass(
}

const cancellable = new Gio.Cancellable();
this.icon_name = Util.getConstantByKey('ACQUIRING_VPN_ICON');
this.icon_name = Util.CONSTANTS['ACQUIRING_VPN_ICON'];

if (this.checked) {
this._handleCheckedAction(cancellable);
Expand Down
98 changes: 74 additions & 24 deletions snx-vpn-indicator@diegodario88.github.io/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,21 @@ function parseSessionParameters(formattedString) {
}));
}

/**
* Returns the value of the constant based on the given key.
*
* @param {string} key - The key of the constant to look for.
* @returns {string} -The value of the constant corresponding to the key.
*/
function getConstantByKey(key) {
const constants = {
SNX_DEVICE_NAME: 'tunsnx',
SNX_LABEL: 'SNX VPN',
SNX_LABEL_EXTENDED: 'SSL Network Extender',
ENABLED_VPN_ICON: 'network-vpn-symbolic',
DISABLED_VPN_ICON: 'network-vpn-disabled-symbolic',
DISCONNECTED_VPN_ICON: 'network-vpn-disconnected-symbolic',
ACQUIRING_VPN_ICON: 'network-vpn-acquiring-symbolic',
NO_ROUTE_VPN_ICON: 'network-vpn-no-route-symbolic',
HOME_DIR: GLib.get_home_dir()
};
return constants[key];
}

/**
* @param {string} body
* @param {string} icon
*/
function vpnNotify(body, icon) {
function VPN_NOTIFY(body, icon) {
const source = new imports.ui.messageTray.Source(
_(getConstantByKey('SNX_LABEL')),
_(CONSTANTS['SNX_LABEL']),
icon
);

imports.ui.main.messageTray.add(source);

const notification = new imports.ui.messageTray.Notification(
source,
_(getConstantByKey('SNX_LABEL_EXTENDED')),
_(CONSTANTS['SNX_LABEL_EXTENDED']),
body
);

Expand All @@ -130,3 +109,74 @@ function vpnNotify(body, icon) {
function getGnomeShellVersion() {
return major;
}

var CONSTANTS = {
SNX_DEVICE_NAME: 'tunsnx',
SNX_LABEL: 'SNX VPN',
SNX_LABEL_EXTENDED: 'SSL Network Extender',
ENABLED_VPN_ICON: 'network-vpn-symbolic',
DISABLED_VPN_ICON: 'network-vpn-disabled-symbolic',
DISCONNECTED_VPN_ICON: 'network-vpn-disconnected-symbolic',
ACQUIRING_VPN_ICON: 'network-vpn-acquiring-symbolic',
NO_ROUTE_VPN_ICON: 'network-vpn-no-route-symbolic',
HOME_DIR: GLib.get_home_dir()
};

var NMDeviceStateReason = {
0: 'No reason given',
1: 'Unknown error',
2: 'Device is now managed',
3: 'Device is now unmanaged',
4: 'The device could not be readied for configuration',
5: 'IP configuration could not be reserved (no available address, timeout, etc)',
6: 'The IP config is no longer valid',
7: 'Secrets were required, but not provided',
8: '802.1x supplicant disconnected',
9: '802.1x supplicant configuration failed',
10: '802.1x supplicant failed',
11: '802.1x supplicant took too long to authenticate',
12: 'PPP service failed to start',
13: 'PPP service disconnected',
14: 'PPP failed',
15: 'DHCP client failed to start',
16: 'DHCP client error',
17: 'DHCP client failed',
18: 'Shared connection service failed to start',
19: 'Shared connection service failed',
20: 'AutoIP service failed to start',
21: 'AutoIP service error',
22: 'AutoIP service failed',
23: 'The line is busy',
24: 'No dial tone',
25: 'No carrier could be established',
26: 'The dialing request timed out',
27: 'The dialing attempt failed',
28: 'Modem initialization failed',
29: 'Failed to select the specified APN',
30: 'Not searching for networks',
31: 'Network registration denied',
32: 'Network registration timed out',
33: 'Failed to register with the requested network',
34: 'PIN check failed',
35: 'Necessary firmware for the device may be missing',
36: 'The device was removed',
37: 'NetworkManager went to sleep',
38: "The device's active connection disappeared",
39: 'Device disconnected by user or client',
40: 'Carrier/link changed',
41: "The device's existing connection was assumed",
42: 'The supplicant is now available',
43: 'The modem could not be found',
44: 'The Bluetooth connection failed or timed out',
45: "GSM Modem's SIM Card not inserted",
46: "GSM Modem's SIM Pin required",
47: "GSM Modem's SIM Puk required",
48: "GSM Modem's SIM wrong",
49: 'InfiniBand device does not support connected mode',
50: 'A dependency of the connection failed',
51: 'Problem with the RFC 2684 Ethernet over ADSL bridge',
52: 'ModemManager not running',
53: 'The WiFi network could not be found',
54: 'A secondary connection of the base connection failed',
55: 'DCB or FCoE configuration failed'
};

0 comments on commit 0b6a287

Please sign in to comment.