-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorMiddleware.js
More file actions
executable file
·122 lines (102 loc) · 3.22 KB
/
gorMiddleware.js
File metadata and controls
executable file
·122 lines (102 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
"use strict";
const readline = require("readline");
const StringDecoder = require("string_decoder").StringDecoder;
const decoder = new StringDecoder("utf8");
const SQS = require('./awsSQS');
const CONFIG = require('./config');
const convertHexString = (hex) => {
var bytes = [];
for (var i = 0; i < hex.length - 1; i += 2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return decoder.write(Buffer.from(bytes));
};
const handleRequest = (request) => {
let components = request.split("\n");
let [type, reqId] = components[0].split(" ");
let [method, endpoint] = components[1].split(' ');
// Check request type
if(parseInt(type) !== 1){
return;
}
// Check if endpoint is enabled for analysis
let absoluteEndPoint = endpoint.split("?")[0];
let allowedEndpoints = CONFIG.allowedEndpoints[method];
let isMatchingRequest = allowedEndpoints.some(allowedEndpoint => {
return RegExp(allowedEndpoint).test(absoluteEndPoint);
});
if(!isMatchingRequest){
console.error("Not a matching request: ", endpoint);
return;
}
// Scrap headers from the request
let headers = {};
components.forEach(element => {
try {
let headerName = element.split(': ')[0];
if(CONFIG.allowedHeaders[headerName]){
let val = element.split(': ')[1];
// Remove trailing \r from the value
if(val[val.length - 1] == "\r"){
val = val.substring(0, val.length - 1);
}
headers[headerName] = val;
}
} catch (error) {
console.error("Error in parsing the headers: ", error);
return;
}
});
// Scrap body from the request
let body = components.splice(components.indexOf("\r"), components.length+1).join("");
// handle edge condition where no body is sent in post call
if(body === "\r"){
body = undefined;
}
try {
body = body && JSON.parse(body);
} catch (error) {
console.error("Error in parsing the body: ", error);
return;
}
console.error("reqId: ", reqId);
console.error("method: ", method);
console.error("endpoint: ", endpoint);
console.error("headers: ", headers);
console.error("body: ", body);
SQS.sendMessage({
"reqId": reqId,
"method": method,
"endpoint": endpoint,
"headers": headers,
"body": body
});
};
const fetchConfig = () => {
return new Promise((resolve, reject) => {
setTimeout(function(){
return resolve({});
}, 3000);
});
};
const rl = readline.createInterface({
input: process.stdin
});
console.error("Init GOR middleware");
SQS.purge()
.then(response => {
console.error("SQS purged successfully");
return fetchConfig();
})
.then(config => {
console.error("CONFIG successfully fetched: ", config);
console.error("GOR listner started");
rl.on("line", (input) => {
const str = convertHexString(input);
handleRequest(str);
});
})
.catch(error => {
console.error("Error in setting up GOR listiner: ", error);
});