2021-09-08 18:39:25 +02:00
|
|
|
"""
|
2022-01-13 01:53:36 +01:00
|
|
|
< WGDashboard > - Copyright(C) 2021 Donald Zou [https://github.com/donaldzou]
|
2021-09-08 18:39:25 +02:00
|
|
|
Under Apache-2.0 License
|
|
|
|
"""
|
2022-01-13 01:53:36 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
import sqlite3
|
2021-12-26 00:26:39 +01:00
|
|
|
import configparser
|
|
|
|
import hashlib
|
|
|
|
import ipaddress
|
|
|
|
import json
|
2021-05-04 07:32:34 +02:00
|
|
|
# Python Built-in Library
|
2020-10-18 07:10:13 +02:00
|
|
|
import os
|
2021-12-26 00:26:39 +01:00
|
|
|
import secrets
|
2020-10-18 07:10:13 +02:00
|
|
|
import subprocess
|
2021-07-02 19:23:04 +02:00
|
|
|
import time
|
2021-12-28 20:53:51 +01:00
|
|
|
import re
|
2021-12-26 00:26:39 +01:00
|
|
|
import urllib.parse
|
|
|
|
import urllib.request
|
2022-01-18 16:42:23 +01:00
|
|
|
import urllib.error
|
2021-12-28 20:53:51 +01:00
|
|
|
from datetime import datetime, timedelta
|
2021-04-03 02:48:00 +02:00
|
|
|
from operator import itemgetter
|
2021-05-04 07:32:34 +02:00
|
|
|
# PIP installed library
|
|
|
|
import ifcfg
|
2022-02-11 15:35:58 +01:00
|
|
|
from flask import Flask, request, render_template, redirect, url_for, session, jsonify, g
|
2021-08-05 06:45:15 +02:00
|
|
|
from flask_qrcode import QRcode
|
2021-12-26 00:26:39 +01:00
|
|
|
from icmplib import ping, traceroute
|
2022-01-19 16:27:17 +01:00
|
|
|
# TESTING
|
|
|
|
from flask_socketio import SocketIO
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
# Import other python files
|
2022-03-22 03:33:19 +01:00
|
|
|
from util import *
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-05-14 00:00:40 +02:00
|
|
|
# Dashboard Version
|
2022-01-31 22:09:50 +01:00
|
|
|
DASHBOARD_VERSION = 'v3.0.5'
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2022-01-13 15:37:23 +01:00
|
|
|
# WireGuard's configuration path
|
2022-01-01 01:20:30 +01:00
|
|
|
WG_CONF_PATH = None
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2021-05-14 00:00:40 +02:00
|
|
|
# Dashboard Config Name
|
2021-10-23 23:56:34 +02:00
|
|
|
configuration_path = os.getenv('CONFIGURATION_PATH', '.')
|
2022-01-01 00:57:59 +01:00
|
|
|
DB_PATH = os.path.join(configuration_path, 'db')
|
|
|
|
if not os.path.isdir(DB_PATH):
|
|
|
|
os.mkdir(DB_PATH)
|
2021-12-28 23:51:41 +01:00
|
|
|
DASHBOARD_CONF = os.path.join(configuration_path, 'wg-dashboard.ini')
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2021-05-14 00:00:40 +02:00
|
|
|
# Upgrade Required
|
2021-12-28 20:53:51 +01:00
|
|
|
UPDATE = None
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2021-05-14 00:00:40 +02:00
|
|
|
# Flask App Configuration
|
2021-09-09 03:56:31 +02:00
|
|
|
app = Flask("WGDashboard")
|
2021-12-25 20:44:14 +01:00
|
|
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 5206928
|
2021-05-04 07:32:34 +02:00
|
|
|
app.secret_key = secrets.token_urlsafe(16)
|
2020-10-18 07:10:13 +02:00
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
# Enable QR Code Generator
|
2021-08-05 06:45:15 +02:00
|
|
|
QRcode(app)
|
2022-01-19 16:27:17 +01:00
|
|
|
socketio = SocketIO(app)
|
2022-01-02 20:44:27 +01:00
|
|
|
|
2022-01-13 15:37:23 +01:00
|
|
|
# TODO: use class and object oriented programming
|
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
def connect_db():
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Connect to the database
|
|
|
|
@return: sqlite3.Connection
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
return sqlite3.connect(os.path.join(configuration_path, 'db', 'wgdashboard.db'))
|
2021-09-08 18:39:25 +02:00
|
|
|
|
2021-05-05 03:26:40 +02:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
def get_dashboard_conf():
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get dashboard configuration
|
|
|
|
@return: configparser.ConfigParser
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2022-01-19 15:25:27 +01:00
|
|
|
r_config = configparser.ConfigParser(strict=False)
|
|
|
|
r_config.read(DASHBOARD_CONF)
|
|
|
|
return r_config
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
def set_dashboard_conf(config):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Write to configuration
|
|
|
|
@param config: Input configuration
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
2021-12-28 20:53:51 +01:00
|
|
|
with open(DASHBOARD_CONF, "w", encoding='utf-8') as conf_object:
|
2021-12-26 11:04:39 +01:00
|
|
|
config.write(conf_object)
|
2021-09-08 18:39:25 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Get all keys from a configuration
|
2020-10-23 07:31:10 +02:00
|
|
|
def get_conf_peer_key(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get the peers keys of wireguard interface.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return list of peers keys or text if configuration not running
|
|
|
|
@rtype: list, str
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2021-04-03 20:06:21 +02:00
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
peers_keys = subprocess.check_output(f"wg show {config_name} peers",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2022-01-02 14:35:39 +01:00
|
|
|
peers_keys = peers_keys.decode("UTF-8").split()
|
|
|
|
return peers_keys
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2021-08-14 23:13:16 +02:00
|
|
|
return config_name + " is not running."
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-12-26 06:17:42 +01:00
|
|
|
def get_conf_running_peer_number(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get number of running peers on wireguard interface.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Number of running peers, or test if configuration not running
|
|
|
|
@rtype: int, str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-26 06:17:42 +01:00
|
|
|
running = 0
|
2021-04-03 20:06:21 +02:00
|
|
|
# Get latest handshakes
|
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
data_usage = subprocess.check_output(f"wg show {config_name} latest-handshakes",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2022-03-03 14:46:23 +01:00
|
|
|
return 0
|
2020-12-26 06:17:42 +01:00
|
|
|
data_usage = data_usage.decode("UTF-8").split()
|
|
|
|
count = 0
|
|
|
|
now = datetime.now()
|
2021-12-28 20:53:51 +01:00
|
|
|
time_delta = timedelta(minutes=2)
|
|
|
|
for _ in range(int(len(data_usage) / 2)):
|
2021-04-03 20:06:21 +02:00
|
|
|
minus = now - datetime.fromtimestamp(int(data_usage[count + 1]))
|
2021-12-28 20:53:51 +01:00
|
|
|
if minus < time_delta:
|
2020-12-26 06:17:42 +01:00
|
|
|
running += 1
|
|
|
|
count += 2
|
|
|
|
return running
|
2020-10-18 07:10:13 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-08-15 05:30:05 +02:00
|
|
|
def read_conf_file_interface(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get interface settings.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Dictionary with interface settings
|
|
|
|
@rtype: dict
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2022-01-01 01:20:30 +01:00
|
|
|
conf_location = WG_CONF_PATH + "/" + config_name + ".conf"
|
2022-03-03 14:46:23 +01:00
|
|
|
try:
|
|
|
|
with open(conf_location, 'r', encoding='utf-8') as file_object:
|
|
|
|
file = file_object.read().split("\n")
|
|
|
|
data = {}
|
|
|
|
for i in file:
|
|
|
|
if not regex_match("#(.*)", i):
|
|
|
|
if len(i) > 0:
|
|
|
|
if i != "[Interface]":
|
|
|
|
tmp = re.split(r'\s*=\s*', i, 1)
|
|
|
|
if len(tmp) == 2:
|
|
|
|
data[tmp[0]] = tmp[1]
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
return {}
|
2021-08-15 05:30:05 +02:00
|
|
|
return data
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-13 15:37:23 +01:00
|
|
|
def read_conf_file(config_name):
|
|
|
|
"""
|
|
|
|
Get configurations from file of wireguard interface.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Dictionary with interface and peers settings
|
|
|
|
@rtype: dict
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2022-01-01 01:20:30 +01:00
|
|
|
conf_location = WG_CONF_PATH + "/" + config_name + ".conf"
|
2021-12-29 20:57:44 +01:00
|
|
|
f = open(conf_location, 'r')
|
|
|
|
file = f.read().split("\n")
|
2021-04-03 20:06:21 +02:00
|
|
|
conf_peer_data = {
|
|
|
|
"Interface": {},
|
|
|
|
"Peers": []
|
|
|
|
}
|
|
|
|
peers_start = 0
|
2021-12-29 20:57:44 +01:00
|
|
|
for i in range(len(file)):
|
2022-01-13 01:53:36 +01:00
|
|
|
if not regex_match("#(.*)", file[i]) and regex_match(";(.*)", file[i]):
|
2021-12-29 20:57:44 +01:00
|
|
|
if file[i] == "[Peer]":
|
2021-05-14 00:00:40 +02:00
|
|
|
peers_start = i
|
|
|
|
break
|
2021-12-29 20:57:44 +01:00
|
|
|
else:
|
|
|
|
if len(file[i]) > 0:
|
|
|
|
if file[i] != "[Interface]":
|
|
|
|
tmp = re.split(r'\s*=\s*', file[i], 1)
|
|
|
|
if len(tmp) == 2:
|
|
|
|
conf_peer_data['Interface'][tmp[0]] = tmp[1]
|
2021-04-03 20:06:21 +02:00
|
|
|
conf_peers = file[peers_start:]
|
|
|
|
peer = -1
|
|
|
|
for i in conf_peers:
|
2022-01-13 01:53:36 +01:00
|
|
|
if not regex_match("#(.*)", i) and not regex_match(";(.*)", i):
|
2021-05-14 00:00:40 +02:00
|
|
|
if i == "[Peer]":
|
|
|
|
peer += 1
|
|
|
|
conf_peer_data["Peers"].append({})
|
2021-07-02 19:23:04 +02:00
|
|
|
elif peer > -1:
|
2021-05-14 00:00:40 +02:00
|
|
|
if len(i) > 0:
|
2022-01-19 15:25:27 +01:00
|
|
|
tmp = re.split(r'\s*=\s*', i, 1)
|
2021-05-14 00:00:40 +02:00
|
|
|
if len(tmp) == 2:
|
|
|
|
conf_peer_data["Peers"][peer][tmp[0]] = tmp[1]
|
2021-07-02 19:23:04 +02:00
|
|
|
|
2021-12-29 20:57:44 +01:00
|
|
|
f.close()
|
2021-04-03 20:06:21 +02:00
|
|
|
# Read Configuration File End
|
2021-04-09 06:07:37 +02:00
|
|
|
return conf_peer_data
|
2021-04-03 20:06:21 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
def get_latest_handshake(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get the latest handshake from all peers of a configuration
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: str
|
|
|
|
"""
|
|
|
|
|
2021-07-02 19:23:04 +02:00
|
|
|
# Get latest handshakes
|
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
data_usage = subprocess.check_output(f"wg show {config_name} latest-handshakes",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2021-07-02 19:23:04 +02:00
|
|
|
return "stopped"
|
|
|
|
data_usage = data_usage.decode("UTF-8").split()
|
|
|
|
count = 0
|
|
|
|
now = datetime.now()
|
2021-12-28 20:53:51 +01:00
|
|
|
time_delta = timedelta(minutes=2)
|
|
|
|
for _ in range(int(len(data_usage) / 2)):
|
2021-07-02 19:23:04 +02:00
|
|
|
minus = now - datetime.fromtimestamp(int(data_usage[count + 1]))
|
2021-12-28 20:53:51 +01:00
|
|
|
if minus < time_delta:
|
2021-07-02 19:23:04 +02:00
|
|
|
status = "running"
|
|
|
|
else:
|
|
|
|
status = "stopped"
|
|
|
|
if int(data_usage[count + 1]) > 0:
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("UPDATE %s SET latest_handshake = '%s', status = '%s' WHERE id='%s'"
|
2022-01-19 15:18:03 +01:00
|
|
|
% (config_name, str(minus).split(".", maxsplit=1)[0], status, data_usage[count]))
|
2021-07-02 19:23:04 +02:00
|
|
|
else:
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("UPDATE %s SET latest_handshake = '(None)', status = '%s' WHERE id='%s'"
|
2022-01-19 15:18:03 +01:00
|
|
|
% (config_name, status, data_usage[count]))
|
2021-07-02 19:23:04 +02:00
|
|
|
count += 2
|
2021-12-28 20:53:51 +01:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
def get_transfer(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get transfer from all peers of a configuration
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: str
|
|
|
|
"""
|
2021-04-03 20:06:21 +02:00
|
|
|
# Get transfer
|
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
data_usage = subprocess.check_output(f"wg show {config_name} transfer",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2021-04-03 20:06:21 +02:00
|
|
|
return "stopped"
|
2021-12-29 22:15:00 +01:00
|
|
|
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)):
|
2022-01-02 20:44:27 +01:00
|
|
|
cur_i = g.cur.execute(
|
|
|
|
"SELECT total_receive, total_sent, cumu_receive, cumu_sent, status FROM %s WHERE id='%s'"
|
|
|
|
% (config_name, data_usage[i][0])).fetchall()
|
2021-12-29 20:57:44 +01:00
|
|
|
if len(cur_i) > 0:
|
2022-01-02 20:44:27 +01:00
|
|
|
total_sent = cur_i[0][1]
|
|
|
|
total_receive = cur_i[0][0]
|
2021-12-29 22:15:00 +01:00
|
|
|
cur_total_sent = round(int(data_usage[i][2]) / (1024 ** 3), 4)
|
|
|
|
cur_total_receive = round(int(data_usage[i][1]) / (1024 ** 3), 4)
|
2022-01-02 20:44:27 +01:00
|
|
|
if cur_i[0][4] == "running":
|
2021-12-29 20:57:44 +01:00
|
|
|
if total_sent <= cur_total_sent and total_receive <= cur_total_receive:
|
|
|
|
total_sent = cur_total_sent
|
|
|
|
total_receive = cur_total_receive
|
|
|
|
else:
|
2022-01-13 01:53:36 +01:00
|
|
|
cumulative_receive = cur_i[0][2] + total_receive
|
|
|
|
cumulative_sent = cur_i[0][3] + total_sent
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("UPDATE %s SET cumu_receive = %f, cumu_sent = %f, cumu_data = %f WHERE id = '%s'" %
|
2022-01-13 01:53:36 +01:00
|
|
|
(config_name, round(cumulative_receive, 4), round(cumulative_sent, 4),
|
|
|
|
round(cumulative_sent + cumulative_receive, 4), data_usage[i][0]))
|
2021-12-29 20:57:44 +01:00
|
|
|
total_sent = 0
|
|
|
|
total_receive = 0
|
2022-01-02 20:44:27 +01:00
|
|
|
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]))
|
2021-12-28 20:53:51 +01:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
def get_endpoint(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get endpoint from all peers of a configuration
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: str
|
|
|
|
"""
|
2021-04-03 20:06:21 +02:00
|
|
|
# Get endpoint
|
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
data_usage = subprocess.check_output(f"wg show {config_name} endpoints",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2021-04-03 20:06:21 +02:00
|
|
|
return "stopped"
|
2020-10-18 07:10:13 +02:00
|
|
|
data_usage = data_usage.decode("UTF-8").split()
|
|
|
|
count = 0
|
2021-12-28 20:53:51 +01:00
|
|
|
for _ in range(int(len(data_usage) / 2)):
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("UPDATE " + config_name + " SET endpoint = '%s' WHERE id = '%s'"
|
|
|
|
% (data_usage[count + 1], data_usage[count]))
|
2020-10-18 07:10:13 +02:00
|
|
|
count += 2
|
2021-12-28 20:53:51 +01:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-02-11 15:35:58 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
def get_allowed_ip(conf_peer_data, config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get allowed ips from all peers of a configuration
|
|
|
|
@param conf_peer_data: Configuration peer data
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: None
|
|
|
|
"""
|
2021-04-03 20:06:21 +02:00
|
|
|
# Get allowed ip
|
|
|
|
for i in conf_peer_data["Peers"]:
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("UPDATE " + config_name + " SET allowed_ip = '%s' WHERE id = '%s'"
|
|
|
|
% (i.get('AllowedIPs', '(None)'), i["PublicKey"]))
|
2021-07-02 19:23:04 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
def get_all_peers_data(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Look for new peers from WireGuard
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: None
|
|
|
|
"""
|
2021-07-02 19:23:04 +02:00
|
|
|
conf_peer_data = read_conf_file(config_name)
|
2021-09-08 18:39:25 +02:00
|
|
|
config = get_dashboard_conf()
|
2022-01-13 01:53:36 +01:00
|
|
|
failed_index = []
|
|
|
|
for i in range(len(conf_peer_data['Peers'])):
|
|
|
|
if "PublicKey" in conf_peer_data['Peers'][i].keys():
|
|
|
|
result = g.cur.execute(
|
|
|
|
"SELECT * FROM %s WHERE id='%s'" % (config_name, conf_peer_data['Peers'][i]["PublicKey"])).fetchall()
|
|
|
|
if len(result) == 0:
|
|
|
|
new_data = {
|
|
|
|
"id": conf_peer_data['Peers'][i]['PublicKey'],
|
|
|
|
"private_key": "",
|
|
|
|
"DNS": config.get("Peers", "peer_global_DNS"),
|
|
|
|
"endpoint_allowed_ip": config.get("Peers", "peer_endpoint_allowed_ip"),
|
|
|
|
"name": "",
|
|
|
|
"total_receive": 0,
|
|
|
|
"total_sent": 0,
|
|
|
|
"total_data": 0,
|
|
|
|
"endpoint": "N/A",
|
|
|
|
"status": "stopped",
|
|
|
|
"latest_handshake": "N/A",
|
|
|
|
"allowed_ip": "N/A",
|
|
|
|
"cumu_receive": 0,
|
|
|
|
"cumu_sent": 0,
|
|
|
|
"cumu_data": 0,
|
|
|
|
"traffic": [],
|
|
|
|
"mtu": config.get("Peers", "peer_mtu"),
|
|
|
|
"keepalive": config.get("Peers", "peer_keep_alive"),
|
|
|
|
"remote_endpoint": config.get("Peers", "remote_endpoint"),
|
|
|
|
"preshared_key": ""
|
|
|
|
}
|
|
|
|
if "PresharedKey" in conf_peer_data['Peers'][i].keys():
|
|
|
|
new_data["preshared_key"] = conf_peer_data['Peers'][i]["PresharedKey"]
|
|
|
|
sql = f"""
|
|
|
|
INSERT INTO {config_name}
|
|
|
|
VALUES (:id, :private_key, :DNS, :endpoint_allowed_ip, :name, :total_receive, :total_sent,
|
|
|
|
:total_data, :endpoint, :status, :latest_handshake, :allowed_ip, :cumu_receive, :cumu_sent,
|
|
|
|
:cumu_data, :mtu, :keepalive, :remote_endpoint, :preshared_key);
|
|
|
|
"""
|
|
|
|
g.cur.execute(sql, new_data)
|
2021-08-05 06:45:15 +02:00
|
|
|
else:
|
2022-01-13 01:53:36 +01:00
|
|
|
print("Trying to parse a peer doesn't have public key...")
|
|
|
|
failed_index.append(i)
|
|
|
|
for i in failed_index:
|
|
|
|
conf_peer_data['Peers'].pop(i)
|
2021-08-25 03:04:01 +02:00
|
|
|
# Remove peers no longer exist in WireGuard configuration file
|
2022-01-02 20:44:27 +01:00
|
|
|
db_key = list(map(lambda a: a[0], g.cur.execute("SELECT id FROM %s" % config_name)))
|
2021-08-25 03:04:01 +02:00
|
|
|
wg_key = list(map(lambda a: a['PublicKey'], conf_peer_data['Peers']))
|
|
|
|
for i in db_key:
|
|
|
|
if i not in wg_key:
|
2022-01-02 20:44:27 +01:00
|
|
|
g.cur.execute("DELETE FROM %s WHERE id = '%s'" % (config_name, i))
|
|
|
|
get_latest_handshake(config_name)
|
|
|
|
get_transfer(config_name)
|
|
|
|
get_endpoint(config_name)
|
|
|
|
get_allowed_ip(conf_peer_data, config_name)
|
2021-04-03 20:06:21 +02:00
|
|
|
|
2022-03-22 03:33:19 +01:00
|
|
|
def getLockAccessPeers(config_name):
|
|
|
|
col = g.cur.execute(f"PRAGMA table_info({config_name}_restrict_access)").fetchall()
|
|
|
|
col = [a[1] for a in col]
|
|
|
|
data = g.cur.execute(f"SELECT * FROM {config_name}_restrict_access").fetchall()
|
|
|
|
result = [{col[i]: data[k][i] for i in range(len(col))} for k in range(len(data))]
|
|
|
|
return result
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
def get_peers(config_name, search, sort_t):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get all peers.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@param search: Search string
|
|
|
|
@type search: str
|
|
|
|
@param sort_t: Sorting tag
|
|
|
|
@type sort_t: str
|
|
|
|
@return: list
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
tic = time.perf_counter()
|
|
|
|
col = g.cur.execute("PRAGMA table_info(" + config_name + ")").fetchall()
|
|
|
|
col = [a[1] for a in col]
|
2021-08-14 23:13:16 +02:00
|
|
|
get_all_peers_data(config_name)
|
|
|
|
if len(search) == 0:
|
2022-01-02 20:44:27 +01:00
|
|
|
data = g.cur.execute("SELECT * FROM " + config_name).fetchall()
|
|
|
|
result = [{col[i]: data[k][i] for i in range(len(col))} for k in range(len(data))]
|
2021-08-14 23:13:16 +02:00
|
|
|
else:
|
2022-01-04 22:32:23 +01:00
|
|
|
sql = "SELECT * FROM " + config_name + " WHERE name LIKE '%" + search + "%'"
|
2022-01-02 20:44:27 +01:00
|
|
|
data = g.cur.execute(sql).fetchall()
|
|
|
|
result = [{col[i]: data[k][i] for i in range(len(col))} for k in range(len(data))]
|
2021-12-24 03:26:24 +01:00
|
|
|
if sort_t == "allowed_ip":
|
2022-01-13 01:53:36 +01:00
|
|
|
result = sorted(result, key=lambda d: ipaddress.ip_network(
|
|
|
|
"0.0.0.0/0" if d[sort_t].split(",")[0] == "(None)" else d[sort_t].split(",")[0]))
|
2021-12-24 03:26:24 +01:00
|
|
|
else:
|
|
|
|
result = sorted(result, key=lambda d: d[sort_t])
|
2022-01-02 20:44:27 +01:00
|
|
|
toc = time.perf_counter()
|
|
|
|
print(f"Finish fetching peers in {toc - tic:0.4f} seconds")
|
2021-04-03 02:48:00 +02:00
|
|
|
return result
|
2020-10-18 07:10:13 +02:00
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
def get_conf_pub_key(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get public key for configuration.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return public key or empty string
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 20:57:44 +01:00
|
|
|
try:
|
|
|
|
conf = configparser.ConfigParser(strict=False)
|
2022-01-01 01:20:30 +01:00
|
|
|
conf.read(WG_CONF_PATH + "/" + config_name + ".conf")
|
2021-12-29 20:57:44 +01:00
|
|
|
pri = conf.get("Interface", "PrivateKey")
|
2022-01-13 15:37:23 +01:00
|
|
|
pub = subprocess.check_output(f"echo '{pri}' | wg pubkey", shell=True, stderr=subprocess.STDOUT)
|
2021-12-29 20:57:44 +01:00
|
|
|
conf.clear()
|
|
|
|
return pub.decode().strip("\n")
|
2022-01-13 01:53:36 +01:00
|
|
|
except configparser.NoSectionError:
|
2021-12-29 20:57:44 +01:00
|
|
|
return ""
|
2020-10-18 07:10:13 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
def get_conf_listen_port(config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Get listen port number.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return number of port or empty string
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
conf = configparser.ConfigParser(strict=False)
|
2022-01-01 01:20:30 +01:00
|
|
|
conf.read(WG_CONF_PATH + "/" + config_name + ".conf")
|
2021-09-08 18:39:25 +02:00
|
|
|
port = ""
|
|
|
|
try:
|
|
|
|
port = conf.get("Interface", "ListenPort")
|
2021-12-26 00:26:39 +01:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
2021-09-08 18:39:25 +02:00
|
|
|
if get_conf_status(config_name) == "running":
|
2022-01-13 15:37:23 +01:00
|
|
|
port = subprocess.check_output(f"wg show {config_name} listen-port",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-09-08 18:39:25 +02:00
|
|
|
port = port.decode("UTF-8")
|
2021-05-04 07:32:34 +02:00
|
|
|
conf.clear()
|
|
|
|
return port
|
2021-04-03 20:06:21 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
def get_conf_total_data(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Get configuration's total amount of data
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: list
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
data = g.cur.execute("SELECT total_sent, total_receive, cumu_sent, cumu_receive FROM " + config_name)
|
2020-10-18 07:10:13 +02:00
|
|
|
upload_total = 0
|
|
|
|
download_total = 0
|
2022-01-02 20:44:27 +01:00
|
|
|
for i in data.fetchall():
|
|
|
|
upload_total += i[0]
|
|
|
|
download_total += i[1]
|
|
|
|
upload_total += i[2]
|
|
|
|
download_total += i[3]
|
2021-04-09 06:07:37 +02:00
|
|
|
total = round(upload_total + download_total, 4)
|
2021-05-14 00:00:40 +02:00
|
|
|
upload_total = round(upload_total, 4)
|
|
|
|
download_total = round(download_total, 4)
|
2020-10-18 07:10:13 +02:00
|
|
|
return [total, upload_total, download_total]
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
def get_conf_status(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Check if the configuration is running or not
|
|
|
|
@param config_name:
|
|
|
|
@return: Return a string indicate the running status
|
|
|
|
"""
|
2021-04-09 06:07:37 +02:00
|
|
|
ifconfig = dict(ifcfg.interfaces().items())
|
2021-12-28 20:53:51 +01:00
|
|
|
return "running" if config_name in ifconfig.keys() else "stopped"
|
2020-10-18 07:10:13 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
def get_conf_list():
|
2022-01-02 14:35:39 +01:00
|
|
|
"""Get all wireguard interfaces with status.
|
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@return: Return a list of dicts with interfaces and its statuses
|
|
|
|
@rtype: list
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2020-10-18 07:10:13 +02:00
|
|
|
conf = []
|
2022-01-01 01:20:30 +01:00
|
|
|
for i in os.listdir(WG_CONF_PATH):
|
2021-09-08 18:39:25 +02:00
|
|
|
if regex_match("^(.{1,}).(conf)$", i):
|
2021-08-05 06:45:15 +02:00
|
|
|
i = i.replace('.conf', '')
|
2022-01-04 22:32:23 +01:00
|
|
|
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,
|
2022-03-22 03:33:19 +01:00
|
|
|
keepalive INT NULL, remote_endpoint VARCHAR NULL, preshared_key VARCHAR NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
g.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,
|
2022-01-04 22:32:23 +01:00
|
|
|
PRIMARY KEY (id)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
g.cur.execute(create_table)
|
2021-08-05 06:45:15 +02:00
|
|
|
temp = {"conf": i, "status": get_conf_status(i), "public_key": get_conf_pub_key(i)}
|
|
|
|
if temp['status'] == "running":
|
|
|
|
temp['checked'] = 'checked'
|
|
|
|
else:
|
|
|
|
temp['checked'] = ""
|
|
|
|
conf.append(temp)
|
2021-07-02 19:23:04 +02:00
|
|
|
if len(conf) > 0:
|
|
|
|
conf = sorted(conf, key=itemgetter('conf'))
|
2020-10-18 07:10:13 +02:00
|
|
|
return conf
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
def gen_public_key(private_key):
|
2022-01-02 14:35:39 +01:00
|
|
|
"""Generate the public key.
|
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@param private_key: Private key
|
|
|
|
@type private_key: str
|
|
|
|
@return: Return dict with public key or error message
|
|
|
|
@rtype: dict
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-28 20:53:51 +01:00
|
|
|
with open('private_key.txt', 'w', encoding='utf-8') as file_object:
|
2021-12-26 11:04:39 +01:00
|
|
|
file_object.write(private_key)
|
2021-08-06 05:15:50 +02:00
|
|
|
try:
|
2022-01-13 01:53:36 +01:00
|
|
|
subprocess.check_output("wg pubkey < private_key.txt > public_key.txt", shell=True)
|
2021-12-28 20:53:51 +01:00
|
|
|
with open('public_key.txt', encoding='utf-8') as file_object:
|
2021-12-26 11:04:39 +01:00
|
|
|
public_key = file_object.readline().strip()
|
2021-08-06 05:15:50 +02:00
|
|
|
os.remove('private_key.txt')
|
|
|
|
os.remove('public_key.txt')
|
2021-08-14 23:13:16 +02:00
|
|
|
return {"status": 'success', "msg": "", "data": public_key}
|
2021-12-26 00:26:39 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2021-08-06 05:15:50 +02:00
|
|
|
os.remove('private_key.txt')
|
2021-08-14 23:13:16 +02:00
|
|
|
return {"status": 'failed', "msg": "Key is not the correct length or format", "data": ""}
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-12-28 20:53:51 +01:00
|
|
|
def f_check_key_match(private_key, public_key, config_name):
|
2022-01-13 15:37:23 +01:00
|
|
|
"""
|
|
|
|
Check if private key and public key match
|
|
|
|
@param private_key: Private key
|
|
|
|
@type private_key: str
|
|
|
|
@param public_key: Public key
|
|
|
|
@type public_key: str
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return dictionary with status
|
|
|
|
@rtype: dict
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
result = gen_public_key(private_key)
|
2021-08-06 05:15:50 +02:00
|
|
|
if result['status'] == 'failed':
|
|
|
|
return result
|
|
|
|
else:
|
2022-01-02 20:44:27 +01:00
|
|
|
sql = "SELECT * FROM " + config_name + " WHERE id = ?"
|
|
|
|
match = g.cur.execute(sql, (result['data'],)).fetchall()
|
2021-08-06 05:15:50 +02:00
|
|
|
if len(match) != 1 or result['data'] != public_key:
|
|
|
|
return {'status': 'failed', 'msg': 'Please check your private key, it does not match with the public key.'}
|
|
|
|
else:
|
|
|
|
return {'status': 'success'}
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
|
2021-12-28 20:53:51 +01:00
|
|
|
def check_repeat_allowed_ip(public_key, ip, config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Check if there are repeated IPs
|
|
|
|
@param public_key: Public key of the peer
|
|
|
|
@param ip: IP of the peer
|
|
|
|
@param config_name: configuration name
|
|
|
|
@return: a JSON object
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
peer = g.cur.execute("SELECT COUNT(*) FROM " + config_name + " WHERE id = ?", (public_key,)).fetchone()
|
|
|
|
if peer[0] != 1:
|
2021-08-06 05:15:50 +02:00
|
|
|
return {'status': 'failed', 'msg': 'Peer does not exist'}
|
|
|
|
else:
|
2022-01-02 20:44:27 +01:00
|
|
|
existed_ip = g.cur.execute("SELECT COUNT(*) FROM " +
|
2022-01-13 01:53:36 +01:00
|
|
|
config_name + " WHERE id != ? AND allowed_ip LIKE '" + ip + "/%'", (public_key,)) \
|
2022-01-06 21:17:43 +01:00
|
|
|
.fetchone()
|
2022-01-02 20:44:27 +01:00
|
|
|
if existed_ip[0] != 0:
|
2021-08-14 23:13:16 +02:00
|
|
|
return {'status': 'failed', 'msg': "Allowed IP already taken by another peer."}
|
2021-08-06 05:15:50 +02:00
|
|
|
else:
|
2021-08-14 23:13:16 +02:00
|
|
|
return {'status': 'success'}
|
|
|
|
|
2021-08-15 05:30:05 +02:00
|
|
|
|
2022-01-06 21:17:43 +01:00
|
|
|
def f_available_ips(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Get a list of available IPs
|
|
|
|
@param config_name: Configuration Name
|
|
|
|
@return: list
|
|
|
|
"""
|
2022-01-06 21:17:43 +01:00
|
|
|
config_interface = read_conf_file_interface(config_name)
|
|
|
|
if "Address" in config_interface:
|
|
|
|
existed = []
|
|
|
|
conf_address = config_interface['Address']
|
|
|
|
address = conf_address.split(',')
|
|
|
|
for i in address:
|
|
|
|
add, sub = i.split("/")
|
|
|
|
existed.append(ipaddress.ip_address(add))
|
|
|
|
peers = g.cur.execute("SELECT allowed_ip FROM " + config_name).fetchall()
|
|
|
|
for i in peers:
|
|
|
|
add = i[0].split(",")
|
|
|
|
for k in add:
|
|
|
|
a, s = k.split("/")
|
2022-01-13 01:53:36 +01:00
|
|
|
existed.append(ipaddress.ip_address(a.strip()))
|
2022-01-06 21:17:43 +01:00
|
|
|
available = list(ipaddress.ip_network(address[0], False).hosts())
|
|
|
|
for i in existed:
|
|
|
|
try:
|
|
|
|
available.remove(i)
|
2022-01-13 01:53:36 +01:00
|
|
|
except ValueError:
|
2022-01-06 21:17:43 +01:00
|
|
|
pass
|
|
|
|
available = [str(i) for i in available]
|
|
|
|
return available
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
"""
|
|
|
|
Flask Functions
|
|
|
|
"""
|
2021-08-14 23:13:16 +02:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
@app.teardown_request
|
|
|
|
def close_DB(exception):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Commit to the database for every request
|
|
|
|
@param exception: Exception
|
|
|
|
@return: None
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
if hasattr(g, 'db'):
|
|
|
|
g.db.commit()
|
|
|
|
g.db.close()
|
|
|
|
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.before_request
|
|
|
|
def auth_req():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Action before every request
|
|
|
|
@return: Redirect
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
if getattr(g, 'db', None) is None:
|
|
|
|
g.db = connect_db()
|
|
|
|
g.cur = g.db.cursor()
|
2021-12-29 21:29:29 +01:00
|
|
|
conf = get_dashboard_conf()
|
2021-05-04 07:32:34 +02:00
|
|
|
req = conf.get("Server", "auth_req")
|
2021-12-28 20:53:51 +01:00
|
|
|
session['update'] = UPDATE
|
|
|
|
session['dashboard_version'] = DASHBOARD_VERSION
|
2021-05-04 07:32:34 +02:00
|
|
|
if req == "true":
|
|
|
|
if '/static/' not in request.path and \
|
|
|
|
request.endpoint != "signin" and \
|
|
|
|
request.endpoint != "signout" and \
|
|
|
|
request.endpoint != "auth" and \
|
|
|
|
"username" not in session:
|
2022-01-13 01:53:36 +01:00
|
|
|
print("User not signed in - Attempted access: " + str(request.endpoint))
|
2021-08-06 05:15:50 +02:00
|
|
|
if request.endpoint != "index":
|
|
|
|
session['message'] = "You need to sign in first!"
|
|
|
|
else:
|
|
|
|
session['message'] = ""
|
2021-12-29 21:29:29 +01:00
|
|
|
conf.clear()
|
2022-01-18 16:42:23 +01:00
|
|
|
return redirect("/signin?redirect=" + str(request.url))
|
2021-05-04 07:32:34 +02:00
|
|
|
else:
|
2021-05-14 00:00:40 +02:00
|
|
|
if request.endpoint in ['signin', 'signout', 'auth', 'settings', 'update_acct', 'update_pwd',
|
|
|
|
'update_app_ip_port', 'update_wg_conf_path']:
|
2021-12-29 21:29:29 +01:00
|
|
|
conf.clear()
|
2021-05-04 07:32:34 +02:00
|
|
|
return redirect(url_for("index"))
|
2021-12-29 21:29:29 +01:00
|
|
|
conf.clear()
|
2021-12-28 20:53:51 +01:00
|
|
|
return None
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
"""
|
|
|
|
Sign In / Sign Out
|
|
|
|
"""
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/signin', methods=['GET'])
|
|
|
|
def signin():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Sign in request
|
|
|
|
@return: template
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
message = ""
|
|
|
|
if "message" in session:
|
|
|
|
message = session['message']
|
|
|
|
session.pop("message")
|
2022-02-28 19:29:17 +01:00
|
|
|
return render_template('signin.html', message=message, version=DASHBOARD_VERSION)
|
2021-05-04 07:32:34 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
# Sign Out
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/signout', methods=['GET'])
|
|
|
|
def signout():
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
2022-01-18 16:42:23 +01:00
|
|
|
Sign out request
|
|
|
|
@return: redirect back to sign in
|
|
|
|
"""
|
2021-05-04 07:32:34 +02:00
|
|
|
if "username" in session:
|
|
|
|
session.pop("username")
|
2022-01-18 16:42:23 +01:00
|
|
|
return redirect(url_for('signin'))
|
2021-05-04 07:32:34 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
@app.route('/auth', methods=['POST'])
|
|
|
|
def auth():
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
2022-01-18 16:42:23 +01:00
|
|
|
Authentication request
|
|
|
|
@return: json object indicating verifying
|
|
|
|
"""
|
|
|
|
data = request.get_json()
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2022-01-18 16:42:23 +01:00
|
|
|
password = hashlib.sha256(data['password'].encode())
|
2021-12-26 00:26:39 +01:00
|
|
|
if password.hexdigest() == config["Account"]["password"] \
|
2022-01-18 16:42:23 +01:00
|
|
|
and data['username'] == config["Account"]["username"]:
|
|
|
|
session['username'] = data['username']
|
2021-09-08 18:39:25 +02:00
|
|
|
config.clear()
|
2022-01-18 16:42:23 +01:00
|
|
|
return jsonify({"status": True, "msg": ""})
|
2021-12-28 20:53:51 +01:00
|
|
|
config.clear()
|
2022-01-18 16:42:23 +01:00
|
|
|
return jsonify({"status": False, "msg": "Username or Password is incorrect."})
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Index Page
|
|
|
|
"""
|
2021-09-08 18:39:25 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Index page related
|
|
|
|
@return: Template
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2022-01-13 15:37:23 +01:00
|
|
|
msg = ""
|
|
|
|
if "switch_msg" in session:
|
|
|
|
msg = session["switch_msg"]
|
|
|
|
session.pop("switch_msg")
|
|
|
|
|
|
|
|
return render_template('index.html', conf=get_conf_list(), msg=msg)
|
2021-05-04 07:32:34 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Setting Page
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/settings', methods=['GET'])
|
|
|
|
def settings():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Settings page related
|
|
|
|
@return: Template
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2021-05-04 07:32:34 +02:00
|
|
|
message = ""
|
|
|
|
status = ""
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-05-04 07:32:34 +02:00
|
|
|
if "message" in session and "message_status" in session:
|
|
|
|
message = session['message']
|
|
|
|
status = session['message_status']
|
|
|
|
session.pop("message")
|
|
|
|
session.pop("message_status")
|
|
|
|
required_auth = config.get("Server", "auth_req")
|
2021-05-14 00:00:40 +02:00
|
|
|
return render_template('settings.html', conf=get_conf_list(), message=message, status=status,
|
|
|
|
app_ip=config.get("Server", "app_ip"), app_port=config.get("Server", "app_port"),
|
2021-08-14 23:13:16 +02:00
|
|
|
required_auth=required_auth, wg_conf_path=config.get("Server", "wg_conf_path"),
|
2021-08-15 05:30:05 +02:00
|
|
|
peer_global_DNS=config.get("Peers", "peer_global_DNS"),
|
2021-09-08 18:39:25 +02:00
|
|
|
peer_endpoint_allowed_ip=config.get("Peers", "peer_endpoint_allowed_ip"),
|
|
|
|
peer_mtu=config.get("Peers", "peer_mtu"),
|
2021-12-26 00:26:39 +01:00
|
|
|
peer_keepalive=config.get("Peers", "peer_keep_alive"),
|
|
|
|
peer_remote_endpoint=config.get("Peers", "remote_endpoint"))
|
|
|
|
|
2021-05-14 00:00:40 +02:00
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/update_acct', methods=['POST'])
|
|
|
|
def update_acct():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Change dashboard username
|
|
|
|
@return: Redirect
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
if len(request.form['username']) == 0:
|
|
|
|
session['message'] = "Username cannot be empty."
|
|
|
|
session['message_status'] = "danger"
|
|
|
|
return redirect(url_for("settings"))
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-05-04 07:32:34 +02:00
|
|
|
config.set("Account", "username", request.form['username'])
|
|
|
|
try:
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-05-04 07:32:34 +02:00
|
|
|
session['message'] = "Username update successfully!"
|
|
|
|
session['message_status'] = "success"
|
|
|
|
session['username'] = request.form['username']
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
except Exception:
|
|
|
|
session['message'] = "Username update failed."
|
|
|
|
session['message_status'] = "danger"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
# Update peer default setting
|
2021-08-14 23:13:16 +02:00
|
|
|
@app.route('/update_peer_default_config', methods=['POST'])
|
|
|
|
def update_peer_default_config():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Update new peers default setting
|
|
|
|
@return: None
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-09-08 18:39:25 +02:00
|
|
|
if len(request.form['peer_endpoint_allowed_ip']) == 0 or \
|
|
|
|
len(request.form['peer_global_DNS']) == 0 or \
|
|
|
|
len(request.form['peer_remote_endpoint']) == 0:
|
|
|
|
session['message'] = "Please fill in all required boxes."
|
2021-08-14 23:13:16 +02:00
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return redirect(url_for("settings"))
|
|
|
|
# Check DNS Format
|
2021-12-26 00:26:39 +01:00
|
|
|
dns_addresses = request.form['peer_global_DNS']
|
|
|
|
if not check_DNS(dns_addresses):
|
2021-09-08 18:39:25 +02:00
|
|
|
session['message'] = "Peer DNS Format Incorrect."
|
2021-08-14 23:13:16 +02:00
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return redirect(url_for("settings"))
|
2021-12-26 00:26:39 +01:00
|
|
|
dns_addresses = dns_addresses.replace(" ", "").split(',')
|
|
|
|
dns_addresses = ",".join(dns_addresses)
|
2021-08-14 23:13:16 +02:00
|
|
|
# Check Endpoint Allowed IPs
|
|
|
|
ip = request.form['peer_endpoint_allowed_ip']
|
2021-09-08 18:39:25 +02:00
|
|
|
if not check_Allowed_IPs(ip):
|
2021-12-26 00:26:39 +01:00
|
|
|
session['message'] = "Peer Endpoint Allowed IPs Format Incorrect. " \
|
|
|
|
"Example: 192.168.1.1/32 or 192.168.1.1/32,192.168.1.2/32"
|
2021-09-08 18:39:25 +02:00
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-09-08 18:39:25 +02:00
|
|
|
return redirect(url_for("settings"))
|
|
|
|
# Check MTU Format
|
2021-12-26 11:04:39 +01:00
|
|
|
if not len(request.form['peer_mtu']) > 0 or not request.form['peer_mtu'].isdigit():
|
|
|
|
session['message'] = "MTU format is incorrect."
|
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-12-26 11:04:39 +01:00
|
|
|
return redirect(url_for("settings"))
|
2021-09-08 18:39:25 +02:00
|
|
|
# Check keepalive Format
|
2021-12-26 11:04:39 +01:00
|
|
|
if not len(request.form['peer_keep_alive']) > 0 or not request.form['peer_keep_alive'].isdigit():
|
|
|
|
session['message'] = "Persistent keepalive format is incorrect."
|
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-12-26 11:04:39 +01:00
|
|
|
return redirect(url_for("settings"))
|
2021-09-08 18:39:25 +02:00
|
|
|
# Check peer remote endpoint
|
|
|
|
if not check_remote_endpoint(request.form['peer_remote_endpoint']):
|
2021-12-26 00:26:39 +01:00
|
|
|
session['message'] = "Peer Remote Endpoint format is incorrect. It can only be a valid " \
|
|
|
|
"IP address or valid domain (without http:// or https://). "
|
2021-08-14 23:13:16 +02:00
|
|
|
session['message_status'] = "danger"
|
2021-12-29 21:29:29 +01:00
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return redirect(url_for("settings"))
|
2021-09-08 18:39:25 +02:00
|
|
|
config.set("Peers", "remote_endpoint", request.form['peer_remote_endpoint'])
|
|
|
|
config.set("Peers", "peer_keep_alive", request.form['peer_keep_alive'])
|
|
|
|
config.set("Peers", "peer_mtu", request.form['peer_mtu'])
|
|
|
|
config.set("Peers", "peer_endpoint_allowed_ip", ','.join(clean_IP_with_range(ip)))
|
2021-12-26 00:26:39 +01:00
|
|
|
config.set("Peers", "peer_global_DNS", dns_addresses)
|
2021-08-14 23:13:16 +02:00
|
|
|
try:
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
session['message'] = "Peer Default Settings update successfully!"
|
|
|
|
session['message_status'] = "success"
|
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return redirect(url_for("settings"))
|
|
|
|
except Exception:
|
2021-09-08 18:39:25 +02:00
|
|
|
session['message'] = "Peer Default Settings update failed."
|
2021-08-14 23:13:16 +02:00
|
|
|
session['message_status'] = "danger"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Update dashboard password
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/update_pwd', methods=['POST'])
|
|
|
|
def update_pwd():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Update dashboard password
|
|
|
|
@return: Redirect
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-05-04 07:32:34 +02:00
|
|
|
if hashlib.sha256(request.form['currentpass'].encode()).hexdigest() == config.get("Account", "password"):
|
2021-05-14 00:00:40 +02:00
|
|
|
if hashlib.sha256(request.form['newpass'].encode()).hexdigest() == hashlib.sha256(
|
|
|
|
request.form['repnewpass'].encode()).hexdigest():
|
2021-05-04 07:32:34 +02:00
|
|
|
config.set("Account", "password", hashlib.sha256(request.form['repnewpass'].encode()).hexdigest())
|
|
|
|
try:
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
session['message'] = "Password update successfully!"
|
|
|
|
session['message_status'] = "success"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
2021-05-04 07:32:34 +02:00
|
|
|
except Exception:
|
|
|
|
session['message'] = "Password update failed"
|
|
|
|
session['message_status'] = "danger"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
else:
|
|
|
|
session['message'] = "Your New Password does not match."
|
|
|
|
session['message_status'] = "danger"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
else:
|
|
|
|
session['message'] = "Your Password does not match."
|
|
|
|
session['message_status'] = "danger"
|
|
|
|
config.clear()
|
|
|
|
return redirect(url_for("settings"))
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-05-04 07:32:34 +02:00
|
|
|
@app.route('/update_app_ip_port', methods=['POST'])
|
|
|
|
def update_app_ip_port():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Update dashboard ip and port
|
|
|
|
@return: None
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-05-04 07:32:34 +02:00
|
|
|
config.set("Server", "app_ip", request.form['app_ip'])
|
|
|
|
config.set("Server", "app_port", request.form['app_port'])
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2022-01-18 18:16:10 +01:00
|
|
|
subprocess.Popen('bash wgd.sh restart', shell=True)
|
|
|
|
return ""
|
2021-05-04 07:32:34 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Update WireGuard configuration file path
|
2021-05-05 03:26:40 +02:00
|
|
|
@app.route('/update_wg_conf_path', methods=['POST'])
|
|
|
|
def update_wg_conf_path():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Update configuration path
|
|
|
|
@return: None
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-05-05 03:26:40 +02:00
|
|
|
config.set("Server", "wg_conf_path", request.form['wg_conf_path'])
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-05-05 03:26:40 +02:00
|
|
|
session['message'] = "WireGuard Configuration Path Update Successfully!"
|
|
|
|
session['message_status'] = "success"
|
2022-01-18 18:16:10 +01:00
|
|
|
subprocess.Popen('bash wgd.sh restart', shell=True)
|
2021-05-05 03:26:40 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-08-14 23:13:16 +02:00
|
|
|
@app.route('/update_dashboard_sort', methods=['POST'])
|
|
|
|
def update_dashbaord_sort():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Update configuration sorting
|
|
|
|
@return: Boolean
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-08-14 23:13:16 +02:00
|
|
|
data = request.get_json()
|
|
|
|
sort_tag = ['name', 'status', 'allowed_ip']
|
|
|
|
if data['sort'] in sort_tag:
|
|
|
|
config.set("Server", "dashboard_sort", data['sort'])
|
|
|
|
else:
|
|
|
|
config.set("Server", "dashboard_sort", 'status')
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return "true"
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Update configuration refresh interval
|
2021-05-14 00:00:40 +02:00
|
|
|
@app.route('/update_dashboard_refresh_interval', methods=['POST'])
|
|
|
|
def update_dashboard_refresh_interval():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Change the refresh time.
|
|
|
|
@return: Return text with result
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 18:17:44 +01:00
|
|
|
preset_interval = ["5000", "10000", "30000", "60000"]
|
|
|
|
if request.form["interval"] in preset_interval:
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-12-29 18:17:44 +01:00
|
|
|
config.set("Server", "dashboard_refresh_interval", str(request.form['interval']))
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-12-29 18:17:44 +01:00
|
|
|
return "true"
|
|
|
|
else:
|
|
|
|
return "false"
|
2021-05-14 00:00:40 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Configuration Page
|
2020-10-18 07:10:13 +02:00
|
|
|
@app.route('/configuration/<config_name>', methods=['GET'])
|
2021-12-28 20:53:51 +01:00
|
|
|
def configuration(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Show wireguard interface view.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Template
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-04-03 02:48:00 +02:00
|
|
|
conf_data = {
|
|
|
|
"name": config_name,
|
|
|
|
"status": get_conf_status(config_name),
|
|
|
|
"checked": ""
|
|
|
|
}
|
|
|
|
if conf_data['status'] == "stopped":
|
2021-05-04 07:32:34 +02:00
|
|
|
conf_data['checked'] = "nope"
|
2021-04-03 02:48:00 +02:00
|
|
|
else:
|
|
|
|
conf_data['checked'] = "checked"
|
2021-07-02 19:23:04 +02:00
|
|
|
config_list = get_conf_list()
|
|
|
|
if config_name not in [conf['conf'] for conf in config_list]:
|
|
|
|
return render_template('index.html', conf=get_conf_list())
|
2021-12-29 21:29:29 +01:00
|
|
|
|
|
|
|
refresh_interval = int(config.get("Server", "dashboard_refresh_interval"))
|
|
|
|
dns_address = config.get("Peers", "peer_global_DNS")
|
|
|
|
allowed_ip = config.get("Peers", "peer_endpoint_allowed_ip")
|
|
|
|
peer_mtu = config.get("Peers", "peer_MTU")
|
|
|
|
peer_keep_alive = config.get("Peers", "peer_keep_alive")
|
|
|
|
config.clear()
|
2021-08-14 23:13:16 +02:00
|
|
|
return render_template('configuration.html', conf=get_conf_list(), conf_data=conf_data,
|
2021-12-29 21:29:29 +01:00
|
|
|
dashboard_refresh_interval=refresh_interval,
|
|
|
|
DNS=dns_address,
|
|
|
|
endpoint_allowed_ip=allowed_ip,
|
2021-09-08 18:39:25 +02:00
|
|
|
title=config_name,
|
2021-12-29 21:29:29 +01:00
|
|
|
mtu=peer_mtu,
|
|
|
|
keep_alive=peer_keep_alive)
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-04-03 20:06:21 +02:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Get configuration details
|
2022-03-05 04:09:01 +01:00
|
|
|
# @socketio.on("get_config")
|
|
|
|
@app.route('/get_config/<config_name>', methods=['GET'])
|
|
|
|
def get_conf(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Get configuration setting of wireguard interface.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: TODO
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
2022-03-03 14:46:23 +01:00
|
|
|
result = {
|
|
|
|
"status": True,
|
|
|
|
"message": "",
|
|
|
|
"data": {}
|
|
|
|
}
|
|
|
|
if not session:
|
|
|
|
result["status"] = False
|
|
|
|
result["message"] = "Oops! <br> You're not signed in. Please refresh your page."
|
2022-03-05 04:09:01 +01:00
|
|
|
return jsonify(result)
|
2022-03-03 14:46:23 +01:00
|
|
|
|
2022-01-19 16:27:17 +01:00
|
|
|
if getattr(g, 'db', None) is None:
|
|
|
|
g.db = connect_db()
|
|
|
|
g.cur = g.db.cursor()
|
2021-08-15 05:30:05 +02:00
|
|
|
config_interface = read_conf_file_interface(config_name)
|
2022-03-03 14:46:23 +01:00
|
|
|
|
|
|
|
if config_interface != {}:
|
2022-03-05 04:09:01 +01:00
|
|
|
search = request.args.get('search')
|
2022-03-03 14:46:23 +01:00
|
|
|
if len(search) == 0:
|
|
|
|
search = ""
|
|
|
|
search = urllib.parse.unquote(search)
|
|
|
|
config = get_dashboard_conf()
|
|
|
|
sort = config.get("Server", "dashboard_sort")
|
|
|
|
peer_display_mode = config.get("Peers", "peer_display_mode")
|
|
|
|
wg_ip = config.get("Peers", "remote_endpoint")
|
|
|
|
if "Address" not in config_interface:
|
|
|
|
conf_address = "N/A"
|
|
|
|
else:
|
|
|
|
conf_address = config_interface['Address']
|
|
|
|
result['data'] = {
|
|
|
|
"peer_data": get_peers(config_name, search, sort),
|
|
|
|
"name": config_name,
|
|
|
|
"status": get_conf_status(config_name),
|
|
|
|
"total_data_usage": get_conf_total_data(config_name),
|
|
|
|
"public_key": get_conf_pub_key(config_name),
|
|
|
|
"listen_port": get_conf_listen_port(config_name),
|
|
|
|
"running_peer": get_conf_running_peer_number(config_name),
|
|
|
|
"conf_address": conf_address,
|
|
|
|
"wg_ip": wg_ip,
|
|
|
|
"sort_tag": sort,
|
|
|
|
"dashboard_refresh_interval": int(config.get("Server", "dashboard_refresh_interval")),
|
2022-03-22 03:33:19 +01:00
|
|
|
"peer_display_mode": peer_display_mode,
|
|
|
|
"lock_access_peers": getLockAccessPeers(config_name)
|
2022-03-03 14:46:23 +01:00
|
|
|
}
|
|
|
|
if result['data']['status'] == "stopped":
|
|
|
|
result['data']['checked'] = "nope"
|
|
|
|
else:
|
|
|
|
result['data']['checked'] = "checked"
|
|
|
|
config.clear()
|
2020-10-18 18:23:38 +02:00
|
|
|
else:
|
2022-03-03 14:46:23 +01:00
|
|
|
result['status'] = False
|
|
|
|
result['message'] = "I cannot find this configuration. <br> Please refresh and try again"
|
2022-03-05 04:09:01 +01:00
|
|
|
config.clear()
|
|
|
|
return jsonify(result)
|
2020-10-18 18:23:38 +02:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Turn on / off a configuration
|
2020-10-18 18:23:38 +02:00
|
|
|
@app.route('/switch/<config_name>', methods=['GET'])
|
|
|
|
def switch(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
On/off the wireguard interface.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: redirects
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2020-10-18 18:23:38 +02:00
|
|
|
status = get_conf_status(config_name)
|
|
|
|
if status == "running":
|
2021-04-03 20:06:21 +02:00
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
check = subprocess.check_output("wg-quick down " + config_name,
|
2022-01-17 02:35:24 +01:00
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2022-01-13 15:37:23 +01:00
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
session["switch_msg"] = exc.output.strip().decode("utf-8")
|
2022-03-22 03:33:19 +01:00
|
|
|
return jsonify({"status": False, "reason":"Can't stop peer"})
|
2020-10-18 18:23:38 +02:00
|
|
|
elif status == "stopped":
|
2021-04-03 20:06:21 +02:00
|
|
|
try:
|
2022-01-13 15:37:23 +01:00
|
|
|
subprocess.check_output("wg-quick up " + config_name,
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
session["switch_msg"] = exc.output.strip().decode("utf-8")
|
2022-03-22 03:33:19 +01:00
|
|
|
return jsonify({"status": False, "reason":"Can't turn on peer"})
|
|
|
|
return jsonify({"status": True, "reason":""})
|
2022-01-02 20:44:27 +01:00
|
|
|
|
2022-01-06 21:17:43 +01:00
|
|
|
@app.route('/add_peer_bulk/<config_name>', methods=['POST'])
|
|
|
|
def add_peer_bulk(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Add peers by bulk
|
|
|
|
@param config_name: Configuration Name
|
|
|
|
@return: String
|
|
|
|
"""
|
2022-01-06 21:17:43 +01:00
|
|
|
data = request.get_json()
|
|
|
|
keys = data['keys']
|
|
|
|
endpoint_allowed_ip = data['endpoint_allowed_ip']
|
|
|
|
dns_addresses = data['DNS']
|
|
|
|
enable_preshared_key = data["enable_preshared_key"]
|
|
|
|
amount = data['amount']
|
2022-01-13 01:53:36 +01:00
|
|
|
config_interface = read_conf_file_interface(config_name)
|
|
|
|
if "Address" not in config_interface:
|
|
|
|
return "Configuration must have an IP address."
|
2022-01-06 21:17:43 +01:00
|
|
|
if not amount.isdigit() or int(amount) < 1:
|
|
|
|
return "Amount must be integer larger than 0"
|
|
|
|
amount = int(amount)
|
|
|
|
if not check_DNS(dns_addresses):
|
|
|
|
return "DNS formate is incorrect. Example: 1.1.1.1"
|
|
|
|
if not check_Allowed_IPs(endpoint_allowed_ip):
|
|
|
|
return "Endpoint Allowed IPs format is incorrect."
|
|
|
|
if len(data['MTU']) == 0 or not data['MTU'].isdigit():
|
|
|
|
return "MTU format is not correct."
|
|
|
|
if len(data['keep_alive']) == 0 or not data['keep_alive'].isdigit():
|
|
|
|
return "Persistent Keepalive format is not correct."
|
|
|
|
ips = f_available_ips(config_name)
|
2022-01-13 01:53:36 +01:00
|
|
|
if amount > len(ips):
|
|
|
|
return f"Cannot create more than {len(ips)} peers."
|
2022-01-06 21:17:43 +01:00
|
|
|
wg_command = ["wg", "set", config_name]
|
|
|
|
sql_command = []
|
|
|
|
for i in range(amount):
|
|
|
|
keys[i]['name'] = f"{config_name}_{datetime.now().strftime('%m%d%Y%H%M%S')}_Peer_#_{(i + 1)}"
|
|
|
|
wg_command.append("peer")
|
|
|
|
wg_command.append(keys[i]['publicKey'])
|
|
|
|
keys[i]['allowed_ips'] = ips.pop(0)
|
|
|
|
if enable_preshared_key:
|
|
|
|
keys[i]['psk_file'] = f"{keys[i]['name']}.txt"
|
|
|
|
f = open(keys[i]['psk_file'], "w+")
|
|
|
|
f.write(keys[i]['presharedKey'])
|
|
|
|
f.close()
|
|
|
|
wg_command.append("preshared-key")
|
|
|
|
wg_command.append(keys[i]['psk_file'])
|
|
|
|
else:
|
|
|
|
keys[i]['psk_file'] = ""
|
|
|
|
wg_command.append("allowed-ips")
|
|
|
|
wg_command.append(keys[i]['allowed_ips'])
|
|
|
|
update = ["UPDATE ", config_name, " SET name = '", keys[i]['name'],
|
|
|
|
"', private_key = '", keys[i]['privateKey'], "', DNS = '", dns_addresses,
|
|
|
|
"', endpoint_allowed_ip = '", endpoint_allowed_ip, "' WHERE id = '", keys[i]['publicKey'], "'"]
|
|
|
|
sql_command.append(update)
|
|
|
|
try:
|
|
|
|
status = subprocess.check_output(" ".join(wg_command), shell=True, stderr=subprocess.STDOUT)
|
|
|
|
status = subprocess.check_output("wg-quick save " + config_name, shell=True, stderr=subprocess.STDOUT)
|
|
|
|
get_all_peers_data(config_name)
|
|
|
|
if enable_preshared_key:
|
|
|
|
for i in keys:
|
|
|
|
os.remove(i['psk_file'])
|
|
|
|
for i in range(len(sql_command)):
|
|
|
|
sql_command[i] = "".join(sql_command[i])
|
|
|
|
g.cur.executescript("; ".join(sql_command))
|
|
|
|
return "true"
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
return exc.output.strip()
|
|
|
|
|
|
|
|
|
2020-10-23 07:31:10 +02:00
|
|
|
@app.route('/add_peer/<config_name>', methods=['POST'])
|
|
|
|
def add_peer(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Add Peers
|
|
|
|
@param config_name: configuration name
|
|
|
|
@return: string
|
|
|
|
"""
|
2020-10-23 07:31:10 +02:00
|
|
|
data = request.get_json()
|
|
|
|
public_key = data['public_key']
|
|
|
|
allowed_ips = data['allowed_ips']
|
2021-08-14 23:13:16 +02:00
|
|
|
endpoint_allowed_ip = data['endpoint_allowed_ip']
|
2021-12-29 20:56:19 +01:00
|
|
|
dns_addresses = data['DNS']
|
2021-12-29 18:17:44 +01:00
|
|
|
enable_preshared_key = data["enable_preshared_key"]
|
2022-01-06 21:17:43 +01:00
|
|
|
preshared_key = data['preshared_key']
|
2020-10-23 07:31:10 +02:00
|
|
|
keys = get_conf_peer_key(config_name)
|
2021-12-26 00:26:39 +01:00
|
|
|
if len(public_key) == 0 or len(dns_addresses) == 0 or len(allowed_ips) == 0 or len(endpoint_allowed_ip) == 0:
|
2021-08-15 05:30:05 +02:00
|
|
|
return "Please fill in all required box."
|
2021-12-28 20:53:51 +01:00
|
|
|
if not isinstance(keys, list):
|
2021-08-14 23:13:16 +02:00
|
|
|
return config_name + " is not running."
|
2020-10-23 07:31:10 +02:00
|
|
|
if public_key in keys:
|
2021-08-05 06:45:15 +02:00
|
|
|
return "Public key already exist."
|
2022-01-04 22:32:23 +01:00
|
|
|
check_dup_ip = g.cur.execute(
|
2022-01-06 21:17:43 +01:00
|
|
|
"SELECT COUNT(*) FROM " + config_name + " WHERE allowed_ip LIKE '" + allowed_ips + "/%'", ) \
|
2022-01-04 22:32:23 +01:00
|
|
|
.fetchone()
|
2022-01-02 20:44:27 +01:00
|
|
|
if check_dup_ip[0] != 0:
|
2021-08-06 05:15:50 +02:00
|
|
|
return "Allowed IP already taken by another peer."
|
2021-12-26 00:26:39 +01:00
|
|
|
if not check_DNS(dns_addresses):
|
2021-08-14 23:13:16 +02:00
|
|
|
return "DNS formate is incorrect. Example: 1.1.1.1"
|
2021-09-08 18:39:25 +02:00
|
|
|
if not check_Allowed_IPs(endpoint_allowed_ip):
|
2021-08-14 23:13:16 +02:00
|
|
|
return "Endpoint Allowed IPs format is incorrect."
|
2021-12-26 11:04:39 +01:00
|
|
|
if len(data['MTU']) == 0 or not data['MTU'].isdigit():
|
2022-01-02 20:44:27 +01:00
|
|
|
return "MTU format is not correct."
|
2021-12-26 11:04:39 +01:00
|
|
|
if len(data['keep_alive']) == 0 or not data['keep_alive'].isdigit():
|
2022-01-02 20:44:27 +01:00
|
|
|
return "Persistent Keepalive format is not correct."
|
2021-09-08 18:39:25 +02:00
|
|
|
try:
|
2022-01-02 20:44:27 +01:00
|
|
|
if enable_preshared_key:
|
2022-01-06 21:17:43 +01:00
|
|
|
now = str(datetime.now().strftime("%m%d%Y%H%M%S"))
|
|
|
|
f_name = now + "_tmp_psk.txt"
|
|
|
|
f = open(f_name, "w+")
|
|
|
|
f.write(preshared_key)
|
|
|
|
f.close()
|
2022-01-02 20:44:27 +01:00
|
|
|
status = subprocess.check_output(
|
2022-01-06 21:17:43 +01:00
|
|
|
f"wg set {config_name} peer {public_key} allowed-ips {allowed_ips} preshared-key {f_name}",
|
2022-01-02 20:44:27 +01:00
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2022-01-06 21:17:43 +01:00
|
|
|
os.remove(f_name)
|
2022-01-02 20:44:27 +01:00
|
|
|
elif not enable_preshared_key:
|
2021-12-29 19:57:11 +01:00
|
|
|
status = subprocess.check_output(f"wg set {config_name} peer {public_key} allowed-ips {allowed_ips}",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-09-08 18:39:25 +02:00
|
|
|
status = subprocess.check_output("wg-quick save " + config_name, shell=True, stderr=subprocess.STDOUT)
|
|
|
|
get_all_peers_data(config_name)
|
2022-01-02 20:44:27 +01:00
|
|
|
sql = "UPDATE " + config_name + " SET name = ?, private_key = ?, DNS = ?, endpoint_allowed_ip = ? WHERE id = ?"
|
|
|
|
g.cur.execute(sql, (data['name'], data['private_key'], data['DNS'], endpoint_allowed_ip, public_key))
|
2021-09-08 18:39:25 +02:00
|
|
|
return "true"
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
return exc.output.strip()
|
2021-04-03 20:06:21 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2020-12-27 05:42:41 +01:00
|
|
|
@app.route('/remove_peer/<config_name>', methods=['POST'])
|
|
|
|
def remove_peer(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Remove peer.
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return result of action or recommendations
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-05-05 03:26:40 +02:00
|
|
|
if get_conf_status(config_name) == "stopped":
|
2021-05-14 00:00:40 +02:00
|
|
|
return "Your need to turn on " + config_name + " first."
|
2020-12-27 05:42:41 +01:00
|
|
|
data = request.get_json()
|
2022-01-08 21:26:17 +01:00
|
|
|
delete_keys = data['peer_ids']
|
2020-12-27 05:42:41 +01:00
|
|
|
keys = get_conf_peer_key(config_name)
|
2021-12-28 20:53:51 +01:00
|
|
|
if not isinstance(keys, list):
|
2021-08-14 23:13:16 +02:00
|
|
|
return config_name + " is not running."
|
2020-12-27 05:42:41 +01:00
|
|
|
else:
|
2022-03-22 03:33:19 +01:00
|
|
|
return deletePeers(config_name, delete_keys, g.cur, g.db)
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-02 20:44:27 +01:00
|
|
|
|
2021-08-06 05:15:50 +02:00
|
|
|
@app.route('/save_peer_setting/<config_name>', methods=['POST'])
|
|
|
|
def save_peer_setting(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Save peer configuration.
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return status of action and text with recommendations
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-04-03 02:48:00 +02:00
|
|
|
data = request.get_json()
|
|
|
|
id = data['id']
|
|
|
|
name = data['name']
|
2021-08-06 05:15:50 +02:00
|
|
|
private_key = data['private_key']
|
2021-12-26 00:26:39 +01:00
|
|
|
dns_addresses = data['DNS']
|
2021-08-06 05:15:50 +02:00
|
|
|
allowed_ip = data['allowed_ip']
|
2021-08-14 23:13:16 +02:00
|
|
|
endpoint_allowed_ip = data['endpoint_allowed_ip']
|
2021-12-29 18:17:44 +01:00
|
|
|
preshared_key = data['preshared_key']
|
2022-01-02 20:44:27 +01:00
|
|
|
check_peer_exist = g.cur.execute("SELECT COUNT(*) FROM " + config_name + " WHERE id = ?", (id,)).fetchone()
|
|
|
|
if check_peer_exist[0] == 1:
|
2021-12-28 20:53:51 +01:00
|
|
|
check_ip = check_repeat_allowed_ip(id, allowed_ip, config_name)
|
2021-09-08 18:39:25 +02:00
|
|
|
if not check_IP_with_range(endpoint_allowed_ip):
|
2021-08-16 21:26:15 +02:00
|
|
|
return jsonify({"status": "failed", "msg": "Endpoint Allowed IPs format is incorrect."})
|
2021-12-26 00:26:39 +01:00
|
|
|
if not check_DNS(dns_addresses):
|
2021-08-25 03:04:01 +02:00
|
|
|
return jsonify({"status": "failed", "msg": "DNS format is incorrect."})
|
2021-12-26 11:04:39 +01:00
|
|
|
if len(data['MTU']) == 0 or not data['MTU'].isdigit():
|
|
|
|
return jsonify({"status": "failed", "msg": "MTU format is not correct."})
|
|
|
|
if len(data['keep_alive']) == 0 or not data['keep_alive'].isdigit():
|
|
|
|
return jsonify({"status": "failed", "msg": "Persistent Keepalive format is not correct."})
|
2021-08-06 05:15:50 +02:00
|
|
|
if private_key != "":
|
2021-12-28 20:53:51 +01:00
|
|
|
check_key = f_check_key_match(private_key, id, config_name)
|
2021-08-06 05:15:50 +02:00
|
|
|
if check_key['status'] == "failed":
|
|
|
|
return jsonify(check_key)
|
|
|
|
if check_ip['status'] == "failed":
|
|
|
|
return jsonify(check_ip)
|
|
|
|
try:
|
2021-12-29 18:17:44 +01:00
|
|
|
tmp_psk = open("tmp_edit_psk.txt", "w+")
|
|
|
|
tmp_psk.write(preshared_key)
|
|
|
|
tmp_psk.close()
|
2022-01-13 15:37:23 +01:00
|
|
|
change_psk = subprocess.check_output(f"wg set {config_name} peer {id} preshared-key tmp_edit_psk.txt",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
2021-12-29 18:17:44 +01:00
|
|
|
if change_psk.decode("UTF-8") != "":
|
|
|
|
return jsonify({"status": "failed", "msg": change_psk.decode("UTF-8")})
|
2021-12-24 03:26:24 +01:00
|
|
|
if allowed_ip == "":
|
|
|
|
allowed_ip = '""'
|
|
|
|
allowed_ip = allowed_ip.replace(" ", "")
|
2022-01-13 15:37:23 +01:00
|
|
|
change_ip = subprocess.check_output(f"wg set {config_name} peer {id} allowed-ips {allowed_ip}",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
|
|
|
subprocess.check_output(f'wg-quick save {config_name}', shell=True, stderr=subprocess.STDOUT)
|
2021-08-06 05:15:50 +02:00
|
|
|
if change_ip.decode("UTF-8") != "":
|
2021-08-14 23:13:16 +02:00
|
|
|
return jsonify({"status": "failed", "msg": change_ip.decode("UTF-8")})
|
2022-01-02 20:44:27 +01:00
|
|
|
sql = "UPDATE " + config_name + " SET name = ?, private_key = ?, DNS = ?, endpoint_allowed_ip = ?, mtu = ?, keepalive = ?, preshared_key = ? WHERE id = ?"
|
|
|
|
g.cur.execute(sql, (name, private_key, dns_addresses, endpoint_allowed_ip, data["MTU"],
|
|
|
|
data["keep_alive"], preshared_key, id))
|
2021-08-06 05:15:50 +02:00
|
|
|
return jsonify({"status": "success", "msg": ""})
|
|
|
|
except subprocess.CalledProcessError as exc:
|
2021-08-14 23:13:16 +02:00
|
|
|
return jsonify({"status": "failed", "msg": str(exc.output.decode("UTF-8").strip())})
|
2021-08-06 05:15:50 +02:00
|
|
|
else:
|
2021-08-14 23:13:16 +02:00
|
|
|
return jsonify({"status": "failed", "msg": "This peer does not exist."})
|
2021-04-03 02:48:00 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Get peer settings
|
2021-08-06 05:15:50 +02:00
|
|
|
@app.route('/get_peer_data/<config_name>', methods=['POST'])
|
2021-04-03 02:48:00 +02:00
|
|
|
def get_peer_name(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Get peer settings.
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return settings of peer
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-04-03 02:48:00 +02:00
|
|
|
data = request.get_json()
|
2022-01-02 20:44:27 +01:00
|
|
|
peer_id = data['id']
|
|
|
|
result = g.cur.execute(
|
|
|
|
"SELECT name, allowed_ip, DNS, private_key, endpoint_allowed_ip, mtu, keepalive, preshared_key FROM "
|
|
|
|
+ config_name + " WHERE id = ?", (peer_id,)).fetchall()
|
|
|
|
data = {"name": result[0][0], "allowed_ip": result[0][1], "DNS": result[0][2],
|
|
|
|
"private_key": result[0][3], "endpoint_allowed_ip": result[0][4],
|
|
|
|
"mtu": result[0][5], "keep_alive": result[0][6], "preshared_key": result[0][7]}
|
2021-08-06 05:15:50 +02:00
|
|
|
return jsonify(data)
|
2021-04-03 02:48:00 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2022-01-04 22:32:23 +01:00
|
|
|
# Return available IPs
|
|
|
|
@app.route('/available_ips/<config_name>', methods=['GET'])
|
|
|
|
def available_ips(config_name):
|
2022-03-03 14:46:23 +01:00
|
|
|
result = {"status": True, "message":"", "data": f_available_ips(config_name)}
|
|
|
|
if len(result["data"]) == 0:
|
|
|
|
result['status'] = False
|
|
|
|
result['message'] = f"No more available IP for {config_name}."
|
|
|
|
return jsonify(result)
|
2022-01-04 22:32:23 +01:00
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Check if both key match
|
2021-08-06 05:15:50 +02:00
|
|
|
@app.route('/check_key_match/<config_name>', methods=['POST'])
|
|
|
|
def check_key_match(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Check key matches
|
|
|
|
@param config_name: Name of WG interface
|
|
|
|
@type config_name: str
|
|
|
|
@return: Return dictionary with status
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-08-06 05:15:50 +02:00
|
|
|
data = request.get_json()
|
|
|
|
private_key = data['private_key']
|
|
|
|
public_key = data['public_key']
|
2021-12-28 20:53:51 +01:00
|
|
|
return jsonify(f_check_key_match(private_key, public_key, config_name))
|
2021-08-14 23:13:16 +02:00
|
|
|
|
2021-12-24 03:26:24 +01:00
|
|
|
|
|
|
|
@app.route("/qrcode/<config_name>", methods=['GET'])
|
|
|
|
def generate_qrcode(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Generate QRCode
|
|
|
|
@param config_name: Configuration Name
|
|
|
|
@return: Template containing QRcode img
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
peer_id = request.args.get('id')
|
2022-01-04 22:32:23 +01:00
|
|
|
get_peer = g.cur.execute(
|
|
|
|
"SELECT private_key, allowed_ip, DNS, mtu, endpoint_allowed_ip, keepalive, preshared_key FROM "
|
|
|
|
+ config_name + " WHERE id = ?", (peer_id,)).fetchall()
|
2021-12-24 03:26:24 +01:00
|
|
|
config = get_dashboard_conf()
|
|
|
|
if len(get_peer) == 1:
|
|
|
|
peer = get_peer[0]
|
2022-01-02 20:44:27 +01:00
|
|
|
if peer[0] != "":
|
2021-12-24 03:26:24 +01:00
|
|
|
public_key = get_conf_pub_key(config_name)
|
|
|
|
listen_port = get_conf_listen_port(config_name)
|
|
|
|
endpoint = config.get("Peers", "remote_endpoint") + ":" + listen_port
|
2022-01-02 20:44:27 +01:00
|
|
|
private_key = peer[0]
|
|
|
|
allowed_ip = peer[1]
|
|
|
|
dns_addresses = peer[2]
|
|
|
|
mtu_value = peer[3]
|
|
|
|
endpoint_allowed_ip = peer[4]
|
|
|
|
keepalive = peer[5]
|
|
|
|
preshared_key = peer[6]
|
|
|
|
|
|
|
|
result = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nMTU = " \
|
|
|
|
+ str(mtu_value) + "\nDNS = " + dns_addresses + "\n\n[Peer]\nPublicKey = " + public_key \
|
|
|
|
+ "\nAllowedIPs = " + endpoint_allowed_ip + "\nPersistentKeepalive = " \
|
|
|
|
+ str(keepalive) + "\nEndpoint = " + endpoint
|
2021-12-29 18:17:44 +01:00
|
|
|
if preshared_key != "":
|
2022-01-02 20:44:27 +01:00
|
|
|
result += "\nPresharedKey = " + preshared_key
|
2021-12-29 18:17:44 +01:00
|
|
|
return render_template("qrcode.html", i=result)
|
2021-12-24 03:26:24 +01:00
|
|
|
else:
|
|
|
|
return redirect("/configuration/" + config_name)
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
|
2022-01-13 01:53:36 +01:00
|
|
|
@app.route('/download_all/<config_name>', methods=['GET'])
|
|
|
|
def download_all(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Download all configuration
|
|
|
|
@param config_name: Configuration Name
|
|
|
|
@return: JSON Object
|
|
|
|
"""
|
2022-01-13 01:53:36 +01:00
|
|
|
get_peer = g.cur.execute(
|
|
|
|
"SELECT private_key, allowed_ip, DNS, mtu, endpoint_allowed_ip, keepalive, preshared_key, name FROM "
|
|
|
|
+ config_name + " WHERE private_key != ''").fetchall()
|
|
|
|
config = get_dashboard_conf()
|
|
|
|
data = []
|
|
|
|
public_key = get_conf_pub_key(config_name)
|
|
|
|
listen_port = get_conf_listen_port(config_name)
|
|
|
|
endpoint = config.get("Peers", "remote_endpoint") + ":" + listen_port
|
|
|
|
for peer in get_peer:
|
|
|
|
private_key = peer[0]
|
|
|
|
allowed_ip = peer[1]
|
|
|
|
dns_addresses = peer[2]
|
|
|
|
mtu_value = peer[3]
|
|
|
|
endpoint_allowed_ip = peer[4]
|
|
|
|
keepalive = peer[5]
|
|
|
|
preshared_key = peer[6]
|
|
|
|
filename = peer[7]
|
|
|
|
if len(filename) == 0:
|
|
|
|
filename = "Untitled_Peer"
|
|
|
|
else:
|
|
|
|
filename = peer[7]
|
|
|
|
# Clean filename
|
|
|
|
illegal_filename = [".", ",", "/", "?", "<", ">", "\\", ":", "*", '|' '\"', "com1", "com2", "com3",
|
|
|
|
"com4", "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4",
|
|
|
|
"lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "con", "nul", "prn"]
|
|
|
|
for i in illegal_filename:
|
|
|
|
filename = filename.replace(i, "")
|
|
|
|
if len(filename) == 0:
|
|
|
|
filename = "Untitled_Peer"
|
|
|
|
filename = "".join(filename.split(' '))
|
|
|
|
filename = filename + "_" + config_name
|
|
|
|
psk = ""
|
|
|
|
if preshared_key != "":
|
|
|
|
psk = "\nPresharedKey = " + preshared_key
|
|
|
|
|
|
|
|
return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \
|
|
|
|
dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \
|
|
|
|
public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \
|
|
|
|
endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk
|
|
|
|
data.append({"filename": f"{filename}.conf", "content": return_data})
|
|
|
|
return jsonify({"status": True, "peers": data, "filename": f"{config_name}.zip"})
|
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Download configuration file
|
2021-12-29 18:17:44 +01:00
|
|
|
@app.route('/download/<config_name>', methods=['GET'])
|
2021-08-06 05:15:50 +02:00
|
|
|
def download(config_name):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Download one configuration
|
|
|
|
@param config_name: Configuration name
|
|
|
|
@return: JSON object
|
|
|
|
"""
|
2022-01-02 20:44:27 +01:00
|
|
|
peer_id = request.args.get('id')
|
|
|
|
get_peer = g.cur.execute(
|
|
|
|
"SELECT private_key, allowed_ip, DNS, mtu, endpoint_allowed_ip, keepalive, preshared_key, name FROM "
|
2022-01-04 22:32:23 +01:00
|
|
|
+ config_name + " WHERE id = ?", (peer_id,)).fetchall()
|
2021-09-08 18:39:25 +02:00
|
|
|
config = get_dashboard_conf()
|
2021-08-06 05:15:50 +02:00
|
|
|
if len(get_peer) == 1:
|
|
|
|
peer = get_peer[0]
|
2022-01-02 20:44:27 +01:00
|
|
|
if peer[0] != "":
|
2021-08-06 05:15:50 +02:00
|
|
|
public_key = get_conf_pub_key(config_name)
|
|
|
|
listen_port = get_conf_listen_port(config_name)
|
2021-12-26 00:26:39 +01:00
|
|
|
endpoint = config.get("Peers", "remote_endpoint") + ":" + listen_port
|
2022-01-02 20:44:27 +01:00
|
|
|
private_key = peer[0]
|
|
|
|
allowed_ip = peer[1]
|
|
|
|
dns_addresses = peer[2]
|
|
|
|
mtu_value = peer[3]
|
|
|
|
endpoint_allowed_ip = peer[4]
|
|
|
|
keepalive = peer[5]
|
|
|
|
preshared_key = peer[6]
|
|
|
|
filename = peer[7]
|
2021-08-14 23:13:16 +02:00
|
|
|
if len(filename) == 0:
|
2022-01-02 20:44:27 +01:00
|
|
|
filename = "Untitled_Peer"
|
2021-08-14 23:13:16 +02:00
|
|
|
else:
|
2022-01-02 20:44:27 +01:00
|
|
|
filename = peer[7]
|
2021-08-14 23:13:16 +02:00
|
|
|
# Clean filename
|
|
|
|
illegal_filename = [".", ",", "/", "?", "<", ">", "\\", ":", "*", '|' '\"', "com1", "com2", "com3",
|
|
|
|
"com4", "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4",
|
|
|
|
"lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "con", "nul", "prn"]
|
|
|
|
for i in illegal_filename:
|
|
|
|
filename = filename.replace(i, "")
|
|
|
|
if len(filename) == 0:
|
|
|
|
filename = "Untitled_Peer"
|
|
|
|
filename = "".join(filename.split(' '))
|
|
|
|
filename = filename + "_" + config_name
|
2021-12-29 18:17:44 +01:00
|
|
|
psk = ""
|
|
|
|
if preshared_key != "":
|
2022-01-02 20:44:27 +01:00
|
|
|
psk = "\nPresharedKey = " + preshared_key
|
|
|
|
|
2022-01-13 01:53:36 +01:00
|
|
|
return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \
|
|
|
|
dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \
|
|
|
|
public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \
|
|
|
|
endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk
|
2022-01-04 22:32:23 +01:00
|
|
|
|
2022-01-13 01:53:36 +01:00
|
|
|
return jsonify({"status": True, "filename": f"{filename}.conf", "content": return_data})
|
|
|
|
return jsonify({"status": False, "filename": "", "content": ""})
|
2021-08-06 05:15:50 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-03 23:32:51 +02:00
|
|
|
@app.route('/switch_display_mode/<mode>', methods=['GET'])
|
|
|
|
def switch_display_mode(mode):
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Change display view style.
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@param mode: Mode name
|
|
|
|
@type mode: str
|
|
|
|
@return: Return text with result
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
if mode in ['list', 'grid']:
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2021-09-03 23:32:51 +02:00
|
|
|
config.set("Peers", "peer_display_mode", mode)
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-09-03 23:32:51 +02:00
|
|
|
return "true"
|
2021-12-28 20:53:51 +01:00
|
|
|
return "false"
|
2021-09-03 23:32:51 +02:00
|
|
|
|
|
|
|
|
2022-03-22 03:33:19 +01:00
|
|
|
# APIs
|
|
|
|
@app.route('/api/togglePeerAccess', methods=['POST'])
|
|
|
|
def togglePeerAccess():
|
|
|
|
data = request.get_json()
|
|
|
|
print(data['peerID'])
|
|
|
|
returnData = {"status": True, "reason": ""}
|
|
|
|
required = ['peerID', 'config']
|
|
|
|
if checkJSONAllParameter(required, data):
|
|
|
|
checkUnlock = g.cur.execute(f"SELECT * FROM {data['config']} WHERE id='{data['peerID']}'").fetchone()
|
|
|
|
if checkUnlock:
|
|
|
|
moveUnlockToLock = g.cur.execute(f"INSERT INTO {data['config']}_restrict_access SELECT * FROM {data['config']} WHERE id = '{data['peerID']}'")
|
|
|
|
if g.cur.rowcount == 1:
|
|
|
|
print(g.cur.rowcount)
|
|
|
|
print(deletePeers(data['config'], [data['peerID']], g.cur, g.db))
|
|
|
|
else:
|
|
|
|
moveLockToUnlock = g.cur.execute(f"SELECT * FROM {data['config']}_restrict_access WHERE id='{data['peerID']}'").fetchone()
|
|
|
|
try:
|
|
|
|
if len(moveLockToUnlock[-1]) == 0:
|
|
|
|
status = subprocess.check_output(f"wg set {data['config']} peer {moveLockToUnlock[0]} allowed-ips {moveLockToUnlock[11]}",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
|
|
|
else:
|
|
|
|
now = str(datetime.now().strftime("%m%d%Y%H%M%S"))
|
|
|
|
f_name = now + "_tmp_psk.txt"
|
|
|
|
f = open(f_name, "w+")
|
|
|
|
f.write(moveLockToUnlock[-1])
|
|
|
|
f.close()
|
|
|
|
subprocess.check_output(f"wg set {data['config']} peer {moveLockToUnlock[0]} allowed-ips {moveLockToUnlock[11]} preshared-key {f_name}",
|
|
|
|
shell=True, stderr=subprocess.STDOUT)
|
|
|
|
os.remove(f_name)
|
|
|
|
status = subprocess.check_output(f"wg-quick save {data['config']}", shell=True, stderr=subprocess.STDOUT)
|
|
|
|
g.cur.execute(f"INSERT INTO {data['config']} SELECT * FROM {data['config']}_restrict_access WHERE id = '{data['peerID']}'")
|
|
|
|
if g.cur.rowcount == 1:
|
|
|
|
g.cur.execute(f"DELETE FROM {data['config']}_restrict_access WHERE id = '{data['peerID']}'")
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
returnData["status"] = False
|
|
|
|
returnData["reason"] = exc.output.strip()
|
|
|
|
else:
|
|
|
|
returnData["status"] = False
|
|
|
|
returnData["reason"] = "Please provide all required parameters."
|
|
|
|
|
|
|
|
return jsonify(returnData)
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
"""
|
|
|
|
Dashboard Tools Related
|
|
|
|
"""
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Get all IP for ping
|
|
|
|
@app.route('/get_ping_ip', methods=['POST'])
|
|
|
|
def get_ping_ip():
|
2022-01-18 16:42:23 +01:00
|
|
|
# TODO: convert return to json object
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Get ips for network testing.
|
|
|
|
@return: HTML containing a list of IPs
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-12-29 20:56:19 +01:00
|
|
|
config = request.form['config']
|
2022-01-02 20:44:27 +01:00
|
|
|
peers = g.cur.execute("SELECT id, name, allowed_ip, endpoint FROM " + config).fetchall()
|
2021-09-08 18:39:25 +02:00
|
|
|
html = ""
|
2022-01-02 20:44:27 +01:00
|
|
|
for i in peers:
|
|
|
|
html += '<optgroup label="' + i[1] + ' - ' + i[0] + '">'
|
|
|
|
allowed_ip = str(i[2]).split(",")
|
2021-09-08 18:39:25 +02:00
|
|
|
for k in allowed_ip:
|
|
|
|
k = k.split("/")
|
|
|
|
if len(k) == 2:
|
|
|
|
html += "<option value=" + k[0] + ">" + k[0] + "</option>"
|
2022-01-02 20:44:27 +01:00
|
|
|
endpoint = str(i[3]).split(":")
|
2021-09-08 18:39:25 +02:00
|
|
|
if len(endpoint) == 2:
|
|
|
|
html += "<option value=" + endpoint[0] + ">" + endpoint[0] + "</option>"
|
|
|
|
html += "</optgroup>"
|
|
|
|
return html
|
2021-08-06 05:15:50 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Ping IP
|
|
|
|
@app.route('/ping_ip', methods=['POST'])
|
|
|
|
def ping_ip():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Execute ping command.
|
|
|
|
@return: Return text with result
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
try:
|
|
|
|
result = ping('' + request.form['ip'] + '', count=int(request.form['count']), privileged=True, source=None)
|
|
|
|
returnjson = {
|
|
|
|
"address": result.address,
|
|
|
|
"is_alive": result.is_alive,
|
|
|
|
"min_rtt": result.min_rtt,
|
|
|
|
"avg_rtt": result.avg_rtt,
|
|
|
|
"max_rtt": result.max_rtt,
|
|
|
|
"package_sent": result.packets_sent,
|
|
|
|
"package_received": result.packets_received,
|
|
|
|
"package_loss": result.packet_loss
|
|
|
|
}
|
|
|
|
if returnjson['package_loss'] == 1.0:
|
|
|
|
returnjson['package_loss'] = returnjson['package_sent']
|
|
|
|
return jsonify(returnjson)
|
|
|
|
except Exception:
|
|
|
|
return "Error"
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
# Traceroute IP
|
|
|
|
@app.route('/traceroute_ip', methods=['POST'])
|
|
|
|
def traceroute_ip():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Execute ping traceroute command.
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@return: Return text with result
|
|
|
|
@rtype: str
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
try:
|
|
|
|
result = traceroute('' + request.form['ip'] + '', first_hop=1, max_hops=30, count=1, fast=True)
|
|
|
|
returnjson = []
|
|
|
|
last_distance = 0
|
|
|
|
for hop in result:
|
|
|
|
if last_distance + 1 != hop.distance:
|
|
|
|
returnjson.append({"hop": "*", "ip": "*", "avg_rtt": "", "min_rtt": "", "max_rtt": ""})
|
|
|
|
returnjson.append({"hop": hop.distance, "ip": hop.address, "avg_rtt": hop.avg_rtt, "min_rtt": hop.min_rtt,
|
|
|
|
"max_rtt": hop.max_rtt})
|
|
|
|
last_distance = hop.distance
|
|
|
|
return jsonify(returnjson)
|
|
|
|
except Exception:
|
|
|
|
return "Error"
|
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-09-08 18:39:25 +02:00
|
|
|
"""
|
|
|
|
Dashboard Initialization
|
|
|
|
"""
|
2021-12-26 00:26:39 +01:00
|
|
|
|
|
|
|
|
2021-05-04 08:10:06 +02:00
|
|
|
def init_dashboard():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Create dashboard default configuration.
|
2022-01-02 14:35:39 +01:00
|
|
|
"""
|
2022-03-22 03:33:19 +01:00
|
|
|
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2021-05-04 08:10:06 +02:00
|
|
|
# Set Default INI File
|
2021-12-28 23:51:41 +01:00
|
|
|
if not os.path.isfile(DASHBOARD_CONF):
|
2022-01-13 01:53:36 +01:00
|
|
|
open(DASHBOARD_CONF, "w+").close()
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2022-01-18 16:42:23 +01:00
|
|
|
# Default dashboard account setting
|
2021-05-04 08:10:06 +02:00
|
|
|
if "Account" not in config:
|
|
|
|
config['Account'] = {}
|
|
|
|
if "username" not in config['Account']:
|
|
|
|
config['Account']['username'] = 'admin'
|
|
|
|
if "password" not in config['Account']:
|
|
|
|
config['Account']['password'] = '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918'
|
2022-01-18 16:42:23 +01:00
|
|
|
# Default dashboard server setting
|
2021-05-04 08:10:06 +02:00
|
|
|
if "Server" not in config:
|
|
|
|
config['Server'] = {}
|
2021-05-05 03:26:40 +02:00
|
|
|
if 'wg_conf_path' not in config['Server']:
|
|
|
|
config['Server']['wg_conf_path'] = '/etc/wireguard'
|
2021-05-04 08:10:06 +02:00
|
|
|
if 'app_ip' not in config['Server']:
|
2021-12-22 17:36:29 +01:00
|
|
|
config['Server']['app_ip'] = '0.0.0.0'
|
2021-05-04 08:10:06 +02:00
|
|
|
if 'app_port' not in config['Server']:
|
|
|
|
config['Server']['app_port'] = '10086'
|
|
|
|
if 'auth_req' not in config['Server']:
|
|
|
|
config['Server']['auth_req'] = 'true'
|
2021-12-28 20:53:51 +01:00
|
|
|
if 'version' not in config['Server'] or config['Server']['version'] != DASHBOARD_VERSION:
|
|
|
|
config['Server']['version'] = DASHBOARD_VERSION
|
2021-05-14 00:00:40 +02:00
|
|
|
if 'dashboard_refresh_interval' not in config['Server']:
|
2021-09-03 23:32:51 +02:00
|
|
|
config['Server']['dashboard_refresh_interval'] = '60000'
|
2021-08-14 23:13:16 +02:00
|
|
|
if 'dashboard_sort' not in config['Server']:
|
|
|
|
config['Server']['dashboard_sort'] = 'status'
|
2021-12-30 05:26:15 +01:00
|
|
|
# Default dashboard peers setting
|
2021-08-14 23:13:16 +02:00
|
|
|
if "Peers" not in config:
|
|
|
|
config['Peers'] = {}
|
|
|
|
if 'peer_global_DNS' not in config['Peers']:
|
|
|
|
config['Peers']['peer_global_DNS'] = '1.1.1.1'
|
|
|
|
if 'peer_endpoint_allowed_ip' not in config['Peers']:
|
|
|
|
config['Peers']['peer_endpoint_allowed_ip'] = '0.0.0.0/0'
|
2021-09-03 23:32:51 +02:00
|
|
|
if 'peer_display_mode' not in config['Peers']:
|
|
|
|
config['Peers']['peer_display_mode'] = 'grid'
|
2021-09-08 18:39:25 +02:00
|
|
|
if 'remote_endpoint' not in config['Peers']:
|
|
|
|
config['Peers']['remote_endpoint'] = ifcfg.default_interface()['inet']
|
|
|
|
if 'peer_MTU' not in config['Peers']:
|
|
|
|
config['Peers']['peer_MTU'] = "1420"
|
|
|
|
if 'peer_keep_alive' not in config['Peers']:
|
|
|
|
config['Peers']['peer_keep_alive'] = "21"
|
2021-12-29 21:29:29 +01:00
|
|
|
set_dashboard_conf(config)
|
|
|
|
config.clear()
|
2021-05-05 03:26:40 +02:00
|
|
|
|
2021-12-26 00:26:39 +01:00
|
|
|
|
2021-05-05 03:26:40 +02:00
|
|
|
def check_update():
|
2022-01-18 16:42:23 +01:00
|
|
|
"""
|
|
|
|
Dashboard check update
|
2022-01-02 14:35:39 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
@return: Retunt text with result
|
|
|
|
@rtype: str
|
2021-12-26 00:26:39 +01:00
|
|
|
"""
|
2021-12-29 21:29:29 +01:00
|
|
|
config = get_dashboard_conf()
|
2022-01-18 16:42:23 +01:00
|
|
|
try:
|
|
|
|
data = urllib.request.urlopen("https://api.github.com/repos/donaldzou/WGDashboard/releases").read()
|
|
|
|
output = json.loads(data)
|
|
|
|
release = []
|
|
|
|
for i in output:
|
|
|
|
if not i["prerelease"]:
|
|
|
|
release.append(i)
|
|
|
|
if config.get("Server", "version") == release[0]["tag_name"]:
|
|
|
|
result = "false"
|
|
|
|
else:
|
|
|
|
result = "true"
|
2021-12-28 20:53:51 +01:00
|
|
|
|
2022-01-18 16:42:23 +01:00
|
|
|
return result
|
|
|
|
except urllib.error.HTTPError:
|
|
|
|
return "false"
|
2021-05-05 03:26:40 +02:00
|
|
|
|
2022-01-17 02:35:24 +01:00
|
|
|
|
2021-10-18 01:24:09 +02:00
|
|
|
"""
|
|
|
|
Configure DashBoard before start web-server
|
|
|
|
"""
|
2022-01-17 02:35:24 +01:00
|
|
|
|
|
|
|
|
2021-10-18 01:24:09 +02:00
|
|
|
def run_dashboard():
|
2021-10-17 22:37:45 +02:00
|
|
|
init_dashboard()
|
2022-01-17 02:35:24 +01:00
|
|
|
global UPDATE
|
|
|
|
UPDATE = check_update()
|
2021-10-17 22:37:45 +02:00
|
|
|
config = configparser.ConfigParser(strict=False)
|
|
|
|
config.read('wg-dashboard.ini')
|
2022-01-17 02:35:24 +01:00
|
|
|
# global app_ip
|
2021-10-17 22:37:45 +02:00
|
|
|
app_ip = config.get("Server", "app_ip")
|
2022-01-17 02:35:24 +01:00
|
|
|
# global app_port
|
2021-10-17 22:37:45 +02:00
|
|
|
app_port = config.get("Server", "app_port")
|
2022-01-17 02:35:24 +01:00
|
|
|
global WG_CONF_PATH
|
|
|
|
WG_CONF_PATH = config.get("Server", "wg_conf_path")
|
2021-10-17 22:37:45 +02:00
|
|
|
config.clear()
|
2022-03-05 04:09:01 +01:00
|
|
|
|
2021-10-17 22:37:45 +02:00
|
|
|
return app
|
2021-05-04 08:10:06 +02:00
|
|
|
|
2022-01-17 02:35:24 +01:00
|
|
|
|
2021-10-18 01:24:09 +02:00
|
|
|
"""
|
|
|
|
Get host and port for web-server
|
|
|
|
"""
|
2022-01-17 02:35:24 +01:00
|
|
|
|
|
|
|
|
2021-10-18 01:24:09 +02:00
|
|
|
def get_host_bind():
|
2021-05-04 08:10:06 +02:00
|
|
|
init_dashboard()
|
2021-10-18 01:40:08 +02:00
|
|
|
config = configparser.ConfigParser(strict=False)
|
|
|
|
config.read('wg-dashboard.ini')
|
2021-05-04 07:32:34 +02:00
|
|
|
app_ip = config.get("Server", "app_ip")
|
|
|
|
app_port = config.get("Server", "app_port")
|
2021-10-18 01:24:09 +02:00
|
|
|
return app_ip, app_port
|
|
|
|
|
2022-01-17 02:35:24 +01:00
|
|
|
|
2021-10-18 01:24:09 +02:00
|
|
|
if __name__ == "__main__":
|
2022-01-24 01:19:38 +01:00
|
|
|
init_dashboard()
|
2022-01-17 02:35:24 +01:00
|
|
|
UPDATE = check_update()
|
|
|
|
config = configparser.ConfigParser(strict=False)
|
|
|
|
config.read('wg-dashboard.ini')
|
|
|
|
# global app_ip
|
|
|
|
app_ip = config.get("Server", "app_ip")
|
|
|
|
# global app_port
|
|
|
|
app_port = config.get("Server", "app_port")
|
|
|
|
WG_CONF_PATH = config.get("Server", "wg_conf_path")
|
|
|
|
config.clear()
|
2022-03-04 16:06:14 +01:00
|
|
|
socketio.run(app, host=app_ip, debug=False, port=int(app_port))
|
2022-01-19 16:27:17 +01:00
|
|
|
# app.run(host=app_ip, debug=False, port=app_port)
|