From 0de190d81dccc00a709c15378b74f08e4b33eb9b Mon Sep 17 00:00:00 2001 From: vfosterm Date: Sun, 9 Jun 2019 11:45:36 +0200 Subject: [PATCH] fixed numerous bugs relating to the sudo dialog and Network manager scripts --- nord_nm_gui.py | 61 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/nord_nm_gui.py b/nord_nm_gui.py index f26caff..e4aae74 100644 --- a/nord_nm_gui.py +++ b/nord_nm_gui.py @@ -577,8 +577,7 @@ def randomize_mac(self): try: self.statusbar.showMessage("Randomizing MAC Address", 2000) self.repaint() - output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,UUID', 'connection', 'show', '--active'], - stdout=subprocess.PIPE) + output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,UUID', 'connection', 'show', '--active'], stdout=subprocess.PIPE) output.check_returncode() lines = output.stdout.decode('utf-8').split('\n') @@ -644,10 +643,14 @@ def get_sudo(self): resolution = QtWidgets.QDesktopWidget().screenGeometry() sudo_dialog.move(int((resolution.width() / 2) - (sudo_dialog.frameSize().width() / 2)), int((resolution.height() / 2) - (sudo_dialog.frameSize().height() / 2))) sudo_dialog.accept_box.accepted.connect(self.check_sudo) - sudo_dialog.accept_box.rejected.connect(sudo_dialog.close) + sudo_dialog.accept_box.rejected.connect(self.close_sudo_dialog) QtCore.QMetaObject.connectSlotsByName(sudo_dialog) return sudo_dialog + def close_sudo_dialog(self): # added to clear sudo password when cancel is pressed + self.sudo_password = None + self.sudo_dialog.close() + def check_sudo(self): self.sudo_password = self.sudo_dialog.sudo_password.text() try: @@ -731,6 +734,8 @@ def disable_auto_connect(self): self.sudo_dialog = self.get_sudo() self.sudo_dialog.text_label.setText("

VPN Network Manager requires sudo permissions in order to remove the auto-connect script from the Network Manager directory. Please input the sudo Password or run the program with elevated priveledges.

") self.sudo_dialog.exec_() + if not self.sudo_password: # dialog was canceled + return False try: p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'auto_connect'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -759,7 +764,11 @@ def disable_auto_connect(self): elif self.auto_connect_box.isChecked() and self.get_active_vpn() and not self.sudo_password: self.sudo_dialog = self.get_sudo() self.sudo_dialog.exec_() - self.set_auto_connect() + if self.sudo_password: + self.set_auto_connect() + else: + self.auto_connect_box.setChecked(False) + return False def set_kill_switch(self): @@ -794,6 +803,8 @@ def set_kill_switch(self): p6 = subprocess.Popen(['sudo', '-S', 'chmod', '744', self.network_manager_path + 'kill_switch'], stdin=p5.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p5.stdout.close() p6.stdout.close() + self.config['SETTINGS']['kill_switch'] = 'True' + self.write_conf() self.statusbar.showMessage('Kill switch activated', 2000) self.repaint() @@ -805,6 +816,10 @@ def disable_kill_switch(self): self.sudo_dialog = self.get_sudo() self.sudo_dialog.text_label.setText("

VPN Network Manager requires sudo permissions in order to remove the kill switch script from the Network Manager directory. Please input the sudo Password or run the program with elevated priveledges.

") self.sudo_dialog.exec_() + + if not self.sudo_password: # dialog was canceled + self.killswitch_btn.setChecked(False) + return False try: p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -835,6 +850,19 @@ def disable_kill_switch(self): self.statusbar.showMessage('ERROR disabling kill switch', 2000) self.repaint() + elif self.killswitch_btn.isChecked() and self.get_active_vpn() and self.sudo_password: + self.set_kill_switch() + + elif self.killswitch_btn.isChecked() and self.get_active_vpn() and not self.sudo_password: + self.sudo_dialog = self.get_sudo() + self.sudo_dialog.text_label.setText("

VPN Network Manager requires sudo permissions in order to move the kill switch script to the Network Manager directory. Please input the sudo Password or run the program with elevated priveledges.

") + self.sudo_dialog.exec_() + if self.sudo_password: + self.set_kill_switch() + else: + self.killswitch_btn.setChecked(False) + return False + def check_connection_validity(self): if self.server_type_select.currentText() == 'Double VPN': # perhaps add pop up to give user the choice self.connection_type_select.setCurrentIndex(1) # set to TCP @@ -876,8 +904,12 @@ def connect(self): if self.auto_connect_box.isChecked() and not self.sudo_password: # prompt for sudo password self.sudo_dialog = self.get_sudo() self.sudo_dialog.exec_() - self.set_auto_connect() + if self.sudo_password: #valid password exists + self.set_auto_connect() + else: + self.auto_connect_box.setChecked(False) + return False elif self.auto_connect_box.isChecked() and self.sudo_password: # sudo password exists in memory self.set_auto_connect() @@ -893,13 +925,13 @@ def connect(self): self.sudo_dialog = self.get_sudo() self.sudo_dialog.text_label.setText("

VPN Network Manager requires sudo permissions in order to move the kill switch script to the Network Manager directory. Please input the sudo Password or run the program with elevated priveledges.

") self.sudo_dialog.exec_() + if not self.sudo_password: # dialog was closed + self.killswitch_btn.setChecked(False) + return False self.set_kill_switch() - self.config['SETTINGS']['kill_switch'] = 'True' - self.write_conf() + elif self.killswitch_btn.isChecked() and self.sudo_password: self.set_kill_switch() - self.config['SETTINGS']['kill_switch'] = 'True' - self.write_conf() if self.get_active_vpn(): # if connection successful self.connect_btn.hide() @@ -908,10 +940,17 @@ def connect(self): def disconnect_vpn(self): if self.killswitch_btn.isChecked(): + self.killswitch_btn.setChecked(False) + self.statusbar.showMessage("Disabling Killswitch...", 5000) + self.repaint() self.disable_kill_switch() + time.sleep(5) + if self.auto_connect_box.isChecked(): + self.auto_connect_box.setChecked(False) + self.statusbar.showMessage("Disabling auto-connect...", 1000) + self.disable_auto_connect() self.disable_connection() - if not self.auto_connect_box.isChecked(): - self.remove_connection() + self.remove_connection() self.statusbar.clearMessage() self.repaint()