2024-05-31 23:13:38 +03:00
|
|
|
from fastapi import Request, FastAPI
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
from .collector import parse_traceroute_output, store_traceroute
|
2024-06-01 16:14:29 +03:00
|
|
|
from .db import Database, ensure_table_setup
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
from pprint import pprint as print
|
|
|
|
|
|
|
|
|
|
|
|
# Setup our SQLite before anything else.
|
|
|
|
ensure_table_setup()
|
|
|
|
|
|
|
|
# Setup web framework thingies
|
|
|
|
app = FastAPI()
|
2024-06-01 16:14:29 +03:00
|
|
|
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
|
|
|
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
@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",
|
2024-06-01 16:14:29 +03:00
|
|
|
]
|
|
|
|
+ [None] * 800,
|
2024-05-31 23:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
@app.get("/trace/")
|
|
|
|
def list_traces():
|
|
|
|
db = Database()
|
|
|
|
|
|
|
|
trace = db.list_traces()
|
|
|
|
|
|
|
|
db.end()
|
|
|
|
return trace
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/trace/{origin}")
|
|
|
|
async def create_trace(origin: str, request: Request):
|
2024-05-31 23:13:38 +03:00
|
|
|
raw_data = await request.body()
|
|
|
|
data = raw_data.decode("utf-8", "ignore")
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
print(f"Received data from {origin}:")
|
2024-05-31 23:13:38 +03:00
|
|
|
print(data)
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
trace = parse_traceroute_output(data, origin)
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
print("Parsed data:")
|
|
|
|
print(trace)
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
db = Database()
|
|
|
|
db.create_trace(trace)
|
|
|
|
db.end()
|
2024-05-31 23:13:38 +03:00
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
return {"status": "ok"}
|