Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
fb76ecfd8a | |||
6c84406574 | |||
945065279f | |||
07a8d9bec6 | |||
2c55a6b819 | |||
8f4421fbc3 | |||
5eebc434bb | |||
ed03c3ec4b |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartproxy",
|
"name": "@pushrocks/smartproxy",
|
||||||
"version": "1.0.26",
|
"version": "1.0.30",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartproxy",
|
"name": "@pushrocks/smartproxy",
|
||||||
"version": "1.0.26",
|
"version": "1.0.30",
|
||||||
"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",
|
||||||
|
@ -3,7 +3,7 @@ import * as plugins from './smartproxy.plugins';
|
|||||||
import { SmartproxyRouter } from './smartproxy.classes.router';
|
import { SmartproxyRouter } from './smartproxy.classes.router';
|
||||||
|
|
||||||
export class ProxyWorker {
|
export class ProxyWorker {
|
||||||
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||||
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();
|
||||||
@ -97,8 +97,17 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
}, async (req, res) => {
|
}, async (req, res) => {
|
||||||
console.log('got request');
|
console.log('got request');
|
||||||
const destinationConfig = this.router.routeReq(req);
|
const destinationConfig = this.router.routeReq(req);
|
||||||
|
let destinationUrl: string;
|
||||||
|
if (destinationConfig) {
|
||||||
|
destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`;
|
||||||
|
} else {
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end('This route is not available on this server\n');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(destinationUrl);
|
||||||
const response = await plugins.smartrequest.request(
|
const response = await plugins.smartrequest.request(
|
||||||
`http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`,
|
destinationUrl,
|
||||||
{
|
{
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: req.headers
|
headers: req.headers
|
||||||
@ -117,15 +126,17 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const Websocket = await import('ws');
|
|
||||||
|
|
||||||
// Enable websockets
|
// Enable websockets
|
||||||
const wss = new plugins.ws.Server({ server: this.httpsServer });
|
const wss = new plugins.ws.Server({ server: this.httpsServer });
|
||||||
wss.on('connection', function connection(ws) {
|
wss.on('connection', (ws: plugins.ws) => {
|
||||||
console.log('got connection for wsc');
|
console.log('got connection for wsc');
|
||||||
const wscConnected = plugins.smartpromise.defer();
|
const wscConnected = plugins.smartpromise.defer();
|
||||||
|
|
||||||
const wsc = new Websocket.default(`${ws.url}`);
|
const wsc = new plugins.ws(this.router.routeWs(ws), {
|
||||||
|
headers: {
|
||||||
|
Host: ws.url
|
||||||
|
}
|
||||||
|
});
|
||||||
wsc.on('open', () => {
|
wsc.on('open', () => {
|
||||||
wscConnected.resolve();
|
wscConnected.resolve();
|
||||||
});
|
});
|
||||||
@ -151,10 +162,10 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
|
|||||||
console.log(`OK: now listening for new connections on port ${this.port}`);
|
console.log(`OK: now listening for new connections on port ${this.port}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateCandidates(arrayOfReverseCandidates: plugins.tsclass.IReverseProxyConfig[]) {
|
public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.IReverseProxyConfig[]) {
|
||||||
this.hostCandidates = arrayOfReverseCandidates;
|
this.proxyConfigs = proxyConfigsArg;
|
||||||
this.router.setNewCandidates(arrayOfReverseCandidates);
|
this.router.setNewProxyConfigs(proxyConfigsArg);
|
||||||
for (const hostCandidate of this.hostCandidates) {
|
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,
|
||||||
@ -185,7 +196,7 @@ const proxyWorkerCalls = {
|
|||||||
await proxyWorkerInstance.start();
|
await proxyWorkerInstance.start();
|
||||||
},
|
},
|
||||||
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
|
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
|
||||||
await proxyWorkerInstance.updateCandidates(configArray);
|
await proxyWorkerInstance.updateProxyConfigs(configArray);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,20 +1,32 @@
|
|||||||
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 reverseProxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sets a new set of reverse configs to be routed to
|
* sets a new set of reverse configs to be routed to
|
||||||
* @param reverseCandidatesArg
|
* @param reverseCandidatesArg
|
||||||
*/
|
*/
|
||||||
public setNewCandidates(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]) {
|
public setNewProxyConfigs(reverseCandidatesArg: plugins.tsclass.network.IReverseProxyConfig[]) {
|
||||||
this.reverseCandidates = reverseCandidatesArg;
|
this.reverseProxyConfigs = reverseCandidatesArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* routes a request
|
||||||
|
*/
|
||||||
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.reverseCandidates.find(reverseConfig => {
|
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find(reverseConfig => {
|
||||||
return reverseConfig.hostName === originalHost;
|
return reverseConfig.hostName === originalHost;
|
||||||
});
|
});
|
||||||
return correspodingReverseProxyConfig;
|
return correspodingReverseProxyConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public routeWs(ws: plugins.ws) {
|
||||||
|
const originalHost = plugins.url.parse(ws.url).host;
|
||||||
|
const correspodingReverseProxyConfig = this.reverseProxyConfigs.find(reverseConfig => {
|
||||||
|
return reverseConfig.hostName === originalHost;
|
||||||
|
});
|
||||||
|
return correspodingReverseProxyConfig.destinationIp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
// node native scope
|
// node native scope
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
|
import * as url from 'url';
|
||||||
|
|
||||||
export { http, https };
|
export { http, https, url };
|
||||||
|
|
||||||
// tsclass scope
|
// tsclass scope
|
||||||
import * as tsclass from '@tsclass/tsclass';
|
import * as tsclass from '@tsclass/tsclass';
|
||||||
@ -18,6 +19,6 @@ import * as smartsystem from '@pushrocks/smartsystem';
|
|||||||
export { smartrequest, smartpromise, smartspawn, smartsystem };
|
export { smartrequest, smartpromise, smartspawn, smartsystem };
|
||||||
|
|
||||||
// third party scope
|
// third party scope
|
||||||
import * as ws from 'ws';
|
import ws from 'ws';
|
||||||
|
|
||||||
export { ws };
|
export { ws };
|
||||||
|
Reference in New Issue
Block a user