-
Notifications
You must be signed in to change notification settings - Fork 0
/
moco.js
executable file
·492 lines (429 loc) · 19 KB
/
moco.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env node
// Author: Javier Campos López <[email protected]>
// License: Non-Profit Open Software License 3.0 (NPOSL-3.0)
'use strict'
// Imports
const fs = require(`fs`)
const http = require(`http`)
const readline = require(`readline`)
const url = require(`url`)
// Global constants
const appName = `moco`
const appVersion = `0.1`
const templateConfigFile = `config.moco.json`
// Modifications to the standard classes
String.prototype.capitalize = function (lower) {
return (lower ? this.toLowerCase() : this).replace(/(?:^|\s)\S/g, function (a) { return a.toUpperCase() })
}
if (!String.isString) {
String.isString = (str) => {
return Object.prototype.toString.call(str) === `[object String]`
}
}
if (!Boolean.isBoolean) {
Boolean.isBoolean = (bool) => {
return Object.prototype.toString.call(bool) === `[object Boolean]`
}
}
// Auxiliary functions
const HelpMode = Object.freeze({
USAGE: Symbol(`usage`),
USAGE_EXTENDED: Symbol(`usage_extended`),
COMMANDS: Symbol(`commands`)
})
const printHelp = (helpMode) => {
if (helpMode === HelpMode.USAGE || helpMode === HelpMode.USAGE_EXTENDED) {
console.log(`usage: node ${appName}.js [help | run <path> | template [<path>]]`)
}
if (helpMode === HelpMode.USAGE_EXTENDED) {
console.log(`help`)
console.log(`\tPrint this message`)
console.log(`run <path>, <path> : string`)
console.log(`\tRun ${appName} using configuration file stored in <path>`)
console.log(`template [<path>], <path> : string`)
console.log(`\tConvenience command, saves a configuration template file to <path>. If <path> not specified \`${templateConfigFile}\` will be created in the current directory`)
}
if (helpMode === HelpMode.COMMANDS) {
console.log(`help`)
console.log(`\tPrint the list of ${appName} commands available`)
console.log(`list`)
console.log(`\tList all endpoints`)
console.log(`info <id>, <id> : integer`)
console.log(`\tPrint the endpoint <id> information in readable way`)
console.log(`toggle <id>, <id> : integer`)
console.log(`\tEnable/Disable endpoint <id>`)
console.log(`response <id> <HTTPStatusCode>, <id> : integer, <HTTPStatusCode> : {100 - 999}`)
console.log(`\tSet endpoint <id> to answer with status code <HTTPStatusCode>`)
}
}
const safeForEachHeader = (headers, callback) => {
// It will apply the callback to each header only if the headers object exists
let headersArray = []
if (headers) {
headersArray = Object.keys(headers)
}
headersArray.forEach((header, index, array) => callback(header, index, array))
}
// Get configuration file path
let configFilePath = null
if (/^help$/i.test(process.argv[2])) {
if (process.argv.length !== 3) {
printHelp(HelpMode.USAGE)
process.exit()
}
printHelp(HelpMode.USAGE_EXTENDED)
process.exit()
} else if (/^run$/i.test(process.argv[2])) {
if (process.argv.length !== 4) {
printHelp(HelpMode.USAGE)
process.exit()
}
configFilePath = process.argv[3]
} else if (/^template$/i.test(process.argv[2])) {
if (process.argv.length > 4) {
printHelp(HelpMode.USAGE)
process.exit()
}
const path = process.argv[3] !== undefined ? process.argv[3] : templateConfigFile
const configTemplate = JSON.stringify({
port: 8080,
maskedHost: `example.com`,
endpoints: [
{
request: {
path: `path/to/{*}/endpoint`,
method: `GET`,
headers: {
Accept: `*/*`
}
},
responses: [
{
statusCode: 200,
headers: {
'Content-Type': `application/json`,
'User-Agent': `${appName}/${appVersion}`
},
body: {
message: `Success!`
}
},
{
statusCode: 503,
headers: {
'Content-Type': `application/json`,
'User-Agent': `${appName}/${appVersion}`
},
body: {
message: `Unavailable`
}
}
]
}
]
}, null, 4)
fs.writeFileSync(path, configTemplate, { encoding: `utf8`, mode: 0o644, flag: `wx` })
console.log(`File created in ${path}`)
process.exit()
} else {
printHelp(HelpMode.USAGE)
process.exit()
}
// Read and parse configuration file => config object
fs.readFile(configFilePath, (err, data) => {
if (err) throw err
const throwParseError = (name, value, stringType) => {
throw TypeError(`Found ${name}: ${value}, expected ${stringType} type`)
}
console.log(`Validating configuration file ...`)
const config = JSON.parse(data)
if (!Number.isInteger(config.port)) {
throwParseError(`port`, config.port, `integer`)
}
if (config.maskedHost && !String.isString(config.maskedHost)) {
throwParseError(`maskedHost`, config.maskedHost, `string`)
}
if (!config.endpoints || config.endpoints.length === 0) {
throw Error(`No defined endpoints, ${appName} can't capture anything`)
}
config.endpoints.forEach(endpoint => {
if (!String.isString(endpoint.request.path)) {
throwParseError(`request.path`, endpoint.request.path, `string`)
}
if (!String.isString(endpoint.request.method)) {
throwParseError(`request.method`, endpoint.request.method, `string`)
}
// Create regexp to match incoming requests by path conforming
// RFC 3986: https://www.ietf.org/rfc/rfc3986.txt
// 2.3. Unreserved Characters
// Characters that are allowed in a URI but do not have a reserved
// purpose are called unreserved. These include uppercase and lowercase
// letters, decimal digits, hyphen, period, underscore, and tilde.
//
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
let re = endpoint.request.path
re = re.replace(/\//g, `\\/`)
re = re.replace(/{\*}/g, `[\\w-.~]+`)
re = `^\\/?` + re + `\\/?(\\?|$)`
endpoint.request.path = {
name: endpoint.request.path,
re: new RegExp(re, `i`)
}
if (!endpoint.responses || endpoint.responses.length === 0) {
throw Error(`No response for ${endpoint.request.path.name}, ${appName} can't send anything`)
}
endpoint.responses.forEach(response => {
if (!Number.isInteger(response.statusCode)) {
throwParseError(`response.statusCode`, response.statusCode, `integer`)
}
// Node.js itself defines an invalid HTTP status code as follows, so the same
// condition will be enforced
//
// ERR_HTTP_INVALID_STATUS_CODE
// Status code was outside the regular status code range (100-999).
// Ref: https://nodejs.org/dist/latest-v12.x/docs/api/errors.html#errors_err_http_invalid_status_code
if (response.statusCode < 100 || response.statusCode > 999) {
throw TypeError(`Invalid status code: ${response.statusCode}`)
}
safeForEachHeader(response.headers, header => {
if (!response.headers[header]) {
response.headers[header] = ``
}
})
})
// Add isActive property (by default true) if missing
if (endpoint.isActive === undefined) {
endpoint.isActive = true
} else if (!Boolean.isBoolean(endpoint.isActive)) {
throwParseError(`endpoint.isActive`, endpoint.isActive, `boolean`)
}
// Add activeResponse property if missing
if (endpoint.activeResponse === undefined) {
endpoint.activeResponse = endpoint.responses[0].statusCode
} else if (!Number.isInteger(endpoint.activeResponse)) {
throwParseError(`endpoint.activeResponse`, endpoint.activeResponse, `integer`)
}
})
// Create server cli
const cli = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: `> `
})
// Start HTTP server
const server = http.createServer((originalRequest, originalResponse) => {
const matchHeaders = (endpointHeaders, requestHeaders) => {
if (endpointHeaders) {
const expectedHeaders = Object.keys(endpointHeaders).map(header => header.trim())
for (let i = 0; i < expectedHeaders.length; i++) {
const key = expectedHeaders[i]
const eh = endpointHeaders[key]
const rh = requestHeaders[key.toLowerCase()]
if (!((eh && (rh.includes(eh))) || (!eh && rh))) {
return false
}
}
}
return true
}
// Pause the console server
cli.pause()
let originalBody = ``
originalRequest.on(`data`, (chunk) => {
originalBody = originalBody + chunk
})
originalRequest.on(`end`, () => {
let logAction = ``
const originalURL = new url.URL(originalRequest.url, `http://${config.maskedHost ? config.maskedHost : `localhost:${config.port}`}`)
const capturedEndpoint = config.endpoints.find(endpoint => endpoint.request.path.re.test(originalURL.pathname))
if (capturedEndpoint &&
capturedEndpoint.isActive &&
capturedEndpoint.request.method === originalRequest.method &&
matchHeaders(capturedEndpoint.request.headers, originalRequest.headers)) {
// Take over the incoming request
logAction = `Captured`
const selectedResponse = capturedEndpoint.responses.find(response => response.statusCode === capturedEndpoint.activeResponse)
let selectedHeaders = {}
let selectedBody = ``
if (selectedResponse) {
selectedHeaders = selectedResponse.headers ? selectedResponse.headers : {}
selectedBody = JSON.stringify(selectedResponse.body ? selectedResponse.body : {})
}
originalResponse
.writeHead(capturedEndpoint.activeResponse, selectedHeaders)
.end(selectedBody)
} else if (config.maskedHost) {
// Redirect the incoming request to the "real" server
logAction = `Passed through`
const maskedRequest = http.request(
originalURL,
{
method: originalRequest.method,
headers: originalRequest.headers
},
maskedResponse => {
let maskedBody = ``
maskedResponse.on(`data`, chunk => {
maskedBody = maskedBody + chunk
})
maskedResponse.on(`end`, () => {
originalResponse
.writeHead(maskedResponse.statusCode, maskedResponse.headers)
.end(maskedBody)
})
}
)
maskedRequest.on(`error`, error => {
const errorStatusCode = 500
originalResponse
.writeHead(errorStatusCode, { 'Content-Type': `text/html`, 'User-Agent': `${appName}/${appVersion}` })
.end(
`<!DOCTYPE html>
<html>
<head>
<title>${appName} version ${appVersion}</title>
</head>
<body>
<h1>${errorStatusCode} Internal Server Error</h1>
<p style="font-size:20px">${appName} could not send the request to the masked host</p>
<p style="font-size:20px">reason: ${error.message}</p>
</body>
</html>`
)
})
maskedRequest.write(originalBody)
maskedRequest.end()
} else {
// Default response, no masked host or endpoint defined
logAction = `Ignored`
originalResponse
.writeHead(200, { 'Content-Type': `text/html`, 'User-Agent': `${appName}/${appVersion}` })
.end(
`<!DOCTYPE html>
<html>
<head>
<title>${appName} version ${appVersion}</title>
</head>
<body>
<h1>${appName} version ${appVersion}</h1>
<p style="font-size:20px">${appName} is up and running, if you wanted to capture this URL or if you meant it to reach the masked host, please modify the configuration file accordingly</p>
</body>
</html>`
)
}
// Log the incoming requests to standard output and resume the console
console.log(`${originalRequest.method} ${originalURL.pathname}${originalURL.search} - ${logAction}`)
cli.resume()
})
})
server.listen(config.port, () => {
console.log(`Server running at http://localhost:${config.port}${config.maskedHost ? `/ (masking ${config.maskedHost})` : ``}`)
console.log(`To stop it use Ctrl-C or Ctrl-D\n`)
// Start server console
cli.prompt()
cli.on(`line`, (input) => {
const regexId = /^\d+$/
const list = () => {
// list: List all endpoints
if (config.maskedHost) {
console.log(`Masking ${config.maskedHost}`)
}
config.endpoints.forEach((endpoint, index) => {
console.log(`${index}: ${endpoint.request.method} ${endpoint.request.path.name}, isActive: ${endpoint.isActive}, activeResponse: ${endpoint.activeResponse}`)
})
}
const info = (id) => {
// info <id>: Print the endpoint <id> information in readable way
if (!regexId.test(id)) {
console.log(`usage: info <id>`)
return
}
const endpoint = config.endpoints[id]
if (!endpoint) {
console.log(`There is no endpoint with id: ${id}`)
return
}
console.log(`Request:\n ${endpoint.request.method} ${endpoint.request.path.name}, isActive:${endpoint.isActive}, activeResponse: ${endpoint.activeResponse}`)
safeForEachHeader(endpoint.request.headers, header => {
console.log(` ${header}: ${endpoint.request.headers[header]}`)
})
endpoint.responses.forEach(response => {
console.log(`Response: ${response.statusCode}`)
safeForEachHeader(response.headers, header => {
console.log(` ${header}: ${response.headers[header]}`)
})
if (response.body) {
let bodyString = JSON.stringify(response.body, null, 4)
bodyString = bodyString.substring(1, bodyString.length - 1)
console.log(` body: {${bodyString} }`)
}
})
}
const toggle = (id) => {
// toggle <id>: Enable/Disable <id> endpoint
if (!regexId.test(id)) {
console.log(`usage: toggle <id>`)
return
}
const endpoint = config.endpoints[id]
if (!endpoint) {
console.log(`There is no endpoint with id: ${id}`)
return
}
endpoint.isActive = !endpoint.isActive
console.log(`Endpoint ${id} ${endpoint.isActive ? `enabled` : `disabled`}`)
}
const response = (id, HTTPStatusCode) => {
// response <id> <HTTPStatusCode>: Set endpoint <id> to answer with status code <HTTPStatusCode>
if (!regexId.test(id) || !/^[1-9]\d{2}$/.test(HTTPStatusCode)) {
console.log(`usage: response <id> <HTTPStatusCode>`)
return
}
const endpoint = config.endpoints[id]
if (!endpoint) {
console.log(`There is no endpoint with id: ${id}`)
return
}
endpoint.activeResponse = parseInt(HTTPStatusCode, 10)
console.log(`Response set to: ${endpoint.activeResponse}`)
const selectedResponse = endpoint.responses.find(response => response.statusCode === endpoint.activeResponse)
if (!selectedResponse) {
console.log(`WARNING: No defined response for status code ${endpoint.activeResponse}, ${appName} will return an empty response`)
}
}
const line = input.trim()
let match = null
let spaceAfterCommand = true
if (/^help$/i.test(line)) {
printHelp(HelpMode.COMMANDS)
} else if (/^list$/i.test(line)) {
list()
} else if ((match = /^info(|\s+(?<id>.*))$/i.exec(line))) {
info(match.groups.id)
} else if ((match = /^toggle(|\s+(?<id>.*))$/i.exec(line))) {
toggle(match.groups.id)
} else if ((match = /^response(|\s+(?<id>\S*)(|\s+(?<statusCode>.*)))$/i.exec(line))) {
response(match.groups.id, match.groups.statusCode)
} else if (/^\s*$/.test(line)) {
// nop: just ignore the input and leave a blank line in the prompt
spaceAfterCommand = false
} else {
console.log(`Error: Unknown command: ${line}`)
}
if (spaceAfterCommand) {
console.log(``)
}
cli.prompt()
}).on(`pause`, () => {
console.log(``)
}).on(`resume`, () => {
cli.prompt(true)
}).on(`close`, () => {
console.log(`Stopping server ...`)
server.close(function () {
console.log(`Done!`)
process.exit()
})
})
})
})