Skip to content

Context7 Platform -- Up-to-date docs for AI agents

CTX7-272: ip address header

buggyhunter52a62e6129ba
diff --git a/src/index.ts b/src/index.ts
index 898e30f8..7d59335c 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -48,6 +48,15 @@ const CLI_PORT = (() => {
const sseTransports: Record<string, SSEServerTransport> = {};
function getClientIp(req: IncomingMessage): string | undefined {
+ // Check for X-Forwarded-For header (set by AWS ELB and other load balancers)
+ const forwardedFor = req.headers["x-forwarded-for"];
+ if (forwardedFor) {
+ // X-Forwarded-For can contain multiple IPs, take the first one
+ const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
+ return ips.split(",")[0].trim();
+ }
+
+ // Fall back to socket remote address
return req.socket?.remoteAddress || undefined;
}
diff --git a/src/lib/api.ts b/src/lib/api.ts
index 2a27ff80..b5563d80 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -1,8 +1,48 @@
import { SearchResponse } from "./types.js";
+import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
const CONTEXT7_API_BASE_URL = "https://context7.com/api";
const DEFAULT_TYPE = "txt";
+// Encryption configuration
+const ENCRYPTION_KEY = process.env.CLIENT_IP_ENCRYPTION_KEY || "default";
+const ALGORITHM = 'aes-256-cbc';
+
+function encryptClientIp(clientIp: string): string {
+
+ 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 decryptClientIp(encryptedIp: string): string | null {
+
+ try {
+ const parts = encryptedIp.split(':');
+ if (parts.length !== 2) {
+ // Not encrypted, return as-is
+ return encryptedIp;
+ }
+
+ const iv = Buffer.from(parts[0], 'hex');
+ const encrypted = parts[1];
+ const decipher = createDecipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
+ let decrypted = decipher.update(encrypted, 'hex', 'utf8');
+ decrypted += decipher.final('utf8');
+ return decrypted;
+ } catch (error) {
+ console.error("Error decrypting client IP:", error);
+ return null;
+ }
+}
+
/**
* Searches for libraries matching the given query
* @param query The search query
@@ -16,7 +56,7 @@ export async function searchLibraries(query: string, clientIp?: string): Promise
const headers: Record<string, string> = {};
if (clientIp) {
- headers["X-Client-IP"] = clientIp;
+ headers["X-Client-IP"] = encryptClientIp(clientIp);
}
const response = await fetch(url, { headers });
@@ -70,7 +110,7 @@ export async function fetchLibraryDocumentation(
"X-Context7-Source": "mcp-server",
};
if (clientIp) {
- headers["X-Client-IP"] = clientIp;
+ headers["X-Client-IP"] = encryptClientIp(clientIp);
}
const response = await fetch(url, { headers });