quick-space-610 / app.py
Taf2023's picture
Update Gradio app with multiple files
1b9b049 verified
import gradio as gr
import threading
import socket
import time
import json
import requests
from datetime import datetime
from typing import Dict, List, Optional
import logging
from utils import SOCKS5ProxyServer, ProxyManager
from config import *
# Create proxy manager
proxy_manager = ProxyManager()
def start_proxy_server():
"""Start proxy server"""
success = proxy_manager.start_server()
if success:
status_html = f"""
<div class="status-indicator status-running">
<h4>๐ŸŸข Status: Running</h4>
<p>Proxy Server is running at {proxy_manager.proxy_server.host}:{proxy_manager.proxy_server.port}</p>
</div>
"""
return status_html, "โœ… Proxy Server started successfully"
else:
status_html = """
<div class="status-indicator status-stopped">
<h4>๐Ÿ”ด Status: Stopped</h4>
<p>Failed to start Proxy Server - check logs for details</p>
</div>
"""
return status_html, "โŒ Failed to start Proxy Server"
def stop_proxy_server():
"""Stop proxy server"""
proxy_manager.stop_server()
status_html = """
<div class="status-indicator status-stopped">
<h4>๐Ÿ”ด Status: Stopped</h4>
<p>Proxy Server stopped</p>
</div>
"""
return status_html, "๐Ÿ›‘ Proxy Server stopped"
def restart_proxy_server():
"""Restart proxy server"""
proxy_manager.stop_server()
time.sleep(1)
success = proxy_manager.start_server()
if success:
status_html = f"""
<div class="status-indicator status-running">
<h4>๐ŸŸข Status: Running</h4>
<p>Proxy Server restarted successfully at {proxy_manager.proxy_server.host}:{proxy_manager.proxy_server.port}</p>
</div>
"""
return status_html, "๐Ÿ”„ Proxy Server restarted successfully"
else:
status_html = """
<div class="status-indicator status-stopped">
<h4>๐Ÿ”ด Status: Stopped</h4>
<p>Failed to restart Proxy Server</p>
</div>
"""
return status_html, "โŒ Failed to restart Proxy Server"
def get_server_status():
"""Get current server status"""
if proxy_manager.is_running():
status_html = f"""
<div class="status-indicator status-running">
<h4>๐ŸŸข Status: Running</h4>
<p>Proxy Server is running at {proxy_manager.proxy_server.host}:{proxy_manager.proxy_server.port}</p>
</div>
"""
else:
status_html = """
<div class="status-indicator status-stopped">
<h4>๐Ÿ”ด Status: Stopped</h4>
<p>Proxy Server is not running</p>
</div>
"""
return status_html
def get_current_proxy_url():
"""Get current proxy URL"""
if proxy_manager.is_running():
return f"socks5://localhost:{PROXY_PORT}"
return "Not connected"
def get_server_info():
"""Get server information"""
return json.dumps(proxy_manager.get_server_info(), indent=2, ensure_ascii=False)
def get_usage_stats():
"""Get usage statistics"""
return json.dumps(proxy_manager.get_usage_stats(), indent=2, ensure_ascii=False)
def get_user_stats():
"""Get user statistics"""
return json.dumps(proxy_manager.get_user_stats(), indent=2, ensure_ascii=False)
def get_connection_logs():
"""Get connection logs"""
return json.dumps(proxy_manager.get_recent_logs(), indent=2, ensure_ascii=False)
def refresh_all():
"""Refresh all data"""
return (
get_current_proxy_url(),
get_server_status(),
get_server_info(),
get_usage_stats(),
get_user_stats(),
get_connection_logs()
)
def setup_interface():
"""Create Gradio interface"""
# Custom CSS for styling
css = """
.main-container {
max-width: 1200px;
margin: auto;
padding: 20px;
}
.status-indicator {
padding: 10px;
border-radius: 5px;
margin: 5px 0;
}
.status-running {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-stopped {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.info-box {
background-color: #e9ecef;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
border-left: 4px solid #007bff;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 20px 0;
}
.stat-card {
background: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
border: 1px solid #dee2e6;
}
.stat-number {
font-size: 24px;
font-weight: bold;
color: #007bff;
}
.stat-label {
color: #6c757d;
font-size: 14px;
}
"""
with gr.Blocks(css=css, title="SOCKS5 Proxy Dashboard", theme=gr.themes.Soft()) as demo:
gr.HTML("""
<div class="main-container">
<div style="text-align: center; margin-bottom: 30px;">
<h1>๐Ÿ”’ SOCKS5 Proxy Server Dashboard</h1>
<p style="color: #6c757d;">SOCKS5 Proxy Server Management System with Usage Monitoring</p>
<div style="margin-top: 10px;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #007bff; font-weight: bold;">Built with anycoder</a>
</div>
</div>
</div>
""")
# Current status
with gr.Row():
with gr.Column(scale=1):
gr.HTML('<h3>๐ŸŽฎ Controls</h3>')
with gr.Row():
start_btn = gr.Button("โ–ถ๏ธ Start", variant="primary", size="lg")
stop_btn = gr.Button("โน๏ธ Stop", variant="stop", size="lg")
restart_btn = gr.Button("๐Ÿ”„ Restart", variant="secondary", size="lg")
status_html = gr.HTML()
proxy_url_output = gr.Textbox(
label="๐ŸŒ Current Proxy URL",
value=get_current_proxy_url,
interactive=False,
elem_classes=["info-box"]
)
# Show server status
server_status_output = gr.HTML()
with gr.Column(scale=2):
gr.HTML('<h3>๐Ÿ“Š Usage Statistics</h3>')
with gr.Row():
total_connections = gr.Number(label="๐Ÿ“ˆ Total Connections", value=0)
active_connections = gr.Number(label="๐Ÿ”— Active Connections", value=0)
bandwidth_used = gr.Number(label="๐Ÿ“Š Bandwidth Used (MB)", value=0)
# User data
with gr.Tab("๐Ÿ‘ฅ Users"):
user_stats = gr.JSON(
label="All User Data",
value=get_user_stats
)
# Server data
with gr.Tab("๐Ÿ–ฅ๏ธ Server Info"):
server_info = gr.JSON(
label="Server Information",
value=get_server_info
)
# Usage statistics
with gr.Tab("๐Ÿ“Š Statistics"):
usage_stats = gr.JSON(
label="Usage Statistics",
value=get_usage_stats
)
# Logs
with gr.Tab("๐Ÿ“ Logs"):
logs = gr.JSON(
label="Recent Connection Logs",
value=get_connection_logs
)
# Refresh button
with gr.Row():
refresh_btn = gr.Button("๐Ÿ”„ Refresh All Data", variant="primary")
# Bind events
start_btn.click(start_proxy_server, outputs=[server_status_output, proxy_url_output])
stop_btn.click(stop_proxy_server, outputs=[server_status_output, proxy_url_output])
restart_btn.click(restart_proxy_server, outputs=[server_status_output, proxy_url_output])
refresh_btn.click(
refresh_all,
outputs=[
proxy_url_output,
server_status_output,
server_info,
usage_stats,
user_stats,
logs
]
)
# Auto update data
demo.load(
lambda: refresh_all(),
outputs=[
proxy_url_output,
server_status_output,
server_info,
usage_stats,
user_stats,
logs
]
)
return demo
if __name__ == "__main__":
# Create interface
demo = setup_interface()
# Try to start proxy server automatically and show result
print("Starting SOCKS5 Proxy Server...")
success = proxy_manager.start_server()
if success:
print(f"โœ… SOCKS5 Proxy Server started successfully at {proxy_manager.proxy_server.host}:{proxy_manager.proxy_server.port}")
else:
print("โŒ Failed to start SOCKS5 Proxy Server - please check logs")
# Start UI with appropriate settings
demo.launch(
server_name="0.0.0.0",
server_port=8080,
show_api=False,
share=False,
ssr_mode=False,
inbrowser=False,
debug=False
)