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.
Files changed (142) hide show
  1. package/CHANGELOG.md +773 -0
  2. package/LICENSE +21 -0
  3. package/README.md +341 -0
  4. package/bin/pi-antigravity-rotator.js +27 -0
  5. package/bin/tuxevil-rotator.js +5 -0
  6. package/package.json +63 -0
  7. package/src/account-store.ts +542 -0
  8. package/src/admin-auth.ts +126 -0
  9. package/src/body-limit.ts +52 -0
  10. package/src/cli.ts +293 -0
  11. package/src/compat/cache.ts +38 -0
  12. package/src/compat/model-specs.ts +82 -0
  13. package/src/compat/schema-sanitizer.ts +284 -0
  14. package/src/compat.ts +2357 -0
  15. package/src/compression/index.ts +92 -0
  16. package/src/compression/lite.ts +210 -0
  17. package/src/compression/preservation.ts +183 -0
  18. package/src/compression/rtk/command-detector.ts +170 -0
  19. package/src/compression/rtk/deduplicator.ts +39 -0
  20. package/src/compression/rtk/filter-loader.ts +84 -0
  21. package/src/compression/rtk/filter-types.ts +25 -0
  22. package/src/compression/rtk/filters/aws.json +34 -0
  23. package/src/compression/rtk/filters/biome.json +35 -0
  24. package/src/compression/rtk/filters/build-eslint.json +37 -0
  25. package/src/compression/rtk/filters/build-typescript.json +33 -0
  26. package/src/compression/rtk/filters/build-vite.json +41 -0
  27. package/src/compression/rtk/filters/build-webpack.json +34 -0
  28. package/src/compression/rtk/filters/bundle-install.json +35 -0
  29. package/src/compression/rtk/filters/composer.json +81 -0
  30. package/src/compression/rtk/filters/curl.json +34 -0
  31. package/src/compression/rtk/filters/df.json +34 -0
  32. package/src/compression/rtk/filters/docker-build.json +78 -0
  33. package/src/compression/rtk/filters/docker-logs.json +42 -0
  34. package/src/compression/rtk/filters/docker-ps.json +33 -0
  35. package/src/compression/rtk/filters/dotnet.json +47 -0
  36. package/src/compression/rtk/filters/du.json +33 -0
  37. package/src/compression/rtk/filters/error-stacktrace.json +41 -0
  38. package/src/compression/rtk/filters/gcloud.json +34 -0
  39. package/src/compression/rtk/filters/generic-output.json +32 -0
  40. package/src/compression/rtk/filters/gh.json +98 -0
  41. package/src/compression/rtk/filters/git-branch.json +45 -0
  42. package/src/compression/rtk/filters/git-diff.json +33 -0
  43. package/src/compression/rtk/filters/git-log.json +33 -0
  44. package/src/compression/rtk/filters/git-status.json +40 -0
  45. package/src/compression/rtk/filters/golangci-lint.json +34 -0
  46. package/src/compression/rtk/filters/gradle.json +47 -0
  47. package/src/compression/rtk/filters/json-output.json +40 -0
  48. package/src/compression/rtk/filters/kubectl.json +99 -0
  49. package/src/compression/rtk/filters/make.json +38 -0
  50. package/src/compression/rtk/filters/mypy.json +34 -0
  51. package/src/compression/rtk/filters/npm-audit.json +43 -0
  52. package/src/compression/rtk/filters/npm-install.json +41 -0
  53. package/src/compression/rtk/filters/nx.json +34 -0
  54. package/src/compression/rtk/filters/pip.json +41 -0
  55. package/src/compression/rtk/filters/playwright.json +42 -0
  56. package/src/compression/rtk/filters/poetry-install.json +35 -0
  57. package/src/compression/rtk/filters/prettier.json +40 -0
  58. package/src/compression/rtk/filters/ps.json +34 -0
  59. package/src/compression/rtk/filters/rsync.json +33 -0
  60. package/src/compression/rtk/filters/rubocop.json +34 -0
  61. package/src/compression/rtk/filters/ruff.json +40 -0
  62. package/src/compression/rtk/filters/shell-find.json +33 -0
  63. package/src/compression/rtk/filters/shell-grep.json +37 -0
  64. package/src/compression/rtk/filters/shell-ls.json +33 -0
  65. package/src/compression/rtk/filters/ssh.json +43 -0
  66. package/src/compression/rtk/filters/systemctl-status.json +43 -0
  67. package/src/compression/rtk/filters/terraform-plan.json +34 -0
  68. package/src/compression/rtk/filters/test-cargo.json +42 -0
  69. package/src/compression/rtk/filters/test-go.json +41 -0
  70. package/src/compression/rtk/filters/test-jest.json +43 -0
  71. package/src/compression/rtk/filters/test-pytest.json +41 -0
  72. package/src/compression/rtk/filters/test-vitest.json +43 -0
  73. package/src/compression/rtk/filters/tofu-plan.json +34 -0
  74. package/src/compression/rtk/filters/turbo.json +34 -0
  75. package/src/compression/rtk/filters/uv-sync.json +35 -0
  76. package/src/compression/rtk/filters/wget.json +34 -0
  77. package/src/compression/rtk/index.ts +103 -0
  78. package/src/compression/rtk/line-filter.ts +124 -0
  79. package/src/compression/rtk/smart-truncate.ts +71 -0
  80. package/src/config-defaults.ts +93 -0
  81. package/src/config-normalize.ts +48 -0
  82. package/src/config-storage.ts +21 -0
  83. package/src/dashboard.ts +1169 -0
  84. package/src/db-store.ts +188 -0
  85. package/src/doctor.ts +115 -0
  86. package/src/env.ts +15 -0
  87. package/src/exposure.ts +17 -0
  88. package/src/fetch-with-retry.ts +93 -0
  89. package/src/idempotency.ts +137 -0
  90. package/src/index.ts +267 -0
  91. package/src/key-auth.ts +159 -0
  92. package/src/key-migrations.ts +84 -0
  93. package/src/logger.ts +92 -0
  94. package/src/login.ts +48 -0
  95. package/src/notification-poller.ts +155 -0
  96. package/src/onboarding.ts +754 -0
  97. package/src/paths.ts +153 -0
  98. package/src/providers/adapter.ts +131 -0
  99. package/src/providers/credential-helpers.ts +72 -0
  100. package/src/providers/google-antigravity/code-assist.ts +161 -0
  101. package/src/providers/google-antigravity/credentials.ts +24 -0
  102. package/src/providers/google-antigravity/forward.ts +424 -0
  103. package/src/providers/google-antigravity/index.ts +168 -0
  104. package/src/providers/google-antigravity/login.ts +285 -0
  105. package/src/providers/google-antigravity/oauth.ts +314 -0
  106. package/src/providers/google-antigravity/quota.ts +152 -0
  107. package/src/providers/google-antigravity/translators.ts +1663 -0
  108. package/src/providers/ollama/api-key-validation.ts +59 -0
  109. package/src/providers/ollama/credentials.ts +28 -0
  110. package/src/providers/ollama/forward.ts +306 -0
  111. package/src/providers/ollama/index.ts +85 -0
  112. package/src/providers/ollama/login.ts +55 -0
  113. package/src/providers/ollama/prediction.ts +188 -0
  114. package/src/providers/ollama/quota-poll-store.ts +160 -0
  115. package/src/providers/ollama/quota.ts +189 -0
  116. package/src/providers/ollama/translators.ts +256 -0
  117. package/src/providers/ollama/usage-windows.ts +48 -0
  118. package/src/providers/proxy-dispatcher.ts +269 -0
  119. package/src/providers/registry.ts +70 -0
  120. package/src/proxy.ts +2446 -0
  121. package/src/rate-limit-parser.ts +126 -0
  122. package/src/response-headers.ts +98 -0
  123. package/src/responses-store.ts +159 -0
  124. package/src/rotator.ts +3614 -0
  125. package/src/settings-repository.ts +327 -0
  126. package/src/spend-logger.ts +696 -0
  127. package/src/static/dashboard-keys.js +384 -0
  128. package/src/static/dashboard-logs.js +797 -0
  129. package/src/static/dashboard.css +2740 -0
  130. package/src/static/dashboard.js +3177 -0
  131. package/src/storage.ts +105 -0
  132. package/src/systemd-env.ts +214 -0
  133. package/src/telemetry.ts +384 -0
  134. package/src/token-encryption.ts +199 -0
  135. package/src/types.ts +897 -0
  136. package/src/validators.ts +218 -0
  137. package/src/version-check.ts +225 -0
  138. package/src/virtual-keys.ts +312 -0
  139. package/tools/telemetry-receiver/README.md +147 -0
  140. package/tools/telemetry-receiver/package.json +13 -0
  141. package/tools/telemetry-receiver/receiver.js +2334 -0
  142. package/tuxevil-rotator_logo.png +0 -0
@@ -0,0 +1,199 @@
1
+ import {
2
+ createCipheriv,
3
+ createDecipheriv,
4
+ createHash,
5
+ randomBytes,
6
+ scryptSync,
7
+ } from "node:crypto";
8
+ import type { Config } from "./types.js";
9
+ import { logger } from "./logger.js";
10
+ import { rotatorEnv } from "./env.js";
11
+
12
+ const V1_PREFIX = "enc:v1:";
13
+ const V2_PREFIX = "enc:v2:";
14
+ const KEY_BYTES = 32;
15
+ const V2_SALT_BYTES = 16;
16
+ const V2_KDF_CONTEXT = "pi-antigravity-rotator:encryption:v2";
17
+ const SCRYPT_OPTIONS = {
18
+ N: 16_384,
19
+ r: 8,
20
+ p: 1,
21
+ maxmem: 32 * 1024 * 1024,
22
+ };
23
+ let missingKeyWarned = false;
24
+ const tokenLog = logger.child("token-encryption");
25
+
26
+ /**
27
+ * Returns the configured encryption key from environment variables.
28
+ * Prefers TUXEVIL_ROTATOR_ENCRYPTION_KEY, falls back to ENCRYPTION_KEY.
29
+ */
30
+ export function getEncryptionKey(): string | undefined {
31
+ const key = rotatorEnv("ENCRYPTION_KEY") || process.env.ENCRYPTION_KEY;
32
+ return key && key.trim().length > 0 ? key.trim() : undefined;
33
+ }
34
+
35
+ /**
36
+ * Derives a 32-byte key for AES-256-GCM.
37
+ * High-entropy 64-hex keys are used directly; other key material goes through
38
+ * scrypt with a per-ciphertext salt when encrypting v2 tokens.
39
+ */
40
+ export function deriveKey(
41
+ keyInput: string,
42
+ salt: string | Buffer = V2_KDF_CONTEXT,
43
+ ): Buffer {
44
+ if (/^[0-9a-fA-F]{64}$/.test(keyInput)) {
45
+ return Buffer.from(keyInput, "hex");
46
+ }
47
+ return scryptSync(keyInput, salt, KEY_BYTES, SCRYPT_OPTIONS);
48
+ }
49
+
50
+ /**
51
+ * Derives the v1 key only for decrypting persisted legacy ciphertext.
52
+ * New tokens never use this compatibility path.
53
+ */
54
+ function deriveLegacyV1Key(keyInput: string): Buffer {
55
+ return createHash("sha256").update(keyInput, "utf8").digest();
56
+ }
57
+
58
+ /**
59
+ * Checks whether a refresh token string is already encrypted.
60
+ */
61
+ export function isEncryptedToken(token: string): boolean {
62
+ return (
63
+ typeof token === "string" &&
64
+ (token.startsWith(V1_PREFIX) || token.startsWith(V2_PREFIX))
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Encrypts a plain-text OAuth refresh token using AES-256-GCM.
70
+ * Format: enc:v2:<salt_hex>:<iv_hex>:<tag_hex>:<ciphertext_hex>
71
+ */
72
+ export function encryptRefreshToken(
73
+ plainToken: string,
74
+ keyInput: string,
75
+ ): string {
76
+ if (!plainToken || isEncryptedToken(plainToken)) {
77
+ return plainToken;
78
+ }
79
+ const salt = randomBytes(V2_SALT_BYTES);
80
+ const key = deriveKey(keyInput, salt);
81
+ const iv = randomBytes(12);
82
+ const cipher = createCipheriv("aes-256-gcm", key, iv);
83
+ let encrypted = cipher.update(plainToken, "utf8", "hex");
84
+ encrypted += cipher.final("hex");
85
+ const tag = cipher.getAuthTag().toString("hex");
86
+ return `${V2_PREFIX}${salt.toString("hex")}:${iv.toString("hex")}:${tag}:${encrypted}`;
87
+ }
88
+
89
+ /**
90
+ * Decrypts an AES-256-GCM encrypted refresh token.
91
+ */
92
+ export function decryptRefreshToken(
93
+ encryptedToken: string,
94
+ keyInput: string,
95
+ ): string {
96
+ if (!encryptedToken || !isEncryptedToken(encryptedToken)) {
97
+ return encryptedToken;
98
+ }
99
+ const isV2 = encryptedToken.startsWith(V2_PREFIX);
100
+ const prefix = isV2 ? V2_PREFIX : V1_PREFIX;
101
+ const payload = encryptedToken.slice(prefix.length);
102
+ const parts = payload.split(":");
103
+ if (parts.length !== (isV2 ? 4 : 3)) {
104
+ throw new Error("Malformed encrypted refresh token format");
105
+ }
106
+ const saltHex = isV2 ? parts[0] : undefined;
107
+ const ivHex = isV2 ? parts[1] : parts[0];
108
+ const tagHex = isV2 ? parts[2] : parts[1];
109
+ const ciphertextHex = isV2 ? parts[3] : parts[2];
110
+ const key = isV2
111
+ ? deriveKey(keyInput, Buffer.from(saltHex!, "hex"))
112
+ : deriveLegacyV1Key(keyInput);
113
+ const iv = Buffer.from(ivHex, "hex");
114
+ const tag = Buffer.from(tagHex, "hex");
115
+ const decipher = createDecipheriv("aes-256-gcm", key, iv);
116
+ decipher.setAuthTag(tag);
117
+ let decrypted = decipher.update(ciphertextHex, "hex", "utf8");
118
+ decrypted += decipher.final("utf8");
119
+ return decrypted;
120
+ }
121
+
122
+ /**
123
+ * Transforms a Config object by encrypting all plain account refresh tokens
124
+ * if an encryption key is present.
125
+ */
126
+ export function encryptAccountsInConfig(config: Config, keyInput?: string): Config {
127
+ const key = keyInput || getEncryptionKey();
128
+ if (!key || !config.accounts || !Array.isArray(config.accounts)) {
129
+ return config;
130
+ }
131
+ const encryptedAccounts = config.accounts.map((acc) => {
132
+ if (!acc.refreshToken || isEncryptedToken(acc.refreshToken)) {
133
+ return acc;
134
+ }
135
+ return {
136
+ ...acc,
137
+ refreshToken: encryptRefreshToken(acc.refreshToken, key),
138
+ };
139
+ });
140
+ return {
141
+ ...config,
142
+ accounts: encryptedAccounts,
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Transforms a Config object by decrypting all encrypted account refresh tokens.
148
+ * Returns the decrypted config and whether any plain-text tokens were found that
149
+ * should trigger transparent auto-migration (encryption on next save).
150
+ */
151
+ export function decryptAccountsInConfig(
152
+ config: Config,
153
+ keyInput?: string,
154
+ ): { config: Config; migrated: boolean } {
155
+ const key = keyInput || getEncryptionKey();
156
+ if (!config.accounts || !Array.isArray(config.accounts)) {
157
+ return { config, migrated: false };
158
+ }
159
+
160
+ let migrated = false;
161
+
162
+ const decryptedAccounts = config.accounts.map((acc) => {
163
+ if (!acc.refreshToken) return acc;
164
+
165
+ if (isEncryptedToken(acc.refreshToken)) {
166
+ if (!key) {
167
+ if (!missingKeyWarned) {
168
+ tokenLog.warn(
169
+ `Found encrypted refresh token for ${acc.email} but TUXEVIL_ROTATOR_ENCRYPTION_KEY is not set.`,
170
+ );
171
+ missingKeyWarned = true;
172
+ }
173
+ return acc;
174
+ }
175
+ try {
176
+ const decrypted = decryptRefreshToken(acc.refreshToken, key);
177
+ return { ...acc, refreshToken: decrypted };
178
+ } catch (err) {
179
+ tokenLog.error(
180
+ `Failed to decrypt refresh token for ${acc.email}: ${err}`,
181
+ );
182
+ return acc;
183
+ }
184
+ } else {
185
+ if (key) {
186
+ migrated = true;
187
+ }
188
+ return acc;
189
+ }
190
+ });
191
+
192
+ return {
193
+ config: {
194
+ ...config,
195
+ accounts: decryptedAccounts,
196
+ },
197
+ migrated,
198
+ };
199
+ }