navpan2 commited on
Commit
8ec52b8
·
verified ·
1 Parent(s): 87133e7

Create app.py

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