Skip to content

Commit 6099ccd

Browse files
committed
feat: add optional secret handling for server status reporting
1 parent 9a81ec9 commit 6099ccd

File tree

8 files changed

+81
-56
lines changed

8 files changed

+81
-56
lines changed

reporter/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ReportData struct {
1919
Hostname string `json:"hostname"`
2020
Timeout int `json:"timeout"` // if service receive after timeout second, its means client are offline
2121
Payload utils.ReportDataPayload `json:"payload"`
22+
Secret *string `json:"secret,omitempty"` // optional secret for server status
2223
}
2324

2425
var (
@@ -29,6 +30,7 @@ var (
2930
Interval = flag.Int("interval", 5.0, "Input the INTERVAL, seconed")
3031
IsVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
3132
Verbose = flag.Bool("verbose", false, "Enable verbose logging to show full payload content")
33+
Secret = flag.String("secret", "", "The server status secret, optional")
3234
)
3335

3436
var version = "1.0.0"
@@ -77,6 +79,7 @@ func main() {
7779
Hostname: hostname,
7880
Timeout: interval * 10,
7981
Payload: utils.GetReportDataPaylod(interval, *IsVnstat),
82+
Secret: Secret,
8083
}
8184

8285
if *Mode == "udp" {

scripts/start-tianji-container.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ pnpm start:docker &
77
sleep 10
88

99
# Start reporter with default workspace
10-
/usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" > /dev/null &
10+
if [ -n "$SERVER_STATUS_SECRET" ]; then
11+
/usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" --secret "$SERVER_STATUS_SECRET" > /dev/null &
12+
else
13+
/usr/local/bin/tianji-reporter --url "http://localhost:12345" --workspace "clnzoxcy10001vy2ohi4obbi0" --name "tianji-container" > /dev/null &
14+
fi
1115

1216
# Wait for any process to exit
1317
wait -n

src/client/components/server/AddServerStep.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const AddServerStep: React.FC = React.memo(() => {
4040
}
4141
});
4242

43-
const command = `./tianji-reporter --url ${window.location.origin} --workspace ${workspaceId}`;
43+
const command = `./tianji-reporter --url ${window.location.origin} --workspace ${workspaceId} [--secret <your-secret>]`;
4444

4545
return (
4646
<Steps

src/server/model/serverStatus.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createSubscribeInitializer, subscribeEventBus } from '../ws/shared.js';
77
import { isServerOnline } from '@tianji/shared';
88
import { getCacheManager } from '../cache/index.js';
99
import { logger } from '../utils/logger.js';
10+
import { env } from '../utils/env.js';
1011

1112
// Helper function to get cache key for server map
1213
function getServerMapCacheKey(workspaceId: string): string {
@@ -98,7 +99,7 @@ export async function recordServerStatus(
9899
info: ServerStatusInfo,
99100
requestContext: ServerStatusRequestContext = {}
100101
) {
101-
const { workspaceId, name, hostname, timeout, payload } = info;
102+
const { workspaceId, name, hostname, timeout, payload, secret } = info;
102103

103104
if (!workspaceId || !name || !hostname) {
104105
console.warn(
@@ -108,6 +109,14 @@ export async function recordServerStatus(
108109
return;
109110
}
110111

112+
if (env.serverStatusSecret && env.serverStatusSecret !== secret) {
113+
console.warn(
114+
'[ServerStatus] secret mismatch, request will be ignore',
115+
info
116+
);
117+
return;
118+
}
119+
111120
// Get current server map from cache
112121
const serverMap = await getServerMapFromCache(workspaceId);
113122

src/server/router/serverStatus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ serverStatusRouter.post(
1515
header('x-tianji-report-version').isSemVer(),
1616
body('workspaceId').isString(),
1717
body('name').isString(),
18+
body('secret').optional().isString(),
1819
body('hostname').isString(),
1920
body('timeout').optional().isInt(),
2021
body('payload').isObject()

src/server/utils/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const env = {
137137
debugAIFeature: checkEnvTrusty(process.env.DEBUG_AI_FEATURE), // debug ai feature
138138
debugInsights: checkEnvTrusty(process.env.DEBUG_INSIGHTS) || isDev, // debug insights
139139
enableFunctionWorker: checkEnvTrusty(process.env.ENABLE_FUNCTION_WORKER),
140+
serverStatusSecret: process.env.SERVER_STATUS_SECRET, // an optional secret for server monitoring
140141
};
141142

142143
export function checkEnvTrusty(env: string | undefined): boolean {

src/types/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ServerStatusInfo {
77
timeout: number;
88
updatedAt: number;
99
payload: ServerStatusInfoPayload & ServerStatusRequestContext;
10+
secret?: string; // optional secret
1011
}
1112

1213
export interface ServerStatusInfoPayload {

website/docs/install/environment.md

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,92 @@ Tianji supports various environment variables to customize its behavior. You can
88

99
## Basic Configuration
1010

11-
| Variable | Description | Default | Example |
12-
| --- | --- | --- | --- |
13-
| `PORT` | Server port | `12345` | `3000` |
14-
| `JWT_SECRET` | Secret for JWT tokens | Random Text | `your-secret-key` |
15-
| `ALLOW_REGISTER` | Enable user registration | `false` | `true` |
16-
| `ALLOW_OPENAPI` | Enable OpenAPI access | `true` | `false` |
17-
| `WEBSITE_ID` | Website identifier | - | `your-website-id` |
18-
| `DISABLE_AUTO_CLEAR` | Disable automatic data cleanup | `false` | `true` |
19-
| `DISABLE_ACCESS_LOGS` | Disable access logs | `false` | `true` |
20-
| `DB_DEBUG` | Enable database debugging | `false` | `true` |
11+
| Variable | Description | Default | Example |
12+
| --------------------- | ------------------------------ | ----------- | ----------------- |
13+
| `PORT` | Server port | `12345` | `3000` |
14+
| `JWT_SECRET` | Secret for JWT tokens | Random Text | `your-secret-key` |
15+
| `ALLOW_REGISTER` | Enable user registration | `false` | `true` |
16+
| `ALLOW_OPENAPI` | Enable OpenAPI access | `true` | `false` |
17+
| `WEBSITE_ID` | Website identifier | - | `your-website-id` |
18+
| `DISABLE_AUTO_CLEAR` | Disable automatic data cleanup | `false` | `true` |
19+
| `DISABLE_ACCESS_LOGS` | Disable access logs | `false` | `true` |
20+
| `DB_DEBUG` | Enable database debugging | `false` | `true` |
2121

2222
## Authentication
2323

24-
| Variable | Description | Default | Example |
25-
| --- | --- | --- | --- |
26-
| `DISABLE_ACCOUNT` | Disable account-based authentication | `false` | `true` |
27-
| `AUTH_SECRET` | Authentication secret | MD5 of JWT secret | `your-auth-secret` |
28-
| `AUTH_RESTRICT_EMAIL` | Restrict registration to specific email domains | - | `@example.com` |
24+
| Variable | Description | Default | Example |
25+
| --------------------- | ----------------------------------------------- | ----------------- | ------------------ |
26+
| `DISABLE_ACCOUNT` | Disable account-based authentication | `false` | `true` |
27+
| `AUTH_SECRET` | Authentication secret | MD5 of JWT secret | `your-auth-secret` |
28+
| `AUTH_RESTRICT_EMAIL` | Restrict registration to specific email domains | - | `@example.com` |
2929

3030
### Email Authentication and Email Invitation
3131

32-
| Variable | Description | Default | Example |
33-
| --- | --- | --- | --- |
34-
| `EMAIL_SERVER` | SMTP server for email | - | `smtp://user:[email protected]:587` |
35-
| `EMAIL_FROM` | Email sender address | - | `[email protected]` |
32+
| Variable | Description | Default | Example |
33+
| -------------- | --------------------- | ------- | --------------------------------------- |
34+
| `EMAIL_SERVER` | SMTP server for email | - | `smtp://user:[email protected]:587` |
35+
| `EMAIL_FROM` | Email sender address | - | `[email protected]` |
3636

3737
### GitHub Authentication
3838

39-
| Variable | Description | Default | Example |
40-
| --- | --- | --- | --- |
41-
| `AUTH_GITHUB_ID` | GitHub OAuth client ID | - | `your-github-client-id` |
42-
| `AUTH_GITHUB_SECRET` | GitHub OAuth client secret | - | `your-github-client-secret` |
39+
| Variable | Description | Default | Example |
40+
| -------------------- | -------------------------- | ------- | --------------------------- |
41+
| `AUTH_GITHUB_ID` | GitHub OAuth client ID | - | `your-github-client-id` |
42+
| `AUTH_GITHUB_SECRET` | GitHub OAuth client secret | - | `your-github-client-secret` |
4343

4444
### Google Authentication
4545

46-
| Variable | Description | Default | Example |
47-
| --- | --- | --- | --- |
48-
| `AUTH_GOOGLE_ID` | Google OAuth client ID | - | `your-google-client-id` |
49-
| `AUTH_GOOGLE_SECRET` | Google OAuth client secret | - | `your-google-client-secret` |
46+
| Variable | Description | Default | Example |
47+
| -------------------- | -------------------------- | ------- | --------------------------- |
48+
| `AUTH_GOOGLE_ID` | Google OAuth client ID | - | `your-google-client-id` |
49+
| `AUTH_GOOGLE_SECRET` | Google OAuth client secret | - | `your-google-client-secret` |
5050

5151
### Custom OAuth/OIDC Authentication
5252

53-
| Variable | Description | Default | Example |
54-
| --- | --- | --- | --- |
55-
| `AUTH_CUSTOM_ID` | Custom OAuth/OIDC client ID | - | `your-custom-client-id` |
56-
| `AUTH_CUSTOM_SECRET` | Custom OAuth/OIDC client secret | - | `your-custom-client-secret` |
57-
| `AUTH_CUSTOM_NAME` | Custom provider name | `Custom` | `Enterprise SSO` |
58-
| `AUTH_CUSTOM_TYPE` | Authentication type | `oidc` | `oauth` |
59-
| `AUTH_CUSTOM_ISSUER` | OIDC issuer URL | - | `https://auth.example.com` |
53+
| Variable | Description | Default | Example |
54+
| -------------------- | ------------------------------- | -------- | --------------------------- |
55+
| `AUTH_CUSTOM_ID` | Custom OAuth/OIDC client ID | - | `your-custom-client-id` |
56+
| `AUTH_CUSTOM_SECRET` | Custom OAuth/OIDC client secret | - | `your-custom-client-secret` |
57+
| `AUTH_CUSTOM_NAME` | Custom provider name | `Custom` | `Enterprise SSO` |
58+
| `AUTH_CUSTOM_TYPE` | Authentication type | `oidc` | `oauth` |
59+
| `AUTH_CUSTOM_ISSUER` | OIDC issuer URL | - | `https://auth.example.com` |
6060

6161
## AI Features
6262

63-
| Variable | Description | Default | Example |
64-
| --- | --- | --- | --- |
65-
| `SHARED_OPENAI_API_KEY` | OpenAI API key | - | `your-openai-api-key` |
66-
| `SHARED_OPENAI_BASE_URL` | Custom OpenAI API URL | - | `https://api.openai.com/v1` |
67-
| `SHARED_OPENAI_MODEL_NAME` | OpenAI model to use | `gpt-4o` | `gpt-3.5-turbo` |
68-
| `DEBUG_AI_FEATURE` | Debug AI features | `false` | `true` |
63+
| Variable | Description | Default | Example |
64+
| -------------------------- | --------------------- | -------- | --------------------------- |
65+
| `SHARED_OPENAI_API_KEY` | OpenAI API key | - | `your-openai-api-key` |
66+
| `SHARED_OPENAI_BASE_URL` | Custom OpenAI API URL | - | `https://api.openai.com/v1` |
67+
| `SHARED_OPENAI_MODEL_NAME` | OpenAI model to use | `gpt-4o` | `gpt-3.5-turbo` |
68+
| `DEBUG_AI_FEATURE` | Debug AI features | `false` | `true` |
6969

7070
## Sandbox Configuration
7171

72-
| Variable | Description | Default | Example |
73-
| --- | --- | --- | --- |
74-
| `USE_VM2` | Use VM2 for sandbox execution | `false` | `true` |
75-
| `SANDBOX_MEMORY_LIMIT` | Memory limit for sandbox (MB) | `16` | `32` |
76-
| `PUPPETEER_EXECUTABLE_PATH` | Custom path to Puppeteer executable | - | `/usr/bin/chromium` |
72+
| Variable | Description | Default | Example |
73+
| --------------------------- | ----------------------------------- | ------- | ------------------- |
74+
| `USE_VM2` | Use VM2 for sandbox execution | `false` | `true` |
75+
| `SANDBOX_MEMORY_LIMIT` | Memory limit for sandbox (MB) | `16` | `32` |
76+
| `PUPPETEER_EXECUTABLE_PATH` | Custom path to Puppeteer executable | - | `/usr/bin/chromium` |
7777

7878
## Maps Integration
7979

80-
| Variable | Description | Default | Example |
81-
| --- | --- | --- | --- |
82-
| `AMAP_TOKEN` | AMap (Gaode) API token | - | `your-amap-token` |
83-
| `MAPBOX_TOKEN` | Mapbox API token | - | `your-mapbox-token` |
80+
| Variable | Description | Default | Example |
81+
| -------------- | ---------------------- | ------- | ------------------- |
82+
| `AMAP_TOKEN` | AMap (Gaode) API token | - | `your-amap-token` |
83+
| `MAPBOX_TOKEN` | Mapbox API token | - | `your-mapbox-token` |
8484

8585
## Telemetry
8686

87-
| Variable | Description | Default | Example |
88-
| --- | --- | --- | --- |
89-
| `DISABLE_ANONYMOUS_TELEMETRY` | Disable anonymous telemetry | `false` | `true` |
90-
| `CUSTOM_TRACKER_SCRIPT_NAME` | Custom tracker script name | - | `custom-tracker.js` |
87+
| Variable | Description | Default | Example |
88+
| ----------------------------- | --------------------------- | ------- | ------------------- |
89+
| `DISABLE_ANONYMOUS_TELEMETRY` | Disable anonymous telemetry | `false` | `true` |
90+
| `CUSTOM_TRACKER_SCRIPT_NAME` | Custom tracker script name | - | `custom-tracker.js` |
91+
92+
## Server Status Reporting
93+
94+
| Variable | Description | Default | Example |
95+
| ---------------------- | ---------------------------------- | ------- | --------------------------- |
96+
| `SERVER_STATUS_SECRET` | Secret for server status reporting | - | `your-server-status-secret` |
9197

9298
## Setting Environment Variables
9399

0 commit comments

Comments
 (0)