APIDIAGS - Web API Diagnostics Tool (Linux Edition)
---------------------------------------------------

Purpose:
- Automates troubleshooting checks for API endpoints.
- Performs DNS checks, connectivity tests, port scans, SSL validation, HTTP status and headers checks, latency measurement, authentication tests, and CORS verification.


Developed by: CISCONET Training Solutions
Website: www.cisconetsolutions.com


Modules / Functions Overview:

1. print_banner()
   - Displays a banner with the tool name, purpose, and summary of tasks.
   - Provides instructions and credits.

2. verify_sudo_session()
   - Checks if the script is running with sudo privileges.
   - Prompts for sudo password if required and exits if sudo is not available.

3. check_linux_tools()
   - Verifies that required Linux tools are installed (e.g., curl, traceroute).
   - Exits if any required tool is missing.

4. run_cmd(command)
   - Runs a shell command using subprocess.
   - Captures output and errors, filters unwanted lines.
   - Returns command output and a success flag.

5. dns_lookup(hostname)
   - Performs DNS resolution for the given hostname.
   - Returns resolved IP addresses or an error.

6. curl_ping_check(endpoint)
   - Verifies that the API endpoint is reachable using curl.
   - Returns success or failure status.

7. check_open_port(hostname, port)
   - Checks if a TCP port on the server is open.
   - Returns open/closed status.

8. check_ssl(domain)
   - Connects to the server on port 443 using SSL.
   - Retrieves SSL certificate details (valid from/to dates, remaining days).
   - Detects TLS version and reports errors if the certificate is invalid.

9. check_status_code(endpoint)
   - Retrieves the HTTP status code from the API endpoint.
   - Maps code to a human-readable message (e.g., 200 OK, 404 Not Found).
   - Flags warnings or errors for client/server issues.

10. check_headers(endpoint)
    - Retrieves HTTP response headers using curl.
    - Prints all headers for troubleshooting.

11. measure_latency(endpoint)
    - Measures API response latency using curl’s total time.
    - Returns latency in seconds.

12. check_authentication(endpoint, auth_data)
    - Validates authentication using Bearer token, API key, Basic Auth, or none.
    - Supports API key in header or query parameter.
    - Returns authentication status and a snippet of the response body.

13. check_cors(endpoint, origin)
    - Checks HTTP response headers for CORS settings.
    - Reports Access-Control-Allow-* headers and notes wildcard origins.

14. generate_report_section(title, content, as_html=False)
    - Formats output as plain text or HTML for the report.
    - Ensures readability in reports.

15. run_and_print(title, func, *args, as_html=False)
    - Calls a diagnostic function, prints the result, and formats it for inclusion in the report.

16. main()
    - Orchestrates the tool execution:
        • Prints banner
        • Checks sudo and required Linux tools
        • Prompts for endpoint, authentication, and CORS origin
        • Executes all diagnostics
        • Generates a consolidated report (HTML or plain text)
        • Handles errors and invalid input gracefully

Usage Notes:
- Sudo privileges are required for some checks.
- Input full URLs starting with http:// or https://
- The script can generate HTML report saved in the working directory.



