def last_seen_devices(): """ Post last seen devices to database :return: status code """ if request.headers.getlist("X-Forwarded-For"): ip_addr = request.headers.getlist("X-Forwarded-For")[0] logger.info( "forward from %s to %s", request.remote_addr, request.headers.getlist("X-Forwarded-For")[0], ) else: ip_addr = request.remote_addr if any( ip_range(whitelist_addr, ip_addr) for whitelist_addr in settings.whitelist): app.logger.info("request from whitelist: {}".format(ip_addr)) if request.headers.get("User-Agent") == "Mikrotik/6.x Fetch": app.logger.info("got data from mikrotik") try: data = json.loads(request.values.get("data", [])) except Exception as exc: app.logger.error("malformed request") return abort(400) parsed_data = parse_mikrotik_data(datetime.now(), data) else: app.logger.warning("bad request \n{}".format(request.headers)) return abort(400) app.logger.info("parsed data, got {} devices".format(len(parsed_data))) with db.atomic(): for dev in parsed_data: Device.update_or_create(**dev) app.logger.info("updated last seen devices") return "OK", 200 else: app.logger.warning( "request from outside whitelist: {}".format(ip_addr)) return abort(403)
def index(): """Serve list of people in hs, show panel for logged users""" recent = Device.get_recent(**settings.recent_time) visible_devices = filter_hidden(recent) users = filter_hidden(owners_from_devices(visible_devices)) return render_template("landing.html", users=filter_anon_names(users), headcount=len(users), unknowncount=len(unclaimed_devices(recent)), **common_vars_tpl)
def devices(): recent = Device.get_recent(**settings.recent_time) visible_devices = filter_hidden(recent) users = filter_hidden(owners_from_devices(visible_devices)) if current_user.is_authenticated: unclaimed = unclaimed_devices(recent) mine = current_user.devices return render_template("devices.html", unclaimed=unclaimed, recent=recent, my_devices=mine, users=filter_anon_names(users), headcount=len(users), **common_vars_tpl)
def device_view(mac_address): """Get info about device, claim device, release device""" try: device = Device.get(Device.mac_address == mac_address) except Device.DoesNotExist as exc: app.logger.error("{}".format(exc)) return abort(404) if request.method == "POST": if request.values.get("action") == "claim": claim_device(device) elif request.values.get("action") == "unclaim": unclaim_device(device) set_device_flags(device, []) elif request.values.get("flags"): set_device_flags(device, request.form.getlist("flags")) return render_template("device.html", device=device, **common_vars_tpl)
def now_at_space(): """ Send list of people currently in HS as JSON, only registred people, used by other services in HS, requests should be from hs3.pl domain or from HSWAN """ period = {**settings.recent_time} for key in ["days", "hours", "minutes"]: if key in request.args: period[key] = request.args.get(key, default=0, type=int) devices = filter_hidden(Device.get_recent(**period)) users = filter_hidden(owners_from_devices(devices)) data = { "users": sorted(map(str, filter_anon_names(users))), "headcount": len(users), "unknown_devices": len(unclaimed_devices(devices)), } app.logger.info("sending request for /api/now {}".format(data)) return jsonify(data)
def callback(ch, method, properties, body): logger.info(" [x] %r:%r" % (method.routing_key, body)) Device.update_or_create(mac_address=body.decode().upper(), last_seen=datetime.now())