File size: 5,607 Bytes
eca5751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Network Tools - DNS, ping, port check."""
from __future__ import annotations

import socket
import subprocess
from typing import Dict, Any

from .base import Tool, ToolResult, ToolContext, ToolCategory, ToolSafety


class DNSTool(Tool):
    """DNS lookup: A, AAAA, MX, NS, CNAME, TXT records."""
    category = ToolCategory.NETWORK
    safety = ToolSafety.SAFE
    
    @property
    def name(self) -> str:
        return "dns_lookup"
    
    @property
    def description(self) -> str:
        return "DNS lookup: resolve domain → IP (A/AAAA), MX, NS, CNAME, TXT records."
    
    @property
    def parameters(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "domain": {"type": "string"},
                "record_type": {
                    "type": "string",
                    "enum": ["A", "AAAA", "MX", "NS", "CNAME", "TXT", "SOA", "ANY"],
                    "default": "A",
                },
            },
            "required": ["domain"],
        }
    
    def execute(self, args: Dict[str, Any], context: ToolContext) -> ToolResult:
        domain = args["domain"]
        record_type = args.get("record_type", "A")
        
        try:
            if record_type == "A":
                results = socket.getaddrinfo(domain, None, socket.AF_INET)
                ips = list(set(r[4][0] for r in results))
                return ToolResult(
                    success=True,
                    output=f"A records for {domain}:\n" + "\n".join(f"  {ip}" for ip in ips),
                    metadata={"domain": domain, "records": ips},
                )
            elif record_type == "AAAA":
                results = socket.getaddrinfo(domain, None, socket.AF_INET6)
                ips = list(set(r[4][0] for r in results))
                return ToolResult(
                    success=True,
                    output=f"AAAA records for {domain}:\n" + "\n".join(f"  {ip}" for ip in ips),
                    metadata={"domain": domain, "records": ips},
                )
            else:
                # Use dig if available
                try:
                    result = subprocess.run(
                        ["dig", "+short", domain, record_type],
                        capture_output=True,
                        text=True,
                        timeout=10,
                        check=False,
                    )
                    if result.returncode == 0:
                        records = [r.strip() for r in result.stdout.splitlines() if r.strip()]
                        return ToolResult(
                            success=True,
                            output=f"{record_type} records for {domain}:\n" + "\n".join(f"  {r}" for r in records),
                            metadata={"domain": domain, "records": records},
                        )
                    else:
                        return ToolResult(
                            success=False,
                            error=f"dig failed: {result.stderr}",
                            return_code=1,
                        )
                except FileNotFoundError:
                    return ToolResult(
                        success=False,
                        error="dig not installed. Only A/AAAA supported via socket.",
                        return_code=1,
                    )
        except socket.gaierror as e:
            return ToolResult(success=False, error=f"DNS resolution failed: {e}", return_code=1)
        except Exception as e:
            return ToolResult(success=False, error=str(e), return_code=1)


class PingTool(Tool):
    """Ping host để check connectivity."""
    category = ToolCategory.NETWORK
    safety = ToolSafety.SAFE
    
    @property
    def name(self) -> str:
        return "ping"
    
    @property
    def description(self) -> str:
        return "Ping host để kiểm tra connectivity và đo latency."
    
    @property
    def parameters(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "host": {"type": "string"},
                "count": {"type": "integer", "default": 4},
                "timeout": {"type": "integer", "default": 5},
            },
            "required": ["host"],
        }
    
    def execute(self, args: Dict[str, Any], context: ToolContext) -> ToolResult:
        host = args["host"]
        count = args.get("count", 4)
        
        # Detect OS for ping flag
        import sys
        flag = "-n" if sys.platform == "win32" else "-c"
        wait_flag = "-w" if sys.platform == "win32" else "-W"
        
        try:
            result = subprocess.run(
                ["ping", flag, str(count), wait_flag, str(args.get("timeout", 5)), host],
                capture_output=True,
                text=True,
                timeout=context.timeout,
                check=False,
            )
            return ToolResult(
                success=(result.returncode == 0),
                output=result.stdout,
                error=result.stderr if result.stderr else None,
                return_code=result.returncode,
                metadata={"host": host, "count": count},
            )
        except FileNotFoundError:
            return ToolResult(success=False, error="ping command not found", return_code=1)
        except subprocess.TimeoutExpired:
            return ToolResult(success=False, error="Ping timed out", return_code=124)
        except Exception as e:
            return ToolResult(success=False, error=str(e), return_code=1)