Skip to content

Commit

Permalink
Merge pull request #41 from Isopolito/BugFixes_v15_40-42
Browse files Browse the repository at this point in the history
Bug fixes v15 40 42
  • Loading branch information
Isopolito authored Dec 4, 2022
2 parents 2f11e8b + fb1bde7 commit f4d5090
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 64 deletions.
9 changes: 6 additions & 3 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const VpnIndicator = GObject.registerClass({
this.stateManager = new StateManager();

this.settings = ExtensionUtils.getSettings(`org.gnome.shell.extensions.gnordvpn-local`);
this.settings.connect('changed', (settings, key)=> {
this.settings.connect('changed', (settings, key) => {
switch(key) {
case 'panel-styles':
case 'common-panel-style':
Expand Down Expand Up @@ -87,7 +87,10 @@ const VpnIndicator = GObject.registerClass({

let status = this._vpn.getStatus();
status.loggedin = this.loggedin;
status.currentState = this.stateManager.resolveState(status);

status.currentState = this._vpn.isNordVpnRunning()
? this.stateManager.resolveState(status)
: this.stateManager.resolveState(null);

// Ensure that menus are populated. Since the menu may be created before the VPN is running and able
// to provide available cities, countries, etc
Expand All @@ -106,7 +109,6 @@ const VpnIndicator = GObject.registerClass({
}

_updateMenu(status) {

// Set the status text on the menu
this._statusLabel.text = status.connectStatus;
this._statusPopup.get_label_actor().set_text(status.connectStatus);
Expand Down Expand Up @@ -306,6 +308,7 @@ const VpnIndicator = GObject.registerClass({

this._vpn.applySettingsToNord();
this._buildIndicatorMenu();

this._refresh();
}

Expand Down
6 changes: 4 additions & 2 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"description": "A Gnome extension that shows the NordVPN status in the top bar and provides the ability to configure certain aspects of the connection.",
"name": "gNordVPN-Local",
"shell-version": [
"43"
"40",
"41",
"42"
],
"url": "https://github.com/Isopolito/gNordVPN-Local",
"uuid": "gnordvpn-local@isopolito",
"version": 13
"version": 15
}
15 changes: 7 additions & 8 deletions modules/CommonFavorite.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ var CommonFavorite = class CommonFavorite extends MenuBase {
this._signals = new Signals();
}

disable() {
this._isBuilt = false;
this._destroyMap = {};
this.favList = {};
this.itemList = {};
}

updateFavorite() {

let newFav = {};

Object.keys(Constants.favorites).forEach(key => {
Expand Down Expand Up @@ -182,4 +174,11 @@ var CommonFavorite = class CommonFavorite extends MenuBase {
this._favoriteMenu.show();
}
}

disable() {
this._isBuilt = false;
this._destroyMap = {};
this.favList = {};
this.itemList = {};
}
}
13 changes: 7 additions & 6 deletions modules/ConnectionMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Constants = Me.imports.modules.constants;
const Signals = Me.imports.modules.Signals.Signals;
const MenuBase = Me.imports.modules.MenuBase.MenuBase;
const Favorites = Me.imports.modules.Favorites.Favorites;
const Common = Me.imports.modules.common;

var ConnectionMenu = class ConnectionMenu extends MenuBase {
constructor(connectionLabel, connectionType, favoritesKey, connectionCallback) {
Expand All @@ -34,11 +35,11 @@ var ConnectionMenu = class ConnectionMenu extends MenuBase {
const connectionFavs = this._favorites.get(this._favoritesKey).favorites;

//connectionFavs-favConnectionItems
let toAddToFav = Object.keys(connectionFavs).filter(x => !this._favConnectionItems.includes(x));
let toAddToFav = Common.safeObjectKeys(connectionFavs).filter(x => !this._favConnectionItems.includes(x));
toAddToFav.forEach(connection => { this._toogleConnectionMenuItem(connection, !true); })

//_favConnectionItems-connectionFavs
let toRemoveFromFav = this._favConnectionItems.filter(x => !Object.keys(connectionFavs).includes(x))
let toRemoveFromFav = this._favConnectionItems.filter(x => !Common.safeObjectKeys(connectionFavs).includes(x))
toRemoveFromFav.forEach(connection => { this._toogleConnectionMenuItem(connection, !false); })

}
Expand Down Expand Up @@ -154,7 +155,7 @@ var ConnectionMenu = class ConnectionMenu extends MenuBase {
const connectionFavs = this._favorites.get(this._favoritesKey, this._connections);
this._connections = {...connectionFavs.favorites, ...connectionFavs.itemsMinusFavorites};

for (const connection of Object.keys(connectionFavs.favorites).sort()) {
for (const connection of Common.safeObjectKeys(connectionFavs.favorites).sort()) {
const menuItem = this._buildConnectionMenuItem(connection, true);
this._favConnectionItems.push(connection);
this._connectionMenu.menu.addMenuItem(menuItem);
Expand All @@ -164,13 +165,13 @@ var ConnectionMenu = class ConnectionMenu extends MenuBase {
this._connectionMenu.menu.addMenuItem(this._menuSeperator);


for (const connection of Object.keys(connectionFavs.itemsMinusFavorites).sort()) {
for (const connection of Common.safeObjectKeys(connectionFavs.itemsMinusFavorites).sort()) {
const menuItem = this._buildConnectionMenuItem(connection, false);
this._connectionMenuItems.push(connection);
this._connectionMenu.menu.addMenuItem(menuItem);
}

if (Object.keys(this._connections).length < 1) {
if (Common.safeObjectKeys(this._connections).length < 1) {
this._connectionMenu.hide();
} else {
this._connectionMenu.show();
Expand All @@ -180,7 +181,7 @@ var ConnectionMenu = class ConnectionMenu extends MenuBase {
}

showHide(show=true) {
if (Object.keys(this._connections).length < 1 || !show) {
if (Common.safeObjectKeys(this._connections).length < 1 || !show) {
this._connectionMenu.hide();
} else {
this._connectionMenu.show();
Expand Down
6 changes: 3 additions & 3 deletions modules/StateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ var StateManager = class StateManager {
}

resolveState(status) {
let vpnState = this.states[status.connectStatus] || this.states.ERROR;
if (!status.loggedin) vpnState = this.states['LOGGED OUT'];
let vpnState = (status && this.states[status.connectStatus]) || this.states.ERROR;
if (status && !status.loggedin) vpnState = this.states['LOGGED OUT'];

// If a state override is active, increment it and override the state if appropriate
if (this.stateOverride) {
if (status && this.stateOverride) {
this.stateOverrideCounter += 1;

let overrideFromKey = (this.stateOverride.overrideKeys && (
Expand Down
105 changes: 68 additions & 37 deletions modules/Vpn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Soup = imports.gi.Soup;

const CMD_VPNSTATUS = `nordvpn status`;
const CMD_VPNACCOUNT = `nordvpn account`;
const CMD_COUNTRIES = `nordvpn countries`;
Expand All @@ -20,10 +19,23 @@ var Vpn = class Vpn {
this.executeCommandSync = GLib.spawn_command_line_sync;
this.executeCommandAsync = GLib.spawn_command_line_async;
this.settings = ExtensionUtils.getSettings(`org.gnome.shell.extensions.gnordvpn-local`);

this.session = Soup.Session.new();
this.soupVersion = Soup.get_major_version();
}

_httpGet = (url) => {
const msg = Soup.Message.new("GET", url);
switch (this.soupVersion) {
case 2:
this.session.send_message(msg);
break;
default:
this.session.send(msg, null);
break;
}
return JSON.parse(this._getString(msg.response_body_data.get_data()));
}

// Remove the junk that shows up from messages in the nordvpn output
_processCityCountryOutput = (input) => {
const match = input.match(/(^\w+?,\s(\w+?,\s)+?(\w+?$)|^\s*?\w+?\s*?$)/gm);
Expand Down Expand Up @@ -68,11 +80,24 @@ var Vpn = class Vpn {
return null;
}

setSettingsFromNord() {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_FETCH_SETTINGS);
const normalizedOut = this._getString(standardOut);
_executeCommand(command) {
if (!this.isNordVpnRunning()) return "";
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(command);
return this._getString(standardOut);
}

isNordVpnRunning() {
try {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_VPNSTATUS);
return exitStatus === 0;
} catch {
return false;
}
}

for (const line of normalizedOut.split(`\n`)) {
setSettingsFromNord() {
const standardOut = this._executeCommand(CMD_FETCH_SETTINGS);
for (const line of standardOut.split(`\n`)) {
let parts = line.split(`:`);
const settingName = this._resolveSettingsKey(parts[0]);
const settingValue = this._resolveSettingsValue(parts[1]);
Expand All @@ -86,7 +111,10 @@ var Vpn = class Vpn {
}
}


applySettingsToNord() {
if (!this.isNordVpnRunning()) return;

this.executeCommandSync(`${CMD_SETTINGS} firewall ${this.settings.get_boolean(`firewall`)}`);
this.executeCommandSync(`${CMD_SETTINGS} autoconnect ${this.settings.get_boolean(`autoconnect`)}`);
this.executeCommandSync(`${CMD_SETTINGS} cybersec ${this.settings.get_boolean(`cybersec`)}`);
Expand All @@ -107,8 +135,8 @@ var Vpn = class Vpn {

getAccount() {
// Read the VPN status from the command line
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_VPNACCOUNT);
const allAccountMessages = this._getString(standardOut).split(`\n`);
const standardOut = this._executeCommand(CMD_VPNACCOUNT);
const allAccountMessages = standardOut.split(`\n`);

let emailAddress;
let vpnService;
Expand All @@ -121,21 +149,25 @@ var Vpn = class Vpn {
}

checkLogin() {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_LOGIN);
return this._getString(standardOut).replace(/\s+/g, ` `).includes('You are already logged in.');
const standardOut = this._executeCommand(CMD_LOGIN);
return standardOut.replace(/\s+/g, ` `).includes('You are already logged in.');
}


getStatus() {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_VPNSTATUS);
const standardOut = this._executeCommand(CMD_VPNSTATUS);
const allStatusMessages = standardOut.split(`\n`);

const allStatusMessages = this._getString(standardOut).split(`\n`);
let connectStatus, updateMessage, country, serverNumber, city, serverIp, currentTech, currentProtocol, transfer,
uptime, currentServer;

// NOTE that some of these message formats change across versions, old message formats are left in for backwards compatibility
for (const msg of allStatusMessages) {
if (msg.includes("Status:")) connectStatus = (msg.match(/Status: \w+/) || [``])[0];
else if (msg.includes("Country:")) country = msg.replace("Country: ", "").toUpperCase();
else if (msg.includes("City:")) city = msg.replace("City: ", "");
else if (msg.includes("Server IP:")) serverIp = msg.replace("Server IP: ", "");
else if (msg.includes("IP:")) serverIp = msg.replace("IP: ", "");
else if (msg.includes("Current protocol:")) currentProtocol = msg.replace("Current protocol: ", "");
else if (msg.includes("Current technology:")) currentTech = msg.replace("Current technology: ", "");
else if (msg.includes("Transfer:")) transfer = msg.replace("Transfer: ", "");
Expand All @@ -144,21 +176,24 @@ var Vpn = class Vpn {
else if (msg.includes("Current server:")) {
serverNumber = msg.match(/\d+/);
currentServer = msg.replace("Current server: ", "")
} else if (msg.includes("Hostname:")) {
serverNumber = msg.match(/\d+/);
currentServer = msg.replace("Hostname: ", "")
}
}

return {
connectStatus,
connectStatus: connectStatus || 'N/A',
updateMessage,
country,
city,
serverNumber,
currentServer,
serverIp,
currentTech,
currentProtocol,
transfer,
uptime
country: country || 'N/A',
city: city || 'N/A',
serverNumber: serverNumber || 'N/A',
currentServer: currentServer || 'N/A',
serverIp: serverIp || 'N/A',
currentTech: currentTech || 'N/A',
currentProtocol: currentProtocol || 'N/A',
transfer: transfer || 'N/A',
uptime: uptime || 'N/A'
}
}

Expand All @@ -175,10 +210,10 @@ var Vpn = class Vpn {
}

loginVpn() {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_LOGIN);
const standardOut = this._executeCommand(CMD_LOGIN);

const ref = "Continue in the browser: ";
let url = this._getString(standardOut).replace(/\s+/g, ` `);
let url = standardOut.replace(/\s+/g, ` `);
url = url.substring(url.indexOf(ref) + ref.length).trim();

Gio.app_info_launch_default_for_uri(url, null);
Expand All @@ -202,11 +237,9 @@ var Vpn = class Vpn {

getCountries(withId = false) {
if (withId) {
this.message = Soup.Message.new("GET", "https://api.nordvpn.com/v1/servers/countries");
this.session.send(this.message, null);
let countrieMap;
try {
let data = JSON.parse(this.message.response_body_data.get_data());
let data = this._httpGet("https://api.nordvpn.com/v1/servers/countries");
countrieMap = data.reduce((acc, v) => {
acc[v['name']] = v['id'];
return acc;
Expand All @@ -218,9 +251,9 @@ var Vpn = class Vpn {
return countrieMap;
}

const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(CMD_COUNTRIES);
const countries = this._processCityCountryOutput(this._getString(standardOut));
const standardOut = this._executeCommand(CMD_COUNTRIES);
const countries = this._processCityCountryOutput(standardOut);

let processedCountries = {};
for (let country of countries) {
processedCountries[country.replace(/_/g, " ")] = country;
Expand All @@ -240,9 +273,9 @@ var Vpn = class Vpn {
let processedCities = {};

for (let i = 0; i < citiesSaved.length; i++) {
const [ok, standardOut, standardError, exitStatus] = this.executeCommandSync(`${CMD_CITIES} ${citiesSaved[i]}`);
const cities = this._processCityCountryOutput(this._getString(standardOut));
const standardOut = this._executeCommand(`${CMD_CITIES} ${citiesSaved[i]}`);
const cities = this._processCityCountryOutput(standardOut);

for (let j = 0; j < cities.length; j++) {
if (j > citiesMax) break;
let c = (citiesSaved[i] + ", " + cities[j].replace(",", "")).replace(/_/g, " ");
Expand Down Expand Up @@ -284,10 +317,8 @@ var Vpn = class Vpn {
let servers = {}
try {
for (let i = 0; i < countriesSaved.length; i++) {
this.message = Soup.Message.new("GET", url + "&filters[country_id]=" + countriesSaved[i]);
this.session.send(this.message, null);
let data = this.message.response_body_data.get_data();
JSON.parse(this._getString(data)).forEach(e => {
let data = this._httpGet(url + "&filters[country_id]=" + countriesSaved[i]);
data.forEach(e => {
servers[e['name']] = e['hostname'].replace('.nordvpn.com', '');
});
}
Expand Down
3 changes: 3 additions & 0 deletions modules/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var safeObjectKeys = function(obj) {
return obj ? Object.keys(obj) : [];
}
Loading

0 comments on commit f4d5090

Please sign in to comment.