Files
SillyTavern/src/request-proxy.js
T
338119ab77 Implement private IP range request host validator (#5497)
* feat: implement private IP range request host validator for server-side HTTP requests

* feat: add link-local address support

* fix: use correct config keys

* fix: if config missing use default loopback addresses

* fix: re-use resolved address for connection

* test: add unit coverage for private request filter and proxy interaction

Agent-Logs-Url: https://github.com/SillyTavern/SillyTavern/sessions/1813593e-2263-45e2-aa53-74d39515f1df

Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com>

* test: remove request-proxy.test.js

* perf: cache resolved matches

* fix: remove unused import

* fix: use proper ipv4 loopback cidr

* fix: correct raiseError comment

* test: uses tls.connect for secure endpoints

* Implement private IP range request host validator

Agent-Logs-Url: https://github.com/SillyTavern/SillyTavern/sessions/e76ba122-136e-43ad-b4bc-ea48a01fcdda

Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com>

* Revert "Implement private IP range request host validator"

This reverts commit 14e2714702.

* fix: close request without sending status in CORS forwarding when headers were sent

* fix: not enabled -> disabled

* feat: add enableKeepAlive option to PrivateRequestAgent

Co-authored-by: Copilot <copilot@github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: anthropic-code-agent[bot] <242468646+Claude@users.noreply.github.com>
Co-authored-by: Copilot <copilot@github.com>
2026-04-27 01:51:18 +03:00

64 lines
2.3 KiB
JavaScript

import process from 'node:process';
import http from 'node:http';
import https from 'node:https';
import { ProxyAgent } from 'proxy-agent';
import { isValidUrl, color } from './util.js';
const LOG_HEADER = '[Request Proxy]';
/**
* Initialize request proxy.
* @param {ProxySettings} settings Proxy settings.
* @typedef {object} ProxySettings
* @property {boolean} enabled Whether proxy is enabled.
* @property {string} url Proxy URL.
* @property {string[]} bypass List of URLs to bypass proxy.
* @property {boolean} enableKeepAlive Enable HTTP/HTTPS keep-alive.
* @property {boolean} privateRequestFilterEnabled Whether the private request filter is enabled.
*/
export default function initRequestProxy({ enabled, url, bypass, enableKeepAlive, privateRequestFilterEnabled }) {
try {
// No proxy is enabled, so return
if (!enabled) {
return;
}
if (privateRequestFilterEnabled) {
console.warn(color.yellow(LOG_HEADER), 'Warning: Request proxy is enabled while private request filter is also enabled. Only URLs that BYPASS the request proxy will be checked.');
console.warn(color.yellow(LOG_HEADER), 'To ensure all requests are properly filtered, disable the request proxy.');
}
if (!url) {
console.error(color.red(LOG_HEADER), 'No proxy URL provided');
return;
}
if (!isValidUrl(url)) {
console.error(color.red(LOG_HEADER), 'Invalid proxy URL provided');
return;
}
// ProxyAgent uses proxy-from-env under the hood
// Reference: https://github.com/Rob--W/proxy-from-env
process.env.all_proxy = url;
if (Array.isArray(bypass) && bypass.length > 0) {
process.env.no_proxy = bypass.join(',');
}
const httpAgent = http.globalAgent;
const httpsAgent = https.globalAgent;
const proxyAgent = new ProxyAgent({ httpAgent, httpsAgent, keepAlive: enableKeepAlive });
http.globalAgent = proxyAgent;
https.globalAgent = proxyAgent;
console.info();
console.info(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(url));
console.info();
} catch (error) {
console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error);
}
}