tuxevil-rotator 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +773 -0
- package/LICENSE +21 -0
- package/README.md +341 -0
- package/bin/pi-antigravity-rotator.js +27 -0
- package/bin/tuxevil-rotator.js +5 -0
- package/package.json +63 -0
- package/src/account-store.ts +542 -0
- package/src/admin-auth.ts +126 -0
- package/src/body-limit.ts +52 -0
- package/src/cli.ts +293 -0
- package/src/compat/cache.ts +38 -0
- package/src/compat/model-specs.ts +82 -0
- package/src/compat/schema-sanitizer.ts +284 -0
- package/src/compat.ts +2357 -0
- package/src/compression/index.ts +92 -0
- package/src/compression/lite.ts +210 -0
- package/src/compression/preservation.ts +183 -0
- package/src/compression/rtk/command-detector.ts +170 -0
- package/src/compression/rtk/deduplicator.ts +39 -0
- package/src/compression/rtk/filter-loader.ts +84 -0
- package/src/compression/rtk/filter-types.ts +25 -0
- package/src/compression/rtk/filters/aws.json +34 -0
- package/src/compression/rtk/filters/biome.json +35 -0
- package/src/compression/rtk/filters/build-eslint.json +37 -0
- package/src/compression/rtk/filters/build-typescript.json +33 -0
- package/src/compression/rtk/filters/build-vite.json +41 -0
- package/src/compression/rtk/filters/build-webpack.json +34 -0
- package/src/compression/rtk/filters/bundle-install.json +35 -0
- package/src/compression/rtk/filters/composer.json +81 -0
- package/src/compression/rtk/filters/curl.json +34 -0
- package/src/compression/rtk/filters/df.json +34 -0
- package/src/compression/rtk/filters/docker-build.json +78 -0
- package/src/compression/rtk/filters/docker-logs.json +42 -0
- package/src/compression/rtk/filters/docker-ps.json +33 -0
- package/src/compression/rtk/filters/dotnet.json +47 -0
- package/src/compression/rtk/filters/du.json +33 -0
- package/src/compression/rtk/filters/error-stacktrace.json +41 -0
- package/src/compression/rtk/filters/gcloud.json +34 -0
- package/src/compression/rtk/filters/generic-output.json +32 -0
- package/src/compression/rtk/filters/gh.json +98 -0
- package/src/compression/rtk/filters/git-branch.json +45 -0
- package/src/compression/rtk/filters/git-diff.json +33 -0
- package/src/compression/rtk/filters/git-log.json +33 -0
- package/src/compression/rtk/filters/git-status.json +40 -0
- package/src/compression/rtk/filters/golangci-lint.json +34 -0
- package/src/compression/rtk/filters/gradle.json +47 -0
- package/src/compression/rtk/filters/json-output.json +40 -0
- package/src/compression/rtk/filters/kubectl.json +99 -0
- package/src/compression/rtk/filters/make.json +38 -0
- package/src/compression/rtk/filters/mypy.json +34 -0
- package/src/compression/rtk/filters/npm-audit.json +43 -0
- package/src/compression/rtk/filters/npm-install.json +41 -0
- package/src/compression/rtk/filters/nx.json +34 -0
- package/src/compression/rtk/filters/pip.json +41 -0
- package/src/compression/rtk/filters/playwright.json +42 -0
- package/src/compression/rtk/filters/poetry-install.json +35 -0
- package/src/compression/rtk/filters/prettier.json +40 -0
- package/src/compression/rtk/filters/ps.json +34 -0
- package/src/compression/rtk/filters/rsync.json +33 -0
- package/src/compression/rtk/filters/rubocop.json +34 -0
- package/src/compression/rtk/filters/ruff.json +40 -0
- package/src/compression/rtk/filters/shell-find.json +33 -0
- package/src/compression/rtk/filters/shell-grep.json +37 -0
- package/src/compression/rtk/filters/shell-ls.json +33 -0
- package/src/compression/rtk/filters/ssh.json +43 -0
- package/src/compression/rtk/filters/systemctl-status.json +43 -0
- package/src/compression/rtk/filters/terraform-plan.json +34 -0
- package/src/compression/rtk/filters/test-cargo.json +42 -0
- package/src/compression/rtk/filters/test-go.json +41 -0
- package/src/compression/rtk/filters/test-jest.json +43 -0
- package/src/compression/rtk/filters/test-pytest.json +41 -0
- package/src/compression/rtk/filters/test-vitest.json +43 -0
- package/src/compression/rtk/filters/tofu-plan.json +34 -0
- package/src/compression/rtk/filters/turbo.json +34 -0
- package/src/compression/rtk/filters/uv-sync.json +35 -0
- package/src/compression/rtk/filters/wget.json +34 -0
- package/src/compression/rtk/index.ts +103 -0
- package/src/compression/rtk/line-filter.ts +124 -0
- package/src/compression/rtk/smart-truncate.ts +71 -0
- package/src/config-defaults.ts +93 -0
- package/src/config-normalize.ts +48 -0
- package/src/config-storage.ts +21 -0
- package/src/dashboard.ts +1169 -0
- package/src/db-store.ts +188 -0
- package/src/doctor.ts +115 -0
- package/src/env.ts +15 -0
- package/src/exposure.ts +17 -0
- package/src/fetch-with-retry.ts +93 -0
- package/src/idempotency.ts +137 -0
- package/src/index.ts +267 -0
- package/src/key-auth.ts +159 -0
- package/src/key-migrations.ts +84 -0
- package/src/logger.ts +92 -0
- package/src/login.ts +48 -0
- package/src/notification-poller.ts +155 -0
- package/src/onboarding.ts +754 -0
- package/src/paths.ts +153 -0
- package/src/providers/adapter.ts +131 -0
- package/src/providers/credential-helpers.ts +72 -0
- package/src/providers/google-antigravity/code-assist.ts +161 -0
- package/src/providers/google-antigravity/credentials.ts +24 -0
- package/src/providers/google-antigravity/forward.ts +424 -0
- package/src/providers/google-antigravity/index.ts +168 -0
- package/src/providers/google-antigravity/login.ts +285 -0
- package/src/providers/google-antigravity/oauth.ts +314 -0
- package/src/providers/google-antigravity/quota.ts +152 -0
- package/src/providers/google-antigravity/translators.ts +1663 -0
- package/src/providers/ollama/api-key-validation.ts +59 -0
- package/src/providers/ollama/credentials.ts +28 -0
- package/src/providers/ollama/forward.ts +306 -0
- package/src/providers/ollama/index.ts +85 -0
- package/src/providers/ollama/login.ts +55 -0
- package/src/providers/ollama/prediction.ts +188 -0
- package/src/providers/ollama/quota-poll-store.ts +160 -0
- package/src/providers/ollama/quota.ts +189 -0
- package/src/providers/ollama/translators.ts +256 -0
- package/src/providers/ollama/usage-windows.ts +48 -0
- package/src/providers/proxy-dispatcher.ts +269 -0
- package/src/providers/registry.ts +70 -0
- package/src/proxy.ts +2446 -0
- package/src/rate-limit-parser.ts +126 -0
- package/src/response-headers.ts +98 -0
- package/src/responses-store.ts +159 -0
- package/src/rotator.ts +3614 -0
- package/src/settings-repository.ts +327 -0
- package/src/spend-logger.ts +696 -0
- package/src/static/dashboard-keys.js +384 -0
- package/src/static/dashboard-logs.js +797 -0
- package/src/static/dashboard.css +2740 -0
- package/src/static/dashboard.js +3177 -0
- package/src/storage.ts +105 -0
- package/src/systemd-env.ts +214 -0
- package/src/telemetry.ts +384 -0
- package/src/token-encryption.ts +199 -0
- package/src/types.ts +897 -0
- package/src/validators.ts +218 -0
- package/src/version-check.ts +225 -0
- package/src/virtual-keys.ts +312 -0
- package/tools/telemetry-receiver/README.md +147 -0
- package/tools/telemetry-receiver/package.json +13 -0
- package/tools/telemetry-receiver/receiver.js +2334 -0
- package/tuxevil-rotator_logo.png +0 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { pbkdf2Sync, randomBytes } from "node:crypto";
|
|
2
|
+
import type { VirtualKey } from "./types.js";
|
|
3
|
+
import { isDbConfigured, queryDb } from "./db-store.js";
|
|
4
|
+
|
|
5
|
+
const KEY_PREFIX = "rk-";
|
|
6
|
+
const CACHE_TTL_MS = 60_000; // 1 minute in-memory cache
|
|
7
|
+
|
|
8
|
+
// PBKDF2 salts for virtual-key hashing. New keys use the current salt;
|
|
9
|
+
// the legacy salt is kept so pre-rebrand keys continue to verify.
|
|
10
|
+
const VK_SALT_CURRENT = "tuxevil-rotator-vk-salt";
|
|
11
|
+
const VK_SALT_LEGACY = "pi-antigravity-rotator-vk-salt";
|
|
12
|
+
|
|
13
|
+
interface CachedKey {
|
|
14
|
+
key: VirtualKey | null;
|
|
15
|
+
fetchedAt: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const keyCache = new Map<string, CachedKey>(); // tokenHash -> CachedKey
|
|
19
|
+
let hasKeysCache: { value: boolean; fetchedAt: number } | null = null;
|
|
20
|
+
|
|
21
|
+
// Debounced last_active updates to prevent DB write spam
|
|
22
|
+
const pendingLastActive = new Set<string>();
|
|
23
|
+
let lastActiveFlushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
24
|
+
|
|
25
|
+
export function hashKeyWithSalt(rawKey: string, salt: string): string {
|
|
26
|
+
return pbkdf2Sync(rawKey.trim(), salt, 10_000, 32, "sha256").toString("hex");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function hashKey(rawKey: string): string {
|
|
30
|
+
return hashKeyWithSalt(rawKey, VK_SALT_CURRENT);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function legacyHashKey(rawKey: string): string {
|
|
34
|
+
return hashKeyWithSalt(rawKey, VK_SALT_LEGACY);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function maskKey(rawKey: string): string {
|
|
38
|
+
if (rawKey.length <= 10) return `${rawKey.slice(0, 4)}...`;
|
|
39
|
+
return `${rawKey.slice(0, 6)}...${rawKey.slice(-4)}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function mapRowToVirtualKey(row: Record<string, unknown>): VirtualKey {
|
|
43
|
+
return {
|
|
44
|
+
tokenHash: String(row.token_hash),
|
|
45
|
+
keyName: String(row.key_name),
|
|
46
|
+
keyAlias: String(row.key_alias),
|
|
47
|
+
userId: row.user_id ? String(row.user_id) : null,
|
|
48
|
+
models: Array.isArray(row.models) ? (row.models as string[]) : [],
|
|
49
|
+
metadata:
|
|
50
|
+
row.metadata && typeof row.metadata === "object"
|
|
51
|
+
? (row.metadata as Record<string, unknown>)
|
|
52
|
+
: {},
|
|
53
|
+
blocked: Boolean(row.blocked),
|
|
54
|
+
lastActive: row.last_active
|
|
55
|
+
? new Date(row.last_active as string | number | Date).toISOString()
|
|
56
|
+
: null,
|
|
57
|
+
createdAt: new Date(
|
|
58
|
+
row.created_at as string | number | Date,
|
|
59
|
+
).toISOString(),
|
|
60
|
+
createdBy: row.created_by ? String(row.created_by) : null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function clearVirtualKeyCache(): void {
|
|
65
|
+
keyCache.clear();
|
|
66
|
+
hasKeysCache = null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Checks whether any virtual keys exist in DB. Cache for 10 seconds.
|
|
71
|
+
*/
|
|
72
|
+
export async function hasAnyVirtualKeys(): Promise<boolean> {
|
|
73
|
+
if (!isDbConfigured()) return false;
|
|
74
|
+
|
|
75
|
+
const now = Date.now();
|
|
76
|
+
if (hasKeysCache && now - hasKeysCache.fetchedAt < 10_000) {
|
|
77
|
+
return hasKeysCache.value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const res = await queryDb<{ count: string }>(
|
|
82
|
+
"SELECT COUNT(*)::text as count FROM rotator_virtual_keys",
|
|
83
|
+
);
|
|
84
|
+
const count = parseInt(res.rows[0]?.count || "0", 10);
|
|
85
|
+
const exists = count > 0;
|
|
86
|
+
hasKeysCache = { value: exists, fetchedAt: now };
|
|
87
|
+
return exists;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
console.error("Failed to check virtual keys count:", err);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Generate a new virtual key (format: rk-{32 random hex chars}).
|
|
96
|
+
* Returns raw key (shown only once to caller) and the stored VirtualKey object.
|
|
97
|
+
*/
|
|
98
|
+
export async function generateVirtualKey(params: {
|
|
99
|
+
alias: string;
|
|
100
|
+
userId?: string;
|
|
101
|
+
models?: string[];
|
|
102
|
+
metadata?: Record<string, unknown>;
|
|
103
|
+
createdBy?: string;
|
|
104
|
+
}): Promise<{ rawKey: string; key: VirtualKey }> {
|
|
105
|
+
if (!isDbConfigured()) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
"Virtual keys require PostgreSQL database (TUXEVIL_ROTATOR_DATABASE_URL)",
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const randomHex = randomBytes(16).toString("hex");
|
|
112
|
+
const rawKey = `${KEY_PREFIX}${randomHex}`;
|
|
113
|
+
const tokenHash = hashKey(rawKey);
|
|
114
|
+
const keyName = maskKey(rawKey);
|
|
115
|
+
|
|
116
|
+
const res = await queryDb(
|
|
117
|
+
`INSERT INTO rotator_virtual_keys
|
|
118
|
+
(token_hash, key_name, key_alias, user_id, models, metadata, created_by)
|
|
119
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
120
|
+
RETURNING *`,
|
|
121
|
+
[
|
|
122
|
+
tokenHash,
|
|
123
|
+
keyName,
|
|
124
|
+
params.alias,
|
|
125
|
+
params.userId || null,
|
|
126
|
+
params.models && params.models.length > 0 ? params.models : [],
|
|
127
|
+
JSON.stringify(params.metadata || {}),
|
|
128
|
+
params.createdBy || "admin",
|
|
129
|
+
],
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
const key = mapRowToVirtualKey(res.rows[0]);
|
|
133
|
+
keyCache.set(tokenHash, { key, fetchedAt: Date.now() });
|
|
134
|
+
hasKeysCache = { value: true, fetchedAt: Date.now() };
|
|
135
|
+
|
|
136
|
+
return { rawKey, key };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Look up a key by its raw value (e.g. "rk-a1b2...").
|
|
141
|
+
*/
|
|
142
|
+
export async function lookupVirtualKey(
|
|
143
|
+
rawKey: string,
|
|
144
|
+
): Promise<VirtualKey | null> {
|
|
145
|
+
if (!isDbConfigured() || !rawKey || !rawKey.startsWith(KEY_PREFIX)) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const tokenHash = hashKey(rawKey);
|
|
150
|
+
const now = Date.now();
|
|
151
|
+
const cached = keyCache.get(tokenHash);
|
|
152
|
+
|
|
153
|
+
if (cached && now - cached.fetchedAt < CACHE_TTL_MS) {
|
|
154
|
+
return cached.key;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
const res = await queryDb(
|
|
159
|
+
"SELECT * FROM rotator_virtual_keys WHERE token_hash = $1 OR token_hash = $2 LIMIT 1",
|
|
160
|
+
[tokenHash, legacyHashKey(rawKey)],
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (res.rows.length === 0) {
|
|
164
|
+
keyCache.set(tokenHash, { key: null, fetchedAt: now });
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const key = mapRowToVirtualKey(res.rows[0]);
|
|
169
|
+
keyCache.set(tokenHash, { key, fetchedAt: now });
|
|
170
|
+
return key;
|
|
171
|
+
} catch (err) {
|
|
172
|
+
console.error("Failed to lookup virtual key:", err);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* List all virtual keys ordered by created_at DESC.
|
|
179
|
+
*/
|
|
180
|
+
export async function listVirtualKeys(): Promise<VirtualKey[]> {
|
|
181
|
+
if (!isDbConfigured()) return [];
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const res = await queryDb(
|
|
185
|
+
"SELECT * FROM rotator_virtual_keys ORDER BY created_at DESC",
|
|
186
|
+
);
|
|
187
|
+
return res.rows.map(mapRowToVirtualKey);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
console.error("Failed to list virtual keys:", err);
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Get a virtual key by token hash.
|
|
196
|
+
*/
|
|
197
|
+
export async function getVirtualKeyByHash(
|
|
198
|
+
tokenHash: string,
|
|
199
|
+
): Promise<VirtualKey | null> {
|
|
200
|
+
if (!isDbConfigured()) return null;
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
const res = await queryDb(
|
|
204
|
+
"SELECT * FROM rotator_virtual_keys WHERE token_hash = $1",
|
|
205
|
+
[tokenHash],
|
|
206
|
+
);
|
|
207
|
+
if (res.rows.length === 0) return null;
|
|
208
|
+
return mapRowToVirtualKey(res.rows[0]);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
console.error("Failed to get virtual key by hash:", err);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Update alias, models, blocked status, or metadata of a key.
|
|
217
|
+
*/
|
|
218
|
+
export async function updateVirtualKey(
|
|
219
|
+
tokenHash: string,
|
|
220
|
+
updates: {
|
|
221
|
+
alias?: string;
|
|
222
|
+
models?: string[];
|
|
223
|
+
blocked?: boolean;
|
|
224
|
+
metadata?: Record<string, unknown>;
|
|
225
|
+
},
|
|
226
|
+
): Promise<VirtualKey | null> {
|
|
227
|
+
if (!isDbConfigured()) return null;
|
|
228
|
+
|
|
229
|
+
const current = await getVirtualKeyByHash(tokenHash);
|
|
230
|
+
if (!current) return null;
|
|
231
|
+
|
|
232
|
+
const newAlias = updates.alias !== undefined ? updates.alias : current.keyAlias;
|
|
233
|
+
const newModels =
|
|
234
|
+
updates.models !== undefined ? updates.models : current.models;
|
|
235
|
+
const newBlocked =
|
|
236
|
+
updates.blocked !== undefined ? updates.blocked : current.blocked;
|
|
237
|
+
const newMetadata =
|
|
238
|
+
updates.metadata !== undefined
|
|
239
|
+
? JSON.stringify(updates.metadata)
|
|
240
|
+
: JSON.stringify(current.metadata || {});
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
const res = await queryDb(
|
|
244
|
+
`UPDATE rotator_virtual_keys
|
|
245
|
+
SET key_alias = $1, models = $2, blocked = $3, metadata = $4
|
|
246
|
+
WHERE token_hash = $5
|
|
247
|
+
RETURNING *`,
|
|
248
|
+
[newAlias, newModels, newBlocked, newMetadata, tokenHash],
|
|
249
|
+
);
|
|
250
|
+
if (res.rows.length === 0) return null;
|
|
251
|
+
|
|
252
|
+
const updated = mapRowToVirtualKey(res.rows[0]);
|
|
253
|
+
keyCache.set(tokenHash, { key: updated, fetchedAt: Date.now() });
|
|
254
|
+
return updated;
|
|
255
|
+
} catch (err) {
|
|
256
|
+
console.error("Failed to update virtual key:", err);
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Delete a virtual key by hash.
|
|
263
|
+
*/
|
|
264
|
+
export async function deleteVirtualKey(tokenHash: string): Promise<boolean> {
|
|
265
|
+
if (!isDbConfigured()) return false;
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
const res = await queryDb(
|
|
269
|
+
"DELETE FROM rotator_virtual_keys WHERE token_hash = $1",
|
|
270
|
+
[tokenHash],
|
|
271
|
+
);
|
|
272
|
+
keyCache.delete(tokenHash);
|
|
273
|
+
hasKeysCache = null;
|
|
274
|
+
return (res.rowCount ?? 0) > 0;
|
|
275
|
+
} catch (err) {
|
|
276
|
+
console.error("Failed to delete virtual key:", err);
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Debounced background update of last_active timestamp for a key.
|
|
283
|
+
*/
|
|
284
|
+
export function touchVirtualKeyLastActive(tokenHash: string): void {
|
|
285
|
+
if (!isDbConfigured()) return;
|
|
286
|
+
pendingLastActive.add(tokenHash);
|
|
287
|
+
|
|
288
|
+
if (!lastActiveFlushTimer) {
|
|
289
|
+
lastActiveFlushTimer = setTimeout(() => {
|
|
290
|
+
lastActiveFlushTimer = null;
|
|
291
|
+
void flushLastActive();
|
|
292
|
+
}, 10_000);
|
|
293
|
+
lastActiveFlushTimer.unref();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async function flushLastActive(): Promise<void> {
|
|
298
|
+
if (pendingLastActive.size === 0) return;
|
|
299
|
+
const hashes = Array.from(pendingLastActive);
|
|
300
|
+
pendingLastActive.clear();
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
await queryDb(
|
|
304
|
+
`UPDATE rotator_virtual_keys
|
|
305
|
+
SET last_active = NOW()
|
|
306
|
+
WHERE token_hash = ANY($1::text[])`,
|
|
307
|
+
[hashes],
|
|
308
|
+
);
|
|
309
|
+
} catch (err) {
|
|
310
|
+
console.error("Failed to update virtual key last_active:", err);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Telemetry Receiver
|
|
2
|
+
|
|
3
|
+
Minimal HTTP server that receives anonymous telemetry events from tuxevil-rotator instances and stores them as JSONL files (one per day).
|
|
4
|
+
|
|
5
|
+
[View the hosted public stats page](https://telemetry.tuxevil.com/stats).
|
|
6
|
+
|
|
7
|
+
**Zero dependencies** — runs on Node.js 18+ with native `http`/`fs`.
|
|
8
|
+
|
|
9
|
+
## Deploy to your VPS
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Copy this directory to your VPS
|
|
13
|
+
scp -r tools/telemetry-receiver/ user@your-vps:/opt/telemetry-receiver/
|
|
14
|
+
|
|
15
|
+
# On the VPS:
|
|
16
|
+
cd /opt/telemetry-receiver
|
|
17
|
+
export PORT=3800
|
|
18
|
+
export DATA_DIR=/var/lib/rotator-telemetry
|
|
19
|
+
export STATS_TOKEN=$(openssl rand -hex 32)
|
|
20
|
+
node receiver.js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### With systemd
|
|
24
|
+
|
|
25
|
+
```ini
|
|
26
|
+
# /etc/systemd/system/rotator-telemetry.service
|
|
27
|
+
[Unit]
|
|
28
|
+
Description=Tuxevil Rotator Telemetry Receiver
|
|
29
|
+
After=network.target
|
|
30
|
+
|
|
31
|
+
[Service]
|
|
32
|
+
Type=simple
|
|
33
|
+
User=telemetry
|
|
34
|
+
WorkingDirectory=/opt/telemetry-receiver
|
|
35
|
+
ExecStart=/usr/bin/node receiver.js
|
|
36
|
+
Restart=always
|
|
37
|
+
RestartSec=5
|
|
38
|
+
Environment=PORT=3800
|
|
39
|
+
Environment=DATA_DIR=/var/lib/rotator-telemetry
|
|
40
|
+
Environment=STATS_TOKEN=your-secret-token-here
|
|
41
|
+
|
|
42
|
+
[Install]
|
|
43
|
+
WantedBy=multi-user.target
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
sudo systemctl enable --now rotator-telemetry
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### With nginx reverse proxy (HTTPS)
|
|
51
|
+
|
|
52
|
+
```nginx
|
|
53
|
+
server {
|
|
54
|
+
listen 443 ssl;
|
|
55
|
+
server_name telemetry.yourdomain.com;
|
|
56
|
+
|
|
57
|
+
ssl_certificate /etc/letsencrypt/live/telemetry.yourdomain.com/fullchain.pem;
|
|
58
|
+
ssl_certificate_key /etc/letsencrypt/live/telemetry.yourdomain.com/privkey.pem;
|
|
59
|
+
|
|
60
|
+
location / {
|
|
61
|
+
proxy_pass http://127.0.0.1:3800;
|
|
62
|
+
proxy_set_header X-Forwarded-For $remote_addr;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Endpoints
|
|
68
|
+
|
|
69
|
+
| Method | Path | Auth | Description |
|
|
70
|
+
|---|---|---|---|
|
|
71
|
+
| `POST` | `/v1/events` | none | Receive telemetry payload |
|
|
72
|
+
| `GET` | `/v1/stats` | `Bearer STATS_TOKEN` | Aggregate statistics |
|
|
73
|
+
| `GET` | `/v1/public-stats` | none | Public aggregate stats for badges (5-min cache) |
|
|
74
|
+
| `GET` | `/stats` | none | Public aggregate stats page |
|
|
75
|
+
| `GET` | `/v1/health` | none | Health check |
|
|
76
|
+
|
|
77
|
+
### Public Stats Response
|
|
78
|
+
|
|
79
|
+
`GET /v1/public-stats` returns high-level aggregates with no auth required:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"installs": 226,
|
|
84
|
+
"installsFormatted": "226",
|
|
85
|
+
"requests": 361288770,
|
|
86
|
+
"requestsFormatted": "361.2M",
|
|
87
|
+
"savingsUsd": 67947.29,
|
|
88
|
+
"savingsFormatted": "$68K",
|
|
89
|
+
"tokensInput": 48500000000,
|
|
90
|
+
"tokensInputFormatted": "48.5B",
|
|
91
|
+
"tokensOutput": 12300000000,
|
|
92
|
+
"tokensOutputFormatted": "12.3B",
|
|
93
|
+
"updatedAt": "2026-07-28T12:00:00.000Z"
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Results are cached for 5 minutes. The `*Formatted` fields are human-readable (e.g. `361.2M`, `$68K`) and suitable for shields.io dynamic badges.
|
|
98
|
+
|
|
99
|
+
## Environment Variables
|
|
100
|
+
|
|
101
|
+
| Variable | Default | Description |
|
|
102
|
+
|---|---|---|
|
|
103
|
+
| `PORT` | `3800` | Listen port |
|
|
104
|
+
| `DATA_DIR` | `./data` | Directory for JSONL files |
|
|
105
|
+
| `STATS_TOKEN` | *(empty)* | Bearer token for `/v1/stats` (required to enable stats) |
|
|
106
|
+
|
|
107
|
+
## Data Format
|
|
108
|
+
|
|
109
|
+
Each JSONL line:
|
|
110
|
+
|
|
111
|
+
```json
|
|
112
|
+
{
|
|
113
|
+
"event": "boot",
|
|
114
|
+
"installId": "a1b2c3d4-...",
|
|
115
|
+
"version": "1.7.0",
|
|
116
|
+
"nodeVersion": "v22.12.0",
|
|
117
|
+
"os": "linux",
|
|
118
|
+
"arch": "x64",
|
|
119
|
+
"ts": "2026-04-29T12:00:00.000Z",
|
|
120
|
+
"receivedAt": "2026-04-29T12:00:01.123Z",
|
|
121
|
+
"accountCount": 5,
|
|
122
|
+
"modelsUsed": ["gemini-3-flash", "claude-opus-4-6-thinking"],
|
|
123
|
+
"totalRequests": 1234,
|
|
124
|
+
"uptimeSeconds": 86400,
|
|
125
|
+
"routingHealthState": "healthy",
|
|
126
|
+
"flaggedCount": 0,
|
|
127
|
+
"disabledCount": 0,
|
|
128
|
+
"proCount": 3,
|
|
129
|
+
"freeCount": 2,
|
|
130
|
+
"featuresUsed": { "dashboard": true, "proAdvisor": false, "freshWindowToggle": true, "hostedLogin": false }
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Query stats
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
curl -H "Authorization: Bearer YOUR_STATS_TOKEN" https://telemetry.yourdomain.com/v1/stats | jq .
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Security
|
|
141
|
+
|
|
142
|
+
- **No PII is stored in telemetry event files**: no emails, tokens, project IDs, request bodies, or error text
|
|
143
|
+
- Source IPs are used transiently for in-memory rate limiting and may be visible to your network, host, or reverse-proxy logs. The receiver does not write IPs into telemetry JSONL event files.
|
|
144
|
+
- Email-pattern detection in validation: payloads containing `@` email patterns are rejected
|
|
145
|
+
- Payloads capped at 4KB
|
|
146
|
+
- Rate limited: 12 req/min per IP
|
|
147
|
+
- Stats endpoint requires Bearer token
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tuxevil-rotator-telemetry-receiver",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Minimal telemetry receiver for tuxevil-rotator",
|
|
5
|
+
"private": true,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node receiver.js"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18"
|
|
12
|
+
}
|
|
13
|
+
}
|