From c3e285a9ee4e9e579166155f7bd996a0c2ffae0c Mon Sep 17 00:00:00 2001 From: UpstreamData Date: Wed, 18 May 2022 11:06:21 -0600 Subject: [PATCH] fix some bugs in web monitor --- tools/web_monitor/func/__init__.py | 6 ++++-- tools/web_monitor/scan/__init__.py | 6 +++++- tools/web_monitor/templates/scan.html | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/web_monitor/func/__init__.py b/tools/web_monitor/func/__init__.py index dc5ba037..67e9bdd2 100644 --- a/tools/web_monitor/func/__init__.py +++ b/tools/web_monitor/func/__init__.py @@ -1,11 +1,13 @@ import os import ipaddress +dir_path = "\\".join(os.path.dirname(os.path.realpath(__file__)).split("\\")[:-1]) + def get_current_miner_list(): cur_miners = [] - if os.path.exists(os.path.join(os.getcwd(), "miner_list.txt")): - with open(os.path.join(os.getcwd(), "miner_list.txt")) as file: + if os.path.exists(os.path.join(dir_path, "miner_list.txt")): + with open(os.path.join(dir_path, "miner_list.txt")) as file: for line in file.readlines(): cur_miners.append(line.strip()) cur_miners = sorted(cur_miners, key=lambda x: ipaddress.ip_address(x)) diff --git a/tools/web_monitor/scan/__init__.py b/tools/web_monitor/scan/__init__.py index 1023ca75..82e32b7b 100644 --- a/tools/web_monitor/scan/__init__.py +++ b/tools/web_monitor/scan/__init__.py @@ -5,6 +5,10 @@ from tools.web_monitor.func import get_current_miner_list from .ws import router as ws_router +import os + +dir_path = "\\".join(os.path.dirname(os.path.realpath(__file__)).split("\\")[:-1]) + router = APIRouter() router.include_router(ws_router) @@ -19,7 +23,7 @@ def scan(request: Request): @router.post("/add_miners") async def add_miners_scan(request: Request): miners = await request.json() - with open("miner_list.txt", "a+") as file: + with open(os.path.join(dir_path, "miner_list.txt"), "a+") as file: for miner_ip in miners["miners"]: file.write(miner_ip + "\n") return scan diff --git a/tools/web_monitor/templates/scan.html b/tools/web_monitor/templates/scan.html index 1b63015c..ac931552 100644 --- a/tools/web_monitor/templates/scan.html +++ b/tools/web_monitor/templates/scan.html @@ -56,7 +56,7 @@ window.post = function(url, data) { return fetch(url, {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}); } - var ws = new WebSocket("ws://{{request.url.hostname}}:{{request.url.port}}/scan/ws"); + var ws = new WebSocket("ws://{{request.url.hostname}}:{% if request.port %}{{request.url.port}}{% else %}80{% endif %}/scan/ws"); ws.onmessage = function(event) { if (event.data == "Done") { document.getElementById("scanButton").innerHTML = "Scan"