-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
617 lines (524 loc) · 23.3 KB
/
main.py
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import http.server
import socketserver
import json
import os
import subprocess
import logging
import webbrowser
import signal
import sys
from urllib.parse import unquote, urlparse, parse_qs
from io import BytesIO
import ssl
from functools import wraps
from time import time
from collections import defaultdict
from http import HTTPStatus
from concurrent.futures import ThreadPoolExecutor
import mimetypes
import threading
import datetime
import gzip
import brotli
from cert import generate_self_signed_cert, check_cert_expiry, start_cert_renewal_timer
import sqlite3
from passlib.hash import bcrypt # for password hashing
import secrets
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import re
# Load configuration from config file
def load_config(config_file='config.json'):
try:
with open(config_file, 'r') as f:
config = json.load(f)
return config
except FileNotFoundError:
logging.error(f"Configuration file {config_file} not found.")
raise
except json.JSONDecodeError:
logging.error(f"Error decoding JSON from {config_file}.")
raise
config = load_config()
# ---------------------- SERVER CONFIGURATION ----------------------
# Load all configuration options from config.json
PORT = config.get('server_config', {}).get('port', 8080)
ENTRY_POINT = config.get('server_config', {}).get('entry_point', 'index.html')
HTDOCS_DIR = config.get('server_config', {}).get('htdocs_dir', 'htdocs')
DOWNLOADS_DIR = config.get('server_config', {}).get('downloads_dir', os.path.join(HTDOCS_DIR, 'downloads'))
LOG_FILE = config.get('server_config', {}).get('log_file', 'server.log')
# HTTPS configuration
USE_HTTPS = config.get('server_features', {}).get('use_https', False)
CERTFILE = config.get('https_config', {}).get('certfile', 'cert.pem')
KEYFILE = config.get('https_config', {}).get('keyfile', 'key.pem')
CERT_CONFIG = config.get('https_config', {}).get('cert_config', {})
CERT_RENEWAL_DAYS = config.get('https_config', {}).get('cert_renewal_days', 30)
# Rate limiting configuration
ENABLE_RATE_LIMITING = config.get('server_features', {}).get('enable_rate_limiting', True)
RATE_LIMIT = config.get('rate_limiting_config', {}).get('rate_limit', 10) # requests per minute
RATE_LIMIT_BURST = config.get('rate_limiting_config', {}).get('rate_limit_burst', 5) # Allow bursts of requests
rate_limit_data = defaultdict(lambda: {'count': 0, 'last_reset': time()})
# IP Filtering
ENABLE_IP_FILTERING = config.get('server_features', {}).get('enable_ip_filtering', False)
WHITELIST = config.get('ip_filtering_config', {}).get('whitelist', [])
BLACKLIST = config.get('ip_filtering_config', {}).get('blacklist', [])
# Static file caching
ENABLE_STATIC_FILE_CACHE = config.get('server_features', {}).get('enable_static_file_cache', True)
CACHE = {}
CACHE_MAX_SIZE = config.get('caching_config', {}).get('cache_max_size', 10 * 1024 * 1024) # 10MB
CACHE_TTL = config.get('caching_config', {}).get('cache_ttl', 60) # 60 seconds
# Compression
ENABLE_GZIP_COMPRESSION = config.get('server_features', {}).get('enable_gzip_compression', True)
ENABLE_BROTLI_COMPRESSION = config.get('server_features', {}).get('enable_brotli_compression', True)
# Basic Authentication
ENABLE_BASIC_AUTH = config.get('server_features', {}).get('enable_basic_auth', False)
AUTH_USERS = config.get('basic_auth_config', {}).get('auth_users', {})
# Virtual Hosts
ENABLE_VIRTUAL_HOSTS = config.get('server_features', {}).get('enable_virtual_hosts', False)
VIRTUAL_HOSTS = config.get('virtual_hosts_config', {}).get('virtual_hosts', {})
# Custom error pages
ENABLE_CUSTOM_ERROR_PAGES = config.get('server_features', {}).get('enable_custom_error_pages', False)
ERROR_PAGES = config.get('error_pages_config', {}).get('error_pages', {})
# URL Redirection
ENABLE_URL_REDIRECTS = config.get('server_features', {}).get('enable_url_redirects', False)
URL_REDIRECTS = config.get('url_redirects_config', {}).get('url_redirects', {})
# ---------------------- DATABASE SETUP ----------------------
DB_FILE = config.get('database_config', {}).get('db_file', 'server.db')
def init_db():
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
verified INTEGER DEFAULT 0
)
""")
conn.commit()
conn.close()
# Initialize database
init_db()
# ---------------------- EMAIL CONFIGURATION ----------------------
ENABLE_EMAIL_VERIFICATION = config.get('email_config', {}).get('enable_email_verification', False)
EMAIL_HOST = config.get('email_config', {}).get('email_host', 'smtp.example.com')
EMAIL_PORT = config.get('email_config', {}).get('email_port', 587)
EMAIL_USER = config.get('email_config', {}).get('email_user', '[email protected]')
EMAIL_PASSWORD = config.get('email_config', {}).get('email_password', 'your_password')
EMAIL_FROM = config.get('email_config', {}).get('email_from', '[email protected]')
SITE_URL = config.get('email_config', {}).get('site_url', 'http://localhost:8080') # Used for verification links
# ---------------------- DECORATORS ----------------------
def rate_limited(func):
@wraps(func)
def wrapper(handler, *args, **kwargs):
if not ENABLE_RATE_LIMITING:
return func(handler, *args, **kwargs)
client_ip = handler.client_address[0]
data = rate_limit_data[client_ip]
# Reset rate limit if enough time has passed
if time() - data['last_reset'] >= 60:
data['count'] = 0
data['last_reset'] = time()
if data['count'] >= RATE_LIMIT:
handler.send_error(429, "Too Many Requests")
return
data['count'] += 1
return func(handler, *args, **kwargs)
return wrapper
def ip_allowed(func):
@wraps(func)
def wrapper(handler, *args, **kwargs):
if not ENABLE_IP_FILTERING:
return func(handler, *args, **kwargs)
client_ip = handler.client_address[0]
if WHITELIST and client_ip not in WHITELIST:
handler.send_error(403, "Forbidden")
return
if BLACKLIST and client_ip in BLACKLIST:
handler.send_error(403, "Forbidden")
return
return func(handler, *args, **kwargs)
return wrapper
def basic_auth(func):
@wraps(func)
def wrapper(handler, *args, **kwargs):
if not ENABLE_BASIC_AUTH:
return func(handler, *args, **kwargs)
auth_header = handler.headers.get('Authorization')
if auth_header:
auth_type, encoded_credentials = auth_header.split(' ', 1)
if auth_type.lower() == 'basic':
try:
username, password = encoded_credentials.encode().decode('base64').split(':', 1)
except:
handler.send_error(401, 'Invalid Authorization header')
return
if AUTH_USERS.get(username) == password:
return func(handler, *args, **kwargs)
handler.send_error(401, 'Unauthorized')
handler.send_header('WWW-Authenticate', 'Basic realm="Restricted Area"')
return wrapper
# ---------------------- REQUEST HANDLER ----------------------
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.entry_point = kwargs.pop('entry_point', ENTRY_POINT)
self.executor = kwargs.pop('executor', None)
super().__init__(*args, directory=HTDOCS_DIR, **kwargs)
@ip_allowed
@rate_limited
@basic_auth
def do_GET(self):
# Virtual Host Routing
if ENABLE_VIRTUAL_HOSTS:
host = self.headers.get('Host')
virtual_host = VIRTUAL_HOSTS.get(host)
if virtual_host:
self.directory = virtual_host.get('htdocs_dir', HTDOCS_DIR)
self.entry_point = virtual_host.get('entry_point', ENTRY_POINT)
# URL Redirection
if ENABLE_URL_REDIRECTS:
redirect = URL_REDIRECTS.get(self.path)
if redirect:
self.send_response(302) # Temporary redirect
self.send_header('Location', redirect)
self.end_headers()
return
# Handle verification
if self.path.startswith('/verify?token='):
self.verify_email(self.path.split('=')[1])
return
if self.path == '/':
self.path = '/' + self.entry_point
elif self.path == '/list-downloads':
self.list_downloads()
elif self.path.startswith('/downloads/'):
self.serve_download()
else:
self.serve_static_file()
@ip_allowed
@rate_limited
@basic_auth
def do_POST(self):
if self.path == '/upload':
self.handle_file_upload()
elif self.path == '/register':
self.handle_registration()
else:
self.send_error(404, "File not found")
def handle_php(self):
try:
php_file_path = self.translate_path(self.path)
if not os.path.exists(php_file_path):
self.send_error(404, "File not found")
return
future = self.executor.submit(subprocess.run, ['php', php_file_path], capture_output=True)
process = future.result()
if process.returncode != 0:
self.send_error(500, "PHP execution failed")
logging.error(f"PHP execution failed: {process.stderr.decode()}")
return
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(process.stdout)
except Exception as e:
self.send_error(500, "Internal server error")
logging.error(f"Exception while handling PHP file: {e}")
def handle_file_upload(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
boundary = self.headers['Content-Type'].split("=")[1].encode()
parts = body.split(boundary)
for part in parts:
if b'Content-Disposition' in part:
headers, content = part.split(b'\r\n\r\n', 1)
headers = headers.decode()
filename = headers.split('filename="')[1].split('"')[0]
filepath = os.path.join(DOWNLOADS_DIR, filename)
with open(filepath, 'wb') as f:
f.write(content.rstrip(b'\r\n--'))
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
response = BytesIO()
response.write(b"File uploaded successfully")
self.wfile.write(response.getvalue())
def handle_registration(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length).decode('utf-8')
data = parse_qs(body)
username = data.get('username', [''])[0]
password = data.get('password', [''])[0]
email = data.get('email', [''])[0]
# Basic Validation (You should enhance this)
if not all([username, password, email]):
self.send_error(400, "Missing username, password or email.")
return
if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
self.send_error(400, "Invalid email address.")
return
try:
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("SELECT 1 FROM users WHERE username = ?", (username,))
user_exists = cursor.fetchone()
if user_exists:
self.send_error(400, "Username already exists.")
return
cursor.execute("SELECT 1 FROM users WHERE email = ?", (email,))
email_exists = cursor.fetchone()
if email_exists:
self.send_error(400, "Email already exists.")
return
# Hash the password
hashed_password = bcrypt.hash(password)
cursor.execute("INSERT INTO users (username, password, email) VALUES (?, ?, ?)",
(username, hashed_password, email))
conn.commit()
conn.close()
if ENABLE_EMAIL_VERIFICATION:
self.send_verification_email(username, email)
self.send_response(201, "User registered successfully.")
self.send_header("Content-type", "text/plain")
self.end_headers()
except Exception as e:
self.send_error(500, f"Registration failed: {e}")
def send_verification_email(self, username, email):
if ENABLE_EMAIL_VERIFICATION:
token = secrets.token_urlsafe(20)
try:
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("UPDATE users SET verification_token = ? WHERE username = ?", (token, username))
conn.commit()
conn.close()
verification_link = f"{SITE_URL}/verify?token={token}"
msg = MIMEMultipart()
msg['Subject'] = "Verify your email address"
msg['From'] = EMAIL_FROM
msg['To'] = email
html = f"""
<html>
<head></head>
<body>
<p>Hello {username},</p>
<p>Please click the link below to verify your email address:</p>
<p><a href="{verification_link}">{verification_link}</a></p>
</body>
</html>
"""
part1 = MIMEText(html, 'html')
msg.attach(part1)
with smtplib.SMTP(EMAIL_HOST, EMAIL_PORT) as server:
server.starttls()
server.login(EMAIL_USER, EMAIL_PASSWORD)
server.sendmail(EMAIL_FROM, email, msg.as_string())
logging.info(f"Verification email sent to {email}")
except Exception as e:
logging.error(f"Failed to send verification email: {e}")
self.send_error(500, "Failed to send verification email.")
def verify_email(self, token):
try:
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("SELECT username FROM users WHERE verification_token = ?", (token,))
user = cursor.fetchone()
if user:
username = user[0]
cursor.execute("UPDATE users SET verified = 1, verification_token = NULL WHERE username = ?", (username,))
conn.commit()
conn.close()
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"Email verified successfully!")
else:
conn.close()
self.send_error(400, "Invalid or expired token.")
except Exception as e:
logging.error(f"Failed to verify email: {e}")
self.send_error(500, "Failed to verify email.")
def serve_static_file(self):
file_path = self.translate_path(self.path)
# Check if file exists
if not os.path.exists(file_path):
self.send_error(404, 'File not found')
return
# Check for custom error document
if not os.path.exists(file_path):
error_code = 404
if hasattr(HTTPStatus, str(error_code)) and ENABLE_CUSTOM_ERROR_PAGES:
self.send_error(error_code)
return
# Serve from cache if available
if file_path in CACHE:
entry = CACHE[file_path]
if time() - entry['timestamp'] < CACHE_TTL:
self.send_response(200)
self.send_header("Content-type", entry['content_type'])
self.send_header("Content-Encoding", entry['content_encoding'])
self.send_header("Content-Length", entry['content_length'])
self.end_headers()
with open(file_path, 'rb') as f:
self.wfile.write(f.read())
return
# Open the file
try:
with open(file_path, 'rb') as f:
content = f.read()
except Exception as e:
self.send_error(500, f"Internal server error: {e}")
return
# Compression
content_encoding = None
if ENABLE_GZIP_COMPRESSION and 'gzip' in self.headers.get('Accept-Encoding', ''):
content_encoding = 'gzip'
content = gzip.compress(content)
elif ENABLE_BROTLI_COMPRESSION and 'br' in self.headers.get('Accept-Encoding', ''):
content_encoding = 'br'
content = brotli.compress(content)
# Determine content type
content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
# Cache the file if caching is enabled
if ENABLE_STATIC_FILE_CACHE:
if len(CACHE) >= CACHE_MAX_SIZE:
CACHE.pop(next(iter(CACHE)))
CACHE[file_path] = {
'timestamp': time(),
'content_type': content_type,
'content_encoding': content_encoding,
'content_length': len(content)
}
# Serve the file
self.send_response(200)
self.send_header("Content-type", content_type)
if content_encoding:
self.send_header("Content-Encoding", content_encoding)
self.send_header("Content-Length", len(content))
self.end_headers()
self.wfile.write(content)
def list_downloads(self):
try:
files = os.listdir(DOWNLOADS_DIR)
files = [f for f in files if os.path.isfile(os.path.join(DOWNLOADS_DIR, f))]
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
response = json.dumps({"files": files})
self.wfile.write(response.encode())
except Exception as e:
self.send_error(500, "Internal server error")
logging.error(f"Exception while listing downloads: {e}")
def serve_download(self):
file_path = self.path.lstrip('/')
full_path = os.path.join(HTDOCS_DIR, file_path)
if os.path.exists(full_path) and os.path.isfile(full_path):
mime_type, _ = mimetypes.guess_type(full_path)
self.send_response(200)
self.send_header("Content-Type", mime_type or "application/octet-stream")
self.send_header("Content-Disposition", f'attachment; filename="{os.path.basename(full_path)}"')
self.send_header("Content-Length", str(os.path.getsize(full_path)))
self.end_headers()
with open(full_path, 'rb') as f:
self.wfile.write(f.read())
else:
self.send_error(404, "File not found")
def list_directory(self, path):
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
f = BytesIO()
displaypath = unquote(self.path)
f.write(b'<!DOCTYPE html>\n<html>\n<head>\n<title>Directory listing for %s</title>\n</head>\n' % displaypath.encode())
f.write(b'<body>\n<h2>Directory listing for %s</h2>\n' % displaypath.encode())
f.write(b'<hr>\n<ul>\n')
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
f.write(b'<li><a href="%s">%s</a></li>\n' % (linkname.encode(), displayname.encode()))
f.write(b'</ul>\n<hr>\n</body>\n</html>\n')
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
return f
def send_error(self, code, message=None):
# Use custom error pages if configured
if ENABLE_CUSTOM_ERROR_PAGES and str(code) in ERROR_PAGES:
error_page_path = ERROR_PAGES[str(code)]
try:
with open(error_page_path, 'rb') as f:
content = f.read()
self.send_response(code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(content)
return
except:
logging.error(f"Failed to load custom error page: {error_page_path}")
# Default error handling
self.error_message_format = '''
<!DOCTYPE html>
<html>
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code: %(code)d</p>
<p>Message: %(message)s</p>
</body>
</html>
'''
super().send_error(code, message)
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
def log_message(self, format, *args):
with open(LOG_FILE, "a") as log_file:
log_file.write("%s - - [%s] %s\n" %
(self.client_address[0],
self.log_date_time_string(),
format % args))
# ---------------------- SERVER FUNCTIONS ----------------------
def run_server():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
try:
if USE_HTTPS:
generate_self_signed_cert(CERTFILE, KEYFILE, CERT_CONFIG)
# Start a timer to check certificate expiry periodically
timer = start_cert_renewal_timer(CERTFILE, KEYFILE, CERT_CONFIG, CERT_RENEWAL_DAYS)
with ThreadPoolExecutor() as executor:
handler = lambda *args, **kwargs: CustomHTTPRequestHandler(*args, entry_point=ENTRY_POINT, executor=executor,
**kwargs)
with socketserver.ThreadingTCPServer(("0.0.0.0", PORT), handler) as httpd:
if USE_HTTPS:
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=CERTFILE, keyfile=KEYFILE, server_side=True)
logging.info(f"Serving on port {PORT} with HTTPS")
webbrowser.open(f'https://localhost:{PORT}/{ENTRY_POINT}')
else:
logging.info(f"Serving on port {PORT}")
webbrowser.open(f'http://localhost:{PORT}/{ENTRY_POINT}')
# Handle graceful shutdown on Ctrl-C
def signal_handler(sig, frame):
logging.info('Shutting down server...')
httpd.shutdown()
if USE_HTTPS:
timer.cancel() # Cancel the timer
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
httpd.serve_forever()
except Exception as e:
logging.error(f"Failed to start server: {e}")
if __name__ == "__main__":
run_server()