def agent_update(pk: int, codesigntoken: str = None, force: bool = False) -> str: from agents.utils import get_exegen_url agent = Agent.objects.get(pk=pk) if pyver.parse(agent.version) <= pyver.parse("1.3.0"): return "not supported" # skip if we can't determine the arch if agent.arch is None: logger.warning( f"Unable to determine arch on {agent.hostname}. Skipping agent update." ) return "noarch" version = settings.LATEST_AGENT_VER inno = agent.win_inno_exe if codesigntoken is not None and pyver.parse(version) >= pyver.parse( "1.5.0"): base_url = get_exegen_url() + "/api/v1/winagents/?" params = { "version": version, "arch": agent.arch, "token": codesigntoken } url = base_url + urllib.parse.urlencode(params) else: url = agent.winagent_dl if not force: if agent.pendingactions.filter(action_type="agentupdate", status="pending").exists(): agent.pendingactions.filter(action_type="agentupdate", status="pending").delete() PendingAction.objects.create( agent=agent, action_type="agentupdate", details={ "url": url, "version": version, "inno": inno, }, ) nats_data = { "func": "agentupdate", "payload": { "url": url, "version": version, "inno": inno, }, } asyncio.run(agent.nats_cmd(nats_data, wait=False)) return "created"
def generate_winagent_exe( client: int, site: int, agent_type: str, rdp: int, ping: int, power: int, arch: str, token: str, api: str, file_name: str, ) -> Union[Response, FileResponse]: from agents.utils import get_exegen_url inno = (f"winagent-v{settings.LATEST_AGENT_VER}.exe" if arch == "64" else f"winagent-v{settings.LATEST_AGENT_VER}-x86.exe") try: codetoken = CodeSignToken.objects.first().token base_url = get_exegen_url() + "/api/v1/winagents/?" params = { "version": settings.LATEST_AGENT_VER, "arch": arch, "token": codetoken, } dl_url = base_url + urllib.parse.urlencode(params) except: codetoken = "" dl_url = settings.DL_64 if arch == "64" else settings.DL_32 data = { "client": client, "site": site, "agenttype": agent_type, "rdp": str(rdp), "ping": str(ping), "power": str(power), "goarch": "amd64" if arch == "64" else "386", "token": token, "inno": inno, "url": dl_url, "api": api, "codesigntoken": codetoken, } headers = {"Content-type": "application/json"} errors = [] with tempfile.NamedTemporaryFile() as fp: for url in settings.EXE_GEN_URLS: try: r = requests.post( f"{url}/api/v1/exe", json=data, headers=headers, stream=True, timeout=900, ) except Exception as e: errors.append(str(e)) else: errors = [] break if errors: logger.error(errors) return notify_error( "Something went wrong. Check debug error log for exact error message" ) with open(fp.name, "wb") as f: for chunk in r.iter_content(chunk_size=1024): # type: ignore if chunk: f.write(chunk) del r return FileResponse(open(fp.name, "rb"), as_attachment=True, filename=file_name)