2021-03-24 16:07:09 -04:00
|
|
|
import datetime
|
2021-01-20 16:58:56 -05:00
|
|
|
import json
|
2021-01-25 11:24:39 -05:00
|
|
|
import logging
|
2021-03-24 16:07:09 -04:00
|
|
|
import tracemalloc
|
2021-01-20 16:58:56 -05:00
|
|
|
|
|
|
|
import aprsd
|
2021-02-18 16:31:52 -05:00
|
|
|
from aprsd import messaging, plugin, stats
|
2021-01-20 16:58:56 -05:00
|
|
|
import flask
|
|
|
|
import flask_classful
|
2021-01-25 11:24:39 -05:00
|
|
|
from flask_httpauth import HTTPBasicAuth
|
|
|
|
from werkzeug.security import check_password_hash, generate_password_hash
|
|
|
|
|
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
|
|
|
auth = HTTPBasicAuth()
|
|
|
|
users = None
|
|
|
|
|
|
|
|
|
|
|
|
# HTTPBasicAuth doesn't work on a class method.
|
|
|
|
# This has to be out here. Rely on the APRSDFlask
|
|
|
|
# class to initialize the users from the config
|
|
|
|
@auth.verify_password
|
|
|
|
def verify_password(username, password):
|
|
|
|
global users
|
|
|
|
|
|
|
|
if username in users and check_password_hash(users.get(username), password):
|
|
|
|
return username
|
2021-01-20 16:58:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
class APRSDFlask(flask_classful.FlaskView):
|
|
|
|
config = None
|
|
|
|
|
|
|
|
def set_config(self, config):
|
2021-01-25 11:24:39 -05:00
|
|
|
global users
|
2021-01-20 16:58:56 -05:00
|
|
|
self.config = config
|
2021-01-25 11:24:39 -05:00
|
|
|
self.users = {}
|
|
|
|
for user in self.config["aprsd"]["web"]["users"]:
|
|
|
|
self.users[user] = generate_password_hash(
|
|
|
|
self.config["aprsd"]["web"]["users"][user],
|
|
|
|
)
|
|
|
|
|
|
|
|
users = self.users
|
2021-01-20 16:58:56 -05:00
|
|
|
|
2021-03-30 09:55:14 -04:00
|
|
|
@auth.login_required
|
2021-01-20 16:58:56 -05:00
|
|
|
def index(self):
|
2021-03-24 10:45:03 -04:00
|
|
|
return flask.render_template("index.html", message=self.stats())
|
2021-01-20 16:58:56 -05:00
|
|
|
|
2021-01-25 11:24:39 -05:00
|
|
|
@auth.login_required
|
|
|
|
def messages(self):
|
|
|
|
track = messaging.MsgTrack()
|
|
|
|
msgs = []
|
|
|
|
for id in track:
|
|
|
|
LOG.info(track[id].dict())
|
|
|
|
msgs.append(track[id].dict())
|
|
|
|
|
|
|
|
return flask.render_template("messages.html", messages=json.dumps(msgs))
|
|
|
|
|
2021-02-18 16:31:52 -05:00
|
|
|
@auth.login_required
|
|
|
|
def plugins(self):
|
|
|
|
pm = plugin.PluginManager()
|
|
|
|
pm.reload_plugins()
|
|
|
|
|
|
|
|
return "reloaded"
|
|
|
|
|
2021-01-25 11:24:39 -05:00
|
|
|
@auth.login_required
|
|
|
|
def save(self):
|
|
|
|
"""Save the existing queue to disk."""
|
|
|
|
track = messaging.MsgTrack()
|
|
|
|
track.save()
|
|
|
|
return json.dumps({"messages": "saved"})
|
|
|
|
|
2021-01-20 16:58:56 -05:00
|
|
|
def stats(self):
|
2021-01-21 20:58:47 -05:00
|
|
|
stats_obj = stats.APRSDStats()
|
2021-01-20 16:58:56 -05:00
|
|
|
track = messaging.MsgTrack()
|
2021-03-24 16:07:09 -04:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
current, peak = tracemalloc.get_traced_memory()
|
2021-01-21 20:58:47 -05:00
|
|
|
|
|
|
|
result = {
|
2021-01-20 16:58:56 -05:00
|
|
|
"version": aprsd.__version__,
|
2021-01-21 20:58:47 -05:00
|
|
|
"uptime": stats_obj.uptime,
|
2021-01-20 16:58:56 -05:00
|
|
|
"size_tracker": len(track),
|
2021-01-21 20:58:47 -05:00
|
|
|
"stats": stats_obj.stats(),
|
2021-03-24 16:07:09 -04:00
|
|
|
"time": now.strftime("%m-%d-%Y %H:%M:%S"),
|
|
|
|
"memory_current": current,
|
|
|
|
"memory_peak": peak,
|
2021-01-20 16:58:56 -05:00
|
|
|
}
|
2021-01-21 20:58:47 -05:00
|
|
|
return json.dumps(result)
|
2021-01-20 16:58:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
def init_flask(config):
|
2021-01-25 11:24:39 -05:00
|
|
|
flask_app = flask.Flask(
|
|
|
|
"aprsd",
|
|
|
|
static_url_path="",
|
|
|
|
static_folder="web/static",
|
|
|
|
template_folder="web/templates",
|
|
|
|
)
|
2021-01-20 16:58:56 -05:00
|
|
|
server = APRSDFlask()
|
|
|
|
server.set_config(config)
|
2021-03-24 10:45:03 -04:00
|
|
|
flask_app.route("/", methods=["GET"])(server.index)
|
2021-01-20 16:58:56 -05:00
|
|
|
flask_app.route("/stats", methods=["GET"])(server.stats)
|
2021-01-25 11:24:39 -05:00
|
|
|
flask_app.route("/messages", methods=["GET"])(server.messages)
|
|
|
|
flask_app.route("/save", methods=["GET"])(server.save)
|
2021-02-18 16:31:52 -05:00
|
|
|
flask_app.route("/plugins", methods=["GET"])(server.plugins)
|
2021-01-20 16:58:56 -05:00
|
|
|
return flask_app
|