smartproxy/ts/smartproxy.classes.router.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-08-20 16:42:52 +00:00
import * as plugins from './smartproxy.plugins';
2019-08-20 17:02:13 +00:00
export class SmartproxyRouter {
2019-09-23 17:20:53 +00:00
public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
2019-08-20 17:02:13 +00:00
2019-08-22 10:49:29 +00:00
/**
* sets a new set of reverse configs to be routed to
* @param reverseCandidatesArg
*/
2019-09-23 17:20:53 +00:00
public setNewProxyConfigs(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]) {
this.reverseProxyConfigs = reverseCandidatesArg;
2019-08-22 10:49:29 +00:00
}
2019-09-23 17:20:53 +00:00
/**
* routes a request
*/
2019-08-22 10:49:29 +00:00
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
const originalHost = req.headers.host;
2021-02-02 15:55:25 +00:00
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find((reverseConfig) => {
2019-08-22 10:49:29 +00:00
return reverseConfig.hostName === originalHost;
});
return correspodingReverseProxyConfig;
2019-08-20 17:02:13 +00:00
}
2019-09-28 23:06:07 +00:00
public routeWs(ws: plugins.wsDefault) {
const originalHost = plugins.url.parse(ws.url).host;
2021-02-02 15:55:25 +00:00
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find((reverseConfig) => {
return reverseConfig.hostName === originalHost;
});
return correspodingReverseProxyConfig.destinationIp;
}
2019-08-20 17:02:13 +00:00
}