Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
23fa6a6511 | |||
ff8e185ec3 | |||
c32a56d921 | |||
69233e8a3b | |||
23e6977d81 | |||
9f57b3b638 | |||
5df0a53efe | |||
6d1a781b9a | |||
81c9f3b72e | |||
c8ac0498c8 | |||
ee0900f3c0 | |||
7c46d16686 | |||
10cd3b3528 | |||
0e6c09aba5 | |||
2f4916f552 |
1460
package-lock.json
generated
1460
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartproxy",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.14",
|
||||
"private": false,
|
||||
"description": "a proxy for handling high workloads of proxying",
|
||||
"main": "dist/index.js",
|
||||
@ -17,14 +17,15 @@
|
||||
"@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/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.20",
|
||||
"@tsclass/tsclass": "^2.0.3",
|
||||
"@types/ws": "^6.0.2",
|
||||
"ws": "^7.1.2"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
|
11
test/test.ts
11
test/test.ts
@ -1,9 +1,18 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartproxy from '../ts/index';
|
||||
|
||||
let testProxy: smartproxy.SmartProxy;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
const testProxy = new smartproxy.SmartProxy();
|
||||
testProxy = new smartproxy.SmartProxy();
|
||||
});
|
||||
|
||||
tap.test('should start the testproxy', async () => {
|
||||
await testProxy.start();
|
||||
});
|
||||
|
||||
tap.test('should close the testproxy', async () => {
|
||||
await testProxy.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
5
ts/smartproxy.classes.proxyworker.ts
Normal file
5
ts/smartproxy.classes.proxyworker.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import * as plugins from './smartproxy.plugins';
|
||||
|
||||
export class ProxyWorker {
|
||||
|
||||
}
|
@ -2,7 +2,7 @@ import * as plugins from './smartproxy.plugins';
|
||||
|
||||
export class SmartproxyRouter {
|
||||
|
||||
public routeReq(req: plugins.express.Request) {
|
||||
return 'https://lossless.gmbh';
|
||||
public routeReq(req: plugins.http.IncomingMessage) {
|
||||
return 'lossless.gmbh';
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ import * as plugins from './smartproxy.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { SmartproxyRouter } from './smartproxy.classes.router';
|
||||
import { Socket } from 'net';
|
||||
|
||||
export class SmartProxy {
|
||||
public expressInstance: plugins.express.Express;
|
||||
public httpsServer: plugins.https.Server | plugins.http.Server;
|
||||
public router = new SmartproxyRouter();
|
||||
|
||||
@ -19,8 +19,27 @@ export class SmartProxy {
|
||||
* starts the proxyInstance
|
||||
*/
|
||||
public async start() {
|
||||
this.expressInstance = plugins.express();
|
||||
this.httpsServer = plugins.http.createServer(this.expressInstance);
|
||||
this.httpsServer = plugins.http.createServer(async (req, res) => {
|
||||
req.headers.host = this.router.routeReq(req);
|
||||
const response = await plugins.smartrequest.request(
|
||||
`https://${req.headers.host}${req.url}`,
|
||||
{
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
},
|
||||
true
|
||||
);
|
||||
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();
|
||||
});
|
||||
});
|
||||
for (const hostCandidate of this.hostCandidates) {
|
||||
/* this.httpsServer.addContext(hostCandidate.hostName, {
|
||||
cert: hostCandidate.publicKey,
|
||||
@ -28,23 +47,43 @@ export class SmartProxy {
|
||||
}); */
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
};
|
||||
// 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.expressInstance.use(plugins.httpProxyMiddleware(proxyOptions));
|
||||
this.httpsServer.listen(3000);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async update() {
|
||||
await this.start();
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
this.httpsServer.close(() => {
|
||||
done.resolve();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,22 @@
|
||||
// node native scope
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import crypto from 'crypto';
|
||||
import http from 'http';
|
||||
import https from 'https';
|
||||
|
||||
export { http, https };
|
||||
export { crypto, http, https };
|
||||
|
||||
// pushrocks scope
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export {
|
||||
smartrequest,
|
||||
smartpromise
|
||||
};
|
||||
|
||||
// third party scope
|
||||
import express from 'express';
|
||||
import httpProxyMiddleware from 'http-proxy-middleware';
|
||||
import ws from 'ws';
|
||||
|
||||
export { express, httpProxyMiddleware };
|
||||
export {
|
||||
ws
|
||||
};
|
||||
|
Reference in New Issue
Block a user