fix(core): update

This commit is contained in:
2019-08-21 23:41:06 +02:00
parent c32a56d921
commit ff8e185ec3
5 changed files with 99 additions and 18 deletions

View File

@ -21,10 +21,14 @@ export class SmartProxy {
public async start() {
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);
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]);
@ -42,15 +46,44 @@ export class SmartProxy {
key: hostCandidate.privateKey
}); */
}
this.httpsServer.on('upgrade', (req, socket: Socket) => {
})
// 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.httpsServer.listen(3000);
}
public async update() {
await this.start();
}
public async stop() {
const done = plugins.smartpromise.defer();
this.httpsServer.close(() => {
done.resolve();
});
}
}

View File

@ -1,12 +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
smartrequest,
smartpromise
};
// third party scope
import ws from 'ws';
export {
ws
};