2022-07-29 00:49:46 +02:00
|
|
|
import * as plugins from './smartproxy.plugins.js';
|
2019-08-20 18:42:52 +02:00
|
|
|
|
2022-07-29 00:49:46 +02:00
|
|
|
export class ProxyRouter {
|
2019-09-23 19:20:53 +02:00
|
|
|
public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
2019-08-20 19:02:13 +02:00
|
|
|
|
2019-08-22 12:49:29 +02:00
|
|
|
/**
|
|
|
|
* sets a new set of reverse configs to be routed to
|
|
|
|
* @param reverseCandidatesArg
|
|
|
|
*/
|
2019-09-23 19:20:53 +02:00
|
|
|
public setNewProxyConfigs(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]) {
|
|
|
|
this.reverseProxyConfigs = reverseCandidatesArg;
|
2019-08-22 12:49:29 +02:00
|
|
|
}
|
2019-09-23 19:20:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* routes a request
|
|
|
|
*/
|
2019-08-22 12:49:29 +02:00
|
|
|
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
|
|
|
|
const originalHost = req.headers.host;
|
2025-02-04 00:38:39 +01:00
|
|
|
if (!originalHost) {
|
|
|
|
console.error('No host header found in request');
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// Strip port from host if present
|
|
|
|
const hostWithoutPort = originalHost.split(':')[0];
|
2021-02-02 15:55:25 +00:00
|
|
|
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find((reverseConfig) => {
|
2025-02-04 00:38:39 +01:00
|
|
|
return reverseConfig.hostName === hostWithoutPort;
|
2019-08-22 12:49:29 +02:00
|
|
|
});
|
2025-02-04 00:38:39 +01:00
|
|
|
if (!correspodingReverseProxyConfig) {
|
|
|
|
console.error(`No config found for host: ${hostWithoutPort}`);
|
|
|
|
}
|
2019-08-22 12:49:29 +02:00
|
|
|
return correspodingReverseProxyConfig;
|
2019-08-20 19:02:13 +02:00
|
|
|
}
|
|
|
|
}
|