1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 15:20:09 +01:00

Added dockerfile! Thanks @ikidd!

This commit is contained in:
Donald Cheng Hong Zou 2022-04-05 21:39:47 -04:00
parent 46da285831
commit 4848739b6e
6 changed files with 228 additions and 21 deletions

28
Dockerfile Normal file
View File

@ -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

18
docker-compose.yml Normal file
View File

@ -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

16
entrypoint.sh Normal file
View File

@ -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

View File

@ -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,6 +1744,126 @@ 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
"""
@ -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)

View File

@ -13,9 +13,6 @@
{% include "navbar.html" %}
<div class="container-fluid" id="right_body">
{% include "sidebar.html" %}
<div class="col-md-9 ml-sm-auto col-lg-10 px-md-4 mt-4 mb-4">
</div>
<div id="config_body">
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-md-4 mt-4 mb-4">
<div class="info mt-4">
@ -28,7 +25,7 @@
<h1 class="mb-3"><samp id="conf_name">{{ title }}</samp></h1>
</div>
<div class="col">
<small class="text-muted"><strong>SWITCH</strong></small><br>
<small class="text-muted"><strong>TOGGLE</strong></small><br>
<!-- <div id="conf_status_btn" class="info_loading"></div> -->
<div id="switch" class="info_loading">
<input type="checkbox" class="toggle--switch" id="toggle--switch">

0
src/wgd.sh Normal file → Executable file
View File