import base64, json, os, sys, urllib.parse, urllib.request, urllib.error def _cfg(): return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) def _base(): return str(_cfg().get("server_url") or "https://jbxcloud.joesecurity.org").rstrip("/") + "/api/v2" def post_form(path, fields): body = dict(fields) body["apikey"] = str(_cfg().get("api_key") or "") body["accept-tac"] = "1" data = urllib.parse.urlencode({k: str(v) for k, v in body.items() if v not in (None, "")}).encode("utf-8") req = urllib.request.Request(_base() + path, data=data, headers={"Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}, method="POST") with urllib.request.urlopen(req, timeout=90) as r: raw = r.read() return json.loads(raw) if raw else {} def main(): inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) web_id = inputs.get("web_id") if not web_id: raise Exception("web_id is required") report_type = inputs.get("report_type") or "json" fields = { "apikey": str(_cfg().get("api_key") or ""), "accept-tac": "1", "webid": web_id, "type": report_type, } data = urllib.parse.urlencode({k: str(v) for k, v in fields.items() if v not in (None, "")}).encode("utf-8") req = urllib.request.Request(_base() + "/analysis/download", data=data, headers={"Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}, method="POST") with urllib.request.urlopen(req, timeout=90) as r: content = r.read() print(json.dumps({ "web_id": web_id, "report_type": report_type, "content_base64": base64.b64encode(content).decode("ascii"), })) try: main() except urllib.error.HTTPError as e: print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) sys.exit(1) except Exception as e: print(json.dumps({"error": str(e)})) sys.exit(1)