diff options
author | Kali Kaneko (leap communications) <kali@leap.se> | 2017-06-01 02:55:05 +0200 |
---|---|---|
committer | Kali Kaneko (leap communications) <kali@leap.se> | 2017-06-01 02:55:05 +0200 |
commit | 2db7202e6c3a520a5b6fe395dd74d40d8e53c481 (patch) | |
tree | abe64597254360a130692eaa65780990f43a12a5 /src/leap/bitmask/vpn | |
parent | 6f02e93d436f8c36f80453a042e406a041d24c4f (diff) |
[refactor] hide mac/linux switch inside process module
Diffstat (limited to 'src/leap/bitmask/vpn')
-rw-r--r-- | src/leap/bitmask/vpn/_control.py | 18 | ||||
-rw-r--r-- | src/leap/bitmask/vpn/process.py | 75 |
2 files changed, 48 insertions, 45 deletions
diff --git a/src/leap/bitmask/vpn/_control.py b/src/leap/bitmask/vpn/_control.py index 8211e62..74182a9 100644 --- a/src/leap/bitmask/vpn/_control.py +++ b/src/leap/bitmask/vpn/_control.py @@ -5,8 +5,8 @@ from twisted.internet.task import LoopingCall from twisted.internet import reactor from twisted.logger import Logger -from .process import VPNProcess, VPNCanary -from .constants import IS_LINUX, IS_MAC +from .process import VPNProcess +from .constants import IS_MAC log = Logger() @@ -57,16 +57,10 @@ class VPNControl(object): kwargs = {'openvpn_verb': 7, 'remotes': self._remotes, 'restartfun': self.restart} - if IS_LINUX: - vpnproc = VPNProcess(*args, **kwargs) - if vpnproc.get_openvpn_process(): - log.info('Another vpn process is running. Will try to stop it.') - vpnproc.stop_if_already_running() - elif IS_MAC: - # start the main vpn subprocess - vpnproc = VPNCanary(*args, **kwargs) - else: - raise Exception('This platform does not support VPN yet!') + vpnproc = VPNProcess(*args, **kwargs) + if vpnproc.get_openvpn_process(): + log.info('Another vpn process is running. Will try to stop it.') + vpnproc.stop_if_already_running() try: cmd = vpnproc.getCommand() diff --git a/src/leap/bitmask/vpn/process.py b/src/leap/bitmask/vpn/process.py index 429b7b7..895cd0a 100644 --- a/src/leap/bitmask/vpn/process.py +++ b/src/leap/bitmask/vpn/process.py @@ -33,13 +33,14 @@ from leap.bitmask.vpn import _status from leap.bitmask.vpn import _management from leap.bitmask.vpn.launchers import darwin +from leap.bitmask.vpn.constants import IS_MAC, IS_LINUX # OpenVPN verbosity level - from flags.py OPENVPN_VERBOSITY = 1 -class VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): +class _VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): """ A ProcessProtocol class that can be used to spawn a process that will @@ -233,41 +234,49 @@ class VPNProcess(protocol.ProcessProtocol, _management.VPNManagement): self.log.debug('Process Exited Already') -class VPNCanary(VPNProcess): +if IS_LINUX: - """ - Special form of the VPNProcess, for Darwin Launcher (windows might use same - strategy). - - This is a Canary Process that does not run openvpn itself, but it's - notified by the privileged process when the process dies. + VPNProcess = _VPNProcess - This is a workaround to allow the state machine to be notified when openvpn - process is spawned by the privileged helper. - """ - def setupHelper(self): - self.helper = darwin.HelperCommand() - - def connectionMade(self): - VPNProcess.connectionMade(self) - reactor.callLater(2, self.registerPID) +elif IS_MAC: - def registerPID(self): - cmd = 'openvpn_set_watcher %s' % self.pid - self.helper.send(cmd) + class _VPNCanary(_VPNProcess): - def killProcess(self): - cmd = 'openvpn_force_stop' - self.helper.send(cmd) + """ + Special form of _VPNProcess, for Darwin Launcher (windows might use + same strategy). - def getVPNCommand(self): - return VPNProcess.getCommand(self) + This is a Canary Process that does not run openvpn itself, but it's + notified by the privileged process when the process dies. - def getCommand(self): - canary = '''import sys, signal, time -def receive_signal(signum, stack): - sys.exit() -signal.signal(signal.SIGTERM, receive_signal) -while True: - time.sleep(60)''' - return ['python', '-c', '%s' % canary] + This is a workaround to allow the state machine to be notified when + openvpn process is spawned by the privileged helper. + """ + def setupHelper(self): + self.helper = darwin.HelperCommand() + + def connectionMade(self): + VPNProcess.connectionMade(self) + reactor.callLater(2, self.registerPID) + + def registerPID(self): + cmd = 'openvpn_set_watcher %s' % self.pid + self.helper.send(cmd) + + def killProcess(self): + cmd = 'openvpn_force_stop' + self.helper.send(cmd) + + def getVPNCommand(self): + return VPNProcess.getCommand(self) + + def getCommand(self): + canary = '''import sys, signal, time + def receive_signal(signum, stack): + sys.exit() + signal.signal(signal.SIGTERM, receive_signal) + while True: + time.sleep(60)''' + return ['python', '-c', '%s' % canary] + + VPNProcess = _VPNCanary |