Beispiel #1
0
def __import(server_opts: ServerOpts, hub_password: str, vpn_opts: ToolOpts,
             group: str, certs_file: str, output_opts: OutputOpts):
    executor = VPNAuthExecutor(vpn_opts, server_opts, hub_password)
    data = JsonHelper.read(certs_file, strict=False)
    tmp_dir = FileHelper.tmp_dir('vpn_auth')
    command_file = FileHelper.touch(tmp_dir.joinpath('vpncmd.txt'))
    vpn_acc = {}
    for k, v in data.items():
        cert_file = tmp_dir.joinpath(f'{k}.cert')
        FileHelper.write_file(cert_file, v['cert_key'])
        commands = [
            f'CAAdd /{cert_file}',
            f'UserCreate {k} /GROUP:{group or "none"} /RealName:none /Note:none',
            f'UserSignedSet {k} /CN:{v["fqdn"]} /SERIAL:{v["serial_number"]}'
        ]
        vpn_acc[k] = {
            'vpn_server': server_opts.host,
            'vpn_port': server_opts.port,
            'vpn_hub': server_opts.hub,
            'vpn_account': server_opts.hub,
            'vpn_auth_type': 'cert',
            'vpn_user': k,
            'vpn_cert_key': v['cert_key'],
            'vpn_private_key': v['private_key'],
        }
        FileHelper.write_file(command_file,
                              '\n'.join(commands) + '\n',
                              append=True)
    executor.exec_command(f'/IN:{command_file}', log_lvl=logger.INFO)
    logger.sep(logger.INFO)
    out = output_opts.make_file(
        f'{server_opts.hub}-{output_opts.to_file("json")}')
    logger.info(f'Export VPN accounts to {out}...')
    JsonHelper.dump(out, vpn_acc)
    logger.done()
Beispiel #2
0
 def create_config(self, vpn_service: str, auto_connman_dhcp: bool):
     if self.is_connman():
         FileHelper.write_file(self.connman_dhcp, str(auto_connman_dhcp))
         return
     if not FileHelper.is_readable(self.origin_resolv_cfg):
         logger.info(
             f'Backup System DNS config file to [{self.origin_resolv_cfg}]...'
         )
         FileHelper.backup(DNSResolver.DNS_SYSTEM_FILE,
                           self.origin_resolv_cfg,
                           remove=False)
     if not FileHelper.is_readable(self.origin_resolv_cfg):
         logger.error(
             f'Not found origin DNS config file [{self.origin_resolv_cfg}]')
         sys.exit(ErrorCode.FILE_CORRUPTED)
     if not FileHelper.is_readable(self.vpn_hook_cfg):
         FileHelper.touch(self.vpn_hook_cfg, 0o0644)
     self._resolver().setup(vpn_service, self.origin_resolv_cfg,
                            self.vpn_resolv_cfg, self.vpn_hook_cfg)
     self._resolver().restart(_all=True)