{{ title }}
+ TOGGLE
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a742b50 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM ubuntu:20.04 + +ARG WG_ADDRESS=$WG_ADDRESS + +RUN apt-get update && \ + apt-get install -y --no-install-recommends iproute2 wireguard-tools iptables nano net-tools python3 python3-pip python3-venv procps openresolv inotify-tools && \ + apt-get clean + +RUN mkdir -p /etc/wireguard/ +RUN mkdir -p /opt/wgdashboard +RUN mkdir -p /opt/wgdashboard_tmp +# configure wireguard +RUN wg genkey | tee /opt/wgdashboard_tmp/privatekey | wg pubkey | tee /opt/wgdashboard_tmp/publickey + +RUN cd / && echo "[Interface]" > wg0.conf && echo "SaveConfig = true" >> wg0.conf && echo -n "PrivateKey = " >> wg0.conf && cat /opt/wgdashboard_tmp/privatekey >> wg0.conf \ + && echo "ListenPort = 51820" >> wg0.conf && echo "Address = ${WG_ADDRESS}" >> wg0.conf && chmod 700 wg0.conf + +COPY ./src /opt/wgdashboard_tmp +RUN pip3 install -r /opt/wgdashboard_tmp/requirements.txt --no-cache-dir +RUN rm -rf /opt/wgdashboard_tmp +COPY ./entrypoint.sh /entrypoint.sh +RUN chmod u+x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +WORKDIR /opt/wgdashboard + +EXPOSE 10086 +EXPOSE 51820/udp \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cae4893 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3.5" + +services: + web: + build: + context: ./ + dockerfile: ./Dockerfile + cap_add: + - NET_ADMIN + - SYS_MODULE + restart: unless-stopped + volumes: + - /lib/modules:/lib/modules + - ./src:/opt/wgdashboard + - ./config:/etc/wireguard + ports: + - 10086:10086 + - 51820:51820/udp \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..b08d553 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# if [ -z "$(ls -A /etc/wireguard)" ]; then +# mv /wg0.conf /etc/wireguard +# echo "Moved conf file to /etc/wireguard" +# else +# rm wg0.conf +# echo "Removed unneeded conf file" +# fi + +# wg-quick up wg0 +chmod u+x /opt/wgdashboard/wgd.sh +if [ ! -f "/opt/wgdashboard/wg-dashboard.ini" ]; then + /opt/wgdashboard/wgd.sh install +fi +/opt/wgdashboard/wgd.sh debug \ No newline at end of file diff --git a/src/dashboard.py b/src/dashboard.py index c9c9d4a..8fcfd6b 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -27,6 +27,7 @@ from icmplib import ping, traceroute # Import other python files from util import * +import threading # Dashboard Version DASHBOARD_VERSION = 'v3.1' @@ -260,21 +261,32 @@ def get_transfer(config_name): total_receive = cur_i[0][0] cur_total_sent = round(int(data_usage[i][2]) / (1024 ** 3), 4) cur_total_receive = round(int(data_usage[i][1]) / (1024 ** 3), 4) - if cur_i[0][4] == "running": - if total_sent <= cur_total_sent and total_receive <= cur_total_receive: - total_sent = cur_total_sent - total_receive = cur_total_receive - else: - cumulative_receive = cur_i[0][2] + total_receive - cumulative_sent = cur_i[0][3] + total_sent - g.cur.execute("UPDATE %s SET cumu_receive = %f, cumu_sent = %f, cumu_data = %f WHERE id = '%s'" % - (config_name, round(cumulative_receive, 4), round(cumulative_sent, 4), - round(cumulative_sent + cumulative_receive, 4), data_usage[i][0])) - total_sent = 0 - total_receive = 0 - g.cur.execute("UPDATE %s SET total_receive = %f, total_sent = %f, total_data = %f WHERE id = '%s'" % - (config_name, round(total_receive, 4), round(total_sent, 4), - round(total_receive + total_sent, 4), data_usage[i][0])) + # if cur_i[0][4] == "running": + cumulative_receive = cur_i[0][2] + total_receive + cumulative_sent = cur_i[0][3] + total_sent + if total_sent <= cur_total_sent and total_receive <= cur_total_receive: + total_sent = cur_total_sent + total_receive = cur_total_receive + else: + # cumulative_receive = cur_i[0][2] + total_receive + # cumulative_sent = cur_i[0][3] + total_sent + g.cur.execute("UPDATE %s SET cumu_receive = %f, cumu_sent = %f, cumu_data = %f WHERE id = '%s'" % + (config_name, round(cumulative_receive, 4), round(cumulative_sent, 4), + round(cumulative_sent + cumulative_receive, 4), data_usage[i][0])) + total_sent = 0 + total_receive = 0 + g.cur.execute("UPDATE %s SET total_receive = %f, total_sent = %f, total_data = %f WHERE id = '%s'" % + (config_name, round(total_receive, 4), round(total_sent, 4), + round(total_receive + total_sent, 4), data_usage[i][0])) + # now = datetime.now() + # now_string = now.strftime("%d/%m/%Y %H:%M:%S") + # g.cur.execute(f''' + # INSERT INTO {config_name}_transfer (id, total_receive, total_sent, total_data, cumu_receive, cumu_sent, cumu_data, time) + # VALUES ('{data_usage[i][0]}', {round(total_receive, 4)}, {round(total_sent, 4)}, {round(total_receive + total_sent, 4)},{round(cumulative_receive, 4)}, {round(cumulative_sent, 4)}, + # {round(cumulative_sent + cumulative_receive, 4)}, '{now_string}') + # ''') + + def get_endpoint(config_name): @@ -311,6 +323,7 @@ def get_allowed_ip(conf_peer_data, config_name): % (i.get('AllowedIPs', '(None)'), i["PublicKey"])) + def get_all_peers_data(config_name): """ Look for new peers from WireGuard @@ -519,6 +532,14 @@ def get_conf_list(): ) """ g.cur.execute(create_table) + create_table = f""" + CREATE TABLE IF NOT EXISTS {i}_transfer ( + id VARCHAR NOT NULL, total_receive FLOAT NULL, + total_sent FLOAT NULL, total_data FLOAT NULL, + cumu_receive FLOAT NULL, cumu_sent FLOAT NULL, cumu_data FLOAT NULL, time DATETIME + ) + """ + g.cur.execute(create_table) temp = {"conf": i, "status": get_conf_status(i), "public_key": get_conf_pub_key(i), "port": get_conf_listen_port(i)} if temp['status'] == "running": temp['checked'] = 'checked' @@ -1723,10 +1744,130 @@ def traceroute_ip(): return "Error" +import atexit +@atexit.register +def goodbye(): + global stop_thread + global bgThread + stop_thread = True + + print("Exiting Python Script!") + +def get_all_transfer_thread(): + print("waiting 15 sec ") + time.sleep(7) + global stop_thread + # with app.app_context(): + try: + db = connect_db() + cur = db.cursor() + while True: + if stop_thread: + break + conf = [] + for i in os.listdir(WG_CONF_PATH): + if regex_match("^(.{1,}).(conf)$", i): + i = i.replace('.conf', '') + create_table = f""" + CREATE TABLE IF NOT EXISTS {i} ( + id VARCHAR NOT NULL, private_key VARCHAR NULL, DNS VARCHAR NULL, + endpoint_allowed_ip VARCHAR NULL, name VARCHAR NULL, total_receive FLOAT NULL, + total_sent FLOAT NULL, total_data FLOAT NULL, endpoint VARCHAR NULL, + status VARCHAR NULL, latest_handshake VARCHAR NULL, allowed_ip VARCHAR NULL, + cumu_receive FLOAT NULL, cumu_sent FLOAT NULL, cumu_data FLOAT NULL, mtu INT NULL, + keepalive INT NULL, remote_endpoint VARCHAR NULL, preshared_key VARCHAR NULL, + PRIMARY KEY (id) + ) + """ + cur.execute(create_table) + create_table = f""" + CREATE TABLE IF NOT EXISTS {i}_restrict_access ( + id VARCHAR NOT NULL, private_key VARCHAR NULL, DNS VARCHAR NULL, + endpoint_allowed_ip VARCHAR NULL, name VARCHAR NULL, total_receive FLOAT NULL, + total_sent FLOAT NULL, total_data FLOAT NULL, endpoint VARCHAR NULL, + status VARCHAR NULL, latest_handshake VARCHAR NULL, allowed_ip VARCHAR NULL, + cumu_receive FLOAT NULL, cumu_sent FLOAT NULL, cumu_data FLOAT NULL, mtu INT NULL, + keepalive INT NULL, remote_endpoint VARCHAR NULL, preshared_key VARCHAR NULL, + PRIMARY KEY (id) + ) + """ + cur.execute(create_table) + create_table = f""" + CREATE TABLE IF NOT EXISTS {i}_transfer ( + id VARCHAR NOT NULL, total_receive FLOAT NULL, + total_sent FLOAT NULL, total_data FLOAT NULL, + cumu_receive FLOAT NULL, cumu_sent FLOAT NULL, cumu_data FLOAT NULL, time DATETIME + ) + """ + cur.execute(create_table) + db.commit() + temp = {"conf": i, "status": get_conf_status(i), "public_key": get_conf_pub_key(i), "port": get_conf_listen_port(i)} + if temp['status'] == "running": + temp['checked'] = 'checked' + else: + temp['checked'] = "" + conf.append(temp) + if len(conf) > 0: + conf = sorted(conf, key=itemgetter('conf')) + + + print("adding...........") + # l = get_conf_list() + for i in conf: + print(i['conf']) + config_name = i['conf'] + try: + data_usage = subprocess.check_output(f"wg show {config_name} transfer", + shell=True, stderr=subprocess.STDOUT) + data_usage = data_usage.decode("UTF-8").split("\n") + final = [] + for i in data_usage: + final.append(i.split("\t")) + data_usage = final + for i in range(len(data_usage)): + cur_i = cur.execute( + "SELECT total_receive, total_sent, cumu_receive, cumu_sent, status FROM %s WHERE id='%s'" + % (config_name, data_usage[i][0])).fetchall() + if len(cur_i) > 0: + total_sent = cur_i[0][1] + total_receive = cur_i[0][0] + cur_total_sent = round(int(data_usage[i][2]) / (1024 ** 3), 4) + cur_total_receive = round(int(data_usage[i][1]) / (1024 ** 3), 4) + # if cur_i[0][4] == "running": + cumulative_receive = cur_i[0][2] + total_receive + cumulative_sent = cur_i[0][3] + total_sent + if total_sent <= cur_total_sent and total_receive <= cur_total_receive: + total_sent = cur_total_sent + total_receive = cur_total_receive + else: + # cumulative_receive = cur_i[0][2] + total_receive + # cumulative_sent = cur_i[0][3] + total_sent + cur.execute("UPDATE %s SET cumu_receive = %f, cumu_sent = %f, cumu_data = %f WHERE id = '%s'" % + (config_name, round(cumulative_receive, 4), round(cumulative_sent, 4), + round(cumulative_sent + cumulative_receive, 4), data_usage[i][0])) + total_sent = 0 + total_receive = 0 + cur.execute("UPDATE %s SET total_receive = %f, total_sent = %f, total_data = %f WHERE id = '%s'" % + (config_name, round(total_receive, 4), round(total_sent, 4), + round(total_receive + total_sent, 4), data_usage[i][0])) + now = datetime.now() + now_string = now.strftime("%d/%m/%Y %H:%M:%S") + cur.execute(f''' + INSERT INTO {config_name}_transfer (id, total_receive, total_sent, total_data, cumu_receive, cumu_sent, cumu_data, time) + VALUES ('{data_usage[i][0]}', {round(total_receive, 4)}, {round(total_sent, 4)}, {round(total_receive + total_sent, 4)},{round(cumulative_receive, 4)}, {round(cumulative_sent, 4)}, + {round(cumulative_sent + cumulative_receive, 4)}, '{now_string}') + ''') + # get_transfer(i['conf']) + db.commit() + except subprocess.CalledProcessError: + print(i['conf'] + " stopped") + time.sleep(15) + except KeyboardInterrupt: + return True """ Dashboard Initialization """ - + def init_dashboard(): """ @@ -1824,7 +1965,8 @@ def run_dashboard(): global WG_CONF_PATH WG_CONF_PATH = config.get("Server", "wg_conf_path") config.clear() - + x = threading.Thread(target=get_all_transfer_thread) + x.start() return app @@ -1853,4 +1995,10 @@ if __name__ == "__main__": app_port = config.get("Server", "app_port") WG_CONF_PATH = config.get("Server", "wg_conf_path") config.clear() + global bgThread + global stop_thread + stop_thread = False + + bgThread = threading.Thread(target=get_all_transfer_thread) + bgThread.start() app.run(host=app_ip, debug=False, port=app_port) diff --git a/src/templates/configuration.html b/src/templates/configuration.html index e8db51a..c21c8f0 100644 --- a/src/templates/configuration.html +++ b/src/templates/configuration.html @@ -13,9 +13,6 @@ {% include "navbar.html" %}