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
8 // http://www.apache.org/licenses/LICENSE-2.0
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=================================================
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.
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');
33 privateKey = fs.readFileSync('cert/key.crt', 'utf8');
34 certificate = fs.readFileSync('cert/cert.crt', 'utf8');
35 credentials = {key: privateKey,
39 console.log("Could not load cert and key")
46 const sleep = (milliseconds) => {
47 return new Promise(resolve => setTimeout(resolve, milliseconds))
50 app.get("*/events/A1-POLICY-AGENT-READ/users/policy-agent*", inititate_long_poll);
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) {
57 millis=(millis < 0 ? 10000 : Math.min(millis, 60000)) //Max poll is 60 sec
58 console.log("Setting poll time to (ms): " + millis)
60 do_poll(req, res, req.url, Date.now()+millis)
63 function do_poll(req,res, url, millis) {
65 hostname: 'localhost',
70 http.get(options, function(resp) {
72 // Receiving chunk by chunk
73 resp.on('data', (chunk) => {
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)
86 //Eventually return the response
87 res.statusCode=resp.statusCode
88 res.header(resp.headers);
93 }.bind({millis:millis, url:url})).on("error", (err) => {
94 console.log("Error when reading from backend: " + err.message);
96 res.send("ERROR detected in frontend: "+ err.message)
101 //Catch all, resend from proxy
102 app.use(proxy('localhost:'+proxyport, {
107 var httpServer = http.createServer(app);
109 console.log("Running on http: "+httpPort)
110 httpServer.listen(httpPort, "::");
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, "::");