|
|
|
|
|
""" |
|
|
Virtual ISP Stack with OpenVPN Integration |
|
|
HuggingFace Spaces Entry Point |
|
|
|
|
|
This application provides a complete Virtual ISP stack with OpenVPN server integration, |
|
|
allowing users to manage VPN connections, generate client configurations, and monitor |
|
|
network traffic through a RESTful API. |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
import logging |
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__)) |
|
|
|
|
|
from flask import Flask, send_from_directory, jsonify |
|
|
from flask_cors import CORS |
|
|
from models.user import db |
|
|
from routes.user import user_bp |
|
|
from routes.isp_api import init_engines, isp_api |
|
|
|
|
|
|
|
|
logging.basicConfig( |
|
|
level=logging.INFO, |
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
|
|
) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), 'static')) |
|
|
|
|
|
|
|
|
CORS(app, origins="*") |
|
|
|
|
|
|
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'vpn-isp-stack-secret-key-change-in-production') |
|
|
|
|
|
|
|
|
database_path = os.path.join(os.path.dirname(__file__), 'database', 'app.db') |
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{database_path}" |
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
|
|
|
|
|
|
|
|
db.init_app(app) |
|
|
|
|
|
|
|
|
app.register_blueprint(user_bp, url_prefix='/api') |
|
|
app.register_blueprint(isp_api, url_prefix='/api') |
|
|
|
|
|
|
|
|
app.config.update({ |
|
|
"dhcp": { |
|
|
"network": "10.0.0.0/24", |
|
|
"range_start": "10.0.0.10", |
|
|
"range_end": "10.0.0.100", |
|
|
"lease_time": 3600, |
|
|
"gateway": "10.0.0.1", |
|
|
"dns_servers": ["8.8.8.8", "8.8.4.4"] |
|
|
}, |
|
|
"nat": { |
|
|
"port_range_start": 10000, |
|
|
"port_range_end": 65535, |
|
|
"session_timeout": 300 |
|
|
}, |
|
|
"firewall": { |
|
|
"default_policy": "ACCEPT", |
|
|
"log_blocked": True |
|
|
}, |
|
|
"tcp": { |
|
|
"initial_window": 65535, |
|
|
"max_retries": 3, |
|
|
"timeout": 30 |
|
|
}, |
|
|
"openvpn": { |
|
|
"server_ip": "10.8.0.1", |
|
|
"server_port": 1194, |
|
|
"network": "10.8.0.0/24" |
|
|
}, |
|
|
"logger": { |
|
|
"log_level": "INFO", |
|
|
"log_file": "/tmp/virtual_isp.log" |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
with app.app_context(): |
|
|
try: |
|
|
db.create_all() |
|
|
logger.info("Database tables created successfully") |
|
|
except Exception as e: |
|
|
logger.error(f"Error creating database tables: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
init_engines(app.config) |
|
|
logger.info("All engines initialized successfully") |
|
|
except Exception as e: |
|
|
logger.error(f"Error initializing engines: {e}") |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
"""Main index page""" |
|
|
return serve_static('') |
|
|
|
|
|
@app.route('/health') |
|
|
def health_check(): |
|
|
"""Health check endpoint for monitoring""" |
|
|
return jsonify({ |
|
|
'status': 'healthy', |
|
|
'service': 'Virtual ISP Stack with OpenVPN', |
|
|
'version': '1.0.0' |
|
|
}) |
|
|
|
|
|
@app.route('/api') |
|
|
def api_info(): |
|
|
"""API information endpoint""" |
|
|
return jsonify({ |
|
|
'service': 'Virtual ISP Stack API', |
|
|
'version': '1.0.0', |
|
|
'endpoints': { |
|
|
'openvpn': { |
|
|
'status': '/api/openvpn/status', |
|
|
'start': '/api/openvpn/start', |
|
|
'stop': '/api/openvpn/stop', |
|
|
'clients': '/api/openvpn/clients', |
|
|
'config': '/api/openvpn/config/<client_name>', |
|
|
'stats': '/api/openvpn/stats', |
|
|
'configs': '/api/openvpn/configs' |
|
|
}, |
|
|
'dhcp': { |
|
|
'leases': '/api/dhcp/leases' |
|
|
}, |
|
|
'nat': { |
|
|
'sessions': '/api/nat/sessions', |
|
|
'stats': '/api/nat/stats' |
|
|
}, |
|
|
'firewall': { |
|
|
'rules': '/api/firewall/rules', |
|
|
'logs': '/api/firewall/logs', |
|
|
'stats': '/api/firewall/stats' |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
@app.route('/<path:path>') |
|
|
def serve_static(path): |
|
|
"""Serve static files""" |
|
|
static_folder_path = app.static_folder |
|
|
if static_folder_path is None: |
|
|
return jsonify({'error': 'Static folder not configured'}), 404 |
|
|
|
|
|
if path != "" and os.path.exists(os.path.join(static_folder_path, path)): |
|
|
return send_from_directory(static_folder_path, path) |
|
|
else: |
|
|
index_path = os.path.join(static_folder_path, 'index.html') |
|
|
if os.path.exists(index_path): |
|
|
return send_from_directory(static_folder_path, 'index.html') |
|
|
else: |
|
|
return jsonify({ |
|
|
'message': 'Virtual ISP Stack with OpenVPN Integration', |
|
|
'status': 'running', |
|
|
'api_docs': '/api' |
|
|
}) |
|
|
|
|
|
@app.errorhandler(404) |
|
|
def not_found(error): |
|
|
"""Handle 404 errors""" |
|
|
return jsonify({'error': 'Endpoint not found', 'api_docs': '/api'}), 404 |
|
|
|
|
|
@app.errorhandler(500) |
|
|
def internal_error(error): |
|
|
"""Handle 500 errors""" |
|
|
return jsonify({'error': 'Internal server error'}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
port = int(os.environ.get('PORT', 7860)) |
|
|
|
|
|
logger.info(f"Starting Virtual ISP Stack with OpenVPN on port {port}") |
|
|
|
|
|
|
|
|
app.run( |
|
|
host='0.0.0.0', |
|
|
port=port, |
|
|
debug=False, |
|
|
threaded=True |
|
|
) |
|
|
|
|
|
|