Compare commits
86 Commits
Author | SHA1 | Date | |
---|---|---|---|
7b0f824d29 | |||
b5796b86d5 | |||
1f8ea59221 | |||
d717568572 | |||
28d050851f | |||
acbd109985 | |||
cc38a6d10e | |||
748b07efe2 | |||
be4fd0978a | |||
4521010b82 | |||
bd1f1a4c1c | |||
d3bdd56660 | |||
c38a7c4c32 | |||
858628196a | |||
4910679058 | |||
97db2012ca | |||
0ee13b4e06 | |||
21f5882fa3 | |||
48b43f9f0d | |||
d3d476fd53 | |||
b80b8a0a20 | |||
384943f697 | |||
e9239ed978 | |||
baf1844866 | |||
0b3d7f8a06 | |||
c38a2745e9 | |||
a0f39d1c5b | |||
c67ac868a5 | |||
90e1a0453e | |||
d7765fb5dc | |||
0fdd17b430 | |||
0562de6aa1 | |||
7b550a35aa | |||
fb66aac6e7 | |||
208790cfcf | |||
5978bbaf66 | |||
1c47eafe5f | |||
69e3a71354 | |||
21e92bf0c1 | |||
d732e6e7aa | |||
5fdfcdb407 | |||
49e2e90bda | |||
b8e53e7b42 | |||
1136841b3d | |||
42cbc51d22 | |||
2d16403ad1 | |||
afe847499a | |||
f980bb70b4 | |||
f192a8f041 | |||
64bf3aef6d | |||
a5e3cbd05b | |||
2f0fad999a | |||
5e6477720d | |||
bad8bf0688 | |||
4f1db106fb | |||
d47829a8b2 | |||
ca55d06244 | |||
7284924b26 | |||
10857aa12b | |||
c968c7f844 | |||
b07015f6c4 | |||
ca4ddade17 | |||
17eaea4124 | |||
d3d8f6ff57 | |||
906661c7f4 | |||
ea46caebb7 | |||
973e896fbf | |||
4605035b01 | |||
1e4e2c4ab6 | |||
30896b045f | |||
08f382b9fa | |||
1629dc1f5a | |||
b33acdea41 | |||
101470dcd4 | |||
ca73849541 | |||
b64523b0b2 | |||
963ad6efa4 | |||
d271029302 | |||
018fcbf71e | |||
fa04732241 | |||
da19fab8d8 | |||
8d318dca28 | |||
d03bfcc793 | |||
4ba2686977 | |||
d24c4d4b7a | |||
e1d4d6cf38 |
@ -12,20 +12,12 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
auditProductionDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
@ -36,6 +28,7 @@ auditProductionDependencies:
|
|||||||
- npmci command npm audit --audit-level=high --only=prod --production
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
auditDevDependencies:
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
@ -96,10 +89,9 @@ codequality:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
@ -119,11 +111,10 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "current file",
|
"command": "npm test",
|
||||||
"type": "node",
|
"name": "Run npm test",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"args": [
|
"type": "node-terminal"
|
||||||
"${relativeFile}"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "test.ts",
|
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
|
||||||
"args": [
|
|
||||||
"test/test.ts"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartproxy",
|
"gitrepo": "smartproxy",
|
||||||
"shortDescription": "a proxy for handling high workloads of proxying",
|
"description": "a proxy for handling high workloads of proxying",
|
||||||
"npmPackagename": "@pushrocks/smartproxy",
|
"npmPackagename": "@pushrocks/smartproxy",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "push.rocks"
|
"projectDomain": "push.rocks"
|
||||||
|
21541
package-lock.json
generated
21541
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
36
package.json
36
package.json
@ -1,36 +1,34 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartproxy",
|
"name": "@pushrocks/smartproxy",
|
||||||
"version": "2.0.9",
|
"version": "3.0.35",
|
||||||
"private": false,
|
"private": false,
|
||||||
"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)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"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/smartdelay": "^2.0.13",
|
||||||
"@pushrocks/smartpromise": "^3.1.3",
|
"@pushrocks/smartpromise": "^3.1.7",
|
||||||
"@pushrocks/smartrequest": "^1.1.51",
|
"@pushrocks/smartrequest": "^2.0.6",
|
||||||
"@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.2"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
13
test/test.ts
13
test/test.ts
@ -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
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartproxy',
|
||||||
|
version: '3.0.35',
|
||||||
|
description: 'a proxy for handling high workloads of proxying'
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
export * from './smartproxy.classes.smartproxy';
|
export * from './smartproxy.classes.networkproxy.js';
|
||||||
|
export * from './smartproxy.portproxy.js';
|
||||||
|
export * from './smartproxy.classes.sslredirect.js'
|
@ -1,15 +1,27 @@
|
|||||||
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 } = {};
|
||||||
|
|
||||||
|
public alreadyAddedReverseConfigs: {
|
||||||
|
[hostName: string]: plugins.tsclass.network.IReverseProxyConfig;
|
||||||
|
} = {};
|
||||||
|
|
||||||
|
constructor(optionsArg: INetworkProxyOptions) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* starts the proxyInstance
|
* starts the proxyInstance
|
||||||
*/
|
*/
|
||||||
@ -104,33 +116,46 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
-----END CERTIFICATE-----
|
-----END CERTIFICATE-----
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
async (req, res) => {
|
async (originRequest, originResponse) => {
|
||||||
console.log('got request');
|
/**
|
||||||
const destinationConfig = this.router.routeReq(req);
|
* endRequest function
|
||||||
|
* can be used to prematurely end a request
|
||||||
// endRequest function
|
*/
|
||||||
const endRequest = (
|
const endOriginReqRes = (
|
||||||
statusArg: number = 404,
|
statusArg: number = 404,
|
||||||
messageArg: string = 'This route is not available on this server.',
|
messageArg: string = 'This route is not available on this server.',
|
||||||
headers: plugins.http.OutgoingHttpHeaders = {}
|
headers: plugins.http.OutgoingHttpHeaders = {}
|
||||||
) => {
|
) => {
|
||||||
res.writeHead(statusArg, messageArg);
|
originResponse.writeHead(statusArg, messageArg);
|
||||||
res.end(messageArg);
|
originResponse.end(messageArg);
|
||||||
|
if (originRequest.socket !== originResponse.socket) {
|
||||||
|
console.log('hey, something is strange.')
|
||||||
|
}
|
||||||
|
originResponse.destroy();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(`got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`);
|
||||||
|
const destinationConfig = this.router.routeReq(originRequest);
|
||||||
|
|
||||||
|
if (!destinationConfig) {
|
||||||
|
console.log(`${originRequest.headers.host} can't be routed properly. Terminating request.`);
|
||||||
|
endOriginReqRes();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// authentication
|
// authentication
|
||||||
if (destinationConfig.authentication) {
|
if (destinationConfig.authentication) {
|
||||||
const authInfo = destinationConfig.authentication;
|
const authInfo = destinationConfig.authentication;
|
||||||
switch (authInfo.type) {
|
switch (authInfo.type) {
|
||||||
case 'Basic':
|
case 'Basic':
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = originRequest.headers.authorization;
|
||||||
if (authHeader) {
|
if (authHeader) {
|
||||||
if (!authHeader.includes('Basic ')) {
|
if (!authHeader.includes('Basic ')) {
|
||||||
return endRequest(401, 'Authentication required', {
|
return endOriginReqRes(401, 'Authentication required', {
|
||||||
'WWW-Authenticate': 'Basic realm="Access to the staging site", charset="UTF-8"',
|
'WWW-Authenticate': 'Basic realm="Access to the staging site", charset="UTF-8"',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const authStringBase64 = req.headers.authorization.replace('Basic ', '');
|
const authStringBase64 = originRequest.headers.authorization.replace('Basic ', '');
|
||||||
const authString: string = plugins.smartstring.base64.decode(authStringBase64);
|
const authString: string = plugins.smartstring.base64.decode(authStringBase64);
|
||||||
const userPassArray = authString.split(':');
|
const userPassArray = authString.split(':');
|
||||||
const user = userPassArray[0];
|
const user = userPassArray[0];
|
||||||
@ -138,12 +163,12 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
if (user === authInfo.user && pass === authInfo.pass) {
|
if (user === authInfo.user && pass === authInfo.pass) {
|
||||||
console.log('request successfully authenticated');
|
console.log('request successfully authenticated');
|
||||||
} else {
|
} else {
|
||||||
return endRequest(403, 'Forbidden: Wrong credentials');
|
return endOriginReqRes(403, 'Forbidden: Wrong credentials');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return endRequest(
|
return endOriginReqRes(
|
||||||
403,
|
403,
|
||||||
'Forbidden: unsupported authentication method configured. Please report to the admin.'
|
'Forbidden: unsupported authentication method configured. Please report to the admin.'
|
||||||
);
|
);
|
||||||
@ -152,101 +177,176 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
|
|
||||||
let destinationUrl: string;
|
let destinationUrl: string;
|
||||||
if (destinationConfig) {
|
if (destinationConfig) {
|
||||||
destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`;
|
destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${originRequest.url}`;
|
||||||
} else {
|
} else {
|
||||||
return endRequest();
|
return endOriginReqRes();
|
||||||
}
|
}
|
||||||
console.log(destinationUrl);
|
console.log(destinationUrl);
|
||||||
const response = await plugins.smartrequest.request(
|
const proxyResponse = await plugins.smartrequest.request(
|
||||||
destinationUrl,
|
destinationUrl,
|
||||||
{
|
{
|
||||||
method: req.method,
|
method: originRequest.method,
|
||||||
headers: req.headers,
|
headers: originRequest.headers,
|
||||||
|
keepAlive: true,
|
||||||
},
|
},
|
||||||
true, // lets make this streaming
|
true, // lets make this streaming
|
||||||
(request) => {
|
(proxyRequest) => {
|
||||||
req.on('data', (data) => {
|
originRequest.on('data', (data) => {
|
||||||
request.write(data);
|
proxyRequest.write(data);
|
||||||
});
|
});
|
||||||
req.on('end', (data) => {
|
originRequest.on('end', (data) => {
|
||||||
request.end();
|
proxyRequest.end();
|
||||||
});
|
});
|
||||||
|
originRequest.on('error', () => {
|
||||||
|
proxyRequest.end();
|
||||||
|
});
|
||||||
|
originRequest.on('close', () => {
|
||||||
|
proxyRequest.end();
|
||||||
|
});
|
||||||
|
originRequest.on('timeout', () => {
|
||||||
|
proxyRequest.end();
|
||||||
|
originRequest.destroy();
|
||||||
|
});
|
||||||
|
proxyRequest.on('error', () => {
|
||||||
|
endOriginReqRes();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
res.statusCode = response.statusCode;
|
originResponse.statusCode = proxyResponse.statusCode;
|
||||||
console.log(response.statusCode);
|
console.log(proxyResponse.statusCode);
|
||||||
for (const defaultHeader of Object.keys(this.defaultHeaders)) {
|
for (const defaultHeader of Object.keys(this.defaultHeaders)) {
|
||||||
res.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
|
originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
|
||||||
}
|
}
|
||||||
for (const header of Object.keys(response.headers)) {
|
for (const header of Object.keys(proxyResponse.headers)) {
|
||||||
res.setHeader(header, response.headers[header]);
|
originResponse.setHeader(header, proxyResponse.headers[header]);
|
||||||
}
|
}
|
||||||
response.on('data', (data) => {
|
proxyResponse.on('data', (data) => {
|
||||||
res.write(data);
|
originResponse.write(data);
|
||||||
});
|
});
|
||||||
response.on('end', () => {
|
proxyResponse.on('end', () => {
|
||||||
res.end();
|
originResponse.end();
|
||||||
});
|
});
|
||||||
|
proxyResponse.on('error', () => {
|
||||||
|
originResponse.destroy();
|
||||||
|
});
|
||||||
|
proxyResponse.on('close', () => {
|
||||||
|
originResponse.end();
|
||||||
|
});
|
||||||
|
proxyResponse.on('timeout', () => {
|
||||||
|
originResponse.end();
|
||||||
|
originResponse.destroy()
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 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();
|
let wscConnectedDeferred: plugins.smartpromise.Deferred<plugins.wsDefault>;
|
||||||
|
|
||||||
|
ws.on('message', async (message) => {
|
||||||
|
if (!wscConnectedDeferred) {
|
||||||
|
wscConnectedDeferred = plugins.smartpromise.defer();
|
||||||
const wsc = new plugins.wsDefault(this.router.routeWs(ws), {
|
const wsc = new plugins.wsDefault(this.router.routeWs(ws), {
|
||||||
headers: {
|
headers: {
|
||||||
Host: ws.url,
|
Host: ws.url,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
wsc.on('open', () => {
|
|
||||||
wscConnected.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('message', async (message) => {
|
wsc.on('open', () => {
|
||||||
await wscConnected.promise;
|
wscConnectedDeferred.resolve(wsc);
|
||||||
wsc.emit('message', message);
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
const wsc = await wscConnectedDeferred.promise;
|
||||||
|
wsc.emit('message', message);
|
||||||
wsc.on('message', (message) => {
|
wsc.on('message', (message) => {
|
||||||
ws.emit('message', message);
|
ws.emit('message', message);
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle closing
|
// handle closing
|
||||||
|
const cleanUp = () => {
|
||||||
|
ws.terminate();
|
||||||
|
wsc.terminate();
|
||||||
|
};
|
||||||
|
|
||||||
ws.on('close', (message) => {
|
ws.on('close', (message) => {
|
||||||
wsc.close();
|
cleanUp();
|
||||||
});
|
});
|
||||||
|
|
||||||
wsc.on('close', (message) => {
|
wsc.on('close', (message) => {
|
||||||
ws.close();
|
cleanUp();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
this.httpsServer.keepAliveTimeout = 61000;
|
this.httpsServer.keepAliveTimeout = 61000;
|
||||||
this.httpsServer.headersTimeout = 65000;
|
this.httpsServer.headersTimeout = 65000;
|
||||||
|
|
||||||
this.httpsServer.on('connection', (connection) => {
|
this.httpsServer.on('connection', (connection: plugins.net.Socket) => {
|
||||||
this.socketMap.add(connection);
|
this.socketMap.add(connection);
|
||||||
connection.on('close', () => {
|
console.log(`added connection. now ${this.socketMap.getArray().length} sockets connected.`);
|
||||||
|
const cleanupConnection = () => {
|
||||||
|
if (this.socketMap.checkForObject(connection)) {
|
||||||
this.socketMap.remove(connection);
|
this.socketMap.remove(connection);
|
||||||
|
console.log(`removed connection. ${this.socketMap.getArray().length} sockets remaining.`);
|
||||||
|
connection.destroy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
connection.on('close', () => {
|
||||||
|
cleanupConnection();
|
||||||
});
|
});
|
||||||
|
connection.on('error', () => {
|
||||||
|
cleanupConnection();
|
||||||
|
});
|
||||||
|
connection.on('end', () => {
|
||||||
|
cleanupConnection();
|
||||||
|
});
|
||||||
|
connection.on('timeout', () => {
|
||||||
|
cleanupConnection();
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
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[]) {
|
||||||
|
console.log(`got new proxy configs`);
|
||||||
this.proxyConfigs = proxyConfigsArg;
|
this.proxyConfigs = proxyConfigsArg;
|
||||||
this.router.setNewProxyConfigs(proxyConfigsArg);
|
this.router.setNewProxyConfigs(proxyConfigsArg);
|
||||||
for (const hostCandidate of this.proxyConfigs) {
|
for (const hostCandidate of this.proxyConfigs) {
|
||||||
// console.log(hostCandidate);
|
// console.log(hostCandidate);
|
||||||
|
|
||||||
|
const existingHostNameConfig = this.alreadyAddedReverseConfigs[hostCandidate.hostName];
|
||||||
|
|
||||||
|
if (!existingHostNameConfig) {
|
||||||
|
this.alreadyAddedReverseConfigs[hostCandidate.hostName] = hostCandidate;
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
existingHostNameConfig.publicKey === hostCandidate.publicKey &&
|
||||||
|
existingHostNameConfig.privateKey === hostCandidate.privateKey
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
this.alreadyAddedReverseConfigs[hostCandidate.hostName] = hostCandidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.httpsServer.addContext(hostCandidate.hostName, {
|
this.httpsServer.addContext(hostCandidate.hostName, {
|
||||||
cert: hostCandidate.publicKey,
|
cert: hostCandidate.publicKey,
|
||||||
key: hostCandidate.privateKey,
|
key: hostCandidate.privateKey,
|
||||||
});
|
});
|
||||||
|
this.httpsServer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async addDefaultHeaders(headersArg: { [key: string]: string }) {
|
||||||
|
for (const headerKey of Object.keys(headersArg)) {
|
||||||
|
this.defaultHeaders[headerKey] = headersArg[headerKey];
|
||||||
}
|
}
|
||||||
/* this.httpsServer.close();
|
|
||||||
this.httpsServer.listen(this.port); */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async stop() {
|
public async stop() {
|
||||||
@ -260,23 +360,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 (headers: {[key: string]: string}) => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
|
|
||||||
expose(proxyWorkerCalls);
|
|
||||||
console.log('ProxyWorker initialized');
|
|
@ -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[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,57 +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 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!');
|
|
||||||
}
|
|
||||||
}
|
|
32
ts/smartproxy.classes.sslredirect.ts
Normal file
32
ts/smartproxy.classes.sslredirect.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import * as plugins from './smartproxy.plugins.js';
|
||||||
|
|
||||||
|
export class SslRedirect {
|
||||||
|
httpServer: plugins.http.Server;
|
||||||
|
port: number;
|
||||||
|
constructor(portArg: number) {
|
||||||
|
this.port = portArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async start() {
|
||||||
|
this.httpServer = plugins.http.createServer((request, response) => {
|
||||||
|
const requestUrl = new URL(request.url, `http://${request.headers.host}`);
|
||||||
|
const completeUrlWithoutProtocol = `${requestUrl.host}${requestUrl.pathname}${requestUrl.search}`;
|
||||||
|
const redirectUrl = `https://${completeUrlWithoutProtocol}`;
|
||||||
|
console.log(`Got http request for http://${completeUrlWithoutProtocol}`);
|
||||||
|
console.log(`Redirecting to ${redirectUrl}`);
|
||||||
|
response.writeHead(302, {
|
||||||
|
Location: redirectUrl,
|
||||||
|
});
|
||||||
|
response.end();
|
||||||
|
});
|
||||||
|
this.httpServer.listen(this.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async stop() {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
this.httpServer.close(() => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
await done.promise;
|
||||||
|
}
|
||||||
|
}
|
@ -13,13 +13,12 @@ export { tsclass };
|
|||||||
|
|
||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
import * as lik from '@pushrocks/lik';
|
import * as lik from '@pushrocks/lik';
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
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, smartdelay, smartrequest, smartpromise, smartstring };
|
||||||
|
|
||||||
// third party scope
|
// third party scope
|
||||||
import * as ws from 'ws';
|
import * as ws from 'ws';
|
||||||
|
@ -1,23 +1,17 @@
|
|||||||
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 httpServer: plugins.http.Server;
|
|
||||||
|
|
||||||
const portProxyCalls = {
|
export class PortProxy {
|
||||||
start: async (portArg = 8000) => {
|
netServer: plugins.net.Server;
|
||||||
httpServer = plugins.http.createServer((request, response) => {
|
fromPort: number;
|
||||||
const requestUrl = new URL(request.url, `http://${request.headers.host}`);
|
toPort: number;
|
||||||
const completeUrlWithoutProtocol = `${requestUrl.host}${requestUrl.pathname}${requestUrl.search}`;
|
|
||||||
const redirectUrl = `https://${completeUrlWithoutProtocol}`;
|
constructor(fromPortArg: number, toPortArg: number) {
|
||||||
console.log(`Got http request for http://${completeUrlWithoutProtocol}`);
|
this.fromPort = fromPortArg;
|
||||||
console.log(`Redirecting to ${redirectUrl}`);
|
this.toPort = toPortArg;
|
||||||
response.writeHead(302, {
|
}
|
||||||
Location: redirectUrl,
|
|
||||||
});
|
public async start() {
|
||||||
response.end();
|
|
||||||
});
|
|
||||||
httpServer.listen(7999);
|
|
||||||
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();
|
||||||
@ -27,12 +21,12 @@ const portProxyCalls = {
|
|||||||
to.unpipe();
|
to.unpipe();
|
||||||
from.destroy();
|
from.destroy();
|
||||||
to.destroy();
|
to.destroy();
|
||||||
}
|
};
|
||||||
netServer = net
|
this.netServer = net
|
||||||
.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);
|
||||||
@ -57,26 +51,20 @@ const portProxyCalls = {
|
|||||||
});
|
});
|
||||||
from.on('end', () => {
|
from.on('end', () => {
|
||||||
cleanUpSockets(from, to);
|
cleanUpSockets(from, to);
|
||||||
})
|
});
|
||||||
to.on('end', () => {
|
to.on('end', () => {
|
||||||
cleanUpSockets(from, to);
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
})
|
.listen(this.fromPort);
|
||||||
.listen(portArg);
|
console.log(`PortProxy -> OK: Now listening on port ${this.fromPort}`);
|
||||||
console.log(`PortProxy -> OK: Now listening on port ${portArg}`);
|
}
|
||||||
},
|
|
||||||
stop: async () => {
|
public async stop() {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer();
|
||||||
httpServer.close(() => {
|
this.netServer.close(() => {
|
||||||
netServer.close(() => {
|
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
await done.promise;
|
await done.promise;
|
||||||
},
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export type TPortProxyCalls = typeof portProxyCalls;
|
|
||||||
expose(portProxyCalls);
|
|
||||||
|
|
||||||
console.log('PortProxy Initialized');
|
|
||||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -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"
|
|
||||||
}
|
|
Reference in New Issue
Block a user