File size: 2,404 Bytes
fbf3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
id: http
title: HTTP Client (unsafe)
author: admin
description: Make HTTP requests (GET/POST) with headers and body.
version: 0.1.0
license: Proprietary
"""

import subprocess
import json

ARTIFACTS_DIR = "/data/adaptai/artifacts"


class Tools:
    def get(self, url: str, headers_json: str = "{}") -> dict:
        try:
            headers = json.loads(headers_json)
            hflags = " ".join([f"-H '{k}: {v}'" for k, v in headers.items()])
            cmd = f"curl -sSL -D - {hflags} '{url}'"
            p = subprocess.run(
                ["bash", "-lc", cmd], capture_output=True, text=True, timeout=60
            )
            import os
            import time

            os.makedirs(ARTIFACTS_DIR, exist_ok=True)
            out = {
                "stdout": p.stdout,
                "stderr": p.stderr,
                "exit_code": p.returncode,
                "cmd": cmd,
            }
            if len(out.get("stdout", "")) > 4000:
                fn = f"{ARTIFACTS_DIR}/http_{int(time.time())}.log"
                open(fn, "w", encoding="utf-8").write(out["stdout"])
                out["stdout_file"] = fn
                out["stdout"] = out["stdout"][:4000] + "... [saved]"
            return out
        except Exception as e:
            return {"error": str(e)}

    def post(self, url: str, body: str = "", headers_json: str = "{}") -> dict:
        try:
            headers = json.loads(headers_json)
            hflags = " ".join([f"-H '{k}: {v}'" for k, v in headers.items()])
            cmd = f"curl -sSL -D - -X POST {hflags} --data '{body.replace("'", "'''")}' '{url}'"
            p = subprocess.run(
                ["bash", "-lc", cmd], capture_output=True, text=True, timeout=60
            )
            import os
            import time

            os.makedirs(ARTIFACTS_DIR, exist_ok=True)
            out = {
                "stdout": p.stdout,
                "stderr": p.stderr,
                "exit_code": p.returncode,
                "cmd": cmd,
            }
            if len(out.get("stdout", "")) > 4000:
                fn = f"{ARTIFACTS_DIR}/http_{int(time.time())}.log"
                open(fn, "w", encoding="utf-8").write(out["stdout"])
                out["stdout_file"] = fn
                out["stdout"] = out["stdout"][:4000] + "... [saved]"
            return out
        except Exception as e:
            return {"error": str(e)}