def __dns(vpn_opts: ClientOpts, nic: str, reason: str, new_nameservers: str, old_nameservers: str, debug: bool): logger.info(f'Discover DNS with {reason}::{nic}...') _reason = DHCPReason[reason] if not vpn_opts.is_vpn_nic(nic): logger.warn(f'NIC[{nic}] does not belong to VPN service') sys.exit(0) executor = VPNClientExecutor( vpn_opts, adhoc_task=True).require_install().probe(silent=True, log_lvl=logger.INFO) current = executor.storage.get_current(info=True) if not current: current = executor.storage.find(executor.opts.nic_to_account(nic)) if not current: logger.warn(f'Not found any VPN account') sys.exit(ErrorCode.VPN_ACCOUNT_NOT_FOUND) if executor.opts.nic_to_account(nic) != current.account: logger.warn(f'NIC[{nic}] does not meet current VPN account') sys.exit(ErrorCode.VPN_ACCOUNT_NOT_MATCH) if debug: now = datetime.now().isoformat() FileHelper.write_file( FileHelper.tmp_dir().joinpath('vpn_dns'), append=True, content= f"{now}::{reason}::{nic}::{new_nameservers}::{old_nameservers}\n") executor.device.dns_resolver.resolve(executor.vpn_service, _reason, current.hub, new_nameservers, old_nameservers)
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()
def backup_dir() -> Path: return FileHelper.tmp_dir(prefix=VpnDirectory.BACKUP_FOLDER_PREFIX, with_timestamp=True)