import { expose } from '@pushrocks/smartspawn'; import * as plugins from './smartproxy.plugins'; import { SmartproxyRouter } from './smartproxy.classes.router'; export class ProxyWorker { public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = []; public httpsServer: plugins.https.Server; // | plugins.http.Server; public port = 8001; public router = new SmartproxyRouter(); /** * starts the proxyInstance */ public async start() { 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 NAnoAPua683MD2ZZd4eovEww/imSZvui3NlisSSh1SomABDFxiEaHpewI98n8P1E 3vfg4lyCV5VcUjwrPjnkfEJbX1c1/PXqTtPqSqFn/pI4FuTES6qDugS2EA/XT1ln ODHigOiFCzDbhOMuQjhI8hzuevrRRQIDAQABAoICAQC7nU+HW6qmpQebZ5nbUVT1 Deo6Js+lwudg+3a13ghqzLnBXNW7zkrkV8mNLxW5h3bFhZ+LMcxwrXIPQ29Udmlf USiacC1E5RBZgjSg86xYgNjU4E6EFfZ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gEAu0+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'); 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( destinationUrl, { method: req.method, headers: req.headers }, true // lets make this streaming ); 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', (ws: plugins.ws) => { console.log('got connection for wsc'); const wscConnected = plugins.smartpromise.defer(); const wsc = new plugins.ws(this.router.routeWs(ws), { headers: { Host: 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(this.port); console.log(`OK: now listening for new connections on port ${this.port}`); } public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.IReverseProxyConfig[]) { this.proxyConfigs = proxyConfigsArg; this.router.setNewProxyConfigs(proxyConfigsArg); for (const hostCandidate of this.proxyConfigs) { // console.log(hostCandidate); this.httpsServer.addContext(hostCandidate.hostName, { cert: hostCandidate.publicKey, key: hostCandidate.privateKey, }); } /* this.httpsServer.close(); this.httpsServer.listen(this.port); */ } public async stop() { const done = plugins.smartpromise.defer(); this.httpsServer.close(() => { done.resolve(); }); await done.promise; } } 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[]) => { await proxyWorkerInstance.updateProxyConfigs(configArray); } }; export type TProxyWorkerCalls = typeof proxyWorkerCalls; expose(proxyWorkerCalls); console.log('ProxyWorker initialized');