This commit is contained in:
2022-04-23 13:08:25 +03:00
parent 0533b4f5e8
commit f9c42319aa
3 changed files with 77 additions and 0 deletions

10
Dockerfile Normal file
View File

@@ -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"]

11
requirements.txt Normal file
View File

@@ -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

56
src/app.py Normal file
View File

@@ -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!'