mirror of
https://github.com/upstash/context7.git
synced 2026-09-14 19:09:34 +08:00
6255e26677
* init sdk and monorepo * init sdk and monorepo * comment cleanup * remove bun.lock * cleanup: remove sdk package and deps * update tsconfigs * chore: add temp changelog * ci: update scripts * ci: update github workflows * fmt: workflows * fix: search libraries response type * ci: update pack-mcpb script * update keywords and author * update eslint config for mcp * include license and readme in build * update release.yaml * add readme symlink * include readme in mcp package * chore: add canary release workflow and update configs * chore: format files * ci: add changeset check workflow * canary release trigger * ci: login to npm before release * bump mcp version on package * remove pr trigger from canary release * ci: remove package input * add mcp lint command * update format scripts
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { createCipheriv, randomBytes } from "crypto";
|
|
|
|
const DEFAULT_ENCRYPTION_KEY = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
|
const ENCRYPTION_KEY = process.env.CLIENT_IP_ENCRYPTION_KEY || DEFAULT_ENCRYPTION_KEY;
|
|
const ALGORITHM = "aes-256-cbc";
|
|
|
|
if (ENCRYPTION_KEY === DEFAULT_ENCRYPTION_KEY) {
|
|
console.warn("WARNING: Using default CLIENT_IP_ENCRYPTION_KEY.");
|
|
}
|
|
|
|
function validateEncryptionKey(key: string): boolean {
|
|
// Must be exactly 64 hex characters (32 bytes)
|
|
return /^[0-9a-fA-F]{64}$/.test(key);
|
|
}
|
|
|
|
function encryptClientIp(clientIp: string): string {
|
|
if (!validateEncryptionKey(ENCRYPTION_KEY)) {
|
|
console.error("Invalid encryption key format. Must be 64 hex characters.");
|
|
return clientIp; // Fallback to unencrypted
|
|
}
|
|
|
|
try {
|
|
const iv = randomBytes(16);
|
|
const cipher = createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, "hex"), iv);
|
|
let encrypted = cipher.update(clientIp, "utf8", "hex");
|
|
encrypted += cipher.final("hex");
|
|
return iv.toString("hex") + ":" + encrypted;
|
|
} catch (error) {
|
|
console.error("Error encrypting client IP:", error);
|
|
return clientIp; // Fallback to unencrypted
|
|
}
|
|
}
|
|
|
|
export function generateHeaders(
|
|
clientIp?: string,
|
|
apiKey?: string,
|
|
extraHeaders: Record<string, string> = {}
|
|
): Record<string, string> {
|
|
const headers: Record<string, string> = { ...extraHeaders };
|
|
if (clientIp) {
|
|
headers["mcp-client-ip"] = encryptClientIp(clientIp);
|
|
}
|
|
if (apiKey) {
|
|
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
}
|
|
return headers;
|
|
}
|