{
  "openapi": "3.0.3",
  "info": {
    "title": "PortCheck.PRO REST API",
    "description": "API hiệu năng cao phục vụ kiểm tra cổng mạng trực tuyến (TCP 3-way handshake & UDP Datagram probes), nhận diện IP máy khách, chẩn đoán Nmap chuyên sâu và kiểm tra thông tin mạng. Hệ thống tích hợp sẵn cơ chế bảo mật chống tấn công SSRF, lọc dải mạng riêng tư RFC 1918 / Cloud Metadata và kiểm soát tần suất Rate Limiting.",
    "version": "1.0.0",
    "contact": {
      "name": "PortCheck.PRO Support",
      "url": "https://portcheck.trong.pro"
    },
    "license": {
      "name": "Điều Khoản Dịch Vụ / Terms of Service",
      "url": "https://portcheck.trong.pro"
    }
  },
  "servers": [
    {
      "url": "https://portcheck.trong.pro",
      "description": "Máy chủ chính thức (Production Server)"
    },
    {
      "url": "/",
      "description": "Máy chủ hiện tại (Current Host)"
    }
  ],
  "tags": [
    {
      "name": "Port Scanner",
      "description": "Các điểm cuối kiểm tra trạng thái cổng TCP & UDP, quét dải cổng và chẩn đoán Nmap"
    },
    {
      "name": "Network & Telemetry",
      "description": "Nhận diện địa chỉ IP máy khách và thông tin kết nối mạng"
    }
  ],
  "paths": {
    "/api/check.php": {
      "post": {
        "tags": [
          "Port Scanner"
        ],
        "summary": "Kiểm tra cổng mạng (TCP / UDP) & Chẩn đoán Nmap",
        "description": "Thực hiện kiểm tra trạng thái cổng mạng mục tiêu bằng giao thức TCP (bắt tay 3 bước đầy đủ) hoặc UDP (thăm dò gói tin Datagram). Hỗ trợ quét đơn cổng, danh sách nhiều cổng (tối đa 100 cổng) và quét dải cổng. Tích hợp tùy chọn chẩn đoán chuyên sâu Nmap cho cổng đơn.",
        "operationId": "checkPort",
        "requestBody": {
          "required": true,
          "description": "Tham số mục tiêu, cổng và giao thức kiểm tra",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PortScanRequest"
              },
              "examples": {
                "singlePortTcp": {
                  "summary": "1. Kiểm tra đơn cổng TCP (Cổng 53 DNS)",
                  "value": {
                    "target": "1.1.1.1",
                    "mode": "single",
                    "protocol": "TCP",
                    "port": 53,
                    "run_nmap": false
                  }
                },
                "multiPortList": {
                  "summary": "2. Kiểm tra danh sách nhiều cổng (80, 443, 22, 53)",
                  "value": {
                    "target": "1.1.1.1",
                    "mode": "list",
                    "protocol": "TCP",
                    "ports": "80, 443, 22, 53"
                  }
                },
                "portRange": {
                  "summary": "3. Kiểm tra dải cổng (Từ cổng 80 đến 85)",
                  "value": {
                    "target": "1.1.1.1",
                    "mode": "range",
                    "protocol": "TCP",
                    "start_port": 80,
                    "end_port": 85
                  }
                },
                "singlePortUdp": {
                  "summary": "4. Kiểm tra đơn cổng UDP (Cổng 53 DNS)",
                  "value": {
                    "target": "1.1.1.1",
                    "mode": "single",
                    "protocol": "UDP",
                    "port": 53
                  }
                },
                "nmapDiagnostic": {
                  "summary": "5. Kiểm tra cổng kèm chẩn đoán Nmap",
                  "value": {
                    "target": "1.1.1.1",
                    "mode": "single",
                    "protocol": "TCP",
                    "port": 53,
                    "run_nmap": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Quét cổng thành công. Trả về chi tiết trạng thái từng cổng và báo cáo Nmap (nếu có).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PortScanResponse"
                }
              }
            }
          },
          "400": {
            "description": "Lỗi dữ liệu đầu vào (Địa chỉ IP/tên miền không hợp lệ, cổng bị cấm, hoặc vi phạm chính sách chống SSRF).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "429": {
            "description": "Vượt quá giới hạn tần suất yêu cầu (Rate Limit) hoặc số lượng quét đồng thời tối đa.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RateLimitResponse"
                }
              }
            }
          }
        }
      }
    },
    "/api/ip.php": {
      "get": {
        "tags": [
          "Network & Telemetry"
        ],
        "summary": "Lấy địa chỉ IP máy khách",
        "description": "Trả về địa chỉ IP công khai thực tế của thiết bị gửi yêu cầu (hỗ trợ đọc qua proxy tin cậy, Cloudflare CF-Connecting-IP, X-Forwarded-For).",
        "operationId": "getClientIp",
        "parameters": [
          {
            "name": "format",
            "in": "query",
            "required": false,
            "description": "Định dạng dữ liệu trả về: json (mặc định) hoặc text",
            "schema": {
              "type": "string",
              "enum": [
                "json",
                "text"
              ],
              "default": "json"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Trả về IP thành công",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ClientIpResponse"
                },
                "example": {
                  "ip": "118.69.141.62",
                  "timestamp": 1793030247
                }
              },
              "text/plain": {
                "schema": {
                  "type": "string",
                  "example": "118.69.141.62"
                }
              }
            }
          }
        }
      }
    },
    "/api/client-info.php": {
      "get": {
        "tags": [
          "Network & Telemetry"
        ],
        "summary": "Thông tin mạng máy khách & Tra cứu PTR (Reverse DNS)",
        "description": "Trả về địa chỉ IP, tên miền phân giải ngược (PTR Reverse DNS), User-Agent và giao thức kết nối.",
        "operationId": "getClientInfo",
        "responses": {
          "200": {
            "description": "Thông tin mạng máy khách",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ClientInfoResponse"
                },
                "example": {
                  "ip": "118.69.141.62",
                  "reverse_dns": null,
                  "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
                  "protocol": "HTTP/1.1",
                  "timestamp": 1793030261
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "PortScanRequest": {
        "type": "object",
        "required": [
          "target"
        ],
        "properties": {
          "target": {
            "type": "string",
            "description": "Địa chỉ IPv4, IPv6 hoặc tên miền (Hostname) cần kiểm tra. Hệ thống cấm tuyệt đối IP nội bộ RFC 1918, Loopback, Link-local, và Metadata nhà cung cấp Cloud.",
            "example": "1.1.1.1"
          },
          "mode": {
            "type": "string",
            "enum": [
              "single",
              "list",
              "range"
            ],
            "default": "single",
            "description": "Chế độ kiểm tra: single (đơn cổng), list (danh sách cổng), range (dải cổng)"
          },
          "protocol": {
            "type": "string",
            "enum": [
              "TCP",
              "UDP"
            ],
            "default": "TCP",
            "description": "Giao thức kiểm tra"
          },
          "port": {
            "type": "integer",
            "minimum": 1,
            "maximum": 65535,
            "description": "Số hiệu cổng cần kiểm tra (Dùng cho chế độ single)",
            "example": 53
          },
          "ports": {
            "type": "string",
            "description": "Danh sách các cổng phân cách bằng dấu phẩy, tối đa 100 cổng (Dùng cho chế độ list)",
            "example": "80, 443, 22, 53"
          },
          "start_port": {
            "type": "integer",
            "minimum": 1,
            "maximum": 65535,
            "description": "Cổng bắt đầu (Dùng cho chế độ range)",
            "example": 80
          },
          "end_port": {
            "type": "integer",
            "minimum": 1,
            "maximum": 65535,
            "description": "Cổng kết thúc (Dùng cho chế độ range, khoảng cách tối đa 100 cổng)",
            "example": 85
          },
          "run_nmap": {
            "type": "boolean",
            "default": false,
            "description": "Kích hoạt chẩn đoán bổ sung bằng Nmap (Áp dụng cho chế độ single)"
          }
        }
      },
      "PortResultItem": {
        "type": "object",
        "properties": {
          "port": {
            "type": "integer",
            "example": 53
          },
          "service": {
            "type": "string",
            "example": "DNS"
          },
          "protocol": {
            "type": "string",
            "enum": [
              "TCP",
              "UDP"
            ],
            "example": "TCP"
          },
          "status": {
            "type": "string",
            "enum": [
              "OPEN",
              "CLOSED",
              "FILTERED",
              "BLOCKED"
            ],
            "example": "OPEN"
          },
          "latency_ms": {
            "type": "integer",
            "nullable": true,
            "example": 2,
            "description": "Độ trễ bắt tay mạng (milliseconds), null nếu cổng bị đóng hoặc lọc"
          },
          "reason_code": {
            "type": "string",
            "example": "connection_succeeded"
          },
          "reason_vi": {
            "type": "string",
            "example": "Bắt tay 3 bước TCP thành công (SYN-ACK). Cổng đang mở."
          },
          "reason_en": {
            "type": "string",
            "example": "TCP 3-way handshake succeeded (SYN-ACK). Port is open."
          }
        }
      },
      "NmapReport": {
        "type": "object",
        "nullable": true,
        "properties": {
          "state": {
            "type": "string",
            "example": "OPEN"
          },
          "reason": {
            "type": "string",
            "example": "syn-ack"
          },
          "service": {
            "type": "string",
            "nullable": true,
            "example": "domain"
          },
          "summary_vi": {
            "type": "string",
            "example": "Báo cáo Nmap: Cổng 53/TCP đang MỞ (syn-ack) [domain] trên máy chủ 1.1.1.1."
          },
          "summary_en": {
            "type": "string",
            "example": "Nmap report: Port 53/TCP is OPEN (syn-ack) [domain] on host 1.1.1.1."
          },
          "raw": {
            "type": "string"
          }
        }
      },
      "PortScanResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": true
          },
          "target": {
            "type": "string",
            "example": "1.1.1.1"
          },
          "resolved_ip": {
            "type": "string",
            "example": "1.1.1.1"
          },
          "is_ipv6": {
            "type": "boolean",
            "example": false
          },
          "mode": {
            "type": "string",
            "example": "single"
          },
          "protocol": {
            "type": "string",
            "example": "TCP"
          },
          "count": {
            "type": "integer",
            "example": 1
          },
          "results": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/PortResultItem"
            }
          },
          "nmap": {
            "$ref": "#/components/schemas/NmapReport"
          },
          "timestamp": {
            "type": "integer",
            "example": 1793030283
          }
        }
      },
      "ClientIpResponse": {
        "type": "object",
        "properties": {
          "ip": {
            "type": "string",
            "example": "118.69.141.62"
          },
          "timestamp": {
            "type": "integer",
            "example": 1793030247
          }
        }
      },
      "ClientInfoResponse": {
        "type": "object",
        "properties": {
          "ip": {
            "type": "string",
            "example": "118.69.141.62"
          },
          "reverse_dns": {
            "type": "string",
            "nullable": true,
            "example": null
          },
          "user_agent": {
            "type": "string",
            "example": "Mozilla/5.0"
          },
          "protocol": {
            "type": "string",
            "example": "HTTP/1.1"
          },
          "timestamp": {
            "type": "integer",
            "example": 1793030261
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": false
          },
          "error_vi": {
            "type": "string",
            "example": "Mục tiêu thuộc danh sách cấm hoặc dải mạng nội bộ (127.0.0.0/8)."
          },
          "error_en": {
            "type": "string",
            "example": "Target is in the restricted blocklist or internal network (127.0.0.0/8)."
          }
        }
      },
      "RateLimitResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": false
          },
          "error_vi": {
            "type": "string",
            "example": "Bạn đã vượt quá giới hạn 15 yêu cầu / phút. Vui lòng chờ 45 giây trước khi quét tiếp."
          },
          "error_en": {
            "type": "string",
            "example": "Rate limit exceeded (15 requests/minute). Please wait 45 seconds before retry."
          },
          "retry_after": {
            "type": "integer",
            "example": 45
          }
        }
      }
    }
  }
}
