#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""端口扫描Skill"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from skills.base_skill import BaseSkill
from network_tools import run_port_scan


class PortScanSkill(BaseSkill):
    """端口扫描Skill"""
    
    def get_metadata(self):
        return {
            "name": "port_scan",
            "display_name": "端口扫描",
            "description": "扫描目标主机的开放端口",
            "category": "network",
            "keywords": ["端口", "扫描", "port", "scan", "开放"],
            "parameters": [
                {
                    "name": "host",
                    "type": "string",
                    "required": True,
                    "description": "目标主机IP或域名"
                },
                {
                    "name": "ports",
                    "type": "string",
                    "required": False,
                    "default": "22,80,443,3389,8080",
                    "description": "端口列表,支持:单个(80)、范围(1-1024)、常用端口、全端口确认"
                },
                {
                    "name": "timeout",
                    "type": "float",
                    "required": False,
                    "default": 1.0,
                    "description": "超时时间(秒)"
                },
                {
                    "name": "threads",
                    "type": "int",
                    "required": False,
                    "default": 100,
                    "description": "并发线程数"
                }
            ],
            "examples": [
                "扫描192.168.1.1的端口",
                "端口扫描baidu.com",
                "扫描80,443,3389端口",
                "扫描192.168.1.1的1-1024端口"
            ],
            "requires_admin": False,
            "requires_ui": False
        }
    
    def execute(self, host: str, ports: str = "22,80,443,3389,8080", 
                timeout: float = 1.0, threads: int = 100) -> str:
        """执行端口扫描"""
        return run_port_scan(host, ports, timeout, threads)
