小调整
							parent
							
								
									3fde235c90
								
							
						
					
					
						commit
						3968272843
					
				| 
						 | 
				
			
			@ -60,7 +60,11 @@ module.exports = {
 | 
			
		|||
        head: tags + '\r\n'
 | 
			
		||||
      }
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      res.setHeader('DS-Script-Interceptor', 'error')
 | 
			
		||||
      try {
 | 
			
		||||
        res.setHeader('DS-Script-Interceptor', 'error')
 | 
			
		||||
      } catch (e) {
 | 
			
		||||
        // ignore
 | 
			
		||||
      }
 | 
			
		||||
      log.error('load monkey script error', err)
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,9 +12,9 @@ const proxy = require('./impl/req/proxy')
 | 
			
		|||
const sni = require('./impl/req/sni')
 | 
			
		||||
 | 
			
		||||
// response interceptor impls
 | 
			
		||||
const responseReplace = require('./impl/res/responseReplace')
 | 
			
		||||
const cacheRes = require('./impl/res/cacheRes')
 | 
			
		||||
const script = require('./impl/res/script')
 | 
			
		||||
const responseReplace = require('./impl/res/responseReplace')
 | 
			
		||||
 | 
			
		||||
module.exports = [
 | 
			
		||||
  // request interceptor impls
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,7 +36,7 @@ function loadScript (content, scriptName) {
 | 
			
		|||
  const sc = {
 | 
			
		||||
    grant: [],
 | 
			
		||||
    match: [],
 | 
			
		||||
    content: ''
 | 
			
		||||
    script: ''
 | 
			
		||||
  }
 | 
			
		||||
  for (const string of confItemArr) {
 | 
			
		||||
    const reg = new RegExp('.*@([^\\s]+)\\s(.+)')
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +80,8 @@ const api = {
 | 
			
		|||
    // scripts.jquery = { script: readFile(rootDir, 'jquery.min.js') }
 | 
			
		||||
    scripts.global = { script: readFile(rootDir, 'global.script') }
 | 
			
		||||
    return scripts
 | 
			
		||||
  }
 | 
			
		||||
  },
 | 
			
		||||
  loadScript
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = api
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -64,7 +64,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
 | 
			
		|||
              if (!reqIncpt.requestIntercept) {
 | 
			
		||||
                continue
 | 
			
		||||
              }
 | 
			
		||||
              const goNext = reqIncpt.requestIntercept(context, req, res, ssl)
 | 
			
		||||
              const goNext = reqIncpt.requestIntercept(context, req, res, ssl, next)
 | 
			
		||||
              if (goNext) {
 | 
			
		||||
                next()
 | 
			
		||||
                return
 | 
			
		||||
| 
						 | 
				
			
			@ -252,7 +252,12 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
 | 
			
		|||
            let head = ''
 | 
			
		||||
            let body = ''
 | 
			
		||||
            for (const resIncpt of resIncpts) {
 | 
			
		||||
              const append = resIncpt.responseIntercept(context, req, res, proxyReq, proxyRes, ssl)
 | 
			
		||||
              const append = resIncpt.responseIntercept(context, req, res, proxyReq, proxyRes, ssl, next)
 | 
			
		||||
              // 判断是否已经关闭
 | 
			
		||||
              if (res.writableEnded) {
 | 
			
		||||
                next()
 | 
			
		||||
                return
 | 
			
		||||
              }
 | 
			
		||||
              if (append) {
 | 
			
		||||
                if (append.head) {
 | 
			
		||||
                  head += append.head
 | 
			
		||||
| 
						 | 
				
			
			@ -260,10 +265,8 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
 | 
			
		|||
                if (append.body) {
 | 
			
		||||
                  body += append.body
 | 
			
		||||
                }
 | 
			
		||||
              }
 | 
			
		||||
              if (res.writableEnded) {
 | 
			
		||||
                next()
 | 
			
		||||
                return
 | 
			
		||||
              } else if (append === false) {
 | 
			
		||||
                break // 返回false表示终止拦截器,跳出循环
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
            InsertScriptMiddleware.responseInterceptor(req, res, proxyReq, proxyRes, ssl, next, {
 | 
			
		||||
| 
						 | 
				
			
			@ -302,14 +305,19 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
 | 
			
		|||
      }
 | 
			
		||||
    })().catch(e => {
 | 
			
		||||
      if (!res.writableEnded) {
 | 
			
		||||
        const status = e.status || 500
 | 
			
		||||
        res.writeHead(status, { 'Content-Type': 'text/html;charset=UTF8' })
 | 
			
		||||
        res.write(`DevSidecar Error:<br/>
 | 
			
		||||
        log.error('Request error:', e)
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
          const status = e.status || 500
 | 
			
		||||
          res.writeHead(status, { 'Content-Type': 'text/html;charset=UTF8' })
 | 
			
		||||
          res.write(`DevSidecar Error:<br/>
 | 
			
		||||
目标网站请求错误:【${e.code}】 ${e.message}<br/>
 | 
			
		||||
目标地址:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
 | 
			
		||||
        )
 | 
			
		||||
        res.end()
 | 
			
		||||
        log.error('Request error:', e)
 | 
			
		||||
          )
 | 
			
		||||
          res.end()
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
          // do nothing
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue