diff --git a/src/dashboard.py b/src/dashboard.py index 740594c5..fde8c53b 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -41,23 +41,6 @@ from modules.DashboardPlugins import DashboardPlugins from modules.DashboardWebHooks import DashboardWebHooks from modules.NewConfigurationTemplates import NewConfigurationTemplates -dictConfig({ - 'version': 1, - 'formatters': {'default': { - 'format': '[%(asctime)s] [%(levelname)s] in [%(module)s] %(message)s', - }}, - 'root': { - 'level': 'INFO' - } -}) - -SystemStatus = SystemStatus() - -CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.') -app = Flask("WGDashboard", template_folder=os.path.abspath("./static/dist/WGDashboardAdmin")) -app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 5206928 -app.secret_key = secrets.token_urlsafe(32) - class CustomJsonEncoder(DefaultJSONProvider): def __init__(self, app): super().__init__(app) @@ -70,7 +53,8 @@ class CustomJsonEncoder(DefaultJSONProvider): if type(o) is datetime: return o.strftime("%Y-%m-%d %H:%M:%S") return super().default(self) -app.json = CustomJsonEncoder(app) + + ''' Response Object @@ -83,16 +67,151 @@ def ResponseObject(status=True, message=None, data=None, status_code = 200) -> F }) response.status_code = status_code response.content_type = "application/json" - return response + return response + +''' +Flask App +''' +app = Flask("WGDashboard", template_folder=os.path.abspath("./static/dist/WGDashboardAdmin")) + +def peerInformationBackgroundThread(): + global WireguardConfigurations + app.logger.info("Background Thread #1 Started") + app.logger.info("Background Thread #1 PID:" + str(threading.get_native_id())) + delay = 6 + time.sleep(10) + while True: + with app.app_context(): + try: + curKeys = list(WireguardConfigurations.keys()) + for name in curKeys: + if name in WireguardConfigurations.keys() and WireguardConfigurations.get(name) is not None: + c = WireguardConfigurations.get(name) + if c.getStatus(): + c.getPeersLatestHandshake() + c.getPeersTransfer() + c.getPeersEndpoint() + c.getPeers() + if delay == 6: + c.logPeersTraffic() + c.logPeersHistoryEndpoint() + c.getRestrictedPeersList() + except Exception as e: + app.logger.error(f"[WGDashboard] Background Thread #1 Error", e) + + if delay == 6: + delay = 1 + else: + delay += 1 + time.sleep(10) + +def peerJobScheduleBackgroundThread(): + with app.app_context(): + app.logger.info(f"Background Thread #2 Started") + app.logger.info(f"Background Thread #2 PID:" + str(threading.get_native_id())) + time.sleep(10) + while True: + try: + AllPeerJobs.runJob() + time.sleep(180) + except Exception as e: + app.logger.error("Background Thread #2 Error", e) + +def gunicornConfig(): + _, app_ip = DashboardConfig.GetConfig("Server", "app_ip") + _, app_port = DashboardConfig.GetConfig("Server", "app_port") + return app_ip, app_port + +def ProtocolsEnabled() -> list[str]: + from shutil import which + protocols = [] + if which('awg') is not None and which('awg-quick') is not None: + protocols.append("awg") + if which('wg') is not None and which('wg-quick') is not None: + protocols.append("wg") + return protocols + +def InitWireguardConfigurationsList(startup: bool = False): + if os.path.exists(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]): + confs = os.listdir(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]) + confs.sort() + for i in confs: + if RegexMatch("^(.{1,}).(conf)$", i): + i = i.replace('.conf', '') + try: + if i in WireguardConfigurations.keys(): + if WireguardConfigurations[i].configurationFileChanged(): + with app.app_context(): + WireguardConfigurations[i] = WireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i) + else: + with app.app_context(): + WireguardConfigurations[i] = WireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i, startup=startup) + except WireguardConfiguration.InvalidConfigurationFileException as e: + app.logger.error(f"{i} have an invalid configuration file.") + + if "awg" in ProtocolsEnabled(): + confs = os.listdir(DashboardConfig.GetConfig("Server", "awg_conf_path")[1]) + confs.sort() + for i in confs: + if RegexMatch("^(.{1,}).(conf)$", i): + i = i.replace('.conf', '') + try: + if i in WireguardConfigurations.keys(): + if WireguardConfigurations[i].configurationFileChanged(): + with app.app_context(): + WireguardConfigurations[i] = AmneziaWireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i) + else: + with app.app_context(): + WireguardConfigurations[i] = AmneziaWireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i, startup=startup) + except WireguardConfiguration.InvalidConfigurationFileException as e: + app.logger.error(f"{i} have an invalid configuration file.") + +def startThreads(): + bgThread = threading.Thread(target=peerInformationBackgroundThread, daemon=True) + bgThread.start() + scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True) + scheduleJobThread.start() + +dictConfig({ + 'version': 1, + 'formatters': {'default': { + 'format': '[%(asctime)s] [%(levelname)s] in [%(module)s] %(message)s', + }}, + 'root': { + 'level': 'INFO' + } +}) + + +WireguardConfigurations: dict[str, WireguardConfiguration] = {} +CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.') + +app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 5206928 +app.secret_key = secrets.token_urlsafe(32) +app.json = CustomJsonEncoder(app) +with app.app_context(): + SystemStatus = SystemStatus() + DashboardConfig = DashboardConfig() + EmailSender = EmailSender(DashboardConfig) + AllPeerShareLinks: PeerShareLinks = PeerShareLinks(DashboardConfig, WireguardConfigurations) + AllPeerJobs: PeerJobs = PeerJobs(DashboardConfig, WireguardConfigurations) + DashboardLogger: DashboardLogger = DashboardLogger() + DashboardPlugins: DashboardPlugins = DashboardPlugins(app, WireguardConfigurations) + DashboardWebHooks: DashboardWebHooks = DashboardWebHooks(DashboardConfig) + NewConfigurationTemplates: NewConfigurationTemplates = NewConfigurationTemplates() + InitWireguardConfigurationsList(startup=True) + DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations) + app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients)) -DashboardConfig = DashboardConfig() -EmailSender = EmailSender(DashboardConfig) _, APP_PREFIX = DashboardConfig.GetConfig("Server", "app_prefix") cors = CORS(app, resources={rf"{APP_PREFIX}/api/*": { "origins": "*", "methods": "DELETE, POST, GET, OPTIONS", "allow_headers": ["Content-Type", "wg-dashboard-apikey"] }}) +_, app_ip = DashboardConfig.GetConfig("Server", "app_ip") +_, app_port = DashboardConfig.GetConfig("Server", "app_port") +_, WG_CONF_PATH = DashboardConfig.GetConfig("Server", "wg_conf_path") ''' API Routes @@ -1544,122 +1663,6 @@ Index Page def index(): return render_template('index.html') -def peerInformationBackgroundThread(): - global WireguardConfigurations - app.logger.info("Background Thread #1 Started") - app.logger.info("Background Thread #1 PID:" + str(threading.get_native_id())) - delay = 6 - time.sleep(10) - while True: - with app.app_context(): - try: - curKeys = list(WireguardConfigurations.keys()) - for name in curKeys: - if name in WireguardConfigurations.keys() and WireguardConfigurations.get(name) is not None: - c = WireguardConfigurations.get(name) - if c.getStatus(): - c.getPeersLatestHandshake() - c.getPeersTransfer() - c.getPeersEndpoint() - c.getPeers() - if delay == 6: - c.logPeersTraffic() - c.logPeersHistoryEndpoint() - c.getRestrictedPeersList() - except Exception as e: - app.logger.error(f"[WGDashboard] Background Thread #1 Error", e) - - if delay == 6: - delay = 1 - else: - delay += 1 - time.sleep(10) - -def peerJobScheduleBackgroundThread(): - with app.app_context(): - app.logger.info(f"Background Thread #2 Started") - app.logger.info(f"Background Thread #2 PID:" + str(threading.get_native_id())) - time.sleep(10) - while True: - try: - AllPeerJobs.runJob() - time.sleep(180) - except Exception as e: - app.logger.error("Background Thread #2 Error", e) - -def gunicornConfig(): - _, app_ip = DashboardConfig.GetConfig("Server", "app_ip") - _, app_port = DashboardConfig.GetConfig("Server", "app_port") - return app_ip, app_port - -def ProtocolsEnabled() -> list[str]: - from shutil import which - protocols = [] - if which('awg') is not None and which('awg-quick') is not None: - protocols.append("awg") - if which('wg') is not None and which('wg-quick') is not None: - protocols.append("wg") - return protocols - -def InitWireguardConfigurationsList(startup: bool = False): - if os.path.exists(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]): - confs = os.listdir(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]) - confs.sort() - for i in confs: - if RegexMatch("^(.{1,}).(conf)$", i): - i = i.replace('.conf', '') - try: - if i in WireguardConfigurations.keys(): - if WireguardConfigurations[i].configurationFileChanged(): - with app.app_context(): - WireguardConfigurations[i] = WireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i) - else: - with app.app_context(): - WireguardConfigurations[i] = WireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i, startup=startup) - except WireguardConfiguration.InvalidConfigurationFileException as e: - app.logger.error(f"{i} have an invalid configuration file.") - - if "awg" in ProtocolsEnabled(): - confs = os.listdir(DashboardConfig.GetConfig("Server", "awg_conf_path")[1]) - confs.sort() - for i in confs: - if RegexMatch("^(.{1,}).(conf)$", i): - i = i.replace('.conf', '') - try: - if i in WireguardConfigurations.keys(): - if WireguardConfigurations[i].configurationFileChanged(): - with app.app_context(): - WireguardConfigurations[i] = AmneziaWireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i) - else: - with app.app_context(): - WireguardConfigurations[i] = AmneziaWireguardConfiguration(DashboardConfig, AllPeerJobs, AllPeerShareLinks, DashboardWebHooks, i, startup=startup) - except WireguardConfiguration.InvalidConfigurationFileException as e: - app.logger.error(f"{i} have an invalid configuration file.") - - -_, app_ip = DashboardConfig.GetConfig("Server", "app_ip") -_, app_port = DashboardConfig.GetConfig("Server", "app_port") -_, WG_CONF_PATH = DashboardConfig.GetConfig("Server", "wg_conf_path") - -WireguardConfigurations: dict[str, WireguardConfiguration] = {} - -with app.app_context(): - AllPeerShareLinks: PeerShareLinks = PeerShareLinks(DashboardConfig, WireguardConfigurations) - AllPeerJobs: PeerJobs = PeerJobs(DashboardConfig, WireguardConfigurations) - DashboardLogger: DashboardLogger = DashboardLogger() - DashboardPlugins: DashboardPlugins = DashboardPlugins(app, WireguardConfigurations) - DashboardWebHooks: DashboardWebHooks = DashboardWebHooks(DashboardConfig) - NewConfigurationTemplates: NewConfigurationTemplates = NewConfigurationTemplates() - InitWireguardConfigurationsList(startup=True) - DashboardClients: DashboardClients = DashboardClients(WireguardConfigurations) - app.register_blueprint(createClientBlueprint(WireguardConfigurations, DashboardConfig, DashboardClients)) - -def startThreads(): - bgThread = threading.Thread(target=peerInformationBackgroundThread, daemon=True) - bgThread.start() - scheduleJobThread = threading.Thread(target=peerJobScheduleBackgroundThread, daemon=True) - scheduleJobThread.start() - if __name__ == "__main__": startThreads() DashboardPlugins.startThreads() diff --git a/src/modules/AmneziaWGPeer.py b/src/modules/AmneziaWGPeer.py index 11989989..17101b6b 100644 --- a/src/modules/AmneziaWGPeer.py +++ b/src/modules/AmneziaWGPeer.py @@ -13,85 +13,6 @@ class AmneziaWGPeer(Peer): self.advanced_security = tableData["advanced_security"] super().__init__(tableData, configuration) - def downloadPeer(self) -> dict[str, str]: - filename = self.name - if len(filename) == 0: - filename = "UntitledPeer" - filename = "".join(filename.split(' ')) - filename = f"{filename}_{self.configuration.Name}" - 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, "") - - finalFilename = "" - for i in filename: - if re.match("^[a-zA-Z0-9_=+.-]$", i): - finalFilename += i - - interfaceSection = { - "PrivateKey": self.private_key, - "Address": self.allowed_ip, - "MTU": self.mtu, - "DNS": self.DNS, - "Jc": self.configuration.Jc, - "Jmin": self.configuration.Jmin, - "Jmax": self.configuration.Jmax, - "S1": self.configuration.S1, - "S2": self.configuration.S2, - "H1": self.configuration.H1, - "H2": self.configuration.H2, - "H3": self.configuration.H3, - "H4": self.configuration.H4 - } - peerSection = { - "PublicKey": self.configuration.PublicKey, - "AllowedIPs": self.endpoint_allowed_ip, - "Endpoint": f'{self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}', - "PersistentKeepalive": self.keepalive, - "PresharedKey": self.preshared_key - } - combine = [interfaceSection.items(), peerSection.items()] - peerConfiguration = "" - for s in range(len(combine)): - if s == 0: - peerConfiguration += "[Interface]\n" - else: - peerConfiguration += "\n[Peer]\n" - for (key, val) in combine[s]: - if val is not None and ((type(val) is str and len(val) > 0) or (type(val) is int and val > 0)): - peerConfiguration += f"{key} = {val}\n" - -# peerConfiguration = f'''[Interface] -# PrivateKey = {self.private_key} -# Address = {self.allowed_ip} -# MTU = {str(self.mtu)} -# Jc = {self.configuration.Jc} -# Jmin = {self.configuration.Jmin} -# Jmax = {self.configuration.Jmax} -# S1 = {self.configuration.S1} -# S2 = {self.configuration.S2} -# H1 = {self.configuration.H1} -# H2 = {self.configuration.H2} -# H3 = {self.configuration.H3} -# H4 = {self.configuration.H4} -# ''' -# if len(self.DNS) > 0: -# peerConfiguration += f"DNS = {self.DNS}\n" -# peerConfiguration += f''' -# [Peer] -# PublicKey = {self.configuration.PublicKey} -# AllowedIPs = {self.endpoint_allowed_ip} -# Endpoint = {self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort} -# PersistentKeepalive = {str(self.keepalive)} -# ''' -# if len(self.preshared_key) > 0: -# peerConfiguration += f"PresharedKey = {self.preshared_key}\n" - return { - "fileName": finalFilename, - "file": peerConfiguration - } def updatePeer(self, name: str, private_key: str, preshared_key: str, diff --git a/src/modules/Peer.py b/src/modules/Peer.py index 4724fdf7..9b415b19 100644 --- a/src/modules/Peer.py +++ b/src/modules/Peer.py @@ -150,6 +150,20 @@ class Peer: if self.configuration.configurationInfo.OverridePeerSettings.DNS else self.DNS ) } + + if self.configuration.Protocol == "awg": + interfaceSection.update({ + "Jc": self.configuration.Jc, + "Jmin": self.configuration.Jmin, + "Jmax": self.configuration.Jmax, + "S1": self.configuration.S1, + "S2": self.configuration.S2, + "H1": self.configuration.H1, + "H2": self.configuration.H2, + "H3": self.configuration.H3, + "H4": self.configuration.H4 + }) + peerSection = { "PublicKey": self.configuration.PublicKey, "AllowedIPs": ( diff --git a/src/static/client/src/components/Configuration/configuration.vue b/src/static/client/src/components/Configuration/configuration.vue index a5fa83a3..264eda1a 100644 --- a/src/static/client/src/components/Configuration/configuration.vue +++ b/src/static/client/src/components/Configuration/configuration.vue @@ -55,9 +55,9 @@ window.dayjs = dayjs - {{ props.config.protocol === 'wg' ? 'WireGuard': 'AmneziaWG' }} - + > + {{ props.config.protocol === 'wg' ? 'WireGuard': 'AmneziaWG' }} +
@@ -92,6 +92,8 @@ window.dayjs = dayjs diff --git a/src/static/client/src/components/Configuration/configurationQRCode.vue b/src/static/client/src/components/Configuration/configurationQRCode.vue index 236b1919..3b2d8b67 100644 --- a/src/static/client/src/components/Configuration/configurationQRCode.vue +++ b/src/static/client/src/components/Configuration/configurationQRCode.vue @@ -1,29 +1,40 @@ diff --git a/src/static/client/src/components/SignIn/qrcode.vue b/src/static/client/src/components/SignIn/qrcode.vue index 34055fda..b9732b43 100644 --- a/src/static/client/src/components/SignIn/qrcode.vue +++ b/src/static/client/src/components/SignIn/qrcode.vue @@ -14,7 +14,7 @@ onMounted(() => {