File size: 10,192 Bytes
aaaaa79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Main ISP Application

Integrates all core modules and provides the main application entry point
"""

import os
import sys
import json
import threading
import time
from flask import Flask
from flask_cors import CORS

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

# Import routes and core modules
from routes.isp_api import isp_api, init_engines


def load_config():
    """Load configuration from file or use defaults"""
    config_file = os.path.join(os.path.dirname(__file__), 'config.json')
    
    default_config = {
        "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,
            "host_ip": "0.0.0.0"
        },
        "firewall": {
            "default_policy": "ACCEPT",
            "log_blocked": True,
            "log_accepted": False,
            "max_log_entries": 10000,
            "rules": [
                {
                    "rule_id": "allow_dhcp",
                    "priority": 1,
                    "action": "ACCEPT",
                    "direction": "BOTH",
                    "dest_port": "67,68",
                    "protocol": "UDP",
                    "description": "Allow DHCP traffic",
                    "enabled": True
                },
                {
                    "rule_id": "allow_dns",
                    "priority": 2,
                    "action": "ACCEPT",
                    "direction": "BOTH",
                    "dest_port": "53",
                    "protocol": "UDP",
                    "description": "Allow DNS traffic",
                    "enabled": True
                }
            ]
        },
        "tcp": {
            "initial_window": 65535,
            "max_retries": 3,
            "timeout": 300,
            "time_wait_timeout": 120,
            "mss": 1460
        },
        "router": {
            "router_id": "virtual-isp-router",
            "default_gateway": "10.0.0.1",
            "interfaces": [
                {
                    "name": "virtual0",
                    "ip_address": "10.0.0.1",
                    "netmask": "255.255.255.0",
                    "enabled": True,
                    "mtu": 1500
                }
            ],
            "static_routes": []
        },
        "socket_translator": {
            "connect_timeout": 10,
            "read_timeout": 30,
            "max_connections": 1000,
            "buffer_size": 8192
        },
        "packet_bridge": {
            "websocket_host": "0.0.0.0",
            "websocket_port": 8765,
            "tcp_host": "0.0.0.0",
            "tcp_port": 8766,
            "max_clients": 100,
            "client_timeout": 300
        },
        "session_tracker": {
            "max_sessions": 10000,
            "session_timeout": 3600,
            "cleanup_interval": 300,
            "metrics_retention": 86400
        },
        "logger": {
            "log_level": "INFO",
            "log_to_file": True,
            "log_file_path": "/tmp/virtual_isp.log",
            "log_file_max_size": 10485760,
            "log_file_backup_count": 5,
            "log_to_console": True,
            "structured_logging": True,
            "max_memory_logs": 10000
        },
        "openvpn": {
            "server_config_path": "/etc/openvpn/server/server.conf",
            "ca_cert_path": "/etc/openvpn/server/ca.crt",
            "server_cert_path": "/etc/openvpn/server/server.crt",
            "server_key_path": "/etc/openvpn/server/server.key",
            "dh_path": "/etc/openvpn/server/dh.pem",
            "vpn_network": "10.8.0.0/24",
            "vpn_server_ip": "10.8.0.1",
            "vpn_port": 1194,
            "protocol": "udp",
            "auto_start": False,
            "client_to_client": False,
            "push_routes": [
                "redirect-gateway def1 bypass-dhcp",
                "dhcp-option DNS 8.8.8.8",
                "dhcp-option DNS 8.8.4.4"
            ]
        }
    }
    
    if os.path.exists(config_file):
        try:
            with open(config_file, 'r') as f:
                file_config = json.load(f)
            
            # Merge with defaults
            def merge_config(default, override):
                result = default.copy()
                for key, value in override.items():
                    if key in result and isinstance(result[key], dict) and isinstance(value, dict):
                        result[key] = merge_config(result[key], value)
                    else:
                        result[key] = value
                return result
            
            return merge_config(default_config, file_config)
        
        except Exception as e:
            print(f"Error loading config file: {e}")
            print("Using default configuration")
            return default_config
    else:
        # Save default config
        try:
            with open(config_file, 'w') as f:
                json.dump(default_config, f, indent=2)
            print(f"Created default configuration file: {config_file}")
        except Exception as e:
            print(f"Could not save default config: {e}")
        
        return default_config


def create_app():
    """Create and configure Flask application"""
    app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), 'static'))
    
    # Enable CORS for all routes
    CORS(app, origins="*", allow_headers=["Content-Type", "Authorization"])
    
    # Load configuration
    config = load_config()
    app.config['ISP_CONFIG'] = config
    
    # Register blueprints
    app.register_blueprint(isp_api, url_prefix='/api')
    
    # Initialize engines
    init_engines(config)
    
    # Serve static files
    @app.route('/', defaults={'path': ''})
    @app.route('/<path:path>')
    def serve_static(path):
        static_folder_path = app.static_folder
        if static_folder_path is None:
            return "Static folder not configured", 404
        
        if path != "" and os.path.exists(os.path.join(static_folder_path, path)):
            return app.send_static_file(path)
        else:
            index_path = os.path.join(static_folder_path, 'index.html')
            if os.path.exists(index_path):
                return app.send_static_file('index.html')
            else:
                return """
                <!DOCTYPE html>
                <html>
                <head>
                    <title>Virtual ISP Stack</title>
                    <style>
                        body { font-family: Arial, sans-serif; margin: 40px; }
                        .container { max-width: 800px; margin: 0 auto; }
                        .status { background: #f0f0f0; padding: 20px; border-radius: 5px; }
                        .api-link { color: #0066cc; text-decoration: none; }
                        .api-link:hover { text-decoration: underline; }
                    </style>
                </head>
                <body>
                    <div class="container">
                        <h1>Virtual ISP Stack</h1>
                        <div class="status">
                            <h2>System Status</h2>
                            <p>The Virtual ISP Stack is running successfully!</p>
                            <p><strong>API Endpoint:</strong> <a href="/api/status" class="api-link">/api/status</a></p>
                            <p><strong>System Stats:</strong> <a href="/api/stats" class="api-link">/api/stats</a></p>
                        </div>
                        
                        <h2>Available API Endpoints</h2>
                        <ul>
                            <li><a href="/api/config" class="api-link">GET /api/config</a> - System configuration</li>
                            <li><a href="/api/status" class="api-link">GET /api/status</a> - System status</li>
                            <li><a href="/api/stats" class="api-link">GET /api/stats</a> - System statistics</li>
                            <li><a href="/api/dhcp/leases" class="api-link">GET /api/dhcp/leases</a> - DHCP leases</li>
                            <li><a href="/api/nat/sessions" class="api-link">GET /api/nat/sessions</a> - NAT sessions</li>
                            <li><a href="/api/firewall/rules" class="api-link">GET /api/firewall/rules</a> - Firewall rules</li>
                            <li><a href="/api/tcp/connections" class="api-link">GET /api/tcp/connections</a> - TCP connections</li>
                            <li><a href="/api/router/routes" class="api-link">GET /api/router/routes</a> - Routing table</li>
                            <li><a href="/api/bridge/clients" class="api-link">GET /api/bridge/clients</a> - Bridge clients</li>
                            <li><a href="/api/sessions" class="api-link">GET /api/sessions</a> - Session tracking</li>
                            <li><a href="/api/logs" class="api-link">GET /api/logs</a> - System logs</li>
                        </ul>
                        
                        <h2>WebSocket Bridge</h2>
                        <p>WebSocket server running on port 8765 for packet bridge connections.</p>
                        <p>TCP server running on port 8766 for packet bridge connections.</p>
                    </div>
                </body>
                </html>
                """, 200
    
    return app


def main():
    """Main application entry point"""
    print("Starting Virtual ISP Stack...")
    
    # Create Flask app
    app = create_app()
    
    # Start the application
    print("Virtual ISP Stack started successfully!")
    print("API available at: http://0.0.0.0:5000/api/")
    print("WebSocket bridge at: ws://0.0.0.0:8765")
    print("TCP bridge at: tcp://0.0.0.0:8766")
    
    # Run Flask app
    app.run(host='0.0.0.0', port=5000, debug=False, threaded=True)


if __name__ == '__main__':
    main()