From 72eecaa20c18bc97bac605b66fb54523b7ac2b13 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Apr 2019 16:57:17 +0200 Subject: [PATCH] Lock system & internet control, WIP yubikey control and fix WIP Core import --- Setup.py | 14 +++++--------- internet.py | 29 +++++++++++++++++++++++++++++ yubikey.py | 12 ++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 internet.py create mode 100644 yubikey.py diff --git a/Setup.py b/Setup.py index 2fbc96b..a5bda9d 100644 --- a/Setup.py +++ b/Setup.py @@ -1,4 +1,4 @@ -import io, socket, subprocess +import io, socket, subprocess, shelve from requests import post, get from flask import request, Flask #hostapd system @@ -7,9 +7,6 @@ from flask import request, Flask app = Flask(__name__) -def ap(switch): - pass - def writeconfig(ssid, password): rtline = "\n" with io.open("/etc/wpa_supplicant/wpa_supplicant.conf", "w", encoding="utf8") as note: @@ -17,10 +14,8 @@ def writeconfig(ssid, password): for i in ["ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\ncountry=FR\nnetwork={\nssid=\"", ssid, "\"\nscan_ssid=1\npsk=\"", password, "\"\n}"]: conf += i note.write(conf) - ap(False) subprocess.check_call(["sudo", "wpa_cli", "-i", "wlan0", "reconfigure"]) - def testinternet(): result = True try: @@ -38,11 +33,12 @@ def web_setup(): if testinternet(): mac = io.open("/sys/class/net/wlan0/address").read() id = request.args.get("id") - r = get(f"http://flifloo.ddns.net:5000/locksetup?mac={mac}&id={id}") + with shelve.open("Settings.conf") as settings: + settings["token"] = id + r = get(f"http://vps.flifloo.fr:5000/locksetup?mac={mac}&id={id}") else: - ap(True) return "Cant connect" return "Done" if __name__ == "__main__": - app.run(debug=True, port=5000, host="0.0.0.0") + app.run(debug=True, port=6000, host="0.0.0.0") diff --git a/internet.py b/internet.py new file mode 100644 index 0000000..7d5f6a8 --- /dev/null +++ b/internet.py @@ -0,0 +1,29 @@ +from flask import request, Flask +import lock, shelve + +app = Flask(__name__) +@app.route("/unlock") +def web_unlock(): + if not (request.args.get("token") and request.args.get("state")): + return "Error" + else: + with shelve.open("Settings.conf") as settings: + token = settings["token"] + if request.args.get("token") != token: + return "Invalid Token" + if request.args.get("state") == "open": + lock.unlock() + elif request.args.get("state") == "close": + lock.lock() + elif request.args.get("state") == "switch": + lock.switch() + else: + return "Invalid State" + return "Done" + +@app.route("/state") +def web_state(): + return str(lock.state()) + +if __name__ == "__main__": + app.run(debug=True, port=5000, host="0.0.0.0") diff --git a/yubikey.py b/yubikey.py new file mode 100644 index 0000000..6325196 --- /dev/null +++ b/yubikey.py @@ -0,0 +1,12 @@ +from yubico_client import Yubico +import lock, shelve + +with shelve.open("Settings.conf") as settings: + client = Yubico(settings["id"], settings["secret"], api_urls=('http://localhost/wsapi/2.0/verify',)) +while True: + try: + client.verify(input()) + except: + pass + else: + lock.switch()