2020-02-06 22:41:25 +00:00
|
|
|
import * as plugins from './smartvhost.plugins';
|
2020-02-07 20:00:01 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2020-02-06 22:41:25 +00:00
|
|
|
|
|
|
|
export class SmartVHost {
|
2020-02-07 20:00:01 +00:00
|
|
|
public currentConfig: interfaces.IVHostConfig[];
|
|
|
|
public smartproxy: plugins.smartproxy.SmartProxy;
|
|
|
|
public smartexpress: plugins.smartexpress.Server;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.smartproxy = new plugins.smartproxy.SmartProxy({
|
|
|
|
port: 3000
|
|
|
|
});
|
|
|
|
this.smartexpress = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
|
|
|
forceSsl: false,
|
|
|
|
port: 3001,
|
|
|
|
robots: 'standard'
|
|
|
|
});
|
|
|
|
}
|
2020-02-06 22:41:25 +00:00
|
|
|
|
|
|
|
public async start() {
|
2020-02-07 20:00:01 +00:00
|
|
|
await this.smartproxy.start();
|
|
|
|
await this.smartexpress.start();
|
2020-02-06 22:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {
|
2020-02-07 20:00:01 +00:00
|
|
|
await this.smartproxy.stop();
|
|
|
|
await this.smartexpress.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public setVHostConfigs(configArray: interfaces.IVHostConfig[]) {
|
|
|
|
this.currentConfig = configArray;
|
2020-02-06 22:41:25 +00:00
|
|
|
|
2020-02-07 20:00:01 +00:00
|
|
|
// lets route the traffic using smartproxy
|
|
|
|
const reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
|
|
|
for (const vHostConfig of this.currentConfig) {
|
|
|
|
switch(vHostConfig.type) {
|
|
|
|
case 'folder':
|
|
|
|
reverseConfigs.push({
|
|
|
|
destinationIp: '0.0.0.0',
|
|
|
|
destinationPort: '3001',
|
|
|
|
hostName: vHostConfig.hostName,
|
|
|
|
privateKey: vHostConfig.privateKey,
|
|
|
|
publicKey: vHostConfig.publicKey,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'ipAndPort':
|
|
|
|
reverseConfigs.push({
|
|
|
|
destinationIp: vHostConfig.target.split(':')[0],
|
|
|
|
destinationPort: vHostConfig.target.split(':')[1],
|
|
|
|
hostName: vHostConfig.hostName,
|
|
|
|
privateKey: vHostConfig.privateKey,
|
|
|
|
publicKey: vHostConfig.publicKey
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'localPort':
|
|
|
|
reverseConfigs.push({
|
|
|
|
destinationIp: '0.0.0.0',
|
|
|
|
destinationPort: vHostConfig.target,
|
|
|
|
hostName: vHostConfig.hostName,
|
|
|
|
privateKey: vHostConfig.privateKey,
|
|
|
|
publicKey: vHostConfig.publicKey,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'domain':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`unknown config type ${vHostConfig.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.smartproxy.updateReverseConfigs(reverseConfigs);
|
2020-02-06 22:41:25 +00:00
|
|
|
}
|
|
|
|
}
|