From dc7c31945fb3e6239cfe85e72778ad96dcbb23f5 Mon Sep 17 00:00:00 2001 From: gsd Date: Wed, 11 Feb 2026 23:10:53 +0300 Subject: [PATCH] packet update --- authManager.py | 4 +++ dbService.py | 51 +++++++++++++++++++++++++++++++-- service.py | 1 + ui/src/app/utils/PortNums.ts | 32 +++++++++++++++++++++ webExtensions/packetEndpoint.py | 27 +++++++++++++++++ 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 ui/src/app/utils/PortNums.ts create mode 100644 webExtensions/packetEndpoint.py diff --git a/authManager.py b/authManager.py index 7b017a6..9a3cba7 100644 --- a/authManager.py +++ b/authManager.py @@ -21,6 +21,7 @@ class AuthManager: def __init__(self, args): self.salt = args.web_salt + self.enable = args.web_auth_enable self.code_store = {} async def storeCleaner(self): @@ -45,6 +46,9 @@ class AuthManager: def decorator(func): @wraps(func) async def wrapper(*args, **kwargs): + if not self.enable: + return await func(*args, **kwargs) + request: Request = kwargs.get("request", None) if request is None: raise HTTPException(status_code=500, detail="Authed endpoint need request arg, but is missing") diff --git a/dbService.py b/dbService.py index 146018e..4154ec3 100644 --- a/dbService.py +++ b/dbService.py @@ -1,7 +1,8 @@ from typing import List from pymongo.asynchronous.database import AsyncDatabase +from typing import List -class DbService: +class NodeDbService: def __init__(self, dbStore): self.dbStore:AsyncDatabase = dbStore @@ -84,4 +85,50 @@ class DbService: ] collection = self.dbStore['node_info'] c = await collection.aggregate(pipeline) - return await c.to_list() \ No newline at end of file + return await c.to_list() + +class PacketDbService: + def __init__(self, dbStore): + self.dbStore:AsyncDatabase = dbStore + + async def findPacketsAndGroupCount(self, + after: float = -1, + before: float = -1, + nums: List[int] = [], + portnums: List[int] = []): + pipeline = [] + + match = {} + if after != -1 or before != -1: + match["ts"] = {} + if after != -1: + match["ts"]["$gt"] = after + if before != -1: + match["ts"]["$lt"] = before + + if nums: + if type(nums) != list: + nums = [nums] + match["from"] = {"$in":nums} + + if portnums: + if type(portnums) != list: + portnums = [portnums] + match["portnums"] = {"$in":portnums} + + if match: + pipeline.append({"$match":match}) + + #групировка по количеству на выхлопе _id номер портнума и count число его юзов + pipeline.append({"$group": {"_id": "$portnum", "count": {"$sum": 1}}}) + + ###print(pipeline) + collection = self.dbStore['packet'] + c = await collection.aggregate(pipeline) + return await c.to_list() + +class DbService(NodeDbService, PacketDbService): + def __init__(self, dbStore): + NodeDbService.__init__(self, dbStore) + PacketDbService.__init__(self, dbStore) + \ No newline at end of file diff --git a/service.py b/service.py index c9e78b5..0f567e8 100644 --- a/service.py +++ b/service.py @@ -253,6 +253,7 @@ if __name__ == "__main__": parser.add_argument("--web-host", default="0.0.0.0") parser.add_argument("--web-port", default=8680) parser.add_argument("--web-salt", default=generate_random_string(32)) + parser.add_argument("--web-auth-enable", default=False, action="store_true") #mongodb parser.add_argument("--mongo-url") parser.add_argument("--mongo-host", default="192.168.3.2") diff --git a/ui/src/app/utils/PortNums.ts b/ui/src/app/utils/PortNums.ts new file mode 100644 index 0000000..462d3c6 --- /dev/null +++ b/ui/src/app/utils/PortNums.ts @@ -0,0 +1,32 @@ +export class PortNums { + 0: "UNKNOWN_APP"; + 1: "TEXT_MESSAGE_APP"; + 2: "REMOTE_HARDWARE_APP"; + 3: "POSITION_APP"; + 4: "NODEINFO_APP"; + 5: "ROUTING_APP"; + 6: "ADMIN_APP"; + 7: "TEXT_MESSAGE_COMPRESSED_APP"; + 8: "WAYPOINT_APP"; + 9: "AUDIO_APP"; + 10: "DETECTION_SENSOR_APP"; + 11: "ALERT_APP"; + 32: "REPLY_APP"; + 33: "IP_TUNNEL_APP"; + 34: "PAXCOUNTER_APP"; + 64: "SERIAL_APP"; + 65: "STORE_FORWARD_APP"; + 66: "RANGE_TEST_APP"; + 67: "TELEMETRY_APP"; + 68: "ZPS_APP"; + 69: "SIMULATOR_APP"; + 70: "TRACEROUTE_APP"; + 71: "NEIGHBORINFO_APP"; + 72: "ATAK_PLUGIN"; + 73: "MAP_REPORT_APP"; + 74: "POWERSTRESS_APP"; + 76: "RETICULUM_TUNNEL_APP"; + 256: "PRIVATE_APP"; + 257: "ATAK_FORWARDER"; + 511: "MAX"; +} diff --git a/webExtensions/packetEndpoint.py b/webExtensions/packetEndpoint.py new file mode 100644 index 0000000..d2d9b5a --- /dev/null +++ b/webExtensions/packetEndpoint.py @@ -0,0 +1,27 @@ +from fastapi import FastAPI +from fastapi.responses import HTMLResponse +from fastapi.requests import Request +from fastapi import Query + +from pymongo.asynchronous.database import AsyncDatabase +from extra.MessageDTO import MessageDTO +from typing import List, Annotated +from pymongo import DESCENDING + +class WebExtension: + MESSAGE_PORTNUM = 1 + app: FastAPI + dbStore: AsyncDatabase + def __init__(self, core): + self.core = core + self.app = core.app + + @self.app.get(f"{self.core.context}/packet/stats") + @self.core.authManager.authRequest() + async def findPacketsAndGroupCount(request: Request, + after: float = Query(-1), + before: float = Query(-1), + nums: List[int] = Query([]), + portnums: List[int] = Query([])): + gl = await self.core.dbService.findPacketsAndGroupCount(after, before, nums, portnums) + return gl \ No newline at end of file