script拦截器,支持配置脚本的绝对地址。
parent
6062ace926
commit
e5f5184f11
|
@ -33,7 +33,7 @@ function buildTargetUrl (rOptions, urlConf, interceptOpt, matched) {
|
||||||
targetUrl = urlConf
|
targetUrl = urlConf
|
||||||
} else {
|
} else {
|
||||||
let uri = rOptions.path
|
let uri = rOptions.path
|
||||||
if (uri.indexOf('http') === 0) {
|
if (uri.indexOf('http:') === 0 || uri.indexOf('https:') === 0) {
|
||||||
// eslint-disable-next-line node/no-deprecated-api
|
// eslint-disable-next-line node/no-deprecated-api
|
||||||
const URL = url.parse(uri)
|
const URL = url.parse(uri)
|
||||||
uri = URL.path
|
uri = URL.path
|
||||||
|
|
|
@ -6,9 +6,12 @@ function getScript (key, script) {
|
||||||
|
|
||||||
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
|
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
|
||||||
return `
|
return `
|
||||||
<script crossorigin="anonymous" defer="defer" type="application/javascript"
|
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer integrity="sha256-${hash}"></script>
|
||||||
integrity="sha256-${hash}"
|
`
|
||||||
src="${scriptUrl}"></script>
|
}
|
||||||
|
function getScriptByAbsoluteUrl (scriptUrl) {
|
||||||
|
return `
|
||||||
|
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer></script>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,11 +28,18 @@ module.exports = {
|
||||||
const scripts = monkey.get(setting.script.dirAbsolutePath)
|
const scripts = monkey.get(setting.script.dirAbsolutePath)
|
||||||
let tags = getScript('global', scripts.global.script)
|
let tags = getScript('global', scripts.global.script)
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
|
let scriptTag
|
||||||
|
|
||||||
|
if (key.indexOf('http:') === 0 || key.indexOf('https:') === 0) {
|
||||||
|
scriptTag = getScriptByAbsoluteUrl(key)
|
||||||
|
} else {
|
||||||
const script = scripts[key]
|
const script = scripts[key]
|
||||||
if (script == null) {
|
if (script == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const scriptTag = getScript(key, script.script)
|
scriptTag = getScript(key, script.script)
|
||||||
|
}
|
||||||
|
|
||||||
tags += '\r\n' + scriptTag
|
tags += '\r\n' + scriptTag
|
||||||
}
|
}
|
||||||
res.setHeader('DS-Script-Interceptor', 'true')
|
res.setHeader('DS-Script-Interceptor', 'true')
|
||||||
|
|
|
@ -83,7 +83,7 @@ module.exports = function createOverWallIntercept (overWallConfig) {
|
||||||
const proxyTarget = domain + '/' + path + '/' + hostname + req.url
|
const proxyTarget = domain + '/' + path + '/' + hostname + req.url
|
||||||
|
|
||||||
// const backup = interceptOpt.backup
|
// const backup = interceptOpt.backup
|
||||||
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : (rOptions.protocol + '//' + proxyTarget)
|
const proxy = proxyTarget.indexOf('http:') === 0 || proxyTarget.indexOf('https:') === 0 ? proxyTarget : (rOptions.protocol + '//' + proxyTarget)
|
||||||
// eslint-disable-next-line node/no-deprecated-api
|
// eslint-disable-next-line node/no-deprecated-api
|
||||||
const URL = url.parse(proxy)
|
const URL = url.parse(proxy)
|
||||||
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数
|
||||||
|
|
Loading…
Reference in New Issue