1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#!/usr/bin/python3
# Copyright (c) 2017-2019 David Steele <dsteele@gmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# License-Filename: LICENSE
#
# Copyright 2016-2017 David Steele <steele@debian.org>
# This file is part of comitup
# Available under the terms of the GNU General Public License version 2
# or later
#
import logging
import sys
import time
import urllib
from logging.handlers import TimedRotatingFileHandler
from multiprocessing import Process
from cachetools import TTLCache, cached
from flask import (
Flask,
abort,
jsonify,
redirect,
render_template,
request,
send_from_directory,
)
sys.path.append(".")
sys.path.append("..")
from comitup import client as ciu # noqa
ciu_client = None # type: ignore
LOG_PATH = "/var/log/comitup-web.log"
TEMPLATE_PATH = "/usr/share/comitup/web/templates"
ttl_cache: TTLCache = TTLCache(maxsize=10, ttl=5)
def deflog():
log = logging.getLogger("comitup_web")
log.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(
LOG_PATH,
encoding="utf=8",
when="W0",
backupCount=8,
)
fmtr = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(fmtr)
log.addHandler(handler)
return log
def do_connect(ssid, password, log):
time.sleep(1)
log.debug("Calling client connect")
ciu_client.service = None # type: ignore
ciu_client.ciu_connect(ssid, password) # type: ignore
@cached(cache=ttl_cache)
def cached_points():
return ciu_client.ciu_points() # type: ignore
def create_app(log):
app = Flask(__name__, template_folder=TEMPLATE_PATH)
@app.after_request
def add_header(response):
response.cache_control.max_age = 0
return response
@app.route("/")
def index():
points = cached_points()
for point in points:
point["ssid_encoded"] = urllib.parse.quote( # type: ignore
point["ssid"]
)
log.info("index.html - {} points".format(len(points)))
return render_template(
"index.html", points=points, can_blink=ciu.can_blink()
)
@app.route("/confirm")
def confirm():
ssid = request.args.get("ssid", "")
ssid_encoded = urllib.parse.quote(ssid.encode()) # type: ignore
encrypted = request.args.get("encrypted", "unencrypted")
mode = ciu_client.ciu_info()["imode"] # type: ignore
log.info("confirm.html - ssid {0}, mode {1}".format(ssid, mode))
return render_template(
"confirm.html",
ssid=ssid,
encrypted=encrypted,
ssid_encoded=ssid_encoded,
mode=mode,
can_blink=ciu.can_blink(),
)
@app.route("/connect", methods=["POST"])
def connect():
ssid = urllib.parse.unquote(request.form["ssid"]) # type: ignore
password = request.form["password"].encode()
cached_points()
p = Process(target=do_connect, args=(ssid, password, log))
p.start()
log.info("connect.html - ssid {0}".format(ssid))
return render_template(
"connect.html",
ssid=ssid,
password=password,
)
@app.route("/blink")
def blink():
log.info("blinking")
ciu.blink()
resp = jsonify(success=True)
return resp
@app.route("/img/favicon.ico")
def favicon():
log.info("Returning 404 for favicon request")
abort(404)
@app.route("/img/<path:path>")
def send_image(path):
return send_from_directory(TEMPLATE_PATH + "/images", path)
@app.route("/js/<path:path>")
def send_js(path):
return send_from_directory(TEMPLATE_PATH + "/js", path)
@app.route("/css/<path:path>")
def send_css(path):
return send_from_directory(TEMPLATE_PATH + "/css", path)
@app.route("/<path:path>")
def catch_all(path):
log.info("Redirecting {0}".format(path))
return redirect("http://10.41.0.1/", code=302)
@app.errorhandler(500)
def internal_error(error):
log.error("Internal Error detected")
sys.exit(1)
return app
def main():
log = deflog()
log.info("Starting comitup-web")
global ciu_client
ciu_client = ciu.CiuClient()
ciu_client.ciu_state()
ciu_client.ciu_points()
app = create_app(log)
app.run(host="0.0.0.0", port=80, debug=False, threaded=True)
if __name__ == "__main__":
main()
|