From f9c42319aa7111b468c8c86be88d7c840670928d Mon Sep 17 00:00:00 2001 From: Nikolaos Karaolidis Date: Sat, 23 Apr 2022 13:08:25 +0300 Subject: [PATCH] MVP --- Dockerfile | 10 +++++++++ requirements.txt | 11 ++++++++++ src/app.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 src/app.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a7c6b24 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python + +RUN pip install --upgrade pip +WORKDIR /app + +COPY requirements.txt requirements.txt +RUN pip3 install -r requirements.txt +COPY ./src . + +CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d77771a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +certifi==2021.10.8 +charset-normalizer==2.0.12 +click==8.1.2 +Flask==2.1.1 +idna==3.3 +itsdangerous==2.1.2 +Jinja2==3.1.1 +MarkupSafe==2.1.1 +requests==2.27.1 +urllib3==1.26.9 +Werkzeug==2.1.1 diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..30345b8 --- /dev/null +++ b/src/app.py @@ -0,0 +1,56 @@ +from flask import Flask, request +from reconnect import reboot +from os import environ +from subprocess import call + +app = Flask(__name__) + +# Get router username and password from environment variables +username = environ.get('ROUTER_USERNAME') +password = environ.get('ROUTER_PASSWORD') + +# Get router IP and path from environment variables if they exist +url = environ.get('ROUTER_IP') or '192.168.1.1' +path = environ.get('ROUTER_PATH') or '/ws' + +@app.route('/', methods=['POST']) +def result(): + print(request) + return 'Received!' + +@app.route('/safe', methods=['POST']) +def safe(): + # Try to ping the router 100 times + for i in range(50): + if call(['ping', '-c', '1', url]) == 0: + break + else: + return 'Error: Unable to ping and reboot router automatically!' + + # Try to nslookup 5 different websites + websites = ['www.google.com', 'www.facebook.com', 'www.twitter.com', 'www.reddit.com', 'www.amazon.com'] + success = 0 + + for website in websites: + if call(['nslookup', website]) == 0: + success += 1 + + if success > 3: + return 'Error: DNS seems to be working, debug manually!' + + # Reboot router + status = reboot(url, path, username, password).text + + if '{"status":true}' not in status: + return 'Error: Something went wrong while rebooting router!' + + return 'Success: Rebooted router!' + +@app.route('/force', methods=['POST']) +def force(): + status = reboot(url, path, username, password).text + + if '{"status":true}' not in status: + return 'Error: Something went wrong while rebooting router!' + + return 'Success: Rebooted router!'