2019-08-22 13:09:48 +00:00
|
|
|
import { expose } from '@pushrocks/smartspawn';
|
2019-08-21 13:09:32 +00:00
|
|
|
import * as plugins from './smartproxy.plugins';
|
2019-08-21 21:54:55 +00:00
|
|
|
import { SmartproxyRouter } from './smartproxy.classes.router';
|
2019-08-21 13:09:32 +00:00
|
|
|
|
|
|
|
export class ProxyWorker {
|
2019-08-22 10:49:29 +00:00
|
|
|
public hostCandidates: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
2019-08-22 13:09:48 +00:00
|
|
|
public httpsServer: plugins.https.Server; // | plugins.http.Server;
|
|
|
|
public port = 8001;
|
2019-08-21 21:54:55 +00:00
|
|
|
public router = new SmartproxyRouter();
|
2019-08-21 13:09:32 +00:00
|
|
|
|
2019-08-22 13:09:48 +00:00
|
|
|
public async setPort(portArg: number) {
|
|
|
|
this.port = portArg;
|
|
|
|
}
|
|
|
|
|
2019-08-21 21:54:55 +00:00
|
|
|
/**
|
|
|
|
* starts the proxyInstance
|
|
|
|
*/
|
|
|
|
public async start() {
|
2019-08-22 13:09:48 +00:00
|
|
|
this.httpsServer = plugins.https.createServer(async (req, res) => {
|
2019-08-22 10:49:29 +00:00
|
|
|
const destinationConfig = this.router.routeReq(req);
|
2019-08-21 21:54:55 +00:00
|
|
|
const response = await plugins.smartrequest.request(
|
2019-08-22 10:49:29 +00:00
|
|
|
`http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`,
|
2019-08-21 21:54:55 +00:00
|
|
|
{
|
|
|
|
method: req.method,
|
|
|
|
headers: req.headers
|
|
|
|
},
|
2019-08-22 10:49:29 +00:00
|
|
|
true // lets make this streaming
|
2019-08-21 21:54:55 +00:00
|
|
|
);
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
|
2019-08-22 14:14:50 +00:00
|
|
|
ws.on('message', async message => {
|
2019-08-21 21:54:55 +00:00
|
|
|
await wscConnected.promise;
|
|
|
|
wsc.emit('message', message);
|
|
|
|
});
|
2019-08-22 14:14:50 +00:00
|
|
|
wsc.on('message', message => {
|
2019-08-21 21:54:55 +00:00
|
|
|
ws.emit('message', message);
|
|
|
|
});
|
|
|
|
|
|
|
|
// handle closing
|
2019-08-22 14:14:50 +00:00
|
|
|
ws.on('close', message => {
|
2019-08-21 21:54:55 +00:00
|
|
|
wsc.close();
|
|
|
|
});
|
2019-08-22 14:14:50 +00:00
|
|
|
wsc.on('close', message => {
|
2019-08-21 21:54:55 +00:00
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-22 13:09:48 +00:00
|
|
|
this.httpsServer.listen(this.port);
|
|
|
|
console.log(`OK: now listening for new connections on port ${this.port}`);
|
2019-08-21 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:29 +00:00
|
|
|
public async updateCandidates(arrayOfReverseCandidates: plugins.tsclass.IReverseProxyConfig[]) {
|
|
|
|
this.hostCandidates = arrayOfReverseCandidates;
|
2019-08-22 13:09:48 +00:00
|
|
|
this.router.setNewCandidates(arrayOfReverseCandidates);
|
2019-08-22 10:49:29 +00:00
|
|
|
for (const hostCandidate of this.hostCandidates) {
|
|
|
|
this.httpsServer.addContext(hostCandidate.hostName, {
|
|
|
|
cert: hostCandidate.publicKey,
|
|
|
|
key: hostCandidate.privateKey
|
|
|
|
});
|
|
|
|
}
|
2019-08-21 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {
|
|
|
|
const done = plugins.smartpromise.defer();
|
|
|
|
this.httpsServer.close(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
2019-08-22 13:09:48 +00:00
|
|
|
await done.promise;
|
2019-08-21 21:54:55 +00:00
|
|
|
}
|
2019-08-21 13:09:32 +00:00
|
|
|
}
|
2019-08-22 13:09:48 +00:00
|
|
|
|
|
|
|
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[]) => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
|
|
|
|
expose(proxyWorkerCalls);
|
2019-08-22 14:14:50 +00:00
|
|
|
console.log('ProxyWorker initialized');
|