mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-09-11 14:01:15 +00:00
v20210402
Added more features
This commit is contained in:
110
src/dashboard.py
110
src/dashboard.py
@@ -2,19 +2,21 @@ import os
|
||||
from flask import Flask, request, render_template, redirect, url_for
|
||||
import subprocess
|
||||
from datetime import datetime, date, time, timedelta
|
||||
from tinydb import TinyDB, Query
|
||||
import sqlite3
|
||||
import time
|
||||
import requests
|
||||
|
||||
|
||||
from operator import itemgetter
|
||||
import collections
|
||||
from tinydb import TinyDB, Query
|
||||
conf_location = "/etc/wireguard"
|
||||
|
||||
app = Flask("Wireguard Dashboard")
|
||||
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||
css = ""
|
||||
conf_data = {}
|
||||
|
||||
|
||||
|
||||
|
||||
def get_conf_peer_key(config_name):
|
||||
keys = []
|
||||
try: peer_key = subprocess.check_output("wg show "+config_name+" peers", shell=True)
|
||||
@@ -41,12 +43,31 @@ def get_conf_running_peer_number(config_name):
|
||||
return running
|
||||
|
||||
def get_conf_peers_data(config_name):
|
||||
db = TinyDB('db/'+config_name+'.json')
|
||||
peers = Query()
|
||||
peer_data = {}
|
||||
# Get key
|
||||
try: peer_key = subprocess.check_output("wg show "+config_name+" peers", shell=True)
|
||||
except Exception: return "stopped"
|
||||
peer_key = peer_key.decode("UTF-8").split()
|
||||
for i in peer_key: peer_data[i] = {}
|
||||
for i in peer_key:
|
||||
peer_data[i] = {}
|
||||
if not db.search(peers.id == i):
|
||||
db.insert({
|
||||
"id": i,
|
||||
"name": "",
|
||||
"total_receive": 0,
|
||||
"total_sent": 0,
|
||||
"total_data": 0,
|
||||
"endpoint": 0,
|
||||
"status": 0,
|
||||
"latest_handshake": 0,
|
||||
"allowed_ip": 0,
|
||||
"traffic": []
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
#Get transfer
|
||||
try: data_usage = subprocess.check_output("wg show "+config_name+" transfer", shell=True)
|
||||
@@ -57,7 +78,11 @@ def get_conf_peers_data(config_name):
|
||||
download_total = 0
|
||||
total = 0
|
||||
for i in range(int(len(data_usage)/3)):
|
||||
peer_data[data_usage[count]]['total_recive'] = round(int(data_usage[count+1])/(1024**3),4)
|
||||
db.update({"total_receive": round(int(data_usage[count+1])/(1024**3),4),
|
||||
"total_sent": round(int(data_usage[count+2])/(1024**3),4),
|
||||
"total_data": round((int(data_usage[count+2])+int(data_usage[count+1]))/(1024**3),4)}, peers.id == data_usage[count])
|
||||
|
||||
peer_data[data_usage[count]]['total_receive'] = round(int(data_usage[count+1])/(1024**3),4)
|
||||
peer_data[data_usage[count]]['total_sent'] = round(int(data_usage[count+2])/(1024**3),4)
|
||||
peer_data[data_usage[count]]['total_data'] = round((int(data_usage[count+2])+int(data_usage[count+1]))/(1024**3),4)
|
||||
count += 3
|
||||
@@ -68,6 +93,8 @@ def get_conf_peers_data(config_name):
|
||||
data_usage = data_usage.decode("UTF-8").split()
|
||||
count = 0
|
||||
for i in range(int(len(data_usage)/2)):
|
||||
db.update({"endpoint": data_usage[count+1]}, peers.id == data_usage[count])
|
||||
|
||||
peer_data[data_usage[count]]['endpoint'] = data_usage[count+1]
|
||||
count += 2
|
||||
|
||||
@@ -80,11 +107,16 @@ def get_conf_peers_data(config_name):
|
||||
b = timedelta(minutes=2)
|
||||
for i in range(int(len(data_usage)/2)):
|
||||
minus = now - datetime.fromtimestamp(int(data_usage[count+1]))
|
||||
status = ""
|
||||
if minus < b:
|
||||
peer_data[data_usage[count]]['status'] = "running"
|
||||
status = "running"
|
||||
else:
|
||||
peer_data[data_usage[count]]['status'] = "stopped"
|
||||
peer_data[data_usage[count]]['latest_handshake'] = minus
|
||||
status = "stopped"
|
||||
|
||||
db.update({"latest_handshake": str(minus).split(".")[0], "status": status}, peers.id == data_usage[count])
|
||||
peer_data[data_usage[count]]['latest_handshake'] = str(minus).split(".")[0]
|
||||
count += 2
|
||||
|
||||
#Get allowed ip
|
||||
@@ -93,27 +125,30 @@ def get_conf_peers_data(config_name):
|
||||
data_usage = data_usage.decode("UTF-8").split()
|
||||
count = 0
|
||||
for i in range(int(len(data_usage)/2)):
|
||||
db.update({"allowed_ip": data_usage[count+1]}, peers.id == data_usage[count])
|
||||
peer_data[data_usage[count]]['allowed_ip'] = data_usage[count+1]
|
||||
count += 2
|
||||
|
||||
return peer_data
|
||||
|
||||
result = db.all()
|
||||
result = sorted(result, key=lambda d: d['status'])
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
def get_conf_pub_key(config_name):
|
||||
try: pub_key = subprocess.check_output("wg show "+config_name+" public-key", shell=True)
|
||||
try: pub_key = subprocess.check_output("wg show "+config_name+" public-key", shell=True, stderr=subprocess.STDOUT)
|
||||
except Exception: return "stopped"
|
||||
return pub_key.decode("UTF-8")
|
||||
|
||||
def get_conf_listen_port(config_name):
|
||||
try: pub_key = subprocess.check_output("wg show "+config_name+" listen-port", shell=True)
|
||||
try: pub_key = subprocess.check_output("wg show "+config_name+" listen-port", shell=True, stderr=subprocess.STDOUT)
|
||||
except Exception: return "stopped"
|
||||
return pub_key.decode("UTF-8")
|
||||
|
||||
|
||||
def get_conf_total_data(config_name):
|
||||
try: data_usage = subprocess.check_output("wg show "+config_name+" transfer", shell=True)
|
||||
try: data_usage = subprocess.check_output("wg show "+config_name+" transfer", shell=True, stderr=subprocess.STDOUT)
|
||||
except Exception: return "stopped"
|
||||
data_usage = data_usage.decode("UTF-8").split()
|
||||
count = 0
|
||||
@@ -133,7 +168,7 @@ def get_conf_total_data(config_name):
|
||||
|
||||
|
||||
def get_conf_status(config_name):
|
||||
try: status = subprocess.check_output("wg show "+config_name, shell=True)
|
||||
try: status = subprocess.check_output("wg show "+config_name, shell=True, stderr=subprocess.STDOUT)
|
||||
except Exception: return "stopped"
|
||||
else: return "running"
|
||||
|
||||
@@ -148,6 +183,7 @@ def get_conf_list():
|
||||
temp['checked'] = 'checked'
|
||||
else: temp['checked'] = ""
|
||||
conf.append(temp)
|
||||
conf = sorted(conf, key=itemgetter('status'))
|
||||
return conf
|
||||
|
||||
@app.route('/',methods=['GET'])
|
||||
@@ -157,6 +193,21 @@ def index():
|
||||
|
||||
@app.route('/configuration/<config_name>', methods=['GET'])
|
||||
def conf(config_name):
|
||||
conf_data = {
|
||||
"name": config_name,
|
||||
"status": get_conf_status(config_name),
|
||||
"checked": ""
|
||||
}
|
||||
if conf_data['status'] == "stopped":
|
||||
return redirect('/')
|
||||
else:
|
||||
conf_data['checked'] = "checked"
|
||||
return render_template('configuration.html', conf=get_conf_list(), conf_data=conf_data)
|
||||
|
||||
@app.route('/get_config/<config_name>', methods=['GET'])
|
||||
def get_conf(config_name):
|
||||
db = TinyDB('db/'+config_name+'.json')
|
||||
|
||||
conf_data = {
|
||||
"name": config_name,
|
||||
"status": get_conf_status(config_name),
|
||||
@@ -165,14 +216,12 @@ def conf(config_name):
|
||||
"listen_port": get_conf_listen_port(config_name),
|
||||
"peer_data":get_conf_peers_data(config_name),
|
||||
"running_peer": get_conf_running_peer_number(config_name),
|
||||
"checked": ""
|
||||
}
|
||||
if conf_data['status'] == "stopped":
|
||||
print(conf_data)
|
||||
return redirect('/')
|
||||
else:
|
||||
conf_data['checked'] = "checked"
|
||||
return render_template('configuration.html', conf=get_conf_list(), conf_data=conf_data)
|
||||
return render_template('get_conf.html', conf=get_conf_list(), conf_data=conf_data)
|
||||
|
||||
|
||||
@app.route('/switch/<config_name>', methods=['GET'])
|
||||
@@ -208,6 +257,8 @@ def add_peer(config_name):
|
||||
|
||||
@app.route('/remove_peer/<config_name>', methods=['POST'])
|
||||
def remove_peer(config_name):
|
||||
db = TinyDB("db/"+config_name+".json")
|
||||
peers = Query()
|
||||
data = request.get_json()
|
||||
delete_key = data['peer_id']
|
||||
keys = get_conf_peer_key(config_name)
|
||||
@@ -217,8 +268,35 @@ def remove_peer(config_name):
|
||||
try:
|
||||
status = subprocess.check_output("wg set "+config_name+" peer "+delete_key+" remove", shell=True, stderr=subprocess.STDOUT)
|
||||
status = subprocess.check_output("wg-quick save "+config_name, shell=True, stderr=subprocess.STDOUT)
|
||||
db.remove(peers.id == delete_key)
|
||||
return "true"
|
||||
except subprocess.CalledProcessError as exc:
|
||||
return exc.output.strip()
|
||||
|
||||
@app.route('/save_peer_name/<config_name>', methods=['POST'])
|
||||
def save_peer_name(config_name):
|
||||
data = request.get_json()
|
||||
id = data['id']
|
||||
name = data['name']
|
||||
db = TinyDB("db/"+config_name+".json")
|
||||
peers = Query()
|
||||
|
||||
db.update({"name": name}, peers.id == id)
|
||||
return id + " " + name
|
||||
|
||||
@app.route('/get_peer_name/<config_name>', methods=['POST'])
|
||||
def get_peer_name(config_name):
|
||||
data = request.get_json()
|
||||
id = data['id']
|
||||
db = TinyDB("db/"+config_name+".json")
|
||||
peers = Query()
|
||||
result = db.search(peers.id == id)
|
||||
return result[0]['name']
|
||||
# db.update({"name": name}, peers.id == id)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
app.run(host='0.0.0.0',debug=False, port=10086)
|
Reference in New Issue
Block a user