Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
209e5644c0 | |||
128c9f9751 | |||
0dfb763b17 | |||
2883d2b926 | |||
4113bf551e | |||
2896c92c04 | |||
f9e81ba7cd | |||
8fcada6d4e | |||
f43151916d | |||
d416355ea1 | |||
13c84f7146 | |||
3ddb4c4c75 | |||
b39f607e63 | |||
793e108e7e | |||
3238ea5cfd | |||
20dc9238e4 | |||
2b9b7cf691 | |||
0b7f5c4701 | |||
23fa6a6511 | |||
ff8e185ec3 | |||
c32a56d921 | |||
69233e8a3b | |||
23e6977d81 | |||
9f57b3b638 | |||
5df0a53efe | |||
6d1a781b9a | |||
81c9f3b72e | |||
c8ac0498c8 | |||
ee0900f3c0 | |||
7c46d16686 | |||
10cd3b3528 |
@ -1,5 +1,7 @@
|
|||||||
# gitzone ci_default
|
# gitzone ci_default
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
variables:
|
||||||
|
GIT_STRATEGY: clone
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
|
1765
package-lock.json
generated
1765
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartproxy",
|
"name": "@pushrocks/smartproxy",
|
||||||
"version": "1.0.7",
|
"version": "1.0.23",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a proxy for handling high workloads of proxying",
|
"description": "a proxy for handling high workloads of proxying",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -9,7 +9,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)",
|
"build": "(tsbuild --web)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@ -17,14 +17,18 @@
|
|||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.15",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^3.0.7",
|
||||||
"@types/node": "^12.7.2",
|
"@types/node": "^12.7.2",
|
||||||
"tslint": "^5.11.0",
|
"tslint": "^5.19.0",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/express": "^4.17.1",
|
"@pushrocks/smartnetwork": "^1.1.6",
|
||||||
"@types/http-proxy-middleware": "^0.19.3",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"express": "^4.17.1",
|
"@pushrocks/smartrequest": "^1.1.23",
|
||||||
"http-proxy-middleware": "^0.19.1"
|
"@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": [
|
"files": [
|
||||||
"ts/*",
|
"ts/*",
|
||||||
|
21
test/test.ts
21
test/test.ts
@ -1,9 +1,26 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartproxy from '../ts/index';
|
import * as smartproxy from '../ts/index';
|
||||||
|
|
||||||
|
let testProxy: smartproxy.SmartProxy;
|
||||||
|
|
||||||
|
if (process.env.CI) {
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
const testProxy = new smartproxy.SmartProxy();
|
testProxy = new smartproxy.SmartProxy();
|
||||||
// await testProxy.start();
|
});
|
||||||
|
|
||||||
|
tap.test('should start the testproxy', async () => {
|
||||||
|
await testProxy.start();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should wait for 5 seconds', async tools => {
|
||||||
|
await tools.delayFor(1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should close the testproxy', async () => {
|
||||||
|
await testProxy.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
export interface IHostConfig {
|
|
||||||
hostName: string;
|
|
||||||
destinationIp: string;
|
|
||||||
destinationPort: number;
|
|
||||||
privateKey: string;
|
|
||||||
publicKey: string;
|
|
||||||
}
|
|
109
ts/smartproxy.classes.proxyworker.ts
Normal file
109
ts/smartproxy.classes.proxyworker.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
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 port = 8001;
|
||||||
|
public router = new SmartproxyRouter();
|
||||||
|
|
||||||
|
public async setPort(portArg: number) {
|
||||||
|
this.port = portArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts the proxyInstance
|
||||||
|
*/
|
||||||
|
public async start() {
|
||||||
|
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}`,
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const Websocket = await import('ws');
|
||||||
|
|
||||||
|
// 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 Websocket.default(`${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(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.setNewCandidates(arrayOfReverseCandidates);
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
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');
|
@ -1,8 +1,20 @@
|
|||||||
import * as plugins from './smartproxy.plugins';
|
import * as plugins from './smartproxy.plugins';
|
||||||
|
|
||||||
export class SmartproxyRouter {
|
export class SmartproxyRouter {
|
||||||
|
public reverseCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||||
|
|
||||||
public routeReq(req: plugins.express.Request) {
|
/**
|
||||||
return 'https://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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,45 @@
|
|||||||
import * as plugins from './smartproxy.plugins';
|
import * as plugins from './smartproxy.plugins';
|
||||||
import * as interfaces from './interfaces';
|
|
||||||
|
|
||||||
import { SmartproxyRouter } from './smartproxy.classes.router';
|
import { TProxyWorkerCalls } from './smartproxy.classes.proxyworker';
|
||||||
|
import { TPortProxyCalls } from './smartproxy.portproxy';
|
||||||
|
|
||||||
export class SmartProxy {
|
export class SmartProxy {
|
||||||
public expressInstance: plugins.express.Express;
|
public smartsystem = new plugins.smartsystem.Smartsystem();
|
||||||
public httpsServer: plugins.https.Server | plugins.http.Server;
|
public reverseConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||||
public router = new SmartproxyRouter();
|
public proxyWorkerFunctions: plugins.smartspawn.ModuleThread<TProxyWorkerCalls>;
|
||||||
|
public portProxyFunctions: plugins.smartspawn.ModuleThread<TPortProxyCalls>;
|
||||||
|
|
||||||
public hostCandidates: interfaces.IHostConfig[] = [];
|
public async updateReversConfigs(
|
||||||
|
reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]
|
||||||
public addHostCandidate(hostCandidate: interfaces.IHostConfig) {
|
) {
|
||||||
// TODO search for old hostCandidates with that target
|
// TODO search for old hostCandidates with that target
|
||||||
this.hostCandidates.push(hostCandidate);
|
this.reverseConfigs = reverseConfigsArg;
|
||||||
}
|
if (this.proxyWorkerFunctions) {
|
||||||
|
await this.proxyWorkerFunctions.updateReverseConfigs(this.reverseConfigs);
|
||||||
/**
|
|
||||||
* starts the proxyInstance
|
|
||||||
*/
|
|
||||||
public async start() {
|
|
||||||
this.expressInstance = plugins.express();
|
|
||||||
this.httpsServer = plugins.http.createServer(this.expressInstance);
|
|
||||||
for (const hostCandidate of this.hostCandidates) {
|
|
||||||
/* this.httpsServer.addContext(hostCandidate.hostName, {
|
|
||||||
cert: hostCandidate.publicKey,
|
|
||||||
key: hostCandidate.privateKey
|
|
||||||
}); */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// proxy middleware options
|
|
||||||
const proxyOptions: plugins.httpProxyMiddleware.Config = {
|
|
||||||
target: 'https://nullresolve.lossless.one',
|
|
||||||
changeOrigin: true, // needed for virtual hosted sites
|
|
||||||
ws: true, // proxy websockets
|
|
||||||
router: (req: plugins.express.Request) => {
|
|
||||||
return this.router.routeReq(req);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.expressInstance.use(plugins.httpProxyMiddleware(proxyOptions));
|
|
||||||
this.httpsServer.listen(3000);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async update() {
|
public async start() {
|
||||||
await this.start();
|
this.proxyWorkerFunctions = await plugins.smartspawn.spawn<TProxyWorkerCalls>(
|
||||||
|
new plugins.smartspawn.Worker('./smartproxy.classes.proxyworker')
|
||||||
|
);
|
||||||
|
this.proxyWorkerFunctions.updateReverseConfigs(this.reverseConfigs);
|
||||||
|
|
||||||
|
this.portProxyFunctions = await plugins.smartspawn.spawn<TPortProxyCalls>(
|
||||||
|
new plugins.smartspawn.Worker('./smartproxy.portproxy')
|
||||||
|
);
|
||||||
|
await this.proxyWorkerFunctions.start();
|
||||||
|
|
||||||
|
console.log('successfully spawned portproxy and proxyworkers!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public async stop() {
|
||||||
|
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!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,20 @@ import * as https from 'https';
|
|||||||
|
|
||||||
export { http, https };
|
export { http, https };
|
||||||
|
|
||||||
// third party scope
|
// tsclass scope
|
||||||
import express from 'express';
|
import * as tsclass from '@tsclass/tsclass';
|
||||||
import httpProxyMiddleware from 'http-proxy-middleware';
|
|
||||||
|
|
||||||
export { express, httpProxyMiddleware };
|
export { 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 * as ws from 'ws';
|
||||||
|
|
||||||
|
export { ws };
|
||||||
|
28
ts/smartproxy.portproxy.ts
Normal file
28
ts/smartproxy.portproxy.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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');
|
Reference in New Issue
Block a user