TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 1 | var http = require('http'); |
| 2 | var https = require('https'); |
| 3 | |
| 4 | var express = require('express'); |
| 5 | const stream = require('stream'); |
| 6 | var app = express(); |
| 7 | var fs = require("fs"); |
| 8 | var path = require('path'); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 9 | var ArgumentParser = require('argparse').ArgumentParser; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 10 | var privateKey = fs.readFileSync('cert/private.key', 'utf8'); |
| 11 | var certificate = fs.readFileSync('cert/certificate.crt', 'utf8'); |
| 12 | var credentials = {key: privateKey, cert: certificate}; |
| 13 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 14 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 15 | var bodyParser = require('body-parser') |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 16 | var startTime = Date.now(); |
| 17 | |
| 18 | var dr_callback_ip = '192.168.100.2'; //IP for DR when running as container. Can be changed by env DR_SIM_IP |
| 19 | |
| 20 | //Counters |
| 21 | var ctr_publish_requests = 0; |
| 22 | var ctr_publish_responses = 0; |
| 23 | var lastPublish = ""; |
| 24 | var dwl_volume = 0; |
| 25 | |
| 26 | var parser = new ArgumentParser({ |
| 27 | version: '0.0.1', |
| 28 | addHelp:true, |
| 29 | description: 'Datarouter redirect simulator' |
| 30 | }); |
| 31 | |
| 32 | parser.addArgument('--tc' , { help: 'TC $NoOfTc' } ); |
| 33 | parser.addArgument('--printtc' , |
| 34 | { |
| 35 | help: 'Print complete usage help', |
| 36 | action: 'storeTrue' |
| 37 | } |
| 38 | ); |
| 39 | |
| 40 | var args = parser.parseArgs(); |
| 41 | const tc_normal = "normal"; |
| 42 | const tc_no_publish ="no_publish" |
| 43 | const tc_10p_no_response = "10p_no_response"; |
| 44 | const tc_10first_no_response = "10first_no_response"; |
| 45 | const tc_100first_no_response = "100first_no_response"; |
| 46 | const tc_all_delay_10s = "all_delay_10s"; |
| 47 | const tc_10p_delay_10s = "10p_delay_10s"; |
| 48 | const tc_10p_error_response = "10p_error_response"; |
| 49 | const tc_10first_error_response = "10first_error_response"; |
| 50 | const tc_100first_error_response = "100first_error_response"; |
| 51 | |
| 52 | if (args.tc==tc_normal) { |
| 53 | console.log("TC: " + args.tc) |
| 54 | |
| 55 | } else if (args.tc==tc_no_publish) { |
| 56 | console.log("TC: " + args.tc) |
| 57 | |
| 58 | } else if (args.tc==tc_10p_no_response) { |
| 59 | console.log("TC: " + args.tc) |
| 60 | |
| 61 | } else if (args.tc==tc_10first_no_response) { |
| 62 | console.log("TC: " + args.tc) |
| 63 | |
| 64 | } else if (args.tc==tc_100first_no_response) { |
| 65 | console.log("TC: " + args.tc) |
| 66 | |
| 67 | } else if (args.tc==tc_all_delay_10s) { |
| 68 | console.log("TC: " + args.tc) |
| 69 | |
| 70 | } else if (args.tc==tc_10p_delay_10s) { |
| 71 | console.log("TC: " + args.tc) |
| 72 | |
| 73 | } else if (args.tc==tc_10p_error_response) { |
| 74 | console.log("TC: " + args.tc) |
| 75 | |
| 76 | } else if (args.tc==tc_10first_error_response) { |
| 77 | console.log("TC: " + args.tc) |
| 78 | |
| 79 | } else if (args.tc==tc_100first_error_response) { |
| 80 | console.log("TC: " + args.tc) |
| 81 | } else { |
| 82 | console.log("No TC specified, use: --tc <tc-id>"); |
| 83 | process.exit(0); |
| 84 | } |
| 85 | |
| 86 | if (args.printtc) { |
| 87 | console.log("TC " + tc_normal + ": Normal case, all files publish and DR updated"); |
| 88 | console.log("TC " + tc_no_publish + ": Ok response but no files published"); |
| 89 | console.log("TC " + tc_10p_no_response + ": 10% % no response (file not published)"); |
| 90 | console.log("TC " + tc_10first_no_response + ": 10 first requests give no response (files not published)"); |
| 91 | console.log("TC " + tc_100first_no_response + ": 100 first requests give no response (files not published)"); |
| 92 | console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s, normal publish"); |
| 93 | console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, normal publish"); |
| 94 | console.log("TC " + tc_10p_error_response + ": 10% error response (file not published)"); |
| 95 | console.log("TC " + tc_10first_error_response + ": 10 first requests give error response (file not published)"); |
| 96 | console.log("TC " + tc_100first_error_response + ": 100 first requests give error responses (file not published)"); |
| 97 | |
| 98 | process.exit(0); |
| 99 | } |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 100 | |
| 101 | // parse application/x-www-form-urlencoded |
| 102 | app.use(bodyParser.urlencoded({ extended: false })) |
| 103 | |
| 104 | // parse application/json |
| 105 | app.use(bodyParser.json()) |
| 106 | |
| 107 | // parse application/vnd.api+json as json |
| 108 | app.use(bodyParser.json({ type: 'application/vnd.api+json' })) |
| 109 | |
| 110 | // parse some custom thing into a Buffer |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 111 | app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' })) |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 112 | |
| 113 | // parse an HTML body into a string |
| 114 | app.use(bodyParser.text({ type: 'text/html' })) |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 115 | |
| 116 | //Formatting |
| 117 | function fmtMSS(s){ |
| 118 | return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff to mm:ss |
| 119 | } |
| 120 | function fmtLargeNumber(x) { |
| 121 | return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "); //Format large with space, eg: 1 000 000 |
| 122 | } |
| 123 | |
| 124 | //I'm alive function |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 125 | app.get("/",function(req, res){ |
| 126 | res.send("ok"); |
| 127 | }) |
| 128 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 129 | //Counter readout |
| 130 | app.get("/ctr_publish_requests",function(req, res){ |
| 131 | res.send(""+ctr_publish_requests); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 132 | }) |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 133 | app.get("/ctr_publish_responses",function(req, res){ |
| 134 | res.send(""+ctr_publish_responses); |
| 135 | }) |
| 136 | app.get("/execution_time",function(req, res){ |
| 137 | diff = fmtMSS(Math.floor((Date.now()-startTime)/1000)); |
| 138 | res.send(""+diff); |
| 139 | }) |
| 140 | app.get("/time_lastpublish",function(req, res){ |
| 141 | res.send(""+lastPublish); |
| 142 | }) |
| 143 | app.get("/dwl_volume",function(req, res){ |
| 144 | res.send(""+fmtLargeNumber(dwl_volume)); |
| 145 | }) |
| 146 | app.get("/tc_info",function(req, res){ |
| 147 | res.send(args.tc); |
| 148 | }) |
| 149 | |
| 150 | app.put('/publish/1/:filename', function (req, res) { |
| 151 | console.log(req.url); |
| 152 | console.log("First 25 bytes of body: " + req.body.slice(0,25)) |
| 153 | console.log(req.headers) |
| 154 | ctr_publish_requests++; |
| 155 | if (args.tc == tc_no_publish) { |
| 156 | tr_publish_responses++; |
| 157 | res.send("ok") |
| 158 | return; |
| 159 | } else if (args.tc==tc_10p_no_response && (ctr_publish_requests%10)==0) { |
| 160 | return; |
| 161 | } else if (args.tc==tc_10first_no_response && ctr_publish_requests<11) { |
| 162 | return; |
| 163 | } else if (args.tc==tc_100first_no_response && ctr_publish_requests<101) { |
| 164 | return; |
| 165 | } else if (args.tc==tc_10p_error_response && (ctr_publish_requests%10)==0) { |
| 166 | tr_publish_responses++; |
| 167 | res.send(400, ""); |
| 168 | return; |
| 169 | } else if (args.tc==tc_10first_error_response && ctr_publish_requests<11) { |
| 170 | tr_publish_responses++; |
| 171 | res.send(400, ""); |
| 172 | return; |
| 173 | } else if (args.tc==tc_100first_error_response && ctr_publish_requests<101) { |
| 174 | tr_publish_responses++; |
| 175 | res.send(400, ""); |
| 176 | return; |
| 177 | } else if (args.tc==tc_10p_delay_10s && (ctr_publish_requests%10)==0) { |
| 178 | console.log("sleep begin"); |
| 179 | timer(10000).then(_=>console.log("sleeping done")); |
| 180 | } else if (args.tc==tc_all_delay_10s) { |
| 181 | //var sleep = require('sleep'); |
| 182 | console.log("sleep begin"); |
| 183 | //sleep.sleep(10); |
| 184 | timer(10000).then(_=>console.log("sleeping done")); |
| 185 | } |
| 186 | |
| 187 | //Remaining part if normal file publish |
| 188 | |
| 189 | var filename = req.params.filename; |
| 190 | console.log(filename); |
| 191 | //Create filename (appending file size to name) to store |
| 192 | var storedFilename = path.resolve(__dirname, filename+"-"+req.body.length); |
| 193 | fs.writeFile(storedFilename, "", function (error) { //Store file with zero size |
| 194 | if (error) { console.error(error); } |
| 195 | }); |
| 196 | |
| 197 | //Make callback to update list of publish files in DR sim |
| 198 | //Note the hard code ip-adress, DR sim get this ip if simulators started from the |
| 199 | //script in the 'simulatorgroup' dir. |
| 200 | //Work around: Could not get a normal http put to work from nodejs, using curl instead |
| 201 | var util = require('util'); |
| 202 | var exec = require('child_process').exec; |
| 203 | |
| 204 | var command = 'curl -s -X PUT http://' + dr_callback_ip + ':3906/dr_redir_publish/' +req.params.filename; |
| 205 | |
| 206 | console.log("Callback to DR sim to report file published, cmd: " + command); |
| 207 | child = exec(command, function(error, stdout, stderr){ |
| 208 | console.log('stdout: ' + stdout); |
| 209 | console.log('stderr: ' + stderr); |
| 210 | if(error !== null) { |
| 211 | console.log('exec error: ' + error); |
| 212 | } |
| 213 | |
| 214 | }); |
| 215 | |
| 216 | //Update status variables |
| 217 | ctr_publish_responses++; |
| 218 | lastPublish = fmtMSS(Math.floor((Date.now()-startTime)/1000)); |
| 219 | dwl_volume = dwl_volume + req.body.length; |
| 220 | |
| 221 | res.send("ok") |
| 222 | }); |
| 223 | |
| 224 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 225 | var httpServer = http.createServer(app); |
| 226 | var httpsServer = https.createServer(credentials, app); |
| 227 | |
| 228 | var httpPort=3908 |
| 229 | var httpsPort=3909 |
| 230 | httpServer.listen(httpPort); |
| 231 | console.log("DR-simulator listening (http) at "+httpPort) |
| 232 | httpsServer.listen(httpsPort); |
| 233 | console.log("DR-simulator listening (https) at "+httpsPort) |
| 234 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame^] | 235 | if (process.env.DR_SIM_IP) { |
| 236 | dr_callback_ip=process.env.DR_SIM_IP; |
| 237 | } |
| 238 | console.log("Using IP " + dr_callback_ip + " for callback to DR sim"); |