#!/usr/bin/env python3 import asyncio import logging import os import pkgutil import re import secrets import stat import time from aiohttp import web import aiofiles import aiofiles.os as aios aios.statvfs = aios.wrap(os.statvfs) aios.listdir = aios.wrap(os.listdir) STORAGE_PATH = os.environ.get("PIRATEBOX_STORAGE", "/storage/piratebox") WAP_INTERFACE = os.environ.get("PIRATEBOX_IF", "piratebox0") SERVER_SOCK = os.environ.get("PIRATEBOX_SOCK", "/run/piratebox/piratebox.sock") CHUNK_SIZE = 256 * 1024 routes = web.RouteTableDef() logger = logging.getLogger(__name__) class DiskService: def __init__(self, path): self._path = path self._last_run = asyncio.get_event_loop().time() self._usage = None async def get_usage(self): if self._usage is not None and asyncio.get_event_loop().time() - self._last_run < 60: return self._usage else: st = await aios.statvfs(self._path) self._usage = int(100*(st.f_blocks - st.f_bfree)/st.f_blocks) self._last_run = asyncio.get_event_loop().time() return self._usage async def unlock(self, password): proc = await asyncio.create_subprocess_exec( "e4crypt", "add_key", stdout=asyncio.subprocess.PIPE, stdin=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, stderr = await proc.communicate(password.encode() + b"\n") if proc.returncode != 0: return stderr.decode() return "Success" class ChatService: def __init__(self): self._chat = [] self._chat.append( {"date": "00:00:00", "name": "PirateBox", "color": "def", "data": "Chat and share files anonymously!"}) def post(self, name, data, color, thetime): self._chat.append({ "date": thetime, "name": name, "color": color, "data": data }) def get(self): return self._chat class StaService: def __init__(self, ifname): self.ifname = ifname self._last_run = asyncio.get_event_loop().time() self._count = None async def get_sta_count(self): if self._count is not None and asyncio.get_event_loop().time() - self._last_run < 60: return self._count else: proc = await asyncio.create_subprocess_exec( "iw", self.ifname, "station", "dump", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) count = 0 stdout, stderr = await proc.communicate() for line in stdout.decode().splitlines(): if "Station" in line: count += 1 self._count = count self._last_run = asyncio.get_event_loop().time() return self._count @routes.get("/piratebox/admin/unlock") async def unlock_page(request): res = request.app["index_resources"] content = res[0] content += "
" content += res[1] return web.Response(text=content, content_type="text/html") @routes.post("/piratebox/admin/unlock") async def unlock_page(request): diskservice = request.app["DiskService"] form = await request.post() password = form.get("password", None) if password is None: raise web.HTTPBadRequest() msg = await diskservice.unlock(password) res = request.app["index_resources"] content = res[0] content += msg content += res[1] return web.Response(text=content, content_type="text/html") @routes.get("/piratebox/diskusage") async def diskusage(request): diskservice = request.app["DiskService"] usage = await diskservice.get_usage() return web.json_response({"usage": usage}) @routes.get("/piratebox/station-cnt") async def station_cnt(request): count = await request.app["StaService"].get_sta_count() return web.json_response({"count": count}) @routes.get("/piratebox/chat") async def chat_get(request): chat = request.app["ChatService"].get() return web.json_response(chat) @routes.post("/piratebox/chat") async def chat_post(request): form = await request.post() name = form.get("name", "Anonymous") data = form.get("data", "Thanks for sharing.
Browse files
Back