File size: 1,299 Bytes
760be27
 
 
 
 
 
 
 
 
a72da93
760be27
 
 
 
 
3cd8763
 
 
760be27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import uuid
import time
import logging
import socket
import ipaddress
from urllib.parse import urlparse
import gradio as gr


logger = logging.getLogger(__name__)
# Helper Functions
def format_error(message):
    """Formats error messages for consistent user feedback."""
    return {"error": message}

# def toggle_visibility(show, *components):
#     """Toggles visibility for multiple Gradio components."""
#     return [gr.update(visible=show) for _ in components]

def generate_session_id():
    """Generates a unique session ID for tracking inputs."""
    return str(uuid.uuid4())

def log_runtime(start_time):
    """Logs the runtime of a process."""
    elapsed_time = time.time() - start_time
    logger.info(f"Process completed in {elapsed_time:.2f} seconds.")
    return elapsed_time

def is_public_ip(url):
    """
    Checks whether the resolved IP address of a URL is public (non-local).
    Prevents SSRF by blocking internal addresses like 127.0.0.1 or 192.168.x.x.
    """
    try:
        hostname = urlparse(url).hostname
        ip = socket.gethostbyname(hostname)
        ip_obj = ipaddress.ip_address(ip)
        return ip_obj.is_global  # Only allow globally routable IPs
    except Exception as e:
        logger.warning(f"URL IP validation failed: {e}")
        return False