forked from thaibault/reverse-proxy-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.ts
669 lines (587 loc) · 22.6 KB
/
helper.ts
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// #!/usr/bin/env babel-node
// -*- coding: utf-8 -*-
/** @module web-node */
'use strict'
/* !
region header
[Project page](https://torben.website/webNode)
Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
License
-------
This library written by Torben Sickert stand under a creative commons
naming 3.0 unported license.
See https://creativecommons.org/licenses/by/3.0/deed.de
endregion
*/
// region imports
// NOTE: http2 compatibility mode does work for unencrypted connections yet.
import Tools from 'clientnode'
import {CompilationResult, Mapping, PlainObject} from 'clientnode/type'
import {createConnection as createPlainConnection} from 'net'
import {connect as createSecureConnection} from 'tls'
import {
APIPostEvaluationExpression,
APIPreEvaluationExpression,
APIPostEvaluationResult,
APIPreEvaluationResult,
BufferedHTTPServerRequest,
EvaluationParameters,
EvaluationScopeStateAPI,
EvaluationScopeStateAPIs,
Forwarders,
HeaderTransformation,
HTTPServerResponse,
ParsedContent,
ResolvedAPIExpressions,
ResolvedHeaderTransformation,
ResolvedHeaderTransformations,
ResolvedForwarder,
ResolvedForwarders,
ResolvedStateAPI,
Socket,
StateAPI,
StringReplacer
} from './type'
// endregion
export const EVALUATION_SCOPE_NAMES:Array<string> = [
'data', 'error', 'request', 'response', 'stateAPI', 'stateAPIs', 'Tools'
]
const log = async (...parameters:Array<unknown>):Promise<void> =>
new Promise((resolve:() => void, reject:(error:Error) => void):void => {
process.stdout.write(
`${parameters.join(' ')}\n`,
(error?:Error):void => error ? reject(error) : resolve()
)
})
export const logging = {
log,
debug: log,
info: log,
error: log,
warn: log
}
// region forwarder
export const applyStateAPIs = async (
request:BufferedHTTPServerRequest,
response:HTTPServerResponse,
forwarder:ResolvedForwarder
):Promise<{result:boolean, scope:EvaluationScopeStateAPIs}> => {
const stateAPIScope:EvaluationScopeStateAPIs = {}
let state:EvaluationScopeStateAPI
for (const stateAPI of forwarder.stateAPIs) {
state =
stateAPIScope[stateAPI.name] =
{
configuration: stateAPI,
error: null,
response: null
}
let useStateAPI = false
let index = 1
for (const expression of stateAPI.expressions.pre) {
let result:APIPreEvaluationResult
try {
result = expression(
stateAPI.data,
null,
request,
response,
state,
stateAPIScope,
Tools
)
} catch (error) {
void logging.warn(
`Failed running pre ${index}. expression of state api:`,
error
)
}
if (typeof result === 'number') {
void logging.info(
'Break request caused by state api',
`"${stateAPI.name}" with status code ${result}.`
)
response.statusCode = result
response.end()
return {result: false, scope: stateAPIScope}
}
if (result === 'break')
break
index += 1
if (typeof result === 'boolean')
if (result)
useStateAPI = true
else
break
}
if (useStateAPI) {
void logging.info(`Use state api: "${stateAPI.name}"`)
let error:Error|null = null
if (stateAPI.urlExpression)
try {
stateAPI.url = stateAPI.urlExpression(
stateAPI.data,
null,
request,
response,
state,
stateAPIScope,
Tools
)
} catch (error) {
void logging.warn(`Failed running url expression:`, error)
}
void logging.debug(
`\nState api configuration is: ${Tools.represent(stateAPI)}`
)
try {
state.response =
await fetch(stateAPI.url!, stateAPI.options) as
Response & {data:Mapping<unknown>}
} catch (givenError) {
error = givenError as Error
void logging.warn(
`Running state api request for "${stateAPI.name}" throws`,
'error:',
error
)
}
if (
state.response &&
state.response.headers.has('content-type') &&
/application\/json(;.*)?$/.test(
state.response.headers.get('content-type')!
)
)
try {
state.response.data =
await state.response.json() as PlainObject
} catch (givenError) {
error = givenError as Error
void logging.warn(
'Parsing state api json response for',
`"${stateAPI.name}" throws error:`,
error
)
}
void logging.debug(
`\nState api response is:`, Tools.represent(state.response)
)
index = 1
for (const expression of stateAPI.expressions.post) {
let result:APIPostEvaluationResult = null
try {
result = expression(
stateAPI.data,
error,
request,
response,
state,
stateAPIScope,
Tools
)
} catch (error) {
void logging.warn(
`Failed running ${index}. post expression of state ` +
'api:',
error
)
}
if (typeof result === 'number') {
void logging.info(
'Break request caused by state api',
`"${stateAPI.name}" with status code ${result}.`
)
response.statusCode = result
response.end()
return {result: false, scope: stateAPIScope}
}
if (result === 'break')
break
index += 1
}
}
}
return {result: true, scope: stateAPIScope}
}
export const determineForwarder = (
request:BufferedHTTPServerRequest,
response:HTTPServerResponse,
forwarders:ResolvedForwarders
):ResolvedForwarder|null => {
for (const [name, forwarder] of Object.entries(forwarders)
.sort(([firstName], [secondName]) =>
firstName.localeCompare(secondName)
)
) {
const state:EvaluationScopeStateAPI = {
configuration: forwarder,
error: null,
response: null
}
if (forwarder.useExpression(
forwarder,
null,
request,
response,
state,
{[name]: state},
Tools
)) {
void logging.info(`Determined forwarder is: "${name}".`)
return forwarder
}
}
return null
}
export const resolveForwarders = (forwarders:Forwarders):ResolvedForwarders => {
const resolvedForwarders:ResolvedForwarders = {}
for (const [name, givenForwarder] of Object.entries(forwarders))
if (name !== 'base') {
const forwarder:ResolvedForwarder = Tools.extend(
true,
{name},
Tools.modifyObject<ResolvedForwarder>(
Tools.copy(forwarders.base) as
unknown as
ResolvedForwarder,
givenForwarder as unknown as ResolvedForwarder
)!,
givenForwarder as unknown as ResolvedForwarder
) as unknown as ResolvedForwarder
// region normalize header transformations
const headerTransformations:ResolvedHeaderTransformations = {
retrieve: [],
send: []
}
for (const type of ['retrieve', 'send'] as const)
for (const givenTransformation of (
[] as Array<HeaderTransformation>
).concat(
forwarder.headerTransformations[type] as
unknown as
HeaderTransformation
)) {
const transformation:ResolvedHeaderTransformation = {
...givenTransformation,
sourceRun: ():string => '',
targetRun: ():string => ''
}
if (Object.prototype.hasOwnProperty.call(
transformation, 'source'
))
if (transformation.source instanceof RegExp)
transformation.sourceRun = ():RegExp =>
transformation.source as RegExp
else if (typeof transformation.source === 'string') {
const result:CompilationResult<RegExp|string> =
Tools.stringCompile<RegExp|string>(
transformation.source,
EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
transformation.sourceRun = result.templateFunction
} else
transformation.sourceRun = transformation.source!
if (Object.prototype.hasOwnProperty.call(
transformation, 'target'
))
if (typeof transformation.target === 'string') {
const result:CompilationResult<
string|StringReplacer
> = Tools.stringCompile<string|StringReplacer>(
transformation.target, EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
transformation.targetRun = result.templateFunction
} else
transformation.targetRun = transformation.target!
headerTransformations[type].push(transformation)
}
forwarder.headerTransformations = headerTransformations
// endregion
// region state apis
const stateAPIs:Array<ResolvedStateAPI> = []
const givenStateAPIs:Array<StateAPI> =
([] as Array<StateAPI>).concat(forwarder.stateAPIs || [])
const baseAPI:StateAPI = givenStateAPIs.filter(
(api:StateAPI):boolean => api.name === 'base'
)[0]
const extendedGivenStateAPIs:Array<StateAPI> = []
for (const api of givenStateAPIs)
if (api.name !== 'base')
extendedGivenStateAPIs.push(
Tools.extend(
true,
{},
Tools.modifyObject<StateAPI>(
Tools.copy(baseAPI), api
)!,
api
)
)
for (const api of extendedGivenStateAPIs) {
// region normalize url expression
if (typeof api.urlExpression === 'string') {
const result:CompilationResult<string> =
Tools.stringCompile<string>(
api.urlExpression, EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
api.urlExpression = result.templateFunction
}
// endregion
// region normalize pre / post expressions
const expressions:ResolvedAPIExpressions = {pre: [], post: []}
for (const expression of (
[] as Array<APIPreEvaluationExpression>
).concat(api.expressions?.pre || []))
if (typeof expression === 'string') {
const result:CompilationResult<
APIPreEvaluationResult
> = Tools.stringCompile<APIPreEvaluationResult>(
expression, EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
expressions.pre.push(result.templateFunction)
} else
expressions.pre.push(expression)
for (const expression of (
[] as Array<APIPostEvaluationExpression>
).concat(api.expressions?.post || []))
if (typeof expression === 'string') {
const result:CompilationResult<
APIPostEvaluationResult
> = Tools.stringCompile<APIPostEvaluationResult>(
expression, EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
expressions.post.push(result.templateFunction)
} else
expressions.post.push(expression)
/* eslint-disable @typescript-eslint/no-extra-semi */
;(api as unknown as ResolvedStateAPI).expressions = expressions
/* eslint-enable @typescript-eslint/no-extra-semi */
// endregion
stateAPIs.push(api as unknown as ResolvedStateAPI)
}
forwarder.stateAPIs = stateAPIs
// endregion
// region normalize use expression
if (typeof forwarder.useExpression === 'string') {
const result:CompilationResult<boolean> = Tools.stringCompile(
forwarder.useExpression, EVALUATION_SCOPE_NAMES
)
if (result.error)
throw new Error(result.error)
forwarder.useExpression = result.templateFunction
}
// endregion
resolvedForwarders[name] = forwarder
}
return resolvedForwarders
}
// endregion
export const addParsedContentToRequest = (
bufferedRequest:BufferedHTTPServerRequest
):void => {
if (
bufferedRequest.headers['content-type'] &&
/application\/json(;.*)?$/.test(
bufferedRequest.headers['content-type']
)
)
try {
const data:string =
Buffer.concat(bufferedRequest.socket.buffer.data)
.toString()
bufferedRequest.socket.buffer.body =
data.replace(/^[\s\S]+\s*\n\s*\n\s*([\s\S]+)$/m, '$1')
bufferedRequest.socket.buffer.content =
JSON.parse(bufferedRequest.socket.buffer.body) as ParsedContent
} catch (error) {
void logging.warn('Error parsing given request.', bufferedRequest)
}
}
export const transformHeaders = (
content:string,
headerTransformations:Array<ResolvedHeaderTransformation>,
parameters:EvaluationParameters
):string => {
let newLinePrinted = false
for (const transformation of headerTransformations)
try {
const source:null|RegExp|string|undefined =
transformation.sourceRun(...parameters)
const target:null|string|StringReplacer|undefined =
transformation.targetRun(...parameters)
if (
[null, undefined].includes(source as null) ||
typeof source === 'string' &&
source.trim() === ''
) {
if (
[null, undefined].includes(target as null) ||
typeof target === 'string' &&
target.trim() === ''
)
continue
// Add new header.
content = content.replace(
/(\s*\n)\s*\n\s*/,
(
substring:string,
delimiter:string,
...parameters:Array<unknown>
):string => {
const result:string =
typeof target === 'string' ?
target :
target!(substring, delimiter, ...parameters)
return `${delimiter}${result}${substring}`
}
)
} else {
if (!newLinePrinted) {
void logging.debug()
newLinePrinted = true
}
// Search and replace (or remove) header.
void logging.debug(
`Search for "${source as string}" and replace with`,
`${Tools.represent(target)}.`
)
content = content.replace(source!, target as string)
}
} catch (error) {
void logging.warn(
'\nCould not apply header transformation with source ' +
(transformation.source ?
Tools.represent(transformation.source) :
'"add"'
) +
' and replacement ' +
(transformation.target ?
Tools.represent(transformation.target) :
'"remove"'
) +
':',
error
)
}
return content
}
export const reverseProxyBufferedRequest = async (
request:BufferedHTTPServerRequest,
response:HTTPServerResponse,
forwarder:ResolvedForwarder,
stateAPIScope:EvaluationScopeStateAPIs
):Promise<void> =>
new Promise((resolve:() => void, reject:(error:Error) => void):void => {
const clientSocket:Socket = response.socket
const createConnection:typeof createSecureConnection = forwarder.tls ?
createSecureConnection :
createPlainConnection as unknown as typeof createSecureConnection
const portSuffix:string = (
forwarder.tls && forwarder.port !== 443 ||
!forwarder.tls && forwarder.port !== 80
) ?
`:${String(forwarder.port)}` :
''
const serverSocket = createConnection(
{
host: forwarder.host,
port: forwarder.port,
...(forwarder.tls ?
{servername: forwarder.host, rejectUnauthorized: false} :
{}
)
},
() => {
void logging.info(
`\nConnection to: http${forwarder.tls ? 's' : ''}://` +
`${forwarder.host}${portSuffix} established.`
)
}
)
serverSocket.on('error', (error:Error) => {
void logging.error('Proxy to server error', error)
reject(error)
})
const parameters:EvaluationParameters = [
{clientSocket, serverSocket},
null,
request,
response,
null,
stateAPIScope,
Tools
]
// Send data from server back to client.
if (forwarder.headerTransformations.retrieve.length) {
let headerProcessed = false
serverSocket.on('data', (buffer:Buffer):void => {
if (headerProcessed) {
clientSocket.write(buffer)
return
}
let content:string = buffer.toString()
void logging.info(
`\n <=== Got response header from backend:\n\n${content}`
)
content = transformHeaders(
content,
forwarder.headerTransformations.retrieve,
parameters
)
void logging.info(
`\n <<<< Send response header to client:\n\n${content}`
)
clientSocket.write(content)
headerProcessed = true
resolve()
})
} else
serverSocket.pipe(clientSocket)
serverSocket.on('connect', () => {
let headerProcessed = false
for (const buffer of request.socket.buffer.data) {
if (!headerProcessed) {
let content:string = buffer.toString()
void logging.debug(
`\n ===> Got request header from client:\n\n${content}`
)
if (forwarder.tls)
// NOTE: TLS support was introduced in version 1.1.
content = content.replace(/HTTP\/1\.0/i, 'HTTP/1.1')
// Overwrite proxy host with destination one.
content = content.replace(
/(($|\n)host: )[^\n]+/i,
`$1${forwarder.host}${portSuffix}`
)
content = transformHeaders(
content,
forwarder.headerTransformations.send,
parameters
)
void logging.debug(
`\n >>>> Send request header to backend:\n\n${content}`
)
serverSocket.write(content)
headerProcessed = true
continue
}
serverSocket.write(buffer)
}
})
})
export default reverseProxyBufferedRequest
// region vim modline
// vim: set tabstop=4 shiftwidth=4 expandtab:
// vim: foldmethod=marker foldmarker=region,endregion:
// endregion