likhonsheikh commited on
Commit
3041f43
·
1 Parent(s): 17388ed

🔧 Fix: Replace TUN-dependent VPN with config manager

Browse files

- Fixed TUN device permission errors
- Created container-compatible version
- Added configuration generation interface
- Included deployment scripts and guides

Files changed (4) hide show
  1. Dockerfile +22 -14
  2. README.md +59 -8
  3. app.py +315 -0
  4. requirements.txt +3 -0
Dockerfile CHANGED
@@ -1,19 +1,27 @@
1
- # Base image from OpenVPN-AS
2
- FROM openvpn/openvpn-as:latest
3
 
4
- # Expose required ports (note: Hugging Face only allows HTTP/HTTPS)
5
- EXPOSE 943 443 1194/udp
 
 
 
6
 
7
- # Optional: create directory for data
8
- VOLUME ["/openvpn"]
9
 
10
- # Hugging Face Spaces use a non-root user by default — need root for tun
11
- USER root
 
12
 
13
- # Enable TUN interface setup (will fail silently if not supported)
14
- RUN mkdir -p /dev/net && \
15
- (mknod /dev/net/tun c 10 200 || true) && \
16
- chmod 600 /dev/net/tun || true
17
 
18
- # Default command to run OpenVPN Access Server
19
- CMD ["/usr/local/openvpn_as/scripts/openvpnas", "run"]
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
 
2
 
3
+ # Install system dependencies
4
+ RUN apt-get update && apt-get install -y \
5
+ bash \
6
+ curl \
7
+ && rm -rf /var/lib/apt/lists/*
8
 
9
+ # Create app directory
10
+ WORKDIR /app
11
 
12
+ # Copy requirements and install Python dependencies
13
+ COPY requirements.txt .
14
+ RUN pip install --no-cache-dir -r requirements.txt
15
 
16
+ # Copy application code
17
+ COPY app.py .
 
 
18
 
19
+ # Expose the port
20
+ EXPOSE 7860
21
+
22
+ # Set environment variables for Gradio
23
+ ENV GRADIO_SERVER_NAME=0.0.0.0
24
+ ENV GRADIO_SERVER_PORT=7860
25
+
26
+ # Run the application
27
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,10 +1,61 @@
1
- ---
2
- title: OpenVPN
3
- emoji: 👁
4
- colorFrom: gray
5
- colorTo: red
6
- sdk: docker
7
- pinned: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # OpenVPN Configuration Manager
2
+
3
+ ## Overview
4
+
5
+ This is a **fixed version** of the OpenVPN Hugging Face Space that works around container limitations while providing valuable OpenVPN configuration management.
6
+
7
+ ## Key Features
8
+
9
+ ✅ **Container-Compatible** - No TUN device requirements
10
+ ✅ **Configuration Generation** - Create server and client configs
11
+ ✅ **Certificate Scripts** - Generate SSL certificates
12
+ ✅ **Firewall Rules** - Automatic iptables configuration
13
+ ✅ **Complete Package** - Download all files as ZIP
14
+ ✅ **Deployment Guide** - Step-by-step instructions
15
+
16
+ ## What This Fixes
17
+
18
+ The original space failed with:
19
+ ```
20
+ mknod: /dev/net/tun: Operation not permitted
21
+ ```
22
+
23
+ This version **avoids the TUN device issue** by providing:
24
+ - Configuration file generation instead of direct VPN operation
25
+ - Scripts for certificate creation (run on proper servers)
26
+ - Deployment guides for production environments
27
+
28
+ ## Usage
29
+
30
+ 1. **Generate Configurations** - Create OpenVPN config files
31
+ 2. **Security Setup** - Get certificate and firewall scripts
32
+ 3. **Download Package** - Get all files in one ZIP
33
+ 4. **Follow Guide** - Deploy on proper server infrastructure
34
+
35
+ ## Production Deployment
36
+
37
+ For actual VPN functionality, deploy the generated configurations on:
38
+ - Linux servers with TUN support
39
+ - Proper network configuration
40
+ - Root/sudo access
41
+ - CAP_NET_ADMIN capability
42
+
43
+ ## Technical Details
44
+
45
+ - **Python 3.10** based
46
+ - **Gradio** web interface
47
+ - **No container privileges** required
48
+ - **Zero TUN dependencies**
49
+ - **Configuration-focused** approach
50
+
51
+ ## Files Generated
52
+
53
+ - `server.conf` - Server configuration
54
+ - `client.conf` - Client configuration
55
+ - `generate_certs.sh` - Certificate generation
56
+ - `firewall_rules.sh` - Firewall setup
57
+ - `DEPLOYMENT_GUIDE.md` - Complete instructions
58
+
59
  ---
60
 
61
+ **Note**: This tool generates configurations for deployment on proper VPN servers. The actual VPN functionality requires infrastructure with appropriate network capabilities.
app.py ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ OpenVPN Configuration Manager for Hugging Face Spaces
4
+ Fixed version that works around container limitations
5
+ """
6
+
7
+ import os
8
+ import json
9
+ import subprocess
10
+ import tempfile
11
+ import shutil
12
+ from pathlib import Path
13
+ import gradio as gr
14
+ import zipfile
15
+ import io
16
+ from datetime import datetime
17
+
18
+ class OpenVPNManager:
19
+ def __init__(self):
20
+ self.config_dir = Path("/tmp/openvpn_configs")
21
+ self.config_dir.mkdir(exist_ok=True)
22
+
23
+ def create_sample_config(self, config_type="server"):
24
+ """Create a sample OpenVPN configuration"""
25
+ if config_type == "server":
26
+ return self._create_server_config()
27
+ else:
28
+ return self._create_client_config()
29
+
30
+ def _create_server_config(self):
31
+ """Create a sample server configuration"""
32
+ server_config = """# OpenVPN Server Configuration
33
+ # Fixed for containerized environments
34
+
35
+ port 1194
36
+ proto udp
37
+ dev tun
38
+ ca ca.crt
39
+ cert server.crt
40
+ key server.key
41
+ dh dh.pem
42
+ server 10.8.0.0 255.255.255.0
43
+ ifconfig-pool-persist ipp.txt
44
+ keepalive 10 120
45
+ comp-lzo
46
+ persist-key
47
+ persist-tun
48
+ status openvpn-status.log
49
+ verb 3
50
+ mute 20
51
+ """
52
+ return server_config
53
+
54
+ def _create_client_config(self):
55
+ """Create a sample client configuration"""
56
+ client_config = """# OpenVPN Client Configuration
57
+ # Fixed for containerized environments
58
+
59
+ client
60
+ dev tun
61
+ proto udp
62
+ remote your-server.com 1194
63
+ resolv-retry infinite
64
+ nobind
65
+ persist-key
66
+ persist-tun
67
+ ca ca.crt
68
+ cert client.crt
69
+ key client.key
70
+ ns-cert-type server
71
+ comp-lzo
72
+ verb 3
73
+ mute 20
74
+ """
75
+ return client_config
76
+
77
+ def create_certificate_scripts(self):
78
+ """Create easy-rsa scripts for certificate generation"""
79
+ easy_rsa_script = """#!/bin/bash
80
+ # Easy-RSA Certificate Generation Script
81
+ # Fixed for Hugging Face Spaces
82
+
83
+ echo "=== OpenVPN Certificate Generator ==="
84
+ echo "This script generates certificates for OpenVPN setup"
85
+ echo
86
+
87
+ # Install easy-rsa if not present
88
+ if ! command -v easyrsa &> /dev/null; then
89
+ echo "Installing Easy-RSA..."
90
+ apt-get update && apt-get install -y easy-rsa
91
+ fi
92
+
93
+ # Create PKI directory structure
94
+ echo "Setting up PKI structure..."
95
+ mkdir -p /tmp/openvpn_pki/{ca,server,client}
96
+ cd /tmp/openvpn_pki
97
+
98
+ # Initialize PKI
99
+ echo "Initializing PKI..."
100
+ easyrsa init-pki
101
+
102
+ # Generate CA
103
+ echo "Generating CA certificate..."
104
+ echo "VPN-Server-CA" | easyrsa gen-req ca nopass
105
+ echo "VPN-Server-CA" | easyrsa gen-ca nopass
106
+
107
+ # Generate server certificate
108
+ echo "Generating server certificate..."
109
+ echo "server" | easyrsa gen-req server nopass
110
+ echo "yes" | easyrsa sign-req server server
111
+
112
+ # Generate Diffie-Hellman parameters
113
+ echo "Generating DH parameters (this may take a while)..."
114
+ easyrsa gen-dh
115
+
116
+ # Generate client certificate
117
+ echo "Generating client certificate..."
118
+ echo "client1" | easyrsa gen-req client1 nopass
119
+ echo "yes" | easyrsa sign-req client client1
120
+
121
+ # Copy certificates to organized structure
122
+ cp pki/ca.crt ca/
123
+ cp pki/issued/server.crt server/
124
+ cp pki/private/server.key server/
125
+ cp pki/issued/client1.crt client/
126
+ cp pki/private/client1.key client/
127
+ cp pki/dh.pem server/
128
+
129
+ echo "Certificate generation complete!"
130
+ echo "Certificates are available in /tmp/openvpn_pki/"
131
+ """
132
+ return easy_rsa_script
133
+
134
+ def generate_firewall_rules(self, network="10.8.0.0"):
135
+ """Generate iptables rules for OpenVPN"""
136
+ firewall_script = f"""#!/bin/bash
137
+ # OpenVPN Firewall Rules
138
+ # Run these commands on your server
139
+
140
+ echo "Setting up firewall rules for OpenVPN..."
141
+
142
+ # Allow VPN traffic
143
+ iptables -A INPUT -p udp --dport 1194 -j ACCEPT
144
+ iptables -A FORWARD -s {network}/24 -j ACCEPT
145
+ iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
146
+
147
+ # Enable IP forwarding
148
+ echo 1 > /proc/sys/net/ipv4/ip_forward
149
+
150
+ # NAT for VPN clients
151
+ iptables -t nat -A POSTROUTING -s {network}/24 -o eth0 -j MASQUERADE
152
+
153
+ # Save rules (Ubuntu/Debian)
154
+ iptables-save > /etc/iptables/rules.v4
155
+
156
+ echo "Firewall rules configured successfully!"
157
+ """
158
+ return firewall_script
159
+
160
+ def create_deployment_guide(self):
161
+ """Create deployment instructions"""
162
+ guide = """
163
+ # OpenVPN Deployment Guide
164
+
165
+ ## Important Note for Containerized Environments
166
+
167
+ This application provides OpenVPN configuration management for environments
168
+ that may not support direct TUN device access (like Docker containers).
169
+
170
+ ## Deployment Options
171
+
172
+ ### Option 1: Local Deployment
173
+ For actual VPN functionality, deploy on a proper server with:
174
+ - Linux kernel with TUN support
175
+ - Root or sudo access
176
+ - Proper network configuration
177
+
178
+ ### Option 2: Configuration Management
179
+ Use this application to:
180
+ - Generate OpenVPN configurations
181
+ - Create certificate requests
182
+ - Provide setup scripts
183
+ - Generate firewall rules
184
+
185
+ ## Quick Start
186
+
187
+ 1. **Generate Server Configuration**
188
+ - Use the interface to create server config
189
+ - Download the configuration files
190
+
191
+ 2. **Generate Client Configuration**
192
+ - Create client configs for users
193
+ - Include proper server address
194
+
195
+ 3. **Certificate Generation**
196
+ - Run the provided certificate script
197
+ - Follow the generated instructions
198
+
199
+ 4. **Deployment**
200
+ - Upload configs to your server
201
+ - Run the setup scripts
202
+ - Configure firewall rules
203
+
204
+ ## Security Considerations
205
+
206
+ - Never expose generated certificates
207
+ - Use strong passwords for CA
208
+ - Regularly rotate certificates
209
+ - Monitor VPN access logs
210
+ - Use fail2ban for intrusion prevention
211
+
212
+ ## Troubleshooting
213
+
214
+ If you encounter TUN device errors:
215
+ - Check kernel module: `lsmod | grep tun`
216
+ - Verify permissions: `ls -la /dev/net/tun`
217
+ - Ensure CAP_NET_ADMIN capability
218
+ - Consider using TUN device emulation
219
+ """
220
+ return guide
221
+
222
+ # Initialize the manager
223
+ vpn_manager = OpenVPNManager()
224
+
225
+ def generate_server_config():
226
+ """Generate server configuration"""
227
+ config = vpn_manager.create_sample_config("server")
228
+ return config
229
+
230
+ def generate_client_config():
231
+ """Generate client configuration"""
232
+ config = vpn_manager.create_sample_config("client")
233
+ return config
234
+
235
+ def generate_certificates():
236
+ """Generate certificate scripts"""
237
+ script = vpn_manager.create_certificate_scripts()
238
+ return script
239
+
240
+ def generate_firewall():
241
+ """Generate firewall rules"""
242
+ rules = vpn_manager.generate_firewall_rules()
243
+ return rules
244
+
245
+ def create_all_files():
246
+ """Create a complete package with all files"""
247
+ files = {
248
+ "server.conf": generate_server_config(),
249
+ "client.conf": generate_client_config(),
250
+ "generate_certs.sh": generate_certificates(),
251
+ "firewall_rules.sh": generate_firewall(),
252
+ "DEPLOYMENT_GUIDE.md": vpn_manager.create_deployment_guide()
253
+ }
254
+
255
+ # Create a zip file in memory
256
+ zip_buffer = io.BytesIO()
257
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
258
+ for filename, content in files.items():
259
+ zip_file.writestr(filename, content)
260
+
261
+ zip_buffer.seek(0)
262
+ return zip_buffer.getvalue()
263
+
264
+ # Gradio Interface
265
+ def create_interface():
266
+ with gr.Blocks(title="OpenVPN Configuration Manager", theme=gr.themes.Soft()) as interface:
267
+ gr.Markdown("# 🔒 OpenVPN Configuration Manager")
268
+ gr.Markdown("### Fixed version for Hugging Face Spaces - Container-compatible")
269
+
270
+ with gr.Tab("📄 Generate Configurations"):
271
+ with gr.Row():
272
+ server_btn = gr.Button("Generate Server Config", variant="primary")
273
+ client_btn = gr.Button("Generate Client Config", variant="secondary")
274
+
275
+ server_config = gr.Textbox(label="Server Configuration", lines=15, max_lines=20)
276
+ client_config = gr.Textbox(label="Client Configuration", lines=15, max_lines=20)
277
+
278
+ server_btn.click(fn=generate_server_config, outputs=server_config)
279
+ client_btn.click(fn=generate_client_config, outputs=client_config)
280
+
281
+ with gr.Tab("🛡️ Security Setup"):
282
+ with gr.Row():
283
+ cert_btn = gr.Button("Generate Certificate Scripts", variant="primary")
284
+ firewall_btn = gr.Button("Generate Firewall Rules", variant="secondary")
285
+
286
+ cert_script = gr.Textbox(label="Certificate Generation Script", lines=20, max_lines=25)
287
+ firewall_rules = gr.Textbox(label="Firewall Rules", lines=15, max_lines=20)
288
+
289
+ cert_btn.click(fn=generate_certificates, outputs=cert_script)
290
+ firewall_btn.click(fn=generate_firewall, outputs=firewall_rules)
291
+
292
+ with gr.Tab("📦 Download Package"):
293
+ gr.Markdown("Download all configuration files and scripts as a zip package")
294
+ download_btn = gr.Button("Create Complete Package", variant="primary")
295
+ download_file = gr.File(label="Download All Files")
296
+
297
+ download_btn.click(
298
+ fn=create_all_files,
299
+ outputs=download_file
300
+ )
301
+
302
+ with gr.Tab("ℹ️ Instructions"):
303
+ gr.Markdown(vpn_manager.create_deployment_guide())
304
+
305
+ return interface
306
+
307
+ # Main function
308
+ if __name__ == "__main__":
309
+ interface = create_interface()
310
+ interface.launch(
311
+ server_name="0.0.0.0",
312
+ server_port=7860,
313
+ share=False,
314
+ show_error=True
315
+ )
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==4.19.0
2
+ pathlib2==2.3.7
3
+ requests==2.31.0