|
|
|
@ -344,13 +344,11 @@ export class PortProxy {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --- PORT RANGE-BASED HANDLING ---
|
|
|
|
|
// If global port ranges are defined, enforce port-based routing and ignore SNI.
|
|
|
|
|
if (this.settings.globalPortRanges && this.settings.globalPortRanges.length > 0) {
|
|
|
|
|
if (!isPortInRanges(localPort, this.settings.globalPortRanges)) {
|
|
|
|
|
console.log(`Connection from ${remoteIP} rejected: port ${localPort} is not in global allowed ranges.`);
|
|
|
|
|
socket.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check if the local port falls within any of the global port ranges.
|
|
|
|
|
const isLocalPortInGlobalRange =
|
|
|
|
|
this.settings.globalPortRanges && isPortInRanges(localPort, this.settings.globalPortRanges);
|
|
|
|
|
|
|
|
|
|
if (isLocalPortInGlobalRange) {
|
|
|
|
|
if (this.settings.forwardAllGlobalRanges) {
|
|
|
|
|
// Forward connection to the global targetIP regardless of domain config.
|
|
|
|
|
if (this.settings.defaultAllowedIPs && !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
|
|
|
|
@ -367,30 +365,26 @@ export class PortProxy {
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
// Find a matching domain config based on the incoming local port.
|
|
|
|
|
// Attempt to find a matching forced domain config based on the local port.
|
|
|
|
|
const forcedDomain = this.settings.domains.find(
|
|
|
|
|
domain => domain.portRanges && domain.portRanges.length > 0 && isPortInRanges(localPort, domain.portRanges)
|
|
|
|
|
);
|
|
|
|
|
if (!forcedDomain) {
|
|
|
|
|
console.log(`Connection from ${remoteIP} rejected: port ${localPort} not configured in any domain's portRanges.`);
|
|
|
|
|
socket.destroy();
|
|
|
|
|
if (forcedDomain) {
|
|
|
|
|
const defaultAllowed = this.settings.defaultAllowedIPs && isAllowed(remoteIP, this.settings.defaultAllowedIPs);
|
|
|
|
|
if (!defaultAllowed && !isAllowed(remoteIP, forcedDomain.allowedIPs)) {
|
|
|
|
|
console.log(`Connection from ${remoteIP} rejected: IP not allowed for domain ${forcedDomain.domain} on port ${localPort}.`);
|
|
|
|
|
socket.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log(`Port-based connection from ${remoteIP} on port ${localPort} matched domain ${forcedDomain.domain}.`);
|
|
|
|
|
setupConnection('', undefined, forcedDomain);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Check allowed IPs for the forced domain.
|
|
|
|
|
const defaultAllowed = this.settings.defaultAllowedIPs && isAllowed(remoteIP, this.settings.defaultAllowedIPs);
|
|
|
|
|
if (!defaultAllowed && !isAllowed(remoteIP, forcedDomain.allowedIPs)) {
|
|
|
|
|
console.log(`Connection from ${remoteIP} rejected: IP not allowed for domain ${forcedDomain.domain} on port ${localPort}.`);
|
|
|
|
|
socket.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log(`Port-based connection from ${remoteIP} on port ${localPort} matched domain ${forcedDomain.domain}.`);
|
|
|
|
|
// Proceed immediately using the forced domain; ignore SNI.
|
|
|
|
|
setupConnection('', undefined, forcedDomain);
|
|
|
|
|
return;
|
|
|
|
|
// If no forced domain config is found for this port, fall through to SNI/default handling.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- FALLBACK: SNI-BASED HANDLING (if no global port ranges are defined) ---
|
|
|
|
|
// --- FALLBACK: SNI-BASED HANDLING (or default when SNI is disabled) ---
|
|
|
|
|
if (this.settings.sniEnabled) {
|
|
|
|
|
socket.setTimeout(5000, () => {
|
|
|
|
|
console.log(`Initial data timeout for ${remoteIP}`);
|
|
|
|
|