smartproxy/ts/smartproxy.classes.smartproxy.ts

62 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-08-20 16:42:52 +00:00
import * as plugins from './smartproxy.plugins';
2019-08-22 10:49:29 +00:00
2019-08-22 13:09:48 +00:00
import { TProxyWorkerCalls } from './smartproxy.classes.proxyworker';
import { TPortProxyCalls } from './smartproxy.portproxy';
2019-08-20 16:42:52 +00:00
2020-02-07 12:43:37 +00:00
export interface ISmartProxyOptions {
port?: number;
}
2019-08-20 16:42:52 +00:00
export class SmartProxy {
2019-08-22 13:09:48 +00:00
public smartsystem = new plugins.smartsystem.Smartsystem();
2019-08-22 13:21:57 +00:00
public reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
2019-08-22 13:09:48 +00:00
public proxyWorkerFunctions: plugins.smartspawn.ModuleThread<TProxyWorkerCalls>;
public portProxyFunctions: plugins.smartspawn.ModuleThread<TPortProxyCalls>;
2019-08-20 16:42:52 +00:00
2020-02-07 12:43:37 +00:00
public options: ISmartProxyOptions;
constructor(optionsArg: ISmartProxyOptions = {}) {
this.options = optionsArg;
}
public async updateReverseConfigs(
2019-08-22 14:14:50 +00:00
reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]
) {
2019-08-20 16:42:52 +00:00
// TODO search for old hostCandidates with that target
2019-08-22 13:21:57 +00:00
this.reverseConfigs = reverseConfigsArg;
if (this.proxyWorkerFunctions) {
await this.proxyWorkerFunctions.updateReverseConfigs(this.reverseConfigs);
}
2019-08-20 16:43:15 +00:00
}
2019-08-20 16:42:52 +00:00
2019-08-22 14:14:50 +00:00
public async start() {
this.proxyWorkerFunctions = await plugins.smartspawn.spawn<TProxyWorkerCalls>(
new plugins.smartspawn.Worker('./smartproxy.classes.proxyworker')
);
2019-08-22 13:21:57 +00:00
this.proxyWorkerFunctions.updateReverseConfigs(this.reverseConfigs);
2019-08-22 14:14:50 +00:00
this.portProxyFunctions = await plugins.smartspawn.spawn<TPortProxyCalls>(
new plugins.smartspawn.Worker('./smartproxy.portproxy')
);
2020-02-07 12:43:37 +00:00
await this.portProxyFunctions.start(this.options.port);
2019-08-22 13:09:48 +00:00
await this.proxyWorkerFunctions.start();
2019-08-22 14:14:50 +00:00
2019-08-22 13:21:57 +00:00
console.log('successfully spawned portproxy and proxyworkers!');
2019-08-20 16:42:52 +00:00
}
2021-02-03 00:30:35 +00:00
public async updateDefaultHeaders(defaultHeadersArg: {[key: string]: string}) {
await this.proxyWorkerFunctions.addDefaultHeaders(defaultHeadersArg);
}
2019-08-22 14:14:50 +00:00
public async stop() {
2019-08-22 13:09:48 +00:00
await this.proxyWorkerFunctions.stop();
2019-08-22 22:41:38 +00:00
await plugins.smartspawn.Thread.terminate(this.proxyWorkerFunctions);
2019-08-22 13:09:48 +00:00
console.log('proxy worker stopped');
await this.portProxyFunctions.stop();
2019-08-22 22:41:38 +00:00
await plugins.smartspawn.Thread.terminate(this.portProxyFunctions);
2019-08-22 13:09:48 +00:00
console.log('portproxy stopped');
console.log('Terminated all childs!');
2019-08-20 16:42:52 +00:00
}
}