fix(core): update

This commit is contained in:
Philipp Kunz 2022-07-29 00:49:46 +02:00
parent ca73849541
commit 101470dcd4
12 changed files with 11832 additions and 8967 deletions

21030
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,32 +5,29 @@
"description": "a proxy for handling high workloads of proxying", "description": "a proxy for handling high workloads of proxying",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "(tstest test/)",
"build": "(tsbuild --web)", "build": "(tsbuild --web --allowimplicitany)",
"format": "(gitzone format)" "format": "(gitzone format)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.63",
"@gitzone/tstest": "^1.0.52", "@gitzone/tstest": "^1.0.72",
"@pushrocks/tapbundle": "^3.2.10", "@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^14.14.22", "@types/node": "^18.6.2"
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^4.0.20", "@pushrocks/lik": "^6.0.0",
"@pushrocks/smartnetwork": "^1.1.22", "@pushrocks/smartnetwork": "^3.0.0",
"@pushrocks/smartpromise": "^3.1.3", "@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartrequest": "^1.1.51", "@pushrocks/smartrequest": "^1.1.56",
"@pushrocks/smartspawn": "^2.0.9", "@pushrocks/smartstring": "^4.0.2",
"@pushrocks/smartstring": "^3.0.24", "@tsclass/tsclass": "^4.0.17",
"@pushrocks/smartsystem": "^2.0.9", "@types/ws": "^8.5.3",
"@tsclass/tsclass": "^3.0.29", "ws": "^8.8.1"
"@types/ws": "^7.4.0",
"ws": "^7.4.3"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

View File

@ -1,10 +1,13 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as smartproxy from '../ts/index'; import * as smartproxy from '../ts/index.js';
let testProxy: smartproxy.SmartProxy; let testProxy: smartproxy.NetworkProxy;
tap.test('first test', async () => { tap.test('first test', async () => {
testProxy = new smartproxy.SmartProxy({}); testProxy = new smartproxy.NetworkProxy({
port: 3001
});
expect(testProxy).toBeInstanceOf(smartproxy.NetworkProxy);
}); });
tap.test('should start the testproxy', async () => { tap.test('should start the testproxy', async () => {
@ -12,9 +15,9 @@ tap.test('should start the testproxy', async () => {
}); });
tap.test('should supply reverse proxy config', async () => { tap.test('should supply reverse proxy config', async () => {
testProxy.updateReverseConfigs([ testProxy.updateProxyConfigs([
{ {
destinationIp: 'localhost', destinationIp: '127.0.0.1',
destinationPort: '3000', destinationPort: '3000',
hostName: 'push.rocks', hostName: 'push.rocks',
privateKey: `-----BEGIN PRIVATE KEY----- privateKey: `-----BEGIN PRIVATE KEY-----

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartproxy',
version: '2.0.16',
description: 'a proxy for handling high workloads of proxying'
}

View File

@ -1 +1,2 @@
export * from './smartproxy.classes.smartproxy'; export * from './smartproxy.classes.networkproxy.js'
export * from './smartproxy.portproxy.js';

View File

@ -1,12 +1,17 @@
import { expose } from '@pushrocks/smartspawn'; import * as plugins from './smartproxy.plugins.js';
import * as plugins from './smartproxy.plugins'; import { ProxyRouter } from './smartproxy.classes.router.js';
import { SmartproxyRouter } from './smartproxy.classes.router';
export class ProxyWorker { export interface INetworkProxyOptions {
port: number;
}
export class NetworkProxy {
// INSTANCE
public options: INetworkProxyOptions;
public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = []; public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
public httpsServer: plugins.https.Server; // | plugins.http.Server; public httpsServer: plugins.https.Server;
public port = 8001; public router = new ProxyRouter();
public router = new SmartproxyRouter();
public socketMap = new plugins.lik.ObjectMap<plugins.net.Socket>(); public socketMap = new plugins.lik.ObjectMap<plugins.net.Socket>();
public defaultHeaders: { [key: string]: string } = {}; public defaultHeaders: { [key: string]: string } = {};
@ -14,6 +19,10 @@ export class ProxyWorker {
[hostName: string]: plugins.tsclass.network.IReverseProxyConfig; [hostName: string]: plugins.tsclass.network.IReverseProxyConfig;
} = {}; } = {};
constructor(optionsArg: INetworkProxyOptions) {
this.options = optionsArg;
}
/** /**
* starts the proxyInstance * starts the proxyInstance
*/ */
@ -196,7 +205,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
); );
// Enable websockets // Enable websockets
const wss = new plugins.ws.Server({ server: this.httpsServer }); const wss = new plugins.ws.WebSocketServer({ server: this.httpsServer });
wss.on('connection', (ws: plugins.wsDefault) => { wss.on('connection', (ws: plugins.wsDefault) => {
console.log('got connection for wsc'); console.log('got connection for wsc');
const wscConnected = plugins.smartpromise.defer(); const wscConnected = plugins.smartpromise.defer();
@ -242,9 +251,12 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
connection.setTimeout(120000); connection.setTimeout(120000);
this.socketMap.add(connection); this.socketMap.add(connection);
const cleanupConnection = () => { const cleanupConnection = () => {
if (this.socketMap.checkForObject(connection)) {
this.socketMap.remove(connection); this.socketMap.remove(connection);
connection.end();
connection.removeAllListeners(); connection.removeAllListeners();
connection.destroy(); connection.destroy();
}
}; };
connection.on('close', () => { connection.on('close', () => {
cleanupConnection(); cleanupConnection();
@ -260,8 +272,8 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
}); });
}); });
this.httpsServer.listen(this.port); this.httpsServer.listen(this.options.port);
console.log(`ProxyWorker -> OK: now listening for new connections on port ${this.port}`); console.log(`ProxyWorker -> OK: now listening for new connections on port ${this.options.port}`);
} }
public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]) { public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]) {
@ -306,28 +318,3 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
await done.promise; 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[]) => {
await proxyWorkerInstance.updateProxyConfigs(configArray);
},
addDefaultHeaders: async (headersArg: { [key: string]: string }) => {
proxyWorkerInstance.defaultHeaders = {
...proxyWorkerInstance.defaultHeaders,
...headersArg,
};
},
};
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
expose(proxyWorkerCalls);
console.log('ProxyWorker initialized');

View File

@ -1,6 +1,6 @@
import * as plugins from './smartproxy.plugins'; import * as plugins from './smartproxy.plugins.js';
export class SmartproxyRouter { export class ProxyRouter {
public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = []; public reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
/** /**

View File

@ -1,61 +0,0 @@
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<TProxyWorkerCalls>;
public portProxyFunctions: plugins.smartspawn.ModuleThread<TPortProxyCalls>;
public options: ISmartProxyOptions;
constructor(optionsArg: ISmartProxyOptions = {}) {
this.options = optionsArg;
}
public async updateReverseConfigs(
reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]
) {
// TODO search for old hostCandidates with that target
this.reverseConfigs = reverseConfigsArg;
if (this.proxyWorkerFunctions) {
await this.proxyWorkerFunctions.updateReverseConfigs(this.reverseConfigs);
}
}
public async 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.portProxyFunctions.start(this.options.port);
await this.proxyWorkerFunctions.start();
console.log('successfully spawned portproxy and proxyworkers!');
}
public async updateDefaultHeaders(defaultHeadersArg: {[key: string]: string}) {
await this.proxyWorkerFunctions.addDefaultHeaders(defaultHeadersArg);
}
public async stop() {
await this.proxyWorkerFunctions.stop();
await plugins.smartspawn.Thread.terminate(this.proxyWorkerFunctions);
console.log('proxy worker stopped');
await this.portProxyFunctions.stop();
await plugins.smartspawn.Thread.terminate(this.portProxyFunctions);
console.log('portproxy stopped');
console.log('Terminated all childs!');
}
}

View File

@ -15,11 +15,9 @@ export { tsclass };
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest'; import * as smartrequest from '@pushrocks/smartrequest';
import * as smartspawn from '@pushrocks/smartspawn';
import * as smartstring from '@pushrocks/smartstring'; import * as smartstring from '@pushrocks/smartstring';
import * as smartsystem from '@pushrocks/smartsystem';
export { lik, smartrequest, smartpromise, smartspawn, smartstring, smartsystem }; export { lik, smartrequest, smartpromise, smartstring };
// third party scope // third party scope
import * as ws from 'ws'; import * as ws from 'ws';

View File

@ -1,11 +1,18 @@
import * as plugins from './smartproxy.plugins'; import * as plugins from './smartproxy.plugins.js';
import { expose } from '@pushrocks/smartspawn';
import * as net from 'net'; import * as net from 'net';
let netServer: plugins.net.Server; let netServer: plugins.net.Server;
let httpServer: plugins.http.Server; let httpServer: plugins.http.Server;
const portProxyCalls = { export class PortProxy {
start: async (portArg = 8000) => { fromPort: number;
toPort: number;
constructor(fromPortArg: number, toPortArg: number) {
this.fromPort = fromPortArg;
this.toPort = toPortArg
}
public async start () {
httpServer = plugins.http.createServer((request, response) => { httpServer = plugins.http.createServer((request, response) => {
const requestUrl = new URL(request.url, `http://${request.headers.host}`); const requestUrl = new URL(request.url, `http://${request.headers.host}`);
const completeUrlWithoutProtocol = `${requestUrl.host}${requestUrl.pathname}${requestUrl.search}`; const completeUrlWithoutProtocol = `${requestUrl.host}${requestUrl.pathname}${requestUrl.search}`;
@ -17,7 +24,7 @@ const portProxyCalls = {
}); });
response.end(); response.end();
}); });
httpServer.listen(7999); httpServer.listen(this.fromPort);
const cleanUpSockets = (from: plugins.net.Socket, to: plugins.net.Socket) => { const cleanUpSockets = (from: plugins.net.Socket, to: plugins.net.Socket) => {
from.end(); from.end();
to.end(); to.end();
@ -32,7 +39,7 @@ const portProxyCalls = {
.createServer((from) => { .createServer((from) => {
const to = net.createConnection({ const to = net.createConnection({
host: 'localhost', host: 'localhost',
port: 8001, port: this.toPort,
}); });
from.setTimeout(120000); from.setTimeout(120000);
from.pipe(to); from.pipe(to);
@ -62,10 +69,11 @@ const portProxyCalls = {
cleanUpSockets(from, to); cleanUpSockets(from, to);
}) })
}) })
.listen(portArg); .listen(this.fromPort);
console.log(`PortProxy -> OK: Now listening on port ${portArg}`); console.log(`PortProxy -> OK: Now listening on port ${this.fromPort}`);
}, }
stop: async () => {
public async stop() {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
httpServer.close(() => { httpServer.close(() => {
netServer.close(() => { netServer.close(() => {
@ -73,10 +81,5 @@ const portProxyCalls = {
}); });
}); });
await done.promise; await done.promise;
}, }
}; };
export type TPortProxyCalls = typeof portProxyCalls;
expose(portProxyCalls);
console.log('PortProxy Initialized');

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}