-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
705 lines (584 loc) · 23.1 KB
/
app.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
from flask import Flask, render_template, request, jsonify, redirect, url_for, flash, send_file
import os
import io
import json
import datetime
import subprocess
from database import cleanup_old_traffic_logs, aggregate_traffic_data
from wireguard import get_client_status
from database import (
init_db, get_db_connection, add_client, get_client, get_all_clients,
get_active_clients, update_client, delete_client, update_client_status,
find_available_ip, update_data_usage, get_server_config, update_server_config,
get_traffic_stats
)
from wireguard import (
generate_keypair, generate_preshared_key, restart_wireguard, apply_server_config,
generate_client_config, generate_qr_code, add_client_to_wireguard,
remove_client_from_wireguard, get_client_status
)
from utils import format_bytes, format_timestamp, get_server_stats
# from config import SECRET_KEY, DEBUG, DATABASE_PATH
from config import (
SECRET_KEY, DEBUG, DATABASE_PATH,
WG_INTERFACE, PUBLIC_IP, WG_PORT, ADMIN_PASSWORD
)
app = Flask(__name__)
app.config['SECRET_KEY'] = SECRET_KEY
from apscheduler.schedulers.background import BackgroundScheduler
from functools import wraps
from flask import session, redirect, url_for
# Initialize database using an app context instead of before_first_request
def initialize_database():
with app.app_context():
init_db()
# Call the initialize function
initialize_database()
ADMIN_PASSWORD = ADMIN_PASSWORD # Change this!
SECRET_KEY = SECRET_KEY # Change this!
#login path
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'authenticated' not in session:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# Login route
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
if request.form['password'] == ADMIN_PASSWORD:
session['authenticated'] = True
return redirect(url_for('index'))
return render_template('login.html', error='Invalid password')
return render_template('login.html')
# Logout route
@app.route('/logout')
def logout():
session.pop('authenticated', None)
return redirect(url_for('login'))
# Main dashboard route
@app.route('/')
@login_required
def index():
# Get basic stats
traffic_stats = get_traffic_stats()
active_clients = traffic_stats['active_clients']
total_clients = traffic_stats['total_clients']
monthly_traffic = format_bytes(traffic_stats['monthly_traffic_bytes'])
# Get server stats
server_stats = get_server_stats()
# Format uptime for display
uptime = server_stats['uptime']['formatted']
return render_template(
'index.html',
active_clients=active_clients,
total_clients=total_clients,
monthly_traffic=monthly_traffic,
uptime=uptime
)
@app.route('/clients')
@login_required
def clients(): # This is the main clients page route
all_clients = get_all_clients()
client_status = get_client_status()
# Merge client data with status
for client in all_clients:
status = client_status.get(client['public_key'], {})
client['online'] = status.get('online', False)
client['last_seen_formatted'] = format_timestamp(client['last_seen'])
client['data_usage_formatted'] = format_bytes(client['data_usage'] * 1024 * 1024)
return render_template('clients.html', clients=all_clients)
# Client management routes
@app.route('/api/clients')
@login_required
def clients_api():
"""API endpoint returning all clients with their status"""
all_clients = get_all_clients()
client_status = get_client_status()
# Merge client data with status
for client in all_clients:
status = client_status.get(client['public_key'], {})
client['online'] = status.get('online', False)
client['last_seen_formatted'] = format_timestamp(client['last_seen'])
client['data_usage_formatted'] = format_bytes(client['data_usage'] * 1024 * 1024) # Convert MB to bytes
return jsonify(all_clients)
@app.route('/api/available-ips')
@login_required
def available_ips_api():
"""API endpoint returning available IP addresses"""
available_ips = []
base_ip = '10.8.0.'
with get_db_connection() as conn:
used_ips = [row[0] for row in conn.execute('SELECT assigned_ip FROM clients').fetchall()]
for i in range(2, 255):
ip = f"{base_ip}{i}"
if ip not in used_ips:
available_ips.append(ip)
if len(available_ips) >= 10: # Limit to 10 suggestions
break
return jsonify(available_ips)
@app.route('/api/server-config')
@login_required
def server_config_api():
"""API endpoint returning server configuration"""
config = get_server_config()
if config:
return jsonify(config)
# Use values imported from config module
return jsonify({
'endpoint': f"{PUBLIC_IP}:{WG_PORT}",
'subnet': '10.8.0.0/24', # You might want to import this from config too
'dns_servers': '1.1.1.1, 1.0.0.1', # Consider importing this
'mtu': 1420, # Consider importing this
'keepalive': 25 # Consider importing this
})
@app.route('/clients/add', methods=['GET', 'POST'])
@login_required
def add_client_route():
if request.method == 'POST':
try:
name = request.form['name']
email = request.form['email'] if 'email' in request.form else None
data_limit = int(request.form['data_limit']) if request.form['data_limit'] != 'none' else None
expiry_date = request.form['expiry_date'] if request.form['expiry_date'] else None
# Generate keys
private_key, public_key = generate_keypair()
preshared_key = generate_preshared_key()
# Find available IP or use specified one
if 'assigned_ip' in request.form and request.form['assigned_ip']:
assigned_ip = request.form['assigned_ip']
else:
assigned_ip = find_available_ip()
if not assigned_ip:
flash('No available IP addresses!', 'error')
return redirect(url_for('clients'))
# Add to database
client_id = add_client(
name=name,
email=email,
private_key=private_key,
public_key=public_key,
assigned_ip=assigned_ip,
preshared_key=preshared_key,
data_limit=data_limit,
expiry_date=expiry_date
)
# Add to WireGuard
client = get_client(client_id)
if add_client_to_wireguard(client):
flash('Client added successfully!', 'success')
else:
flash('Client added to database but failed to configure WireGuard', 'warning')
return redirect(url_for('client_detail', client_id=client_id))
except Exception as e:
flash(f'Error adding client: {str(e)}', 'error')
return redirect(url_for('clients'))
# For GET request
available_ips = []
base_ip = '10.8.0.'
with get_db_connection() as conn:
used_ips = [row[0] for row in conn.execute('SELECT assigned_ip FROM clients').fetchall()]
for i in range(2, 255):
ip = f"{base_ip}{i}"
if ip not in used_ips:
available_ips.append(ip)
if len(available_ips) >= 10: # Limit to 10 suggestions
break
return render_template('add_client.html', available_ips=available_ips)
@app.route('/clients/<int:client_id>')
@login_required
def client_detail(client_id):
client = get_client(client_id)
if not client:
flash('Client not found!', 'error')
return redirect(url_for('clients'))
# Get client status from WireGuard
client_status = get_client_status().get(client['public_key'], {})
client['online'] = client_status.get('online', False)
client['last_seen_formatted'] = format_timestamp(client['last_seen'])
client['data_usage_formatted'] = format_bytes(client['data_usage'] * 1024 * 1024)
# Generate client config and QR code
server_config = get_server_config()
client_config = generate_client_config(client, server_config)
qr_code = generate_qr_code(client_config)
return render_template(
'client_detail.html',
client=client,
client_config=client_config,
qr_code=qr_code
)
@app.route('/clients/<int:client_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_client(client_id):
client = get_client(client_id)
if not client:
flash('Client not found!', 'error')
return redirect(url_for('clients'))
if request.method == 'POST':
# Handle the POST request
updates = {
'name': request.form['name'],
'email': request.form['email'],
'data_limit': int(request.form['data_limit']) if request.form['data_limit'] != 'none' else None,
'expiry_date': request.form['expiry_date'] if request.form['expiry_date'] else None,
'is_active': 1 if 'is_active' in request.form else 0
}
update_client(client_id, **updates)
flash('Client updated successfully!', 'success')
return redirect(url_for('client_detail', client_id=client_id))
# For GET request
server_config = get_server_config()
client_config = generate_client_config(client, server_config)
qr_code = generate_qr_code(client_config)
# Get activity logs (implement this function)
activity_logs = get_client_activity_logs(client_id)
return render_template('edit_client.html',
client=client,
client_config=client_config,
qr_code=qr_code,
activity_logs=activity_logs)
@app.route('/clients/<int:client_id>/delete', methods=['POST'])
@login_required
def delete_client_route(client_id):
client = get_client(client_id)
if not client:
flash('Client not found!', 'error')
return redirect(url_for('clients'))
# Remove from WireGuard
remove_client_from_wireguard(client['public_key'])
# Delete from database
delete_client(client_id)
flash('Client deleted successfully!', 'success')
return redirect(url_for('clients'))
@app.route('/logs')
@login_required
def logs():
# Get WireGuard and system logs
try:
# Get last 100 lines of WireGuard logs
wg_logs = subprocess.check_output(
['journalctl', '-u', f'wg-quick@{WG_INTERFACE}', '-n', '100', '--no-pager'],
universal_newlines=True
)
# Get system logs related to WireGuard
sys_logs = subprocess.check_output(
['dmesg', '|', 'grep', '-i', 'wireguard'],
shell=True,
universal_newlines=True
)
except subprocess.CalledProcessError:
wg_logs = "Unable to retrieve WireGuard logs"
sys_logs = "Unable to retrieve system logs"
return render_template('logs.html', wg_logs=wg_logs, sys_logs=sys_logs)
@app.route('/settings')
@login_required
def settings():
# Get current settings
server_config = get_server_config()
return render_template('settings.html', config=server_config)
@app.route('/clients/<int:client_id>/toggle', methods=['POST'])
@login_required
def toggle_client(client_id):
client = get_client(client_id)
if not client:
return jsonify({'success': False, 'message': 'Client not found!'})
new_status = request.json.get('active', False)
update_client_status(client_id, new_status)
# Update in WireGuard
if new_status:
add_client_to_wireguard(client)
else:
remove_client_from_wireguard(client['public_key'])
return jsonify({'success': True})
@app.route('/clients/<int:client_id>/download')
@login_required
def download_client_config(client_id):
client = get_client(client_id)
if not client:
flash('Client not found!', 'error')
return redirect(url_for('clients'))
server_config = get_server_config()
client_config = generate_client_config(client, server_config)
# Create a file-like object
config_file = io.BytesIO(client_config.encode('utf-8'))
return send_file(
config_file,
mimetype='text/plain',
as_attachment=True,
download_name=f"{client['name']}.conf"
)
@app.route('/clients/<int:client_id>/qr')
@login_required
def client_qr_code(client_id):
client = get_client(client_id)
if not client:
flash('Client not found!', 'error')
return redirect(url_for('clients'))
server_config = get_server_config()
client_config = generate_client_config(client, server_config)
qr_code = generate_qr_code(client_config)
return jsonify({'qr_code': qr_code})
# Server configuration routes
@app.route('/server-config', methods=['GET', 'POST'])
@login_required
def server_config():
config = get_server_config()
if request.method == 'POST':
updates = {
'endpoint': request.form['endpoint'],
'subnet': request.form['subnet'],
'dns_servers': request.form['dns_servers'],
'mtu': int(request.form['mtu']),
'keepalive': int(request.form['keepalive'])
}
# Update in database
update_server_config(**updates)
# Apply changes to WireGuard
config = get_server_config() # Get updated config with keys
apply_server_config(config)
flash('Server configuration updated successfully!', 'success')
return redirect(url_for('server_config'))
return render_template('server_config.html', config=config)
# Traffic monitoring routes
@app.route('/traffic')
@login_required
def traffic():
try:
# Get traffic statistics
traffic_stats = get_traffic_stats()
# Get client traffic data
with get_db_connection() as conn:
client_traffic = conn.execute('''
SELECT
c.name,
c.last_seen,
COALESCE(SUM(t.upload), 0) as upload,
COALESCE(SUM(t.download), 0) as download
FROM clients c
LEFT JOIN traffic_logs t ON c.id = t.client_id
WHERE t.timestamp >= date('now', '-30 days') OR t.timestamp IS NULL
GROUP BY c.id
ORDER BY c.name
''').fetchall()
return render_template('traffic.html',
stats=traffic_stats,
client_traffic=client_traffic,
format_bytes=format_bytes,
format_timestamp=format_timestamp)
except Exception as e:
flash(f'Error loading traffic data: {str(e)}', 'error')
return render_template('traffic.html',
stats={'monthly_traffic_formatted': '0 B',
'total_clients': 0,
'active_clients': 0},
client_traffic=[])
@app.route('/api/traffic/stats')
@login_required
def traffic_stats_api():
stats = get_traffic_stats()
stats['monthly_traffic_formatted'] = format_bytes(stats['monthly_traffic_bytes'])
return jsonify(stats)
@app.route('/api/traffic/history')
@login_required
def traffic_history_api():
days = int(request.args.get('days', 30))
with get_db_connection() as conn:
# Get recent detailed logs
recent_data = conn.execute('''
SELECT
date(timestamp) as date,
SUM(upload) as upload,
SUM(download) as download
FROM traffic_logs
WHERE timestamp >= date('now', '-7 days')
GROUP BY date(timestamp)
''').fetchall()
# Get aggregated historical data
historical_data = conn.execute('''
SELECT
date,
SUM(total_upload) as upload,
SUM(total_download) as download
FROM traffic_logs_daily
WHERE date >= date('now', ?)
AND date < date('now', '-7 days')
GROUP BY date
ORDER BY date
''', (f'-{days} days',)).fetchall()
# Combine the data
traffic_data = historical_data + recent_data
result = []
for row in traffic_data:
result.append({
'date': row['date'],
'upload': row['upload'],
'download': row['download'],
'upload_formatted': format_bytes(row['upload']),
'download_formatted': format_bytes(row['download'])
})
return jsonify(sorted(result, key=lambda x: x['date']))
@app.route('/api/traffic/clients')
@login_required
def client_traffic_api():
with get_db_connection() as conn:
client_traffic = conn.execute('''
SELECT
c.id,
c.name,
c.assigned_ip,
SUM(t.upload) as upload,
SUM(t.download) as download
FROM clients c
LEFT JOIN traffic_logs t ON c.id = t.client_id
WHERE t.timestamp >= date('now', '-30 days') OR t.timestamp IS NULL
GROUP BY c.id
ORDER BY (SUM(t.upload) + SUM(t.download)) DESC
''').fetchall()
result = []
for row in client_traffic:
upload = row['upload'] if row['upload'] else 0
download = row['download'] if row['download'] else 0
result.append({
'id': row['id'],
'name': row['name'],
'ip': row['assigned_ip'],
'upload': upload,
'download': download,
'upload_formatted': format_bytes(upload),
'download_formatted': format_bytes(download),
'total_formatted': format_bytes(upload + download)
})
return jsonify(result)
# System status route
@app.route('/api/system-status')
@login_required
def system_status_api():
stats = get_server_stats()
return jsonify(stats)
# Logs page
# @app.route('/logs')
# def logs():
# # Get WireGuard logs
# try:
# journal_logs = subprocess.check_output([
# 'journalctl', '-u', f'wg-quick@{WG_INTERFACE}', '-n', '100', '--no-pager'
# ]).decode('utf-8')
# except:
# journal_logs = "Unable to retrieve logs"
# return render_template('logs.html', logs=journal_logs)
# # Settings page
# @app.route('/settings')
# def settings():
# return render_template('settings.html')
# Dynamic API endpoints for real-time data
@app.route('/api/dashboard-stats')
@login_required
def dashboard_stats_api():
traffic_stats = get_traffic_stats()
server_stats = get_server_stats()
client_status = get_client_status()
# Count online clients
online_count = sum(1 for status in client_status.values() if status.get('online', False))
return jsonify({
'active_clients': traffic_stats['active_clients'],
'online_clients': online_count,
'total_clients': traffic_stats['total_clients'],
'monthly_traffic': format_bytes(traffic_stats['monthly_traffic_bytes']),
'load': server_stats['load'],
'memory': {
'used': format_bytes(server_stats['memory']['used']),
'total': format_bytes(server_stats['memory']['total']),
'percentage': server_stats['memory']['percentage']
},
'disk': {
'used': format_bytes(server_stats['disk']['used']),
'total': format_bytes(server_stats['disk']['total']),
'percentage': server_stats['disk']['percentage']
},
'uptime': server_stats['uptime']['formatted']
})
@app.route('/api/backup')
@login_required
def backup_config():
try:
with get_db_connection() as conn:
# Get server config
server_config = dict(conn.execute('SELECT * FROM server_config').fetchone())
# Get clients
clients = [dict(row) for row in conn.execute('SELECT * FROM clients').fetchall()]
backup = {
'server_config': server_config,
'clients': clients,
'timestamp': datetime.datetime.now().isoformat()
}
return jsonify(backup)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/restore', methods=['POST'])
@login_required
def restore_config():
try:
backup = request.json
with get_db_connection() as conn:
# Restore server config
conn.execute('DELETE FROM server_config')
conn.execute('''
INSERT INTO server_config (endpoint, subnet, dns_servers, mtu, keepalive, private_key, public_key)
VALUES (:endpoint, :subnet, :dns_servers, :mtu, :keepalive, :private_key, :public_key)
''', backup['server_config'])
# Restore clients
conn.execute('DELETE FROM clients')
for client in backup['clients']:
conn.execute('''
INSERT INTO clients (name, email, private_key, public_key, preshared_key, assigned_ip,
created_at, last_seen, is_active, data_limit, data_usage, expiry_date)
VALUES (:name, :email, :private_key, :public_key, :preshared_key, :assigned_ip,
:created_at, :last_seen, :is_active, :data_limit, :data_usage, :expiry_date)
''', client)
conn.commit()
# Restart WireGuard with new config
apply_server_config(backup['server_config'])
return jsonify({'success': True})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/reset', methods=['POST'])
@login_required
def reset_server():
try:
# Initialize new server configuration
init_db()
return jsonify({'success': True})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/clear-logs', methods=['POST'])
@login_required
def clear_logs():
try:
with get_db_connection() as conn:
conn.execute('DELETE FROM traffic_logs')
conn.commit()
return jsonify({'success': True})
except Exception as e:
return jsonify({'error': str(e)}), 500
def setup_scheduled_tasks():
"""Setup scheduled maintenance tasks"""
scheduler = BackgroundScheduler()
# Update client status every minute
scheduler.add_job(get_client_status, 'interval', minutes=1)
# Aggregate traffic data daily
scheduler.add_job(aggregate_traffic_data, 'cron', hour=0, minute=5)
# Clean up old traffic logs weekly
scheduler.add_job(
cleanup_old_traffic_logs,
'cron',
day_of_week='sun',
hour=1,
minute=0,
args=[30] # Keep 30 days of detailed logs
)
scheduler.start()
if __name__ == '__main__':
setup_scheduled_tasks()
app.run(debug=True, host='0.0.0.0')