fix(core): update

This commit is contained in:
2019-08-22 12:49:29 +02:00
parent 3238ea5cfd
commit 793e108e7e
8 changed files with 463 additions and 100 deletions

View File

@ -1,7 +0,0 @@
export interface IHostConfig {
hostName: string;
destinationIp: string;
destinationPort: number;
privateKey: string;
publicKey: string;
}

View File

@ -0,0 +1,22 @@
import * as plugins from './smartproxy.plugins';
import { expose } from '@pushrocks/smartspawn';
class ProxyMaster {
public hostCandidates: plugins.tsclass
public clusterChilds: any[] = [];
}
const defaultProxyMaster = new ProxyMaster();
const proxyMasterCalls = {
terminateMaster: async () => {
process.kill(0);
},
};
export type TProxyMasterCalls = typeof proxyMasterCalls;
expose (proxyMasterCalls);
console.log('Proxymaster started!');

View File

@ -1,8 +1,8 @@
import * as plugins from './smartproxy.plugins';
import * as interfaces from './interfaces';
import { SmartproxyRouter } from './smartproxy.classes.router';
export class ProxyWorker {
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public httpsServer: plugins.https.Server | plugins.http.Server;
public router = new SmartproxyRouter();
@ -11,14 +11,14 @@ export class ProxyWorker {
*/
public async start() {
this.httpsServer = plugins.http.createServer(async (req, res) => {
req.headers.host = this.router.routeReq(req);
const destinationConfig = this.router.routeReq(req);
const response = await plugins.smartrequest.request(
`https://${req.headers.host}${req.url}`,
`http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`,
{
method: req.method,
headers: req.headers
},
true
true // lets make this streaming
);
res.statusCode = response.statusCode;
for (const header of Object.keys(response.headers)) {
@ -31,12 +31,6 @@ export class ProxyWorker {
res.end();
});
});
for (const hostCandidate of this.hostCandidates) {
/* this.httpsServer.addContext(hostCandidate.hostName, {
cert: hostCandidate.publicKey,
key: hostCandidate.privateKey
}); */
}
// Enable websockets
const wss = new plugins.ws.Server({ server: this.httpsServer });
@ -67,8 +61,15 @@ export class ProxyWorker {
this.httpsServer.listen(3000);
}
public async update() {
await this.start();
public async updateCandidates(arrayOfReverseCandidates: plugins.tsclass.IReverseProxyConfig[]) {
this.hostCandidates = arrayOfReverseCandidates;
this.router
for (const hostCandidate of this.hostCandidates) {
this.httpsServer.addContext(hostCandidate.hostName, {
cert: hostCandidate.publicKey,
key: hostCandidate.privateKey
});
}
}
public async stop() {

View File

@ -1,8 +1,20 @@
import * as plugins from './smartproxy.plugins';
export class SmartproxyRouter {
public reverseCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public routeReq(req: plugins.http.IncomingMessage) {
return 'lossless.gmbh';
/**
* sets a new set of reverse configs to be routed to
* @param reverseCandidatesArg
*/
public setNewCandidates(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]) {
this.reverseCandidates = reverseCandidatesArg;
}
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
const originalHost = req.headers.host;
const correspodingReverseProxyConfig = this.reverseCandidates.find(reverseConfig => {
return reverseConfig.hostName === originalHost;
});
return correspodingReverseProxyConfig;
}
}

View File

@ -1,21 +1,24 @@
import * as plugins from './smartproxy.plugins';
import * as interfaces from './interfaces';
import { TProxyMasterCalls } from './smartproxy.classes.proxymaster';
export class SmartProxy {
public hostCandidates: interfaces.IHostConfig[] = [];
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
public proxyMasterFunctions: plugins.smartspawn.ModuleThread<TProxyMasterCalls>;
public addHostCandidate(hostCandidate: interfaces.IHostConfig) {
public addHostCandidate(hostCandidate: plugins.tsclass.network.IReverseProxyConfig) {
// TODO search for old hostCandidates with that target
this.hostCandidates.push(hostCandidate);
}
async start () {
public async start () {
this.proxyMasterFunctions = await plugins.smartspawn.spawn<TProxyMasterCalls>(new plugins.smartspawn.Worker('./smartproxy.classes.proxymaster'));
console.log('successfully spawned proxymaster');
}
async stop () {
public async stop () {
await this.proxyMasterFunctions.terminateMaster();
}

View File

@ -1,17 +1,27 @@
// node native scope
import crypto from 'crypto';
import cluster from 'cluster';
import http from 'http';
import https from 'https';
export { crypto, http, https };
export { cluster, http, https };
// tsclass scope
import * as tsclass from '@tsclass/tsclass';
export {
tsclass
};
// pushrocks scope
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartspawn from '@pushrocks/smartspawn';
export {
smartrequest,
smartpromise
smartpromise,
smartspawn
};
// third party scope