From e162948cb8333e23350167865e0f9628b62b3145 Mon Sep 17 00:00:00 2001 From: DasMoorhuhn Date: Wed, 3 Jul 2024 03:59:38 +0200 Subject: [PATCH] add gatewayfinder and root api endpoint --- python/docker_entrypoint.sh | 1 + python/requierements.txt | 3 +- python/src/api_endpoints.py | 24 +++++++++++++++- python/src/find_gateways.py | 55 +++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 python/src/find_gateways.py diff --git a/python/docker_entrypoint.sh b/python/docker_entrypoint.sh index aeb780b..06a4647 100644 --- a/python/docker_entrypoint.sh +++ b/python/docker_entrypoint.sh @@ -5,6 +5,7 @@ env > .env if [ "$API" = true ]; then python3.12 api_endpoints.py & sleep 1 + python3.12 find_gateways.py & fi python3.12 main.py \ No newline at end of file diff --git a/python/requierements.txt b/python/requierements.txt index b7ed443..17e3ae8 100644 --- a/python/requierements.txt +++ b/python/requierements.txt @@ -3,4 +3,5 @@ pyyaml bs4 requests flask -flask_cors \ No newline at end of file +flask_cors +FindMyIP \ No newline at end of file diff --git a/python/src/api_endpoints.py b/python/src/api_endpoints.py index 6448f3b..0584760 100644 --- a/python/src/api_endpoints.py +++ b/python/src/api_endpoints.py @@ -17,6 +17,18 @@ class API: self.app.config['CORS_HEADERS'] = 'Content-Type' # --------Static Routes------- + @self.app.route('/') + @cross_origin() + def serve_root(): + root_dict = { + "version": "24-07-03", + "mode": 1, + "info": { + "files_size_sum": self.get_file_size() + } + } + return jsonify(root_dict) + @self.app.route('/charts') @cross_origin() def serve_index(): @@ -31,9 +43,19 @@ class API: @self.app.route('/json/') @cross_origin() def serve_json(path): - workdir, filename = os.path.split(os.path.abspath(__file__)) + return send_from_directory(f'{workdir}/data', path) + def get_file_size(self): + workdir, filename = os.path.split(os.path.abspath(__file__)) + files = os.listdir(f'{workdir}/data') + sizes = 0.0 + for file in files: + if file.endswith('.json'): + sizes += round(int(os.path.getsize(f'{workdir}/data/{file}')), 2) + + return sizes + api = API() api.app.run(host='0.0.0.0', port=8000) diff --git a/python/src/find_gateways.py b/python/src/find_gateways.py new file mode 100644 index 0000000..e89ec39 --- /dev/null +++ b/python/src/find_gateways.py @@ -0,0 +1,55 @@ +from time import sleep +import socket +import FindMyIP +import ipaddress +import threading +import requests + +max_threads = 50 +final = {} + + +def check_port(ip, port): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP + # sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP + socket.setdefaulttimeout(2.0) # seconds (float) + result = sock.connect_ex((ip, port)) + if result == 0: + # print("Port is open") + final[ip] = "OPEN" + else: + # print("Port is closed/filtered") + final[ip] = "CLOSED" + sock.close() + except: + final[ip] = "EXCEPTION" + + +port = 8000 +local_ip = FindMyIP.internal() +local_ip = local_ip.split('.')[:-1] +local_ip.append("0") +local_ip = '.'.join(local_ip) + +print(f"Scan on {local_ip}/24 for port {port}") + +for ip in ipaddress.IPv4Network(f'{local_ip}/24'): + threading.Thread(target=check_port, args=[str(ip), port]).start() + # sleep(0.1) + + # limit the number of threads. + while threading.active_count() > max_threads: + sleep(1) + +sorted_ips = dict(sorted(final.items(), key=lambda item: tuple(map(int, item[0].split('.'))))) + +for ip, state in sorted_ips.items(): + if state == "OPEN": + try: + response = requests.get(f'http://{ip}:{port}/') + if response.status_code == 200: + print(ip, state) + print(response.text) + except:pass +