forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprint.ts
More file actions
196 lines (168 loc) · 5.23 KB
/
Copy pathfingerprint.ts
File metadata and controls
196 lines (168 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Device Fingerprint Generator for Rate Limit Mitigation
*
* Ported from antigravity-claude-proxy PR #170
* https://github.com/badrisnarayanan/antigravity-claude-proxy/pull/170
*
* Generates randomized device fingerprints to help distribute API usage
* across different apparent device identities.
*/
import * as crypto from "node:crypto";
import * as os from "node:os";
import { getAntigravityVersion } from "../constants";
const OS_VERSIONS: Record<string, string[]> = {
darwin: ["10.15.7", "11.6.8", "12.6.3", "13.5.2", "14.2.1", "14.5"],
win32: ["10.0.19041", "10.0.19042", "10.0.19043", "10.0.22000", "10.0.22621", "10.0.22631"],
linux: ["5.15.0", "5.19.0", "6.1.0", "6.2.0", "6.5.0", "6.6.0"],
};
const ARCHITECTURES = ["x64", "arm64"];
const IDE_TYPES = [
"ANTIGRAVITY",
] as const;
const PLATFORMS = [
"WINDOWS",
"MACOS",
] as const;
const SDK_CLIENTS = [
"google-cloud-sdk vscode_cloudshelleditor/0.1",
"google-cloud-sdk vscode/1.86.0",
"google-cloud-sdk vscode/1.87.0",
"google-cloud-sdk vscode/1.96.0",
];
export interface ClientMetadata {
ideType: string;
platform: string;
pluginType: string;
}
export interface Fingerprint {
deviceId: string;
sessionToken: string;
userAgent: string;
apiClient: string;
clientMetadata: ClientMetadata;
createdAt: number;
/** @deprecated Kept for backward compat with stored fingerprints */
quotaUser?: string;
}
/**
* Fingerprint version for history tracking.
* Stores a snapshot of a fingerprint with metadata about when/why it was saved.
*/
export interface FingerprintVersion {
fingerprint: Fingerprint;
timestamp: number;
reason: 'initial' | 'regenerated' | 'restored';
}
/** Maximum number of fingerprint versions to keep per account */
export const MAX_FINGERPRINT_HISTORY = 5;
export interface FingerprintHeaders {
"User-Agent": string;
}
function randomFrom<T>(arr: readonly T[]): T {
return arr[Math.floor(Math.random() * arr.length)]!;
}
function generateDeviceId(): string {
return crypto.randomUUID();
}
function generateSessionToken(): string {
return crypto.randomBytes(16).toString("hex");
}
/**
* Generate a randomized device fingerprint.
* Each fingerprint represents a unique "device" identity.
*/
export function generateFingerprint(): Fingerprint {
const platform = randomFrom(["darwin", "win32"] as const);
const arch = randomFrom(ARCHITECTURES);
const osVersion = randomFrom(OS_VERSIONS[platform] ?? OS_VERSIONS.darwin!);
const matchingPlatform =
platform === "win32"
? "WINDOWS"
: "MACOS";
return {
deviceId: generateDeviceId(),
sessionToken: generateSessionToken(),
userAgent: `antigravity/${getAntigravityVersion()} ${platform}/${arch}`,
apiClient: randomFrom(SDK_CLIENTS),
clientMetadata: {
ideType: randomFrom(IDE_TYPES),
platform: matchingPlatform,
pluginType: "GEMINI",
},
createdAt: Date.now(),
};
}
/**
* Collect fingerprint based on actual current system.
* Uses real OS info instead of randomized values.
*/
export function collectCurrentFingerprint(): Fingerprint {
const platform = os.platform();
const arch = os.arch();
const matchingPlatform =
platform === "win32"
? "WINDOWS"
: "MACOS";
return {
deviceId: generateDeviceId(),
sessionToken: generateSessionToken(),
userAgent: `antigravity/${getAntigravityVersion()} ${platform}/${arch}`,
apiClient: "google-cloud-sdk vscode_cloudshelleditor/0.1",
clientMetadata: {
ideType: "ANTIGRAVITY",
platform: matchingPlatform,
pluginType: "GEMINI",
},
createdAt: Date.now(),
};
}
/**
* Update the version in a fingerprint's userAgent to match the current runtime version.
* Called after version fetcher resolves so saved fingerprints always carry the latest version.
* Returns true if the userAgent was changed.
*/
export function updateFingerprintVersion(fingerprint: Fingerprint): boolean {
const currentVersion = getAntigravityVersion();
const versionPattern = /^(antigravity\/)([\d.]+)/;
const match = fingerprint.userAgent.match(versionPattern);
if (!match || match[2] === currentVersion) {
return false;
}
fingerprint.userAgent = fingerprint.userAgent.replace(versionPattern, `$1${currentVersion}`);
return true;
}
/**
* Build HTTP headers from a fingerprint object.
* These headers are used to identify the "device" making API requests.
*/
export function buildFingerprintHeaders(fingerprint: Fingerprint | null): Partial<FingerprintHeaders> {
if (!fingerprint) {
return {};
}
return {
"User-Agent": fingerprint.userAgent,
};
}
/**
* Session-level fingerprint instance.
* Generated once at module load, persists for the lifetime of the process.
*/
let sessionFingerprint: Fingerprint | null = null;
/**
* Get or create the session fingerprint.
* Returns the same fingerprint for all calls within a session.
*/
export function getSessionFingerprint(): Fingerprint {
if (!sessionFingerprint) {
sessionFingerprint = generateFingerprint();
}
return sessionFingerprint;
}
/**
* Regenerate the session fingerprint.
* Call this to get a fresh identity (e.g., after rate limiting).
*/
export function regenerateSessionFingerprint(): Fingerprint {
sessionFingerprint = generateFingerprint();
return sessionFingerprint;
}