56 lines
1.4 KiB
Python
Executable File
56 lines
1.4 KiB
Python
Executable File
from fastapi import Request, FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from datetime import datetime
|
|
|
|
from .collector import parse_traceroute_output, store_traceroute
|
|
from .db import ensure_table_setup
|
|
|
|
from pprint import pprint as print
|
|
|
|
|
|
# Setup our SQLite before anything else.
|
|
ensure_table_setup()
|
|
|
|
# Setup web framework thingies
|
|
app = FastAPI()
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {
|
|
"greeting": "Hello, Kalzu!",
|
|
"instructions": [
|
|
"Try piping traceroute data directly to POST /trace/{hostname}.",
|
|
"{hostname} is for filtering data by sender.",
|
|
"For example the following command using HTTPie:",
|
|
"",
|
|
" $ traceroute peitto.info | http POST localhost:8000/trace/rekku",
|
|
"",
|
|
"",
|
|
"Also take a look at /docs/ and /static/index.html",
|
|
"",
|
|
"",
|
|
"",
|
|
"END OF TRANSMISSION",
|
|
] + [None]*800
|
|
}
|
|
|
|
|
|
@app.post("/trace/{hostname}")
|
|
async def create_trace(hostname: str, request: Request):
|
|
raw_data = await request.body()
|
|
data = raw_data.decode("utf-8", "ignore")
|
|
|
|
print(f"Received data from {hostname}:")
|
|
print(data)
|
|
|
|
trace = parse_traceroute_output(data, datetime.now())
|
|
|
|
print("Parsed data:")
|
|
print(trace)
|
|
|
|
store_traceroute(hostname, trace)
|
|
|
|
return {"status": "ok" }
|