From 6099ccdd222141ca2b928d4105bfbbb3ce2768f0 Mon Sep 17 00:00:00 2001 From: froz Date: Sat, 9 Aug 2025 10:28:06 +0000 Subject: [PATCH 1/2] feat: add optional secret handling for server status reporting --- reporter/main.go | 3 + scripts/start-tianji-container.sh | 6 +- .../components/server/AddServerStep.tsx | 2 +- src/server/model/serverStatus.ts | 11 +- src/server/router/serverStatus.ts | 1 + src/server/utils/env.ts | 1 + src/types/server.ts | 1 + website/docs/install/environment.md | 112 +++++++++--------- 8 files changed, 81 insertions(+), 56 deletions(-) diff --git a/reporter/main.go b/reporter/main.go index 69ef43d71..8ee0cefeb 100644 --- a/reporter/main.go +++ b/reporter/main.go @@ -19,6 +19,7 @@ type ReportData struct { Hostname string `json:"hostname"` Timeout int `json:"timeout"` // if service receive after timeout second, its means client are offline Payload utils.ReportDataPayload `json:"payload"` + Secret *string `json:"secret,omitempty"` // optional secret for server status } var ( @@ -29,6 +30,7 @@ var ( Interval = flag.Int("interval", 5.0, "Input the INTERVAL, seconed") IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only") Verbose = flag.Bool("verbose", false, "Enable verbose logging to show full payload content") + Secret = flag.String("secret", "", "The server status secret, optional") ) var version = "1.0.0" @@ -77,6 +79,7 @@ func main() { Hostname: hostname, Timeout: interval * 10, Payload: utils.GetReportDataPaylod(interval, *IsVnstat), + Secret: Secret, } if *Mode == "udp" { diff --git a/scripts/start-tianji-container.sh b/scripts/start-tianji-container.sh index 09f10e615..cba03ab4f 100644 --- a/scripts/start-tianji-container.sh +++ b/scripts/start-tianji-container.sh @@ -7,7 +7,11 @@ pnpm start:docker & sleep 10 # Start reporter with default workspace -/usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" > /dev/null & +if [ -n "$SERVER_STATUS_SECRET" ]; then + /usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" --secret "$SERVER_STATUS_SECRET" > /dev/null & +else + /usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" > /dev/null & +fi # Wait for any process to exit wait -n diff --git a/src/client/components/server/AddServerStep.tsx b/src/client/components/server/AddServerStep.tsx index 3de9be402..b4968d99f 100644 --- a/src/client/components/server/AddServerStep.tsx +++ b/src/client/components/server/AddServerStep.tsx @@ -40,7 +40,7 @@ export const AddServerStep: React.FC = React.memo(() => { } }); - const command = `./tianji-reporter --url ${window.location.origin} --workspace ${workspaceId}`; + const command = `./tianji-reporter --url ${window.location.origin} --workspace ${workspaceId} [--secret ]`; return ( Date: Sat, 9 Aug 2025 13:13:56 +0000 Subject: [PATCH 2/2] feat: expose public server status history without sensitive data --- src/server/model/serverStatus.ts | 16 +++++++++++++--- src/server/trpc/routers/serverStatus.ts | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/server/model/serverStatus.ts b/src/server/model/serverStatus.ts index 719c5ae2a..3c18f620d 100644 --- a/src/server/model/serverStatus.ts +++ b/src/server/model/serverStatus.ts @@ -65,7 +65,7 @@ async function getServerHistoryFromCache( const cachedValue = await cacheManager.get(key); if (cachedValue) { try { - return JSON.parse(String(cachedValue)); + return JSON.parse(String(cachedValue)); } catch (err) { logger.error('[ServerStatus] Error parsing cached history:', err); return []; @@ -191,9 +191,19 @@ export async function getServerCount(workspaceId: string): Promise { return Object.keys(serverMap).length; } -export async function getServerStatusHistory( +export async function getPublicServerStatusHistory( workspaceId: string, name: string ): Promise { - return await getServerHistoryFromCache(workspaceId, name); + const serverStatus = await getServerHistoryFromCache(workspaceId, name); + return serverStatus.map((item: ServerStatusInfo) => { + // we remove sensitive datas + const { secret, ...rest } = item; + const { top_cpu_processes, top_memory_processes, docker, ...restPayload } = + rest.payload; + return { + ...rest, + payload: restPayload, + }; + }); } diff --git a/src/server/trpc/routers/serverStatus.ts b/src/server/trpc/routers/serverStatus.ts index 451f6df3b..0996e3063 100644 --- a/src/server/trpc/routers/serverStatus.ts +++ b/src/server/trpc/routers/serverStatus.ts @@ -7,7 +7,7 @@ import { } from '../trpc.js'; import { clearOfflineServerStatus, - getServerStatusHistory, + getPublicServerStatusHistory, getServerMapFromCache, } from '../../model/serverStatus.js'; import { OPENAPI_TAG } from '../../utils/const.js'; @@ -57,7 +57,17 @@ export const serverStatusRouter = router({ const filteredServerMap: Record = {}; serverNames.forEach((name) => { if (serverMap[name]) { - filteredServerMap[name] = serverMap[name]; + const { secret, ...rest } = serverMap[name]; + const { + top_cpu_processes, + top_memory_processes, + docker, + ...restPayload + } = rest.payload; + filteredServerMap[name] = { + ...rest, + payload: restPayload, + }; } }); @@ -84,6 +94,6 @@ export const serverStatusRouter = router({ ) .query(async ({ input }) => { const { workspaceId, name } = input; - return getServerStatusHistory(workspaceId, name); + return getPublicServerStatusHistory(workspaceId, name); }), });