diff --git a/test/test.ts b/test/test.ts index ea4b3c3..2fa3a01 100644 --- a/test/test.ts +++ b/test/test.ts @@ -4,7 +4,7 @@ import * as smartproxy from '../ts/index'; let testProxy: smartproxy.SmartProxy; tap.test('first test', async () => { - testProxy = new smartproxy.SmartProxy(); + testProxy = new smartproxy.SmartProxy({}); }); tap.test('should start the testproxy', async () => { @@ -15,7 +15,7 @@ tap.test('should supply reverse proxy config', async () => { testProxy.updateReversConfigs([{ destinationIp: 'localhost', destinationPort: '3000', - hostName: 'central.eu', + hostName: 'push.rocks', privateKey: `-----BEGIN PRIVATE KEY----- MIIJRQIBADANBgkqhkiG9w0BAQEFAASCCS8wggkrAgEAAoICAQDi2F/0kQr96mhe 3yEWvy2mRHOZoSSBtIqg6Bre4ZcMu901/cHNIjFnynNGFl9Se61yZbW2F3PfCt7+ @@ -101,7 +101,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g= }); tap.test('should wait for 60 seconds', async tools => { - await tools.delayFor(60000); + await tools.delayFor(10000); }); tap.test('should close the testproxy', async () => { diff --git a/ts/smartproxy.classes.smartproxy.ts b/ts/smartproxy.classes.smartproxy.ts index c332ab6..b4b8940 100644 --- a/ts/smartproxy.classes.smartproxy.ts +++ b/ts/smartproxy.classes.smartproxy.ts @@ -3,12 +3,22 @@ import * as plugins from './smartproxy.plugins'; import { TProxyWorkerCalls } from './smartproxy.classes.proxyworker'; import { TPortProxyCalls } from './smartproxy.portproxy'; +export interface ISmartProxyOptions { + port?: number; +} + export class SmartProxy { public smartsystem = new plugins.smartsystem.Smartsystem(); public reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[] = []; public proxyWorkerFunctions: plugins.smartspawn.ModuleThread; public portProxyFunctions: plugins.smartspawn.ModuleThread; + public options: ISmartProxyOptions; + + constructor(optionsArg: ISmartProxyOptions = {}) { + this.options = optionsArg; + } + public async updateReversConfigs( reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[] ) { @@ -28,6 +38,8 @@ export class SmartProxy { this.portProxyFunctions = await plugins.smartspawn.spawn( new plugins.smartspawn.Worker('./smartproxy.portproxy') ); + + await this.portProxyFunctions.start(this.options.port); await this.proxyWorkerFunctions.start(); console.log('successfully spawned portproxy and proxyworkers!'); diff --git a/ts/smartproxy.portproxy.ts b/ts/smartproxy.portproxy.ts index b0ec4e3..1179901 100644 --- a/ts/smartproxy.portproxy.ts +++ b/ts/smartproxy.portproxy.ts @@ -1,21 +1,24 @@ import * as plugins from './smartproxy.plugins'; import { expose } from '@pushrocks/smartspawn'; import * as net from 'net'; -const server = net - .createServer(from => { - const to = net.createConnection({ - host: 'localhost', - port: 8001 - }); - from.pipe(to); - to.pipe(from); - }) - .listen(8000); +let netServer: plugins.net.Server; const portProxyCalls = { + start: async (portArg = 8000) => { + netServer = net + .createServer(from => { + const to = net.createConnection({ + host: 'localhost', + port: 8001 + }); + from.pipe(to); + to.pipe(from); + }) + .listen(portArg); + }, stop: async () => { const done = plugins.smartpromise.defer(); - server.close(() => { + netServer.close(() => { done.resolve(); }); await done.promise;