From 0b7f5c4701ba9da486bee5f798a1b02133f374f0 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 21 Aug 2019 23:54:55 +0200 Subject: [PATCH] fix(core): update --- ts/smartproxy.classes.proxyworker.ts | 75 ++++++++++++++++++++++++++++ ts/smartproxy.classes.smartproxy.ts | 75 ++-------------------------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/ts/smartproxy.classes.proxyworker.ts b/ts/smartproxy.classes.proxyworker.ts index 10ad9d0..1256ce5 100644 --- a/ts/smartproxy.classes.proxyworker.ts +++ b/ts/smartproxy.classes.proxyworker.ts @@ -1,5 +1,80 @@ import * as plugins from './smartproxy.plugins'; +import * as interfaces from './interfaces'; +import { SmartproxyRouter } from './smartproxy.classes.router'; export class ProxyWorker { + public httpsServer: plugins.https.Server | plugins.http.Server; + public router = new SmartproxyRouter(); + /** + * starts the proxyInstance + */ + 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 + ); + 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, + key: hostCandidate.privateKey + }); */ + } + + // 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(); + }); + } } diff --git a/ts/smartproxy.classes.smartproxy.ts b/ts/smartproxy.classes.smartproxy.ts index 4b83872..fc2e08f 100644 --- a/ts/smartproxy.classes.smartproxy.ts +++ b/ts/smartproxy.classes.smartproxy.ts @@ -1,12 +1,7 @@ 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 httpsServer: plugins.https.Server | plugins.http.Server; - public router = new SmartproxyRouter(); public hostCandidates: interfaces.IHostConfig[] = []; @@ -15,75 +10,13 @@ export class SmartProxy { this.hostCandidates.push(hostCandidate); } - /** - * starts the proxyInstance - */ - 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 - ); - 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, - key: hostCandidate.privateKey - }); */ - } + async start () { - // 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(); + async stop () { + } - public async stop() { - const done = plugins.smartpromise.defer(); - this.httpsServer.close(() => { - done.resolve(); - }); - } + }