|
|
|
@ -11,6 +11,10 @@ export interface IDomainConfig {
|
|
|
|
|
portRanges?: Array<{ from: number; to: number }>; // Optional port ranges
|
|
|
|
|
// Allow domain-specific timeout override
|
|
|
|
|
connectionTimeout?: number; // Connection timeout override (ms)
|
|
|
|
|
|
|
|
|
|
// NetworkProxy integration options for this specific domain
|
|
|
|
|
useNetworkProxy?: boolean; // Whether to use NetworkProxy for this domain
|
|
|
|
|
networkProxyPort?: number; // Override default NetworkProxy port for this domain
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Port proxy settings including global allowed port ranges */
|
|
|
|
@ -452,12 +456,14 @@ export class PortProxy {
|
|
|
|
|
* @param socket - The incoming client socket
|
|
|
|
|
* @param record - The connection record
|
|
|
|
|
* @param initialData - Initial data chunk (TLS ClientHello)
|
|
|
|
|
* @param customProxyPort - Optional custom port for NetworkProxy (for domain-specific settings)
|
|
|
|
|
*/
|
|
|
|
|
private forwardToNetworkProxy(
|
|
|
|
|
connectionId: string,
|
|
|
|
|
socket: plugins.net.Socket,
|
|
|
|
|
record: IConnectionRecord,
|
|
|
|
|
initialData: Buffer
|
|
|
|
|
initialData: Buffer,
|
|
|
|
|
customProxyPort?: number
|
|
|
|
|
): void {
|
|
|
|
|
// Ensure NetworkProxy is initialized
|
|
|
|
|
if (!this.networkProxy) {
|
|
|
|
@ -475,7 +481,8 @@ export class PortProxy {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const proxyPort = this.networkProxy.getListeningPort();
|
|
|
|
|
// Use the custom port if provided, otherwise use the default NetworkProxy port
|
|
|
|
|
const proxyPort = customProxyPort || this.networkProxy.getListeningPort();
|
|
|
|
|
const proxyHost = 'localhost'; // Assuming NetworkProxy runs locally
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
@ -1486,9 +1493,12 @@ export class PortProxy {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if this connection should be forwarded directly to NetworkProxy based on port
|
|
|
|
|
const shouldUseNetworkProxy = this.settings.useNetworkProxy &&
|
|
|
|
|
this.settings.useNetworkProxy.includes(localPort);
|
|
|
|
|
// Check if this connection should be forwarded directly to NetworkProxy
|
|
|
|
|
// First check port-based forwarding settings
|
|
|
|
|
let shouldUseNetworkProxy = this.settings.useNetworkProxy &&
|
|
|
|
|
this.settings.useNetworkProxy.includes(localPort);
|
|
|
|
|
|
|
|
|
|
// We'll look for domain-specific settings after SNI extraction
|
|
|
|
|
|
|
|
|
|
if (shouldUseNetworkProxy) {
|
|
|
|
|
// For NetworkProxy ports, we want to capture the TLS handshake and forward directly
|
|
|
|
@ -1531,7 +1541,48 @@ export class PortProxy {
|
|
|
|
|
if (SniHandler.isTlsHandshake(chunk)) {
|
|
|
|
|
connectionRecord.isTLS = true;
|
|
|
|
|
|
|
|
|
|
// Forward directly to NetworkProxy without SNI processing
|
|
|
|
|
// Try to extract SNI for domain-specific NetworkProxy handling
|
|
|
|
|
const connInfo = {
|
|
|
|
|
sourceIp: remoteIP,
|
|
|
|
|
sourcePort: socket.remotePort || 0,
|
|
|
|
|
destIp: socket.localAddress || '',
|
|
|
|
|
destPort: socket.localPort || 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Extract SNI to check for domain-specific NetworkProxy settings
|
|
|
|
|
const serverName = SniHandler.processTlsPacket(
|
|
|
|
|
chunk,
|
|
|
|
|
connInfo,
|
|
|
|
|
this.settings.enableTlsDebugLogging
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (serverName) {
|
|
|
|
|
// If we got an SNI, check for domain-specific NetworkProxy settings
|
|
|
|
|
const domainConfig = this.settings.domainConfigs.find((config) =>
|
|
|
|
|
config.domains.some((d) => plugins.minimatch(serverName, d))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Save domain config and SNI in connection record
|
|
|
|
|
connectionRecord.domainConfig = domainConfig;
|
|
|
|
|
connectionRecord.lockedDomain = serverName;
|
|
|
|
|
|
|
|
|
|
// Use domain-specific NetworkProxy port if configured
|
|
|
|
|
if (domainConfig?.useNetworkProxy) {
|
|
|
|
|
const networkProxyPort = domainConfig.networkProxyPort || this.settings.networkProxyPort;
|
|
|
|
|
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Using domain-specific NetworkProxy for ${serverName} on port ${networkProxyPort}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Forward to NetworkProxy with domain-specific port
|
|
|
|
|
this.forwardToNetworkProxy(connectionId, socket, connectionRecord, chunk, networkProxyPort);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Forward directly to NetworkProxy without domain-specific settings
|
|
|
|
|
this.forwardToNetworkProxy(connectionId, socket, connectionRecord, chunk);
|
|
|
|
|
} else {
|
|
|
|
|
// If not TLS, use normal direct connection
|
|
|
|
@ -1658,6 +1709,29 @@ export class PortProxy {
|
|
|
|
|
// Save domain config in connection record
|
|
|
|
|
connectionRecord.domainConfig = domainConfig;
|
|
|
|
|
|
|
|
|
|
// Check if this domain should use NetworkProxy (domain-specific setting)
|
|
|
|
|
if (domainConfig?.useNetworkProxy && this.networkProxy) {
|
|
|
|
|
if (this.settings.enableDetailedLogging) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[${connectionId}] Domain ${serverName} is configured to use NetworkProxy`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const networkProxyPort = domainConfig.networkProxyPort || this.settings.networkProxyPort;
|
|
|
|
|
|
|
|
|
|
if (initialChunk && connectionRecord.isTLS) {
|
|
|
|
|
// For TLS connections with initial chunk, forward to NetworkProxy
|
|
|
|
|
this.forwardToNetworkProxy(
|
|
|
|
|
connectionId,
|
|
|
|
|
socket,
|
|
|
|
|
connectionRecord,
|
|
|
|
|
initialChunk,
|
|
|
|
|
networkProxyPort // Pass the domain-specific NetworkProxy port if configured
|
|
|
|
|
);
|
|
|
|
|
return; // Skip normal connection setup
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IP validation is skipped if allowedIPs is empty
|
|
|
|
|
if (domainConfig) {
|
|
|
|
|
const effectiveAllowedIPs: string[] = [
|
|
|
|
|