BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 1 | // ============LICENSE_START=============================================== |
| 2 | // Copyright (C) 2020 Nordix Foundation. All rights reserved. |
| 3 | // ======================================================================== |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // ============LICENSE_END================================================= |
| 16 | // |
| 17 | |
| 18 | //This script acts like a proxy. All operations, except MR GET messages, are re-sent to the python backend server. |
| 19 | //The MR GET is intercepted and the python backend is polled until a message is available or up to a |
| 20 | //maximum time decided by a query parameter. No query parameter will result in a 10s long poll. |
| 21 | |
| 22 | |
| 23 | const proxy = require('express-http-proxy'); |
| 24 | const app = require('express')(); |
| 25 | const http = require('http'); |
| 26 | const https = require('https'); |
| 27 | const fs = require('fs'); |
| 28 | var privateKey; |
| 29 | var certificate; |
| 30 | var credentials; |
| 31 | |
| 32 | try { |
| 33 | privateKey = fs.readFileSync('cert/key.crt', 'utf8'); |
| 34 | certificate = fs.readFileSync('cert/cert.crt', 'utf8'); |
| 35 | credentials = {key: privateKey, |
| 36 | cert: certificate, |
| 37 | passphrase: 'test'}; |
| 38 | } catch(exp) { |
| 39 | console.log("Could not load cert and key") |
| 40 | } |
| 41 | |
| 42 | const httpPort=3905; |
| 43 | const httpsPort=3906; |
| 44 | const proxyport=2222 |
| 45 | |
| 46 | const sleep = (milliseconds) => { |
| 47 | return new Promise(resolve => setTimeout(resolve, milliseconds)) |
| 48 | } |
| 49 | |
| 50 | app.get("*/events/A1-POLICY-AGENT-READ/users/policy-agent*", inititate_long_poll); |
| 51 | |
| 52 | function inititate_long_poll(req,res) { |
| 53 | var millis=10000 //MR default is 10sec |
| 54 | var tmp=req.query.timeout |
| 55 | if (tmp != undefined) { |
| 56 | millis=parseInt(tmp); |
| 57 | millis=(millis < 0 ? 10000 : Math.min(millis, 60000)) //Max poll is 60 sec |
| 58 | console.log("Setting poll time to (ms): " + millis) |
| 59 | } |
| 60 | do_poll(req, res, req.url, Date.now()+millis) |
| 61 | } |
| 62 | |
| 63 | function do_poll(req,res, url, millis) { |
| 64 | const options = { |
| 65 | hostname: 'localhost', |
| 66 | port: proxyport, |
| 67 | path: url, |
| 68 | method: 'GET' |
| 69 | } |
| 70 | http.get(options, function(resp) { |
| 71 | let data = ''; |
| 72 | // Receiving chunk by chunk |
| 73 | resp.on('data', (chunk) => { |
| 74 | data += chunk; |
| 75 | }); |
| 76 | |
| 77 | // Full response received |
| 78 | resp.on('end', () => { |
| 79 | var payload=data.trim(); |
| 80 | if (resp.statusCode == 200 && payload.length == 2 && payload=="[]" && Date.now()<this.millis) { |
| 81 | // Sleep before next poll |
| 82 | sleep(10).then(() => { |
| 83 | do_poll(req,res, this.url, this.millis) |
| 84 | }) |
| 85 | } else { |
| 86 | //Eventually return the response |
| 87 | res.statusCode=resp.statusCode |
| 88 | res.header(resp.headers); |
| 89 | res.send(data) |
| 90 | return; |
| 91 | } |
| 92 | }); |
| 93 | }.bind({millis:millis, url:url})).on("error", (err) => { |
| 94 | console.log("Error when reading from backend: " + err.message); |
| 95 | res.statusCode=500 |
| 96 | res.send("ERROR detected in frontend: "+ err.message) |
| 97 | return |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | //Catch all, resend from proxy |
| 102 | app.use(proxy('localhost:'+proxyport, { |
| 103 | limit: '5mb', |
| 104 | })); |
| 105 | |
| 106 | //Start serving http |
| 107 | var httpServer = http.createServer(app); |
| 108 | var httpsServer; |
| 109 | console.log("Running on http: "+httpPort) |
| 110 | httpServer.listen(httpPort, "::"); |
| 111 | |
| 112 | //Start serving https if cert is available |
| 113 | if (credentials != undefined) { |
| 114 | httpsServer = https.createServer(credentials, app); |
| 115 | console.log("Running on https: "+httpsPort) |
| 116 | httpsServer.listen(httpsPort, "::"); |
| 117 | } |