Exemplo n.º 1
0
 def kill_by_process(process_name: str, silent=True, log_lvl=logger.DEBUG):
     pid = SystemHelper.exec_command(
         f"ps aux | grep -e '{process_name}' | awk '{{print $2}}'",
         shell=True,
         silent=silent,
         log_lvl=logger.down_lvl(log_lvl))
     if pid:
         SystemHelper.kill_by_pid(pid.split('\n'),
                                  silent=silent,
                                  log_lvl=logger.down_lvl(log_lvl))
Exemplo n.º 2
0
 def pre_exec(self, silent=False, log_lvl=logger.DEBUG, **kwargs):
     logger.log(log_lvl, 'Start VPN Client if not yet running...')
     if not self.is_installed(silent, log_lvl):
         return
     if self.pid_handler.is_running():
         self._prev_is_run = True
         return
     SystemHelper.exec_command(f'{self.opts.vpnclient} start',
                               log_lvl=logger.down_lvl(log_lvl))
     time.sleep(1)
     if not self.pid_handler.is_running(log_lvl=logger.down_lvl(log_lvl)):
         logger.error('Unable start VPN Client')
         sys.exit(ErrorCode.VPN_START_FAILED)
Exemplo n.º 3
0
 def _cleanup_zombie_vpn(self, delay=1, log_lvl=logger.DEBUG):
     time.sleep(delay)
     logger.log(log_lvl, 'Cleanup the VPN zombie processes...')
     SystemHelper.kill_by_process(f'{self.vpn_dir}/vpnclient execsvc',
                                  silent=True,
                                  log_lvl=logger.down_lvl(log_lvl))
     self.device.ip_resolver.cleanup_zombie(f'vpn_')
Exemplo n.º 4
0
 def is_running(self, log_lvl=logger.DEBUG) -> bool:
     logger.log(log_lvl, 'Check if VPN is running...')
     pid = self._find_pid(logger.down_lvl(log_lvl))
     if pid:
         logger.log(log_lvl, f'VPN PID [{pid}]')
         return True
     self.cleanup()
     return False
Exemplo n.º 5
0
 def verify_command(command: str,
                    shell=False,
                    log_lvl=logger.DEBUG) -> bool:
     try:
         SystemHelper.exec_command(command,
                                   shell=shell,
                                   silent=True,
                                   log_lvl=logger.down_lvl(log_lvl))
         return True
     except:
         return False
Exemplo n.º 6
0
 def lease_ip(self,
              vpn_acc: str,
              vpn_nic: str,
              daemon=False,
              is_execute=True):
     logger.log(self.log_lvl, 'Lease a new VPN IP...')
     command = f'{self.ip_tool} {self._lease_ip_opt(vpn_acc, vpn_nic, daemon)}'
     if is_execute:
         SystemHelper.exec_command(command,
                                   silent=self.silent,
                                   log_lvl=logger.down_lvl(self.log_lvl))
     return command
Exemplo n.º 7
0
 def do_disconnect_current(self,
                           must_disable_service=False,
                           log_lvl: int = logger.INFO,
                           silent: bool = True):
     account = self.storage.get_current()
     if not account:
         logger.log(logger.down_lvl(log_lvl), 'Not found any VPN account')
         return
     self.do_disconnect([account],
                        must_disable_service=must_disable_service,
                        log_lvl=log_lvl,
                        silent=silent)
Exemplo n.º 8
0
 def change_host_name(hostname: str, log_lvl=logger.DEBUG):
     prev_regex = re.escape('127.0.1.1') + r'\s+' + re.escape(
         socket.gethostname()) + r'.*'
     SystemHelper.exec_command(f'hostnamectl set-hostname {hostname}',
                               log_lvl=logger.down_lvl(log_lvl))
     FileHelper.replace_in_file('/etc/hosts',
                                {prev_regex: f'127.0.1.1    {hostname}'},
                                regex=True)
     SystemHelper.exec_command(f'hostnamectl', silent=True, log_lvl=log_lvl)
     logger.sep(level=log_lvl, quantity=20)
     SystemHelper.exec_command(f'cat /etc/hosts',
                               silent=True,
                               log_lvl=log_lvl)
     logger.sep(level=log_lvl)
Exemplo n.º 9
0
 def post_exec(self, silent=False, log_lvl=logger.DEBUG, **kwargs):
     logger.log(log_lvl, 'Stop VPN Client if applicable...')
     if not self.is_installed(True, log_lvl):
         return
     if (self._prev_is_run or
             not self.adhoc_task) and not kwargs.get('_force_stop', False):
         return
     lvl = logger.down_lvl(log_lvl)
     if self.pid_handler.is_running(log_lvl=lvl):
         SystemHelper.exec_command(f'{self.opts.vpnclient} stop',
                                   silent=silent,
                                   log_lvl=lvl)
         self._cleanup_zombie_vpn(1, log_lvl=lvl)
         self.pid_handler.cleanup()
Exemplo n.º 10
0
 def do_disconnect(self,
                   accounts: Sequence[str],
                   must_disable_service=False,
                   log_lvl: int = logger.INFO,
                   silent: bool = True):
     cur_acc = self.storage.get_current()
     is_stop = cur_acc is None or cur_acc == ''
     for acc in accounts:
         logger.log(log_lvl, f'Disconnect VPN account [{acc}]...')
         self.exec_command('AccountDisconnect',
                           params=acc,
                           log_lvl=logger.down_lvl(log_lvl),
                           silent=silent)
         self.device.ip_resolver.release_ip(acc,
                                            self.opts.account_to_nic(acc))
         self.device.ip_resolver.cleanup_zombie(
             f'--no-pid.* {self.opts.account_to_nic(acc)}')
         is_stop = acc == cur_acc or is_stop
     if is_stop:
         self.shutdown_vpn_service(is_stop=True,
                                   is_disable=must_disable_service,
                                   log_lvl=log_lvl)
Exemplo n.º 11
0
 def renew_all_ip(self, delay=1, silent=False):
     logger.log(self.log_lvl, 'Refresh all IPs...')
     time.sleep(delay)
     SystemHelper.exec_command(f'{self._refresh_all_ip_opt()}',
                               silent=silent or self.silent,
                               log_lvl=logger.down_lvl(self.log_lvl))
Exemplo n.º 12
0
 def release_ip(self, vpn_acc: str, vpn_nic: str):
     logger.log(self.log_lvl, 'Release the current VPN IP...')
     SystemHelper.exec_command(
         f'{self.ip_tool} {self._release_ip_opt(vpn_acc, vpn_nic)}',
         silent=self.silent,
         log_lvl=logger.down_lvl(self.log_lvl))
Exemplo n.º 13
0
 def do_force_stop(self, log_lvl=logger.INFO):
     if self.is_installed(silent=True):
         self.storage.set_current('')
     self.post_exec(log_lvl=logger.down_lvl(log_lvl), _force_stop=True)
     self.device.dns_resolver.restart()
Exemplo n.º 14
0
 def do_force_start(self, log_lvl=logger.INFO):
     vpn_acc = self.storage.get_default()
     if vpn_acc:
         self.storage.set_current(vpn_acc)
         self.pre_exec(log_lvl=logger.down_lvl(log_lvl))
         self.lease_vpn_ip(vpn_acc, log_lvl=log_lvl)