File size: 3,818 Bytes
93be2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from __future__ import annotations

import json
import socket
import time
from typing import Any, Dict, Optional

import requests

from .registry import ToolRegistry


def t_http_fetch(args: Dict[str, Any]) -> str:
    method = str(args.get("method", "GET")).upper()
    url = args.get("url")
    headers = args.get("headers") or {}
    params = args.get("params") or None
    data = args.get("data") or None
    timeout = int(args.get("timeout", 30))
    if not url:
        return json.dumps({"error": "url required"})
    try:
        r = requests.request(method, url, headers=headers, params=params, data=data, timeout=timeout)
        ct = r.headers.get("content-type", "")
        body: Optional[str]
        if "application/json" in ct:
            try:
                body = json.dumps(r.json())
            except Exception:
                body = r.text
        else:
            body = r.text[:100000]
        return json.dumps({
            "status": r.status_code,
            "headers": dict(r.headers),
            "body": body,
        })
    except Exception as e:
        return json.dumps({"error": str(e)})


def register_tools(reg: ToolRegistry) -> None:
    reg.register(
        name="http_fetch",
        description="HTTP(S) request with method, url, headers, params, data.",
        parameters={
            "type": "object",
            "properties": {
                "method": {"type": "string"},
                "url": {"type": "string"},
                "headers": {"type": "object"},
                "params": {"type": "object"},
                "data": {"type": "string"},
                "timeout": {"type": "integer"},
            },
            "required": ["url"],
        },
        handler=t_http_fetch,
    )
    reg.register(
        name="dns_lookup",
        description="Resolve a hostname to IPs (A/AAAA).",
        parameters={"type": "object", "properties": {"host": {"type": "string"}}, "required": ["host"]},
        handler=lambda a: json.dumps({"host": a.get("host"), "addrs": list({f"{ai[4][0]}" for ai in socket.getaddrinfo(a.get("host"), None)})})
    )
    reg.register(
        name="tcp_probe",
        description="Attempt a TCP connection to host:port with timeout (ms).",
        parameters={"type": "object", "properties": {"host": {"type": "string"}, "port": {"type": "integer"}, "timeout_ms": {"type": "integer"}}, "required": ["host", "port"]},
        handler=lambda a: (lambda h, p, t: (lambda s: (s.settimeout(t), (s.connect_ex((h, p)) or 0)))(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) and json.dumps({"host": h, "port": p, "status": "open"}) if (lambda s: s.settimeout(t) or s.connect_ex((h, p)))(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) == 0 else json.dumps({"host": h, "port": p, "status": "closed"}))(a.get("host"), int(a.get("port")), (int(a.get("timeout_ms", 2000))/1000.0))
    )
    def _http_benchmark(args: Dict[str, Any]) -> str:
        url = args.get("url")
        n = int(args.get("requests", 20))
        if not url:
            return json.dumps({"error": "url required"})
        ok = 0
        start = time.time()
        for _ in range(n):
            try:
                r = requests.get(url, timeout=10)
                if r.status_code < 500:
                    ok += 1
            except Exception:
                pass
        dur = time.time() - start
        rps = ok / dur if dur > 0 else 0.0
        return json.dumps({"url": url, "ok": ok, "total": n, "duration_sec": dur, "req_per_sec": rps})
    reg.register(
        name="http_benchmark",
        description="Simple HTTP GET benchmark (requests/sec over N requests).",
        parameters={"type": "object", "properties": {"url": {"type": "string"}, "requests": {"type": "integer"}}, "required": ["url"]},
        handler=_http_benchmark,
    )