-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathclaude-code-stealth.mjs
More file actions
142 lines (119 loc) · 4.13 KB
/
claude-code-stealth.mjs
File metadata and controls
142 lines (119 loc) · 4.13 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
/**
* Claude Code Stealth Mode
*
* Preload hook used by `scripts/dev-ui.mjs` when running the dev server under
* plain Node (no Bun).
*
* This mirrors `src/auth/claude-code-stealth.ts`, but is shipped as a root-level
* .mjs so it can be loaded via `node --import ./claude-code-stealth.mjs`.
*
* It monkey-patches global fetch to support Anthropic "Claude Code" setup tokens
* (sk-ant-oat...).
*/
const STEALTH_GUARD = Symbol.for("milady.claudeCodeStealthInstalled");
const CLAUDE_CODE_VERSION = "2.1.2";
const CLAUDE_CODE_SYSTEM_PREFIX =
"You are Claude Code, Anthropic's official CLI for Claude.";
const ANTHROPIC_BETA =
"claude-code-20250219,oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14,interleaved-thinking-2025-05-14";
function isSetupToken(value) {
return typeof value === "string" && value.startsWith("sk-ant-oat");
}
function getUrl(input) {
try {
if (typeof input === "string") return new URL(input);
if (input instanceof URL) return input;
return new URL(input.url);
} catch {
return null;
}
}
function addSystemPrefix(body) {
if (!body || typeof body !== "object") return body;
const next = body;
const prefix = { type: "text", text: CLAUDE_CODE_SYSTEM_PREFIX };
if (Array.isArray(next.system)) {
const hasPrefix = next.system.some((block) =>
block?.text?.startsWith("You are Claude Code"),
);
if (!hasPrefix) next.system.unshift(prefix);
} else if (typeof next.system === "string") {
next.system = [prefix, { type: "text", text: next.system }];
} else {
next.system = [prefix];
}
return next;
}
export function installClaudeCodeStealthFetchInterceptor() {
if (globalThis[STEALTH_GUARD]) return;
const originalFetch = globalThis.fetch.bind(globalThis);
const stealthFetch = async function stealthFetch(input, init) {
const url = getUrl(input);
if (!url || url.hostname !== "api.anthropic.com") {
return originalFetch(input, init);
}
const request = input instanceof Request ? input : null;
const headers = new Headers(init?.headers ?? request?.headers ?? undefined);
const apiKey = headers.get("x-api-key");
const authHeader = headers.get("authorization");
const bearerToken = authHeader?.startsWith("Bearer ")
? authHeader.slice(7)
: null;
const setupToken = isSetupToken(apiKey)
? apiKey
: isSetupToken(bearerToken)
? bearerToken
: null;
if (!setupToken) {
return originalFetch(input, init);
}
headers.delete("x-api-key");
headers.set("authorization", `Bearer ${setupToken}`);
headers.set("anthropic-beta", ANTHROPIC_BETA);
headers.set(
"user-agent",
`claude-cli/${CLAUDE_CODE_VERSION} (external, cli)`,
);
headers.set("x-app", "cli");
let body = init?.body ?? request?.body;
if (typeof body === "string") {
try {
const parsed = JSON.parse(body);
const updated = addSystemPrefix(parsed);
body = JSON.stringify(updated);
// eslint-disable-next-line no-console
console.log(
`[stealth] Patched Anthropic request for ${String(updated.model ?? "unknown-model")}`,
);
} catch {
// eslint-disable-next-line no-console
console.log(
"[stealth] Anthropic request body was not JSON; skipping system prefix",
);
}
}
const nextInit = {
...init,
headers,
body: init ? body : undefined,
};
if (request && !init) {
const nextRequest = new Request(request, {
headers,
body: typeof body === "string" ? body : undefined,
});
return originalFetch(nextRequest);
}
return originalFetch(input, nextInit);
};
// Preserve Bun-specific properties like `preconnect` from the original fetch.
if (globalThis.fetch && "preconnect" in globalThis.fetch) {
stealthFetch.preconnect = globalThis.fetch.preconnect;
}
globalThis.fetch = stealthFetch;
globalThis[STEALTH_GUARD] = true;
// eslint-disable-next-line no-console
console.log("[stealth] Claude Code setup token runtime support enabled");
}
// Install immediately when preloaded.
installClaudeCodeStealthFetchInterceptor();