File size: 4,497 Bytes
8ec52b8 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import gradio as gr
import subprocess
import os
import time
import threading
import socket
from pathlib import Path
class VPNManager:
def __init__(self):
self.vpn_process = None
self.status = "Stopped"
self.setup_complete = False
def setup_vpn(self):
"""Setup OpenVPN server configuration"""
try:
# Run setup script
result = subprocess.run(['/app/setup_vpn.sh'],
capture_output=True, text=True)
if result.returncode == 0:
self.setup_complete = True
return "VPN setup completed successfully"
else:
return f"Setup failed: {result.stderr}"
except Exception as e:
return f"Setup error: {str(e)}"
def start_vpn(self):
"""Start OpenVPN server"""
if not self.setup_complete:
return "Please complete setup first"
try:
# Start OpenVPN server
self.vpn_process = subprocess.Popen([
'openvpn',
'--config', '/etc/openvpn/server/server.conf',
'--daemon'
])
time.sleep(2)
if self.vpn_process.poll() is None:
self.status = "Running"
return "VPN server started successfully"
else:
self.status = "Failed"
return "Failed to start VPN server"
except Exception as e:
return f"Start error: {str(e)}"
def stop_vpn(self):
"""Stop OpenVPN server"""
try:
if self.vpn_process:
self.vpn_process.terminate()
self.vpn_process = None
# Kill any remaining openvpn processes
subprocess.run(['pkill', '-f', 'openvpn'],
capture_output=True)
self.status = "Stopped"
return "VPN server stopped"
except Exception as e:
return f"Stop error: {str(e)}"
def get_status(self):
"""Get current VPN status"""
return self.status
def get_client_config(self):
"""Generate client configuration"""
if not self.setup_complete:
return "Setup not complete"
try:
# Read the generated client config
config_path = "/etc/openvpn/server/client.ovpn"
if os.path.exists(config_path):
with open(config_path, 'r') as f:
return f.read()
else:
return "Client config not found"
except Exception as e:
return f"Config error: {str(e)}"
# Initialize VPN manager
vpn_manager = VPNManager()
def setup_interface():
return vpn_manager.setup_vpn()
def start_interface():
return vpn_manager.start_vpn()
def stop_interface():
return vpn_manager.stop_vpn()
def status_interface():
return vpn_manager.get_status()
def config_interface():
return vpn_manager.get_client_config()
# Create Gradio interface
with gr.Blocks(title="VPN Server - Hugging Face") as demo:
gr.Markdown("# VPN Server on Hugging Face")
gr.Markdown("**Port 7860 Access Only** - Manage your VPN server")
with gr.Row():
with gr.Column():
gr.Markdown("### Server Controls")
setup_btn = gr.Button("Setup VPN", variant="primary")
start_btn = gr.Button("Start VPN", variant="secondary")
stop_btn = gr.Button("Stop VPN", variant="stop")
status_btn = gr.Button("Check Status")
with gr.Column():
gr.Markdown("### Output")
output = gr.Textbox(label="Status", lines=3, interactive=False)
with gr.Row():
gr.Markdown("### Client Configuration")
config_btn = gr.Button("Get Client Config")
config_output = gr.Textbox(label="Client Config", lines=10, interactive=False)
# Connect functions to buttons
setup_btn.click(setup_interface, outputs=output)
start_btn.click(start_interface, outputs=output)
stop_btn.click(stop_interface, outputs=output)
status_btn.click(status_interface, outputs=output)
config_btn.click(config_interface, outputs=config_output)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |