Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
703cbedad4 | |||
dd7e9e8416 | |||
da060fa986 | |||
df001e13f3 | |||
ef7e54be34 | |||
d800b6ed6e | |||
af42598464 | |||
93b1048cb7 | |||
29549b126e | |||
736113eb4e | |||
3b2d140836 | |||
70690f6400 | |||
ae561e3e88 | |||
8a02a0c506 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
|||||||
|
|
||||||
# builds
|
# builds
|
||||||
dist/
|
dist/
|
||||||
dist_web/
|
dist_*/
|
||||||
dist_serve/
|
|
||||||
dist_ts_web/
|
|
||||||
|
|
||||||
# custom
|
# custom
|
@ -19,22 +19,35 @@ mirror:
|
|||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
auditProductionDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -49,9 +62,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@ -62,9 +73,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -84,6 +93,8 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"projectType": {
|
"projectType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["website", "element", "service", "npm"]
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11188
package-lock.json
generated
11188
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartproxy",
|
"name": "@pushrocks/smartproxy",
|
||||||
"version": "1.0.37",
|
"version": "2.0.5",
|
||||||
"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_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -13,34 +13,38 @@
|
|||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.17",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tstest": "^1.0.28",
|
"@gitzone/tstest": "^1.0.52",
|
||||||
"@pushrocks/tapbundle": "^3.2.0",
|
"@pushrocks/tapbundle": "^3.2.10",
|
||||||
"@types/node": "^13.7.0",
|
"@types/node": "^14.14.22",
|
||||||
"tslint": "^6.0.0",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.16",
|
"@pushrocks/lik": "^4.0.20",
|
||||||
"@pushrocks/smartnetwork": "^1.1.18",
|
"@pushrocks/smartnetwork": "^1.1.22",
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.1.3",
|
||||||
"@pushrocks/smartrequest": "^1.1.47",
|
"@pushrocks/smartrequest": "^1.1.51",
|
||||||
"@pushrocks/smartspawn": "^2.0.9",
|
"@pushrocks/smartspawn": "^2.0.9",
|
||||||
"@pushrocks/smartstring": "^3.0.18",
|
"@pushrocks/smartstring": "^3.0.24",
|
||||||
"@pushrocks/smartsystem": "^2.0.9",
|
"@pushrocks/smartsystem": "^2.0.9",
|
||||||
"@tsclass/tsclass": "^3.0.3",
|
"@tsclass/tsclass": "^3.0.29",
|
||||||
"@types/ws": "^7.2.1",
|
"@types/ws": "^7.4.0",
|
||||||
"ws": "^7.2.1"
|
"ws": "^7.4.2"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
"ts_web/**/*",
|
"ts_web/**/*",
|
||||||
"dist/**/*",
|
"dist/**/*",
|
||||||
"dist_web/**/*",
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
"dist_ts_web/**/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/**/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
24
readme.md
24
readme.md
@ -8,16 +8,26 @@ a proxy for handling high workloads of proxying
|
|||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartproxy/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartproxy/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartproxy/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartproxy/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartproxy)
|
-- | --
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartproxy)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
Snyk | [](https://lossless.cloud)
|
||||||
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ tap.test('should start the testproxy', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should supply reverse proxy config', async () => {
|
tap.test('should supply reverse proxy config', async () => {
|
||||||
testProxy.updateReversConfigs([
|
testProxy.updateReverseConfigs([
|
||||||
{
|
{
|
||||||
destinationIp: 'localhost',
|
destinationIp: 'localhost',
|
||||||
destinationPort: '3000',
|
destinationPort: '3000',
|
||||||
@ -97,12 +97,12 @@ TzJTbTCteOUUJTrcfZ0gGhGkF4nYLmX5OI+TPqrDJf0fZ+mzAEHzDDVXcBYpYRDr
|
|||||||
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
|
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
|
||||||
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
||||||
-----END CERTIFICATE-----
|
-----END CERTIFICATE-----
|
||||||
`
|
`,
|
||||||
}
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should wait for 60 seconds', async tools => {
|
tap.test('should wait for 60 seconds', async (tools) => {
|
||||||
await tools.delayFor(10000);
|
await tools.delayFor(10000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ export class ProxyWorker {
|
|||||||
public httpsServer: plugins.https.Server; // | plugins.http.Server;
|
public httpsServer: plugins.https.Server; // | plugins.http.Server;
|
||||||
public port = 8001;
|
public port = 8001;
|
||||||
public router = new SmartproxyRouter();
|
public router = new SmartproxyRouter();
|
||||||
public socketMap = new plugins.lik.Objectmap<plugins.net.Socket>();
|
public socketMap = new plugins.lik.ObjectMap<plugins.net.Socket>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* starts the proxyInstance
|
* starts the proxyInstance
|
||||||
@ -101,7 +101,7 @@ TzJTbTCteOUUJTrcfZ0gGhGkF4nYLmX5OI+TPqrDJf0fZ+mzAEHzDDVXcBYpYRDr
|
|||||||
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
|
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
|
||||||
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
||||||
-----END CERTIFICATE-----
|
-----END CERTIFICATE-----
|
||||||
`
|
`,
|
||||||
},
|
},
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
console.log('got request');
|
console.log('got request');
|
||||||
@ -126,7 +126,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
if (authHeader) {
|
if (authHeader) {
|
||||||
if (!authHeader.includes('Basic ')) {
|
if (!authHeader.includes('Basic ')) {
|
||||||
return endRequest(401, 'Authentication required', {
|
return endRequest(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 = req.headers.authorization.replace('Basic ', '');
|
||||||
@ -160,14 +160,14 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
destinationUrl,
|
destinationUrl,
|
||||||
{
|
{
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: req.headers
|
headers: req.headers,
|
||||||
},
|
},
|
||||||
true, // lets make this streaming
|
true, // lets make this streaming
|
||||||
request => {
|
(request) => {
|
||||||
req.on('data', data => {
|
req.on('data', (data) => {
|
||||||
request.write(data);
|
request.write(data);
|
||||||
});
|
});
|
||||||
req.on('end', data => {
|
req.on('end', (data) => {
|
||||||
request.end();
|
request.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -177,7 +177,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
for (const header of Object.keys(response.headers)) {
|
for (const header of Object.keys(response.headers)) {
|
||||||
res.setHeader(header, response.headers[header]);
|
res.setHeader(header, response.headers[header]);
|
||||||
}
|
}
|
||||||
response.on('data', data => {
|
response.on('data', (data) => {
|
||||||
res.write(data);
|
res.write(data);
|
||||||
});
|
});
|
||||||
response.on('end', () => {
|
response.on('end', () => {
|
||||||
@ -194,33 +194,33 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
|
|
||||||
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', () => {
|
wsc.on('open', () => {
|
||||||
wscConnected.resolve();
|
wscConnected.resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.on('message', async message => {
|
ws.on('message', async (message) => {
|
||||||
await wscConnected.promise;
|
await wscConnected.promise;
|
||||||
wsc.emit('message', message);
|
wsc.emit('message', message);
|
||||||
});
|
});
|
||||||
wsc.on('message', message => {
|
wsc.on('message', (message) => {
|
||||||
ws.emit('message', message);
|
ws.emit('message', message);
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle closing
|
// handle closing
|
||||||
ws.on('close', message => {
|
ws.on('close', (message) => {
|
||||||
wsc.close();
|
wsc.close();
|
||||||
});
|
});
|
||||||
wsc.on('close', message => {
|
wsc.on('close', (message) => {
|
||||||
ws.close();
|
ws.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
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) => {
|
||||||
this.socketMap.add(connection);
|
this.socketMap.add(connection);
|
||||||
connection.on('close', () => {
|
connection.on('close', () => {
|
||||||
this.socketMap.remove(connection);
|
this.socketMap.remove(connection);
|
||||||
@ -231,14 +231,14 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
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.port}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.IReverseProxyConfig[]) {
|
public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]) {
|
||||||
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);
|
||||||
this.httpsServer.addContext(hostCandidate.hostName, {
|
this.httpsServer.addContext(hostCandidate.hostName, {
|
||||||
cert: hostCandidate.publicKey,
|
cert: hostCandidate.publicKey,
|
||||||
key: hostCandidate.privateKey
|
key: hostCandidate.privateKey,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/* this.httpsServer.close();
|
/* this.httpsServer.close();
|
||||||
@ -250,7 +250,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
this.httpsServer.close(() => {
|
this.httpsServer.close(() => {
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
await this.socketMap.forEach(async socket => {
|
await this.socketMap.forEach(async (socket) => {
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
});
|
});
|
||||||
await done.promise;
|
await done.promise;
|
||||||
@ -269,7 +269,7 @@ const proxyWorkerCalls = {
|
|||||||
},
|
},
|
||||||
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
|
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
|
||||||
await proxyWorkerInstance.updateProxyConfigs(configArray);
|
await proxyWorkerInstance.updateProxyConfigs(configArray);
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
|
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
|
||||||
|
@ -16,7 +16,7 @@ export class SmartproxyRouter {
|
|||||||
*/
|
*/
|
||||||
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
|
public routeReq(req: plugins.http.IncomingMessage): plugins.tsclass.network.IReverseProxyConfig {
|
||||||
const originalHost = req.headers.host;
|
const originalHost = req.headers.host;
|
||||||
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find(reverseConfig => {
|
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find((reverseConfig) => {
|
||||||
return reverseConfig.hostName === originalHost;
|
return reverseConfig.hostName === originalHost;
|
||||||
});
|
});
|
||||||
return correspodingReverseProxyConfig;
|
return correspodingReverseProxyConfig;
|
||||||
@ -24,7 +24,7 @@ export class SmartproxyRouter {
|
|||||||
|
|
||||||
public routeWs(ws: plugins.wsDefault) {
|
public routeWs(ws: plugins.wsDefault) {
|
||||||
const originalHost = plugins.url.parse(ws.url).host;
|
const originalHost = plugins.url.parse(ws.url).host;
|
||||||
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find(reverseConfig => {
|
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find((reverseConfig) => {
|
||||||
return reverseConfig.hostName === originalHost;
|
return reverseConfig.hostName === originalHost;
|
||||||
});
|
});
|
||||||
return correspodingReverseProxyConfig.destinationIp;
|
return correspodingReverseProxyConfig.destinationIp;
|
||||||
|
@ -19,7 +19,7 @@ export class SmartProxy {
|
|||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateReversConfigs(
|
public async updateReverseConfigs(
|
||||||
reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]
|
reverseConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]
|
||||||
) {
|
) {
|
||||||
// TODO search for old hostCandidates with that target
|
// TODO search for old hostCandidates with that target
|
||||||
|
@ -2,28 +2,75 @@ import * as plugins from './smartproxy.plugins';
|
|||||||
import { expose } from '@pushrocks/smartspawn';
|
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;
|
||||||
|
|
||||||
const portProxyCalls = {
|
const portProxyCalls = {
|
||||||
start: async (portArg = 8000) => {
|
start: async (portArg = 8000) => {
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
httpServer.listen(7999);
|
||||||
|
const cleanUpSockets = (from: plugins.net.Socket, to: plugins.net.Socket) => {
|
||||||
|
from.end();
|
||||||
|
to.end();
|
||||||
|
from.removeAllListeners();
|
||||||
|
to.removeEventListener();
|
||||||
|
from.unpipe();
|
||||||
|
to.unpipe();
|
||||||
|
}
|
||||||
netServer = net
|
netServer = net
|
||||||
.createServer(from => {
|
.createServer((from) => {
|
||||||
const to = net.createConnection({
|
const to = net.createConnection({
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
port: 8001
|
port: 8001,
|
||||||
});
|
});
|
||||||
from.pipe(to);
|
from.pipe(to);
|
||||||
to.pipe(from);
|
to.pipe(from);
|
||||||
|
from.on('error', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
to.on('error', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
from.on('close', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
to.on('close', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
from.on('timeout', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
to.on('timeout', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
});
|
||||||
|
from.on('end', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
})
|
||||||
|
to.on('end', () => {
|
||||||
|
cleanUpSockets(from, to);
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.listen(portArg);
|
.listen(portArg);
|
||||||
console.log(`PortProxy -> OK: Now listening on port ${portArg}`);
|
console.log(`PortProxy -> OK: Now listening on port ${portArg}`);
|
||||||
},
|
},
|
||||||
stop: async () => {
|
stop: async () => {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer();
|
||||||
|
httpServer.close(() => {
|
||||||
netServer.close(() => {
|
netServer.close(() => {
|
||||||
done.resolve();
|
done.resolve();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
await done.promise;
|
await done.promise;
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TPortProxyCalls = typeof portProxyCalls;
|
export type TPortProxyCalls = typeof portProxyCalls;
|
||||||
|
Reference in New Issue
Block a user