mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
Fix : Updated Anthropic-Beta header.
This commit is contained in:
@@ -1,36 +1,106 @@
|
||||
import { createHash, randomBytes } from "crypto";
|
||||
import { createHash, randomBytes, randomUUID } from "crypto";
|
||||
import { CLAUDE_TOOL_SUFFIX, CC_DEFAULT_TOOLS } from "../config/appConstants.js";
|
||||
|
||||
const CLAUDE_VERSION = "2.1.63";
|
||||
const CLAUDE_VERSION = "2.1.92";
|
||||
const CC_ENTRYPOINT = "sdk-cli";
|
||||
|
||||
// Generate billing header matching real Claude Code format:
|
||||
// x-anthropic-billing-header: cc_version=<ver>.<build>; cc_entrypoint=cli; cch=<hash>;
|
||||
// Generate billing header matching real Claude Code 2.1.92+ format:
|
||||
// x-anthropic-billing-header: cc_version=<ver>.<build>; cc_entrypoint=sdk-cli; cch=<hash>;
|
||||
function generateBillingHeader(payload) {
|
||||
const content = JSON.stringify(payload);
|
||||
const cch = createHash("sha256").update(content).digest("hex").slice(0, 5);
|
||||
const buildHash = randomBytes(2).toString("hex").slice(0, 3);
|
||||
return `x-anthropic-billing-header: cc_version=${CLAUDE_VERSION}.${buildHash}; cc_entrypoint=cli; cch=${cch};`;
|
||||
return `x-anthropic-billing-header: cc_version=${CLAUDE_VERSION}.${buildHash}; cc_entrypoint=${CC_ENTRYPOINT}; cch=${cch};`;
|
||||
}
|
||||
|
||||
// Generate a random UUID v4
|
||||
function generateFakeUserID() {
|
||||
const bytes = randomBytes(16);
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
||||
const hex = bytes.toString("hex");
|
||||
return `${hex.slice(0,8)}-${hex.slice(8,12)}-${hex.slice(12,16)}-${hex.slice(16,20)}-${hex.slice(20)}`;
|
||||
// Generate fake user ID in Claude Code 2.1.92+ JSON format:
|
||||
// {"device_id":"<64hex>","account_uuid":"<uuid>","session_id":"<uuid>"}
|
||||
function generateFakeUserID(sessionId) {
|
||||
const deviceId = randomBytes(32).toString("hex");
|
||||
const accountUuid = randomUUID();
|
||||
const sessionUuid = sessionId || randomUUID();
|
||||
return `{"device_id":"${deviceId}","account_uuid":"${accountUuid}","session_id":"${sessionUuid}"}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cloak tools before sending to Claude provider (anti-ban):
|
||||
* - Rename non-CC client tools with _cc suffix in tools[] and messages[]
|
||||
* - Skip tools that are already CC default names (they become decoys as-is)
|
||||
* - Inject CC_DECOY_TOOLS after client tools
|
||||
* Returns { body, toolNameMap } where toolNameMap maps suffixed → original
|
||||
* @param {object} body - Claude API request body
|
||||
* @returns {{ body: object, toolNameMap: Map|null }}
|
||||
*/
|
||||
export function cloakClaudeTools(body) {
|
||||
const tools = body.tools;
|
||||
if (!tools || tools.length === 0) return { body, toolNameMap: null };
|
||||
|
||||
const toolNameMap = new Map();
|
||||
const clientDeclarations = [];
|
||||
|
||||
// All client tools get renamed with suffix
|
||||
for (const tool of tools) {
|
||||
const suffixed = `${tool.name}${CLAUDE_TOOL_SUFFIX}`;
|
||||
toolNameMap.set(suffixed, tool.name);
|
||||
clientDeclarations.push({ ...tool, name: suffixed });
|
||||
}
|
||||
|
||||
// Client tools first, then CC decoy tools (no overlap: client tools all have _cc suffix)
|
||||
const allTools = [...clientDeclarations, ...CC_DECOY_TOOLS];
|
||||
|
||||
// Rename tool_use in message history (all client tools get suffix)
|
||||
const renamedMessages = body.messages?.map(msg => {
|
||||
if (!Array.isArray(msg.content)) return msg;
|
||||
const renamedContent = msg.content.map(block => {
|
||||
if (block.type === "tool_use") {
|
||||
return { ...block, name: `${block.name}${CLAUDE_TOOL_SUFFIX}` };
|
||||
}
|
||||
return block;
|
||||
});
|
||||
return { ...msg, content: renamedContent };
|
||||
});
|
||||
|
||||
return {
|
||||
body: { ...body, tools: allTools, messages: renamedMessages || body.messages },
|
||||
toolNameMap: toolNameMap.size > 0 ? toolNameMap : null
|
||||
};
|
||||
}
|
||||
|
||||
// CC decoy tools — Claude Code native tool names, marked unavailable
|
||||
const CC_DECOY_TOOLS = [
|
||||
{ name: "Task", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskOutput", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskStop", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskCreate", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskGet", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskUpdate", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "TaskList", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Bash", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Glob", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Grep", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Read", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Edit", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Write", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "NotebookEdit", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "WebFetch", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "WebSearch", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "AskUserQuestion", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "Skill", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "EnterPlanMode", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
{ name: "ExitPlanMode", description: "This tool is currently unavailable.", input_schema: { type: "object", properties: {} } },
|
||||
];
|
||||
|
||||
/**
|
||||
* Apply Claude cloaking to request body:
|
||||
* 1. Inject billing header as first system block
|
||||
* 2. Inject fake user ID into metadata
|
||||
*
|
||||
* 2. Inject fake user ID into metadata (JSON format, session_id aligned with X-Claude-Code-Session-Id)
|
||||
* Only applies when using OAuth token (sk-ant-oat).
|
||||
* @param {object} body - Claude API request body
|
||||
* @param {string} apiKey - API key or OAuth token
|
||||
* @param {string} [sessionId] - Session ID to align with X-Claude-Code-Session-Id header
|
||||
* @returns {object} Modified body
|
||||
*/
|
||||
export function applyCloaking(body, apiKey) {
|
||||
export function applyCloaking(body, apiKey, sessionId) {
|
||||
if (!apiKey || !apiKey.includes("sk-ant-oat")) return body;
|
||||
|
||||
const result = { ...body };
|
||||
@@ -50,10 +120,10 @@ export function applyCloaking(body, apiKey) {
|
||||
result.system = [billingBlock];
|
||||
}
|
||||
|
||||
// Inject fake user ID into metadata
|
||||
// Inject fake user ID into metadata (session_id must match X-Claude-Code-Session-Id)
|
||||
const existingUserId = result.metadata?.user_id;
|
||||
if (!existingUserId) {
|
||||
result.metadata = { ...result.metadata, user_id: generateFakeUserID() };
|
||||
result.metadata = { ...result.metadata, user_id: generateFakeUserID(sessionId) };
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -19,6 +19,7 @@ const CLAUDE_IDENTITY_HEADERS = [
|
||||
"x-stainless-arch",
|
||||
"x-stainless-os",
|
||||
"x-stainless-timeout",
|
||||
"x-claude-code-session-id",
|
||||
"package-version",
|
||||
"runtime-version",
|
||||
"os",
|
||||
|
||||
Reference in New Issue
Block a user