Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
13c84f7146 | |||
3ddb4c4c75 | |||
b39f607e63 | |||
793e108e7e | |||
3238ea5cfd | |||
20dc9238e4 | |||
2b9b7cf691 | |||
0b7f5c4701 | |||
23fa6a6511 | |||
ff8e185ec3 | |||
c32a56d921 | |||
69233e8a3b | |||
23e6977d81 | |||
9f57b3b638 | |||
5df0a53efe | |||
6d1a781b9a | |||
81c9f3b72e | |||
c8ac0498c8 | |||
ee0900f3c0 | |||
7c46d16686 | |||
10cd3b3528 | |||
0e6c09aba5 | |||
2f4916f552 | |||
29bddf198f | |||
a5ecf0d9c1 | |||
1ccd53ce69 | |||
1264542410 | |||
ba55019846 | |||
6f967553d6 |
@ -14,4 +14,4 @@
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
||||
}
|
1765
package-lock.json
generated
1765
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
29
package.json
29
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartproxy",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.18",
|
||||
"private": false,
|
||||
"description": "a proxy for handling high workloads of proxying",
|
||||
"main": "dist/index.js",
|
||||
@ -17,13 +17,28 @@
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^12.7.2",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint": "^5.19.0",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/express": "^4.17.1",
|
||||
"@types/http-proxy-middleware": "^0.19.3",
|
||||
"express": "^4.17.1",
|
||||
"http-proxy-middleware": "^0.19.1"
|
||||
}
|
||||
"@pushrocks/smartnetwork": "^1.1.6",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.23",
|
||||
"@pushrocks/smartspawn": "^2.0.8",
|
||||
"@pushrocks/smartsystem": "^2.0.7",
|
||||
"@tsclass/tsclass": "^2.0.5",
|
||||
"@types/ws": "^6.0.2",
|
||||
"ws": "^7.1.2"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
}
|
||||
|
26
readme.md
Normal file
26
readme.md
Normal file
@ -0,0 +1,26 @@
|
||||
# @pushrocks/smartproxy
|
||||
a proxy for handling high workloads of proxying
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartproxy)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartproxy)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartproxy)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartproxy/)
|
||||
|
||||
## Status for master
|
||||
[](https://gitlab.com/pushrocks/smartproxy/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartproxy/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartproxy)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartproxy)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
16
test/test.ts
16
test/test.ts
@ -1,8 +1,22 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartproxy from '../ts/index';
|
||||
|
||||
let testProxy: smartproxy.SmartProxy;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
console.log(smartproxy);
|
||||
testProxy = new smartproxy.SmartProxy();
|
||||
});
|
||||
|
||||
tap.test('should start the testproxy', async () => {
|
||||
await testProxy.start();
|
||||
});
|
||||
|
||||
tap.test('should wait for 5 seconds', async (tools) => {
|
||||
await tools.delayFor(5000);
|
||||
});
|
||||
|
||||
tap.test('should close the testproxy', async () => {
|
||||
await testProxy.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1,7 +0,0 @@
|
||||
export interface IHostConfig {
|
||||
hostName: string;
|
||||
destination: string;
|
||||
destinationPort: number;
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
}
|
50
ts/smartproxy.classes.proxymaster.ts
Normal file
50
ts/smartproxy.classes.proxymaster.ts
Normal file
@ -0,0 +1,50 @@
|
||||
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);
|
81
ts/smartproxy.classes.proxyworker.ts
Normal file
81
ts/smartproxy.classes.proxyworker.ts
Normal file
@ -0,0 +1,81 @@
|
||||
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 router = new SmartproxyRouter();
|
||||
|
||||
/**
|
||||
* starts the proxyInstance
|
||||
*/
|
||||
public async start() {
|
||||
this.httpsServer = plugins.http.createServer(async (req, res) => {
|
||||
const destinationConfig = this.router.routeReq(req);
|
||||
const response = await plugins.smartrequest.request(
|
||||
`http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`,
|
||||
{
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
},
|
||||
true // lets make this streaming
|
||||
);
|
||||
res.statusCode = response.statusCode;
|
||||
for (const header of Object.keys(response.headers)) {
|
||||
res.setHeader(header, response.headers[header]);
|
||||
}
|
||||
response.on('data', data => {
|
||||
res.write(data);
|
||||
});
|
||||
response.on('end', () => {
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
|
||||
// Enable websockets
|
||||
const wss = new plugins.ws.Server({ server: this.httpsServer });
|
||||
wss.on('connection', function connection(ws) {
|
||||
const wscConnected = plugins.smartpromise.defer();
|
||||
const wsc = new plugins.ws(`${ws.url}`);
|
||||
wsc.on('open', () => {
|
||||
wscConnected.resolve();
|
||||
});
|
||||
|
||||
ws.on('message', async (message) => {
|
||||
await wscConnected.promise;
|
||||
wsc.emit('message', message);
|
||||
});
|
||||
wsc.on('message', (message) => {
|
||||
ws.emit('message', message);
|
||||
});
|
||||
|
||||
// handle closing
|
||||
ws.on('close', (message) => {
|
||||
wsc.close();
|
||||
});
|
||||
wsc.on('close', (message) => {
|
||||
ws.close();
|
||||
});
|
||||
});
|
||||
|
||||
this.httpsServer.listen(3000);
|
||||
}
|
||||
|
||||
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() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
this.httpsServer.close(() => {
|
||||
done.resolve();
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,20 @@
|
||||
import * as plugins from './smartproxy.plugins';
|
||||
|
||||
export class SmartproxyRouter {
|
||||
|
||||
}
|
||||
public reverseCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,26 @@
|
||||
import * as plugins from './smartproxy.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { SmartproxyRouter } from './smartproxy.classes.router';
|
||||
import { TProxyMasterCalls } from './smartproxy.classes.proxymaster';
|
||||
|
||||
export class SmartProxy {
|
||||
public expressInstance: plugins.express.Express;
|
||||
public httpsServer: plugins.https.Server;
|
||||
public router = new SmartproxyRouter();
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
/**
|
||||
* starts the proxyInstance
|
||||
*/
|
||||
public async start() {
|
||||
this.expressInstance = plugins.express();
|
||||
this.httpsServer = plugins.https.createServer(this.expressInstance);
|
||||
}
|
||||
|
||||
public async update() {
|
||||
await this.start();
|
||||
public async start () {
|
||||
this.proxyMasterFunctions = await plugins.smartspawn.spawn<TProxyMasterCalls>(new plugins.smartspawn.Worker('./smartproxy.classes.proxymaster'));
|
||||
console.log('successfully spawned proxymaster');
|
||||
await this.proxyMasterFunctions.start();
|
||||
}
|
||||
|
||||
public async stop () {
|
||||
await this.proxyMasterFunctions.terminateMaster();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,15 +1,33 @@
|
||||
// node native scope
|
||||
import * as https from 'https';
|
||||
import * as cluster from 'cluster';
|
||||
import http from 'http';
|
||||
import https from 'https';
|
||||
|
||||
export { cluster, http, https };
|
||||
|
||||
// tsclass scope
|
||||
import * as tsclass from '@tsclass/tsclass';
|
||||
|
||||
export {
|
||||
https
|
||||
tsclass
|
||||
};
|
||||
|
||||
// pushrocks scope
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartspawn from '@pushrocks/smartspawn';
|
||||
import * as smartsystem from '@pushrocks/smartsystem';
|
||||
|
||||
export {
|
||||
smartrequest,
|
||||
smartpromise,
|
||||
smartspawn,
|
||||
smartsystem,
|
||||
};
|
||||
|
||||
// third party scope
|
||||
import express from 'express';
|
||||
import * as httpProxyMiddleware from 'http-proxy-middleware';
|
||||
import ws from 'ws';
|
||||
|
||||
export {
|
||||
express,
|
||||
httpProxyMiddleware
|
||||
ws
|
||||
};
|
||||
|
Reference in New Issue
Block a user