From 3fbc512a49923ac73d2413a083e0bb1f7e163866 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 4 Sep 2012 05:20:31 +0900 Subject: actual split of classes into own modules still a bit rough, but makes everything a bit more readable. --- src/leap/baseapp/eip.py | 175 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/leap/baseapp/eip.py (limited to 'src/leap/baseapp/eip.py') diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py new file mode 100644 index 00000000..e8b9fe53 --- /dev/null +++ b/src/leap/baseapp/eip.py @@ -0,0 +1,175 @@ +import time + +from PyQt4 import QtCore + +from leap.baseapp.dialogs import ErrorDialog +from leap.eip import exceptions as eip_exceptions +from leap.eip.eipconnection import EIPConnection + + +class EIPConductorApp(object): + + def __init__(self, *args, **kwargs): + # + # conductor is in charge of all + # vpn-related configuration / monitoring. + # we pass a tuple of signals that will be + # triggered when status changes. + # + opts = kwargs.pop('opts') + config_file = getattr(opts, 'config_file', None) + + self.conductor = EIPConnection( + watcher_cb=self.newLogLine.emit, + config_file=config_file, + status_signals=(self.statusChange.emit, ), + debug=self.debugmode) + + # XXX remove skip download when sample service is ready + self.conductor.run_checks(skip_download=True) + self.error_check() + if self.conductor.autostart: + self.start_or_stopVPN() + + def error_check(self): + ####### error checking ################ + # + # bunch of self checks. + # XXX move somewhere else alltogether. + # + if self.conductor.missing_definition is True: + dialog = ErrorDialog() + dialog.criticalMessage( + 'The default ' + 'definition.json file cannot be found', + 'error') + + if self.conductor.missing_provider is True: + dialog = ErrorDialog() + dialog.criticalMessage( + 'Missing provider. Add a remote_ip entry ' + 'under section [provider] in eip.cfg', + 'error') + + if self.conductor.missing_vpn_keyfile is True: + dialog = ErrorDialog() + dialog.criticalMessage( + 'Could not find the vpn keys file', + 'error') + + # ... btw, review pending. + # os.kill of subprocess fails if we have + # some of this errors. + + if self.conductor.bad_provider is True: + dialog = ErrorDialog() + dialog.criticalMessage( + 'Bad provider entry. Check that remote_ip entry ' + 'has an IP under section [provider] in eip.cfg', + 'error') + + if self.conductor.bad_keyfile_perms is True: + dialog = ErrorDialog() + dialog.criticalMessage( + 'The vpn keys file has bad permissions', + 'error') + + if self.conductor.missing_auth_agent is True: + dialog = ErrorDialog() + dialog.warningMessage( + 'We could not find any authentication ' + 'agent in your system.
' + 'Make sure you have ' + 'polkit-gnome-authentication-agent-1 ' + 'running and try again.', + 'error') + + if self.conductor.missing_pkexec is True: + dialog = ErrorDialog() + dialog.warningMessage( + 'We could not find pkexec in your ' + 'system.
Do you want to try ' + 'setuid workaround? ' + '(DOES NOTHING YET)', + 'error') + + @QtCore.pyqtSlot() + def statusUpdate(self): + """ + called on timer tick + polls status and updates ui with real time + info about transferred bytes / connection state. + """ + # XXX it's too expensive to poll + # continously. move to signal events instead. + + if not self.eip_service_started: + return + + # XXX remove all access to manager layer + # from here. + if self.conductor.with_errors: + #XXX how to wait on pkexec??? + #something better that this workaround, plz!! + time.sleep(5) + print('errors. disconnect.') + self.start_or_stopVPN() # is stop + + state = self.conductor.poll_connection_state() + if not state: + return + + ts, con_status, ok, ip, remote = state + self.set_statusbarMessage(con_status) + self.setIconToolTip() + + ts = time.strftime("%a %b %d %X", ts) + if self.debugmode: + self.updateTS.setText(ts) + self.status_label.setText(con_status) + self.ip_label.setText(ip) + self.remote_label.setText(remote) + + # status i/o + + status = self.conductor.get_status_io() + if status and self.debugmode: + #XXX move this to systray menu indicators + ts, (tun_read, tun_write, tcp_read, tcp_write, auth_read) = status + ts = time.strftime("%a %b %d %X", ts) + self.updateTS.setText(ts) + self.tun_read_bytes.setText(tun_read) + self.tun_write_bytes.setText(tun_write) + + @QtCore.pyqtSlot() + def start_or_stopVPN(self): + """ + stub for running child process with vpn + """ + if self.eip_service_started is False: + try: + self.conductor.connect() + # XXX move this to error queue + except eip_exceptions.EIPNoCommandError: + dialog = ErrorDialog() + dialog.warningMessage( + 'No suitable openvpn command found. ' + '
(Might be a permissions problem)', + 'error') + if self.debugmode: + self.startStopButton.setText('&Disconnect') + self.eip_service_started = True + + # XXX what is optimum polling interval? + # too little is overkill, too much + # will miss transition states.. + + self.timer.start(250.0) + return + if self.eip_service_started is True: + self.conductor.disconnect() + if self.debugmode: + self.startStopButton.setText('&Connect') + self.eip_service_started = False + self.timer.stop() + return -- cgit v1.2.3 From b0b2b342b698bbe5851e9312cd830938f8d564a5 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 4 Sep 2012 06:01:50 +0900 Subject: further cleaning of main window by moving init functions to their base classes. plus a bit of juggling with order. --- src/leap/baseapp/eip.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/leap/baseapp/eip.py') diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py index e8b9fe53..a67fd916 100644 --- a/src/leap/baseapp/eip.py +++ b/src/leap/baseapp/eip.py @@ -19,6 +19,8 @@ class EIPConductorApp(object): opts = kwargs.pop('opts') config_file = getattr(opts, 'config_file', None) + self.eip_service_started = False + self.conductor = EIPConnection( watcher_cb=self.newLogLine.emit, config_file=config_file, @@ -28,8 +30,15 @@ class EIPConductorApp(object): # XXX remove skip download when sample service is ready self.conductor.run_checks(skip_download=True) self.error_check() - if self.conductor.autostart: - self.start_or_stopVPN() + + # XXX should receive "ready" signal + #if self.conductor.autostart: + #self.start_or_stopVPN() + + # move to eipconductor init? + if self.debugmode: + self.startStopButton.clicked.connect( + lambda: self.start_or_stopVPN()) def error_check(self): ####### error checking ################ -- cgit v1.2.3 From 1826d9a0d5400c21a3f7af73eda2e843f0639271 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 4 Sep 2012 06:37:45 +0900 Subject: add little docstrings to classes --- src/leap/baseapp/eip.py | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'src/leap/baseapp/eip.py') diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py index a67fd916..6c3249ff 100644 --- a/src/leap/baseapp/eip.py +++ b/src/leap/baseapp/eip.py @@ -8,19 +8,25 @@ from leap.eip.eipconnection import EIPConnection class EIPConductorApp(object): + """ + initializes an instance of EIPConnection, + gathers errors, and passes status-change signals + from Qt land along to the conductor. + Connects the eip connect/disconnect logic + to the switches in the app (buttons/menu items). + """ def __init__(self, *args, **kwargs): - # - # conductor is in charge of all - # vpn-related configuration / monitoring. - # we pass a tuple of signals that will be - # triggered when status changes. - # opts = kwargs.pop('opts') config_file = getattr(opts, 'config_file', None) self.eip_service_started = False + # conductor (eip connection) is in charge of all + # vpn-related configuration / monitoring. + # we pass a tuple of signals that will be + # triggered when status changes. + self.conductor = EIPConnection( watcher_cb=self.newLogLine.emit, config_file=config_file, @@ -32,20 +38,18 @@ class EIPConductorApp(object): self.error_check() # XXX should receive "ready" signal + # it is called from LeapWindow now. #if self.conductor.autostart: #self.start_or_stopVPN() - # move to eipconductor init? if self.debugmode: self.startStopButton.clicked.connect( lambda: self.start_or_stopVPN()) def error_check(self): - ####### error checking ################ - # - # bunch of self checks. - # XXX move somewhere else alltogether. - # + + # XXX refactor (by #504) + if self.conductor.missing_definition is True: dialog = ErrorDialog() dialog.criticalMessage( @@ -105,22 +109,23 @@ class EIPConductorApp(object): @QtCore.pyqtSlot() def statusUpdate(self): """ - called on timer tick polls status and updates ui with real time info about transferred bytes / connection state. + right now is triggered by a timer tick + (timer controlled by StatusAwareTrayIcon class) """ - # XXX it's too expensive to poll + # TODO I guess it's too expensive to poll # continously. move to signal events instead. + # (i.e., subscribe to connection status changes + # from openvpn manager) if not self.eip_service_started: return - # XXX remove all access to manager layer - # from here. if self.conductor.with_errors: #XXX how to wait on pkexec??? #something better that this workaround, plz!! - time.sleep(5) + time.sleep(2) print('errors. disconnect.') self.start_or_stopVPN() # is stop @@ -173,8 +178,14 @@ class EIPConductorApp(object): # too little is overkill, too much # will miss transition states.. + # XXX decouple! (timer is init by icons class). + # should bring it here? + # to its own class? + + # XXX get constant from somewhere else self.timer.start(250.0) return + if self.eip_service_started is True: self.conductor.disconnect() if self.debugmode: -- cgit v1.2.3 From 3b752fcfac7a18891e2f948acae0cb4781678647 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 4 Sep 2012 07:11:01 +0900 Subject: put timer constant instead of hardcoded value --- src/leap/baseapp/eip.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/leap/baseapp/eip.py') diff --git a/src/leap/baseapp/eip.py b/src/leap/baseapp/eip.py index 6c3249ff..029ce0ba 100644 --- a/src/leap/baseapp/eip.py +++ b/src/leap/baseapp/eip.py @@ -3,6 +3,7 @@ import time from PyQt4 import QtCore from leap.baseapp.dialogs import ErrorDialog +from leap.baseapp import constants from leap.eip import exceptions as eip_exceptions from leap.eip.eipconnection import EIPConnection @@ -182,8 +183,7 @@ class EIPConductorApp(object): # should bring it here? # to its own class? - # XXX get constant from somewhere else - self.timer.start(250.0) + self.timer.start(constants.TIMER_MILLISECONDS) return if self.eip_service_started is True: -- cgit v1.2.3