-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
298 lines (248 loc) · 7.95 KB
/
app.js
File metadata and controls
298 lines (248 loc) · 7.95 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
*/
'use strict';
const express = require('express');
const request = require('request').defaults({jar: true});
const url = require("url");
const config = require('./config.json');
const bodyParser = require('body-parser');
const fs = require('fs');
const querystring = require('querystring');
const opn = require('opn');
const proxy = express();
const toap = express();
const toapPort = process.env.PORT || config.toapGuiPort || 3001;
const port = process.env.PORT || config.toapProxyPort || 3010;
const readable = require('stream').Readable;
// following variables and modules handle multipart file upload for request module.
const multipartFileStorage = __dirname + '/uploads';
const multer = require('multer');
const upload = multer({ dest: multipartFileStorage });
// module to remove uploaded multipart files.
const findRemoveSync = require('find-remove');
const recordHeaderCookie = config.recordHeaderCookie || false;
let thinkStartTime, thinkEndTime;
let firstTimeWrite = true;
// write to file
let file;
/*
Parse the url
*/
let urlParser;
let slashes;
let origin;
let urlPath;
/*
recording variables
*/
let recordThinkTime;
let recordHeaders;
/*
check if folder exists, else create folder flow in current directory
*/
let flowFolder = config.flowFolder || './flow';
checkDirectorySync(flowFolder);
/*
delete all files older than one hour for multer - 3600 seconds
*/
setInterval(function(){
findRemoveSync(multipartFileStorage, {age: {seconds: 3600}});
}, 3600);
/*
serving static files for toap gui.
*/
toap
.use(express.static(__dirname))
.use(bodyParser.urlencoded({ extended: true }))
.use(bodyParser.json())
.listen(toapPort);
console.log('Toap - GUI running on port ' + toapPort + '!');
let browser = config.browser || 'chrome'; // default to chrome browser
if (browser ==='chrome') {
if (process.platform === 'darwin') {
browser = 'google chrome';
} else if (process.platform === 'linux') {
browser = 'google-chrome';
}
}
opn('http://localhost:'+ toapPort, {app: [browser]});
/*
Toap Express server requests
*/
let server;
toap.post('/startToap', function (req, res) {
thinkStartTime = undefined;
if(typeof req.body.recordThinkTime === 'string') {
req.body.recordThinkTime = JSON.parse(req.body.recordThinkTime);
}
if(typeof req.body.recordHeaders === 'string') {
req.body.recordHeaders = JSON.parse(req.body.recordHeaders);
}
if (req.body.urlToRecord) {
urlParser = url.parse(req.body.urlToRecord);
slashes = urlParser.slashes ? '//' : '';
origin = urlParser.protocol + slashes + urlParser.host;
urlPath = urlParser.path;
if(urlParser.hash) urlPath = urlPath + urlParser.hash;
}
recordThinkTime = req.body.recordThinkTime;
recordHeaders = req.body.recordHeaders;
if (req.body.fileName) {
file = flowFolder + '/' + req.body.fileName + '.json';
}
if (!server) {
server = proxy.listen(port);
console.log('Toap - artillery.io http(s) recorder running on port ' + port + '!');
res.status(200).send({url: 'http://localhost:' + port + urlPath});
} else {
res.status(409).send({error: 'Toap proxy server already in use.'});
}
});
toap.post('/stopToap', function (req, res) {
if (server) {
server.close();
console.log('Toap - artillery.io http(s) recorder stopped !');
server = null;
res.status(200).send('Toap proxy stopped');
}
});
toap.post('/addComments', function (req, res) {
if (req.body && req.body.comments) {
//let comments = {comments: req.body.comments};
let comments = '{\n/*'+req.body.comments+'*/\n}';
writeToFile(comments);
}
});
toap.get('/getToapProxyStatus', function (req, res) {
if (server) {
res.status(200).send({status: 'UP'});
} else {
res.status(200).send({status: 'DOWN', error: 'Toap proxy server is down.'});
}
});
/*
Configure proxy server to use body parser for request body
*/
proxy.use(bodyParser.urlencoded({ extended: true }));
proxy.disable('content-security-policy');
/*
proxy all request
*/
proxy.use('*', upload.any(), function(req, res) {
// set headers for cors
if (req.headers.origin) {
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
} else {
res.setHeader('Access-Control-Allow-Origin', '*');
}
//TODO set headers for Content-Security-Policy.
//res.setHeader('Content-Security-Policy', "default-src http 'unsafe-inline' 'unsafe-eval'");
let method = req.method.toLowerCase();
//call calculateThinkTime to calculate pause between request.
if (recordThinkTime) calculateThinkTime();
// call createFlow function to generate the flow from request
createFlow(method, req.originalUrl, req.body, req.headers);
let reqOptions = {
url: origin + req.originalUrl
};
if (method === 'get') {
//req.pipe(request(origin + req.originalUrl)).pipe(res);
req.pipe(request[method](reqOptions)).pipe(res);
} else {
// if method is delete trim to del to handle by request module.
if (method === 'delete') {
method = 'del';
}
if ("content-type" in req.headers && req.headers['content-type'].includes('multipart/form-data')) {
let filesArray=[];
for (let i = 0; i < req.files.length; i++) {
filesArray.push(fs.createReadStream(req.files[i].path));
}
let formData = {
attachments: filesArray
};
reqOptions.formData = formData;
} else if ('content-type' in req.headers && req.headers['content-type'].includes('application/x-www-form-urlencoded')) {
reqOptions.form = req.body;
} else if ('content-type' in req.headers && req.headers['content-type'].includes('application/json')) {
reqOptions.json = req.body;
}
request[method](reqOptions).pipe(res);
}
});
function createFlow(method, requestUrl, body, headers){
let flowJson = {};
let httpMethod = method;
flowJson[httpMethod] = {};
flowJson[httpMethod].url = requestUrl;
if (recordHeaders) {
if (!recordHeaderCookie) {
delete headers.cookie; //delete cookie information
}
//loop through the headers values and replace the hostname
for (let key in headers) {
if (Object.prototype.hasOwnProperty.call(headers, key)) {
let val = headers[key];
let regex = new RegExp("localhost:" + port, "g");
val = val.replace(regex, urlParser.host);
headers[key] = val;
}
}
flowJson[httpMethod].headers = headers;
}
if ('content-type' in headers && headers['content-type'].includes('application/x-www-form-urlencoded')) {
flowJson[httpMethod].body = querystring.stringify(body);
} else if ('content-type' in headers && headers['content-type'].includes('application/json')) {
flowJson[httpMethod].json = body;
} else if ('content-type' in headers && headers['content-type'].includes('multipart/form-data')) {
writeToFile('\n/*Comment: this is an upload ' + httpMethod + ' request. No body is recorded.*/\n');
flowJson[httpMethod].body = {};
}
writeToFile(flowJson);
}
function writeToFile(object) {
if (typeof object === 'object') {
object = JSON.stringify(object, null, 1);
}
if (firstTimeWrite) {
firstTimeWrite = false;
} else {
object = ',' + object;
}
// using stream to write to file
let ws = fs.createWriteStream(file, {'flags': 'a+'});
let rs = new readable;
rs.push(object);
rs.push(null);
rs.pipe(ws);
}
function calculateThinkTime() {
if (thinkStartTime !== undefined) {
thinkEndTime = Date.now();
let thinkTime = Math.round((thinkEndTime - thinkStartTime)/1000);
// write to json if thinktime is greater than 1 second
if (thinkTime > 1) {
writeToFile({think:thinkTime});
}
}
thinkStartTime = Date.now();
}
function checkDirectorySync(directory) {
try {
fs.statSync(directory);
} catch(e) {
fs.mkdirSync(directory);
}
}
function isDirSync(aPath) {
try {
return fs.statSync(aPath).isDirectory();
} catch (e) {
if (e.code === 'ENOENT') {
return false;
} else {
throw e;
}
}
}