diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f76c0b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Created by .ignore support plugin (hsz.mobi) +.idea/ +/venv/ +/src/config.ini diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..0b64536 --- /dev/null +++ b/dockerfile @@ -0,0 +1,8 @@ +FROM python:3-alpine + +COPY . /code/ +WORKDIR /code/ + +RUN pip install -r /code/requirements.txt + +CMD ["python","src/api.py"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c26e8d7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +click==7.1.2 +Flask==1.1.2 +Flask-Cors==3.0.10 +itsdangerous==1.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +mysql-connector-python==8.0.23 +protobuf==3.14.0 +six==1.15.0 +wakeonlan==1.1.6 +Werkzeug==1.0.1 diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..caadc29 --- /dev/null +++ b/src/app.py @@ -0,0 +1,47 @@ +from flask import Flask, request, jsonify +from flask_cors import CORS +import configparser +import mysql.connector +import datetime +from wakeonlan import send_magic_packet + + +app = Flask(__name__) +CORS(app) + + +def create_connection(config): + mydb = mysql.connector.connect( + host=config['database']['db_host'], + user=config['database']['db_user'], + password=config['database']['db_pass'], + database=config['database']['db_table'] + ) + return mydb + + +def verifyapikey(apikey, config): + if apikey == config['setup']['apikey']: + return True + return False + + +@app.route('/wakeup', methods=['POST']) +def wakeup(): + if verifyapikey(request.values['apikey'], config): + db = create_connection(config) + cursor = db.cursor() + sql = """SELECT device_mac FROM devices WHERE device_name = %s;""" + cursor.execute(sql, [request.values['device_name']]) + macaddr = cursor.fetchone() + cursor.close() + db.close() + send_magic_packet(macaddr[0]) + return jsonify(result='success', timestamp=datetime.datetime.now()) + return jsonify(result='wrong api key') + + +if __name__ == '__main__': + config = configparser.ConfigParser() + config.read('config.ini') + app.run(host='0.0.0.0', debug=True) \ No newline at end of file