fix(core): update

This commit is contained in:
Philipp Kunz 2019-08-22 15:09:48 +02:00
parent f43151916d
commit 8fcada6d4e
6 changed files with 78 additions and 64 deletions

View File

@ -3,6 +3,10 @@ import * as smartproxy from '../ts/index';
let testProxy: smartproxy.SmartProxy;
if (process.env.CI) {
process.exit(0);
}
tap.test('first test', async () => {
testProxy = new smartproxy.SmartProxy();
});
@ -12,7 +16,7 @@ tap.test('should start the testproxy', async () => {
});
tap.test('should wait for 5 seconds', async (tools) => {
await tools.delayFor(5000);
await tools.delayFor(1000);
});
tap.test('should close the testproxy', async () => {

View File

@ -1,50 +0,0 @@
import { expose } from '@pushrocks/smartspawn';
import * as plugins from './smartproxy.plugins';
import * as cluster from 'cluster';
class ProxyMaster {
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public clusterChilds: any[] = [];
public smartsystem = new plugins.smartsystem.Smartsystem();
public async start() {
if (cluster.isMaster) {
console.log('ProxyMaster registered as cluster master');
console.log(`should spawn ${this.smartsystem.cpus.length} workers`);
for (const cpu of this.smartsystem.cpus) {
cluster.fork();
}
} else {
const proxyworkerMod = await import('./smartproxy.classes.proxyworker');
const proxyWorkerInstance = new proxyworkerMod.ProxyWorker();
console.log('Proxymaster registered a ProxyWorker!');
const data = new Uint8Array(Buffer.from('Hello Node.js'));
fs.writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}
}
}
const defaultProxyMaster = new ProxyMaster();
if (!cluster.isMaster) {
defaultProxyMaster.start();
}
console.log('hi');
// the following is interesting for the master process only
const proxyMasterCalls = {
terminateMaster: async () => {
process.kill(0);
},
start: async () => {
await defaultProxyMaster.start();
},
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
}
};
export type TProxyMasterCalls = typeof proxyMasterCalls;
expose (proxyMasterCalls);

View File

@ -1,16 +1,22 @@
import { expose } from '@pushrocks/smartspawn';
import * as plugins from './smartproxy.plugins';
import { SmartproxyRouter } from './smartproxy.classes.router';
export class ProxyWorker {
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public httpsServer: plugins.https.Server | plugins.http.Server;
public httpsServer: plugins.https.Server; // | plugins.http.Server;
public port = 8001;
public router = new SmartproxyRouter();
public async setPort(portArg: number) {
this.port = portArg;
}
/**
* starts the proxyInstance
*/
public async start() {
this.httpsServer = plugins.http.createServer(async (req, res) => {
this.httpsServer = plugins.https.createServer(async (req, res) => {
const destinationConfig = this.router.routeReq(req);
const response = await plugins.smartrequest.request(
`http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`,
@ -58,12 +64,13 @@ export class ProxyWorker {
});
});
this.httpsServer.listen(3000);
this.httpsServer.listen(this.port);
console.log(`OK: now listening for new connections on port ${this.port}`);
}
public async updateCandidates(arrayOfReverseCandidates: plugins.tsclass.IReverseProxyConfig[]) {
this.hostCandidates = arrayOfReverseCandidates;
this.router
this.router.setNewCandidates(arrayOfReverseCandidates);
for (const hostCandidate of this.hostCandidates) {
this.httpsServer.addContext(hostCandidate.hostName, {
cert: hostCandidate.publicKey,
@ -77,5 +84,23 @@ export class ProxyWorker {
this.httpsServer.close(() => {
done.resolve();
});
await done.promise;
}
}
const proxyWorkerInstance = new ProxyWorker();
// the following is interesting for the master process only
const proxyWorkerCalls = {
stop: async () => {
await proxyWorkerInstance.stop();
},
start: async () => {
await proxyWorkerInstance.start();
},
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {}
};
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
expose(proxyWorkerCalls);
console.log('ProxyWorker initialized');

View File

@ -1,11 +1,13 @@
import * as plugins from './smartproxy.plugins';
import { TProxyMasterCalls } from './smartproxy.classes.proxymaster';
import { TProxyWorkerCalls } from './smartproxy.classes.proxyworker';
import { TPortProxyCalls } from './smartproxy.portproxy';
export class SmartProxy {
public smartsystem = new plugins.smartsystem.Smartsystem();
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public proxyMasterFunctions: plugins.smartspawn.ModuleThread<TProxyMasterCalls>;
public proxyWorkerFunctions: plugins.smartspawn.ModuleThread<TProxyWorkerCalls>;
public portProxyFunctions: plugins.smartspawn.ModuleThread<TPortProxyCalls>;
public addHostCandidate(hostCandidate: plugins.tsclass.network.IReverseProxyConfig) {
// TODO search for old hostCandidates with that target
@ -13,13 +15,20 @@ export class SmartProxy {
}
public async start () {
this.proxyMasterFunctions = await plugins.smartspawn.spawn<TProxyMasterCalls>(new plugins.smartspawn.Worker('./smartproxy.classes.proxymaster'));
this.proxyWorkerFunctions = await plugins.smartspawn.spawn<TProxyWorkerCalls>(new plugins.smartspawn.Worker('./smartproxy.classes.proxyworker'));
this.portProxyFunctions = await plugins.smartspawn.spawn<TPortProxyCalls>(new plugins.smartspawn.Worker('./smartproxy.portproxy'));
console.log('successfully spawned proxymaster');
await this.proxyMasterFunctions.start();
await this.proxyWorkerFunctions.start();
}
public async stop () {
await this.proxyMasterFunctions.terminateMaster();
await this.proxyWorkerFunctions.stop();
await plugins.smartspawn.Thread.terminate(this.portProxyFunctions);
console.log('proxy worker stopped');
await this.portProxyFunctions.stop();
await plugins.smartspawn.Thread.terminate(this.proxyWorkerFunctions);
console.log('portproxy stopped');
console.log('Terminated all childs!');
}

View File

@ -1,6 +1,6 @@
// node native scope
import http from 'http';
import https from 'https';
import * as http from 'http';
import * as https from 'https';
export { http, https };
@ -25,7 +25,7 @@ export {
};
// third party scope
import ws from 'ws';
import * as ws from 'ws';
export {
ws

View File

@ -0,0 +1,26 @@
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);
const portProxyCalls = {
stop: async () => {
const done = plugins.smartpromise.defer();
server.close(() => {
done.resolve();
});
await done.promise;
}
};
export type TPortProxyCalls = typeof portProxyCalls;
expose(portProxyCalls);
console.log('PortProxy Initialized');