@@ -14,6 +14,18 @@ function countUtf8Bytes(s) {
14
14
return b
15
15
}
16
16
17
+ var contentTypeIsText = ( headers ) => {
18
+ if ( ! headers [ "content-type" ] ||
19
+ headers [ "content-type" ] . indexOf ( 'text/' ) !== - 1 ||
20
+ headers [ "content-type" ] . indexOf ( 'javascript' ) !== - 1 ||
21
+ headers [ "content-type" ] . indexOf ( 'urlencoded' ) !== - 1 ||
22
+ headers [ "content-type" ] . indexOf ( 'json' ) !== - 1 ) {
23
+ return true
24
+ } else {
25
+ return false
26
+ }
27
+ }
28
+
17
29
var enableCors = function ( req , res ) {
18
30
if ( req . headers [ 'access-control-request-method' ] ) {
19
31
res . setHeader ( 'access-control-allow-methods' , req . headers [ 'access-control-request-method' ] ) ;
@@ -101,7 +113,7 @@ let getHostFromReq = (req) => { //return target
101
113
102
114
let Proxy = ( { blockedSites, urlModify, httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace} ) => {
103
115
let stream = fs . createWriteStream ( "web-records.csv" , { flags :'a' } )
104
- let handleRespond = ( { req, res, body, gbFlag} ) => {
116
+ let handleRespond = ( { req, res, body, gbFlag} ) => { // text file
105
117
let myRe
106
118
let { host, httpType} = getHostFromReq ( req )
107
119
let location = res . getHeaders ( ) [ 'location' ]
@@ -233,11 +245,18 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
233
245
}
234
246
bodyLength += data . length
235
247
bodyList . push ( data )
236
- if ( res . getHeader ( 'content-type' ) && res . getHeader ( 'content-type' ) . indexOf ( 'video' ) !== - 1 ) {
248
+ if ( bodyLength >= 2500000 && contentTypeIsText ( proxyRes . headers ) !== true ) {
249
+ let body = Buffer . concat ( bodyList )
237
250
let fwdStr = req . headers [ 'X-Forwarded-For' ] || req . headers [ 'x-forwarded-for' ]
238
- if ( ( host . indexOf ( 'cdn' ) !== - 1 && bodyLength >= 105000000 ) ||
239
- ( host . indexOf ( 'cdn' ) === - 1 && bodyLength >= 2500000 ) ) {
251
+ let contentType = proxyRes . headers [ 'content-type' ]
252
+ let contentLen = proxyRes . headers [ 'content-length' ]
253
+ if ( contentLen >= 155000000 ||
254
+ ( host . indexOf ( 'googlevideo' ) !== - 1 && contentLen >= 2500000 ) ) {
240
255
}
256
+ console . log ( `route:${ fwdStr } , content-type:${ contentType } ,bulk length:${ bodyLength } , content-length:${ contentLen } , ${ host } ` )
257
+ bodyList = [ ]
258
+ bodyLength = 0
259
+ res . write ( body )
241
260
}
242
261
} )
243
262
proxyRes . on ( 'end' , function ( ) {
@@ -247,7 +266,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
247
266
let body = Buffer . concat ( bodyList )
248
267
let gbFlag = false
249
268
if ( proxyRes . headers [ "content-encoding" ] === 'gzip' ||
250
- proxyRes . headers [ "content-encoding" ] === 'br' ) {
269
+ proxyRes . headers [ "content-encoding" ] === 'br' ) { // gzip/br encoding
251
270
let gunzipped
252
271
try {
253
272
if ( proxyRes . headers [ "content-encoding" ] === 'br' ) {
@@ -261,11 +280,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
261
280
// res.status(404).send(`{"error": "${e}"}`)
262
281
return
263
282
}
264
- if ( ! proxyRes . headers [ "content-type" ] ||
265
- proxyRes . headers [ "content-type" ] . indexOf ( 'text/' ) !== - 1 ||
266
- proxyRes . headers [ "content-type" ] . indexOf ( 'javascript' ) !== - 1 ||
267
- proxyRes . headers [ "content-type" ] . indexOf ( 'urlencoded' ) !== - 1 ||
268
- proxyRes . headers [ "content-type" ] . indexOf ( 'json' ) !== - 1 ) {
283
+ if ( contentTypeIsText ( proxyRes . headers ) === true ) { //gzip and text
269
284
if ( ! gunzipped ) {
270
285
// res.status(404).send(`{"error":"failed unzip"}`)
271
286
redirect2HomePage ( { res, httpprefix, serverName, } )
@@ -292,29 +307,20 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
292
307
res . setHeader ( 'service-worker-allowed' , '/' )
293
308
}
294
309
handleRespond ( { req, res, body, gbFlag} )
295
- } else {
310
+ } else { // gzip and non-text
296
311
// console.log(`2========>${logGet()}`)
312
+ let fwdStr = req . headers [ 'X-Forwarded-For' ] || req . headers [ 'x-forwarded-for' ]
313
+ let contentType = proxyRes . headers [ 'content-type' ]
314
+ let contentLen = proxyRes . headers [ 'content-length' ]
315
+ console . log ( `end,route:${ fwdStr } , content-type:${ contentType } ,gzip length:${ bodyLength } , content-length:${ contentLen } , ${ host } ` )
297
316
try {
298
- let key = "content-encoding"
299
- if ( key in proxyRes . headers ) {
300
- res . setHeader ( key , proxyRes . headers [ key ] ) ;
301
- }
302
- logSave ( `2: res.headers:${ JSON . stringify ( res . getHeaders ( ) ) } ` )
303
- if ( req . headers [ 'debugflag' ] === 'true' ) {
304
- res . removeHeader ( 'content-encoding' )
305
- res . setHeader ( 'content-type' , 'text/plain' )
306
- body = logGet ( )
307
- }
308
317
res . end ( body )
309
318
} catch ( e ) {
310
319
console . log ( `error:${ e } ` )
311
320
}
312
321
}
313
322
} else if ( proxyRes . statusCode === 301 || proxyRes . statusCode === 302 || proxyRes . statusCode === 307 || proxyRes . statusCode === 308 ||
314
- ( proxyRes . headers [ "content-type" ] &&
315
- ( proxyRes . headers [ "content-type" ] . indexOf ( 'text/' ) !== - 1 ||
316
- proxyRes . headers [ "content-type" ] . indexOf ( 'javascript' ) !== - 1 ||
317
- proxyRes . headers [ "content-type" ] . indexOf ( 'json' ) !== - 1 ) ) ) {
323
+ contentTypeIsText ( proxyRes . headers ) === true ) { // text with non gzip encoding
318
324
logSave ( `utf-8 text...` )
319
325
let originBody = body
320
326
body = body . toString ( 'utf-8' ) ;
@@ -326,17 +332,12 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
326
332
gbFlag = true
327
333
}
328
334
handleRespond ( { req, res, body, gbFlag} )
329
- } else {
335
+ } else { // non-gzip and non-text body
330
336
logSave ( `3========>${ logGet ( ) } ` )
331
- if ( req . headers [ 'debugflag' ] === 'true' ) {
332
- res . removeHeader ( 'content-encoding' )
333
- res . setHeader ( 'content-type' , 'text/plain' )
334
- body = logGet ( )
335
- }
336
- if ( res . getHeader ( 'content-type' ) && res . getHeader ( 'content-type' ) . indexOf ( 'video' ) !== - 1 ) {
337
- let fwdStr = req . headers [ 'X-Forwarded-For' ] || req . headers [ 'x-forwarded-for' ]
338
- console . log ( `route:${ fwdStr } , length:${ bodyLength } , ${ host } ` )
339
- }
337
+ let fwdStr = req . headers [ 'X-Forwarded-For' ] || req . headers [ 'x-forwarded-for' ]
338
+ let contentType = proxyRes . headers [ 'content-type' ]
339
+ let contentLen = proxyRes . headers [ 'content-length' ]
340
+ console . log ( `end,route:${ fwdStr } , content-type:${ contentType } ,length:${ bodyLength } , content-length:${ contentLen } , ${ host } ` )
340
341
res . end ( body )
341
342
}
342
343
} )
@@ -362,18 +363,18 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
362
363
. map ( cookie => cookie . toString ( ) )
363
364
proxyRes . headers [ 'set-cookie' ] = modifiedSetCookieHeaders
364
365
Object . keys ( proxyRes . headers ) . forEach ( function ( key ) {
365
- if ( key === 'content-encoding' ||
366
- key === 'content-security-policy' ||
366
+ if ( key === 'content-security-policy' ||
367
367
key === 'x-frame-options' ||
368
- ( key === 'content-length' && proxyRes . headers [ "content-type" ] &&
369
- ( proxyRes . headers [ "content-type" ] . indexOf ( 'text/' ) !== - 1 ||
370
- proxyRes . headers [ "content-type" ] . indexOf ( 'javascript' ) !== - 1 ||
371
- proxyRes . headers [ "content-type" ] . indexOf ( 'json' ) !== - 1 ) ) ) {
368
+ ( key === 'content-length' && contentTypeIsText ( proxyRes . headers ) === true ) ) {
372
369
logSave ( `skip header:${ key } ` )
373
370
return
374
371
}
375
372
try {
376
- res . setHeader ( key , proxyRes . headers [ key ] ) ;
373
+ if ( key === 'content-encoding' && contentTypeIsText ( proxyRes . headers ) === true ) {
374
+ res . setHeader ( key , 'gzip' ) // for text response, we need to set it gzip encoding cuz we will do gzip on it
375
+ } else {
376
+ res . setHeader ( key , proxyRes . headers [ key ] )
377
+ }
377
378
} catch ( e ) {
378
379
logSave ( `error:${ e } ` )
379
380
return
@@ -392,8 +393,11 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
392
393
}
393
394
} ,
394
395
onProxyReq : ( proxyReq , req , res ) => {
395
- let myRe = new RegExp ( `/${ httpprefix } /${ serverName } .*?/` , 'g' ) // match group
396
- req . url = req . url . replace ( myRe , '/' )
396
+ let myRe = new RegExp ( `/http[s]?/${ serverName } [0-9:]*?` , 'g' ) // match group
397
+ req . url = req . url . replace ( myRe , '' )
398
+ if ( req . url . length === 0 ) {
399
+ req . url = '/'
400
+ }
397
401
398
402
let fwdStr = req . headers [ 'X-Forwarded-For' ] || req . headers [ 'x-forwarded-for' ]
399
403
0 commit comments