mirror of
https://github.com/upstash/context7.git
synced 2025-11-29 16:40:11 +00:00
CTX7-272: ip address header
This commit is contained in:
parent
086d4c2083
commit
294df1d487
12
src/index.ts
12
src/index.ts
@ -165,10 +165,14 @@ ${resultsText}`,
|
||||
),
|
||||
},
|
||||
async ({ context7CompatibleLibraryID, tokens = DEFAULT_MINIMUM_TOKENS, topic = "" }) => {
|
||||
const fetchDocsResponse = await fetchLibraryDocumentation(context7CompatibleLibraryID, {
|
||||
tokens,
|
||||
topic,
|
||||
}, clientIp);
|
||||
const fetchDocsResponse = await fetchLibraryDocumentation(
|
||||
context7CompatibleLibraryID,
|
||||
{
|
||||
tokens,
|
||||
topic,
|
||||
},
|
||||
clientIp
|
||||
);
|
||||
|
||||
if (!fetchDocsResponse) {
|
||||
return {
|
||||
|
||||
@ -5,8 +5,10 @@ const CONTEXT7_API_BASE_URL = "https://context7.com/api";
|
||||
const DEFAULT_TYPE = "txt";
|
||||
|
||||
// Encryption configuration
|
||||
const ENCRYPTION_KEY = process.env.CLIENT_IP_ENCRYPTION_KEY || "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
||||
const ALGORITHM = 'aes-256-cbc';
|
||||
const ENCRYPTION_KEY =
|
||||
process.env.CLIENT_IP_ENCRYPTION_KEY ||
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
||||
const ALGORITHM = "aes-256-cbc";
|
||||
|
||||
// Validate encryption key
|
||||
function validateEncryptionKey(key: string): boolean {
|
||||
@ -22,10 +24,10 @@ 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;
|
||||
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
|
||||
@ -39,17 +41,17 @@ export function decryptClientIp(encryptedIp: string): string | null {
|
||||
}
|
||||
|
||||
try {
|
||||
const parts = encryptedIp.split(':');
|
||||
const parts = encryptedIp.split(":");
|
||||
if (parts.length !== 2) {
|
||||
// Not encrypted, return as-is
|
||||
return encryptedIp;
|
||||
}
|
||||
|
||||
const iv = Buffer.from(parts[0], 'hex');
|
||||
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');
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user