smartproxy/ts/smartproxy.classes.proxyworker.ts

204 lines
8.5 KiB
TypeScript
Raw Normal View History

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-21 21:54:55 +00:00
/**
* starts the proxyInstance
*/
public async start() {
2019-09-20 16:40:55 +00:00
this.httpsServer = plugins.https.createServer({
key: `-----BEGIN PRIVATE KEY-----
MIIJRQIBADANBgkqhkiG9w0BAQEFAASCCS8wggkrAgEAAoICAQDi2F/0kQr96mhe
3yEWvy2mRHOZoSSBtIqg6Bre4ZcMu901/cHNIjFnynNGFl9Se61yZbW2F3PfCt7+
kQlHug1Cx+LFssvz+hLlB5cqJQZfRKx92DhbROygtxG9r7UBmx/fwx+JQ+HOHX9R
b+szLBZqxrNDBFl2SRdviconYgVnHbaqcAPj/lK6D6x94qgUEX+vMjbIruuiCe3u
RbYse/quzAednVnY/+BuGVn8SEb2EVVFnBEsOxxYpy5ZzGR48O3YnWkM2oPpJhrp
mMYLcARMnDmIQDVstD1i+MM2lVhx/pm9xKKUgWNJC7lyz2xRscZ4pOtLkfN94leH
U98nIvxfQe7tQFKN9K52yjdtoT0UaIEUFbZyddkoNka1Xx6r+rE96046BLT2lVs0
/rnTxZUFH6vP3z9UNktmpxtnZSk67Pj6QAqZtgT0amXEpBlk7vBYSjHsyJ3+5R1y
oSjhAqeejq6M67NDOflrag5LSTkeTe4dqk0laVb1gjcse18AOlgf7pw5H79zclYH
NAnoAPua683MD2ZZd4eovE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/VDZLZqcbZ2UpOuz4+kAKmbYE9GplxKQZZO7wWEox7Mid/uUdcqEo4QKn
no6ujOuzQzn5a2oOS0k5Hk3uHapNJWlW9YI3LHtfADpYH+6cOR+/c3JWBzQJ6AD7
muvNzA9mWXeHqLxMMP4pkmb7otzZYrEkodUqJgAQxcYhGh6XsCPfJ/D9RN734OJc
gleVXFI8Kz455HxCW19XNfz16k7T6kqhZ/6SOBbkxEuqg7oEthAP109ZZzgx4oDo
hQsw24TjLkI4SPIc7nr60UUCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAu0+zrg0C
mlSv4Yi24OwB7TBvx+WHesl1IilCUdTiiUMo3NumvsU9Dr3Jkd0jGqYI0eyH4gIt
KrhAveXfEw7tAOEHiYicmAdIFtyzh++ZWb8mgbBeqij1MP/76Jv+cc0lUqpfRo/A
qytAsPAILuyL1o1jh28JHcq+v+WYn/FEhjUlH6emhGKGlsAjhUPjzK8MEshNolhj
t2UXw9WB5B2xWvrqlNMy0F3NAZBkZ/+k21HZo6FmVi+q6OEGcOo7wJt6wrH/lko9
LxX96GC1JoN1Pfr2FoTKy1WHzrSfyGmDIUCrbaYQ58UuMOR+5eIPPdkf/030u5eX
xXhF2fBujD57E2zQGh/l2MrOjamcSo0+wYhOqlX3WNdaKNAzPqloBnF6w7eqLYde
h9He39ySmxjENwv3miOjEP1sBeMBSRfL/ckEonfK5uJgYA5nVMQ3ojUeDMZzLfFE
Ue2WHt+uPyYk7mMZfOrK2uHzI2/Coqj7lbfRodFwj+fCArYBck2NZannDPKA6X8V
TzJTbTCteOUUJTrcfZ0gGhGkF4nYLmX5OI+TPqrDJf0fZ+mzAEHzDDVXcBYpYRDr
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
-----END CERTIFICATE-----
`
}, async (req, res) => {
console.log('got request');
2019-08-22 10:49:29 +00:00
const destinationConfig = this.router.routeReq(req);
2019-09-20 17:19:33 +00:00
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);
2019-08-21 21:54:55 +00:00
const response = await plugins.smartrequest.request(
2019-09-20 17:19:33 +00:00
destinationUrl,
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();
});
});
2019-08-22 14:22:19 +00:00
const Websocket = await import('ws');
2019-08-21 21:54:55 +00:00
// Enable websockets
const wss = new plugins.ws.Server({ server: this.httpsServer });
wss.on('connection', function connection(ws) {
2019-09-20 16:40:55 +00:00
console.log('got connection for wsc');
2019-08-21 21:54:55 +00:00
const wscConnected = plugins.smartpromise.defer();
2019-08-22 22:48:14 +00:00
2019-08-22 14:22:19 +00:00
const wsc = new Websocket.default(`${ws.url}`);
2019-08-21 21:54:55 +00:00
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) {
2019-09-20 16:40:55 +00:00
// console.log(hostCandidate);
2019-08-22 10:49:29 +00:00
this.httpsServer.addContext(hostCandidate.hostName, {
cert: hostCandidate.publicKey,
2019-09-20 16:40:55 +00:00
key: hostCandidate.privateKey,
2019-08-22 10:49:29 +00:00
});
}
2019-09-20 16:40:55 +00:00
/* this.httpsServer.close();
this.httpsServer.listen(this.port); */
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();
},
2019-09-20 16:40:55 +00:00
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
await proxyWorkerInstance.updateCandidates(configArray);
}
2019-08-22 13:09:48 +00:00
};
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
expose(proxyWorkerCalls);
2019-08-22 14:14:50 +00:00
console.log('ProxyWorker initialized');