script拦截器,支持配置脚本的绝对地址。

pull/294/head
王良 2024-04-13 23:10:07 +08:00
parent 6062ace926
commit e5f5184f11
3 changed files with 20 additions and 10 deletions

View File

@ -33,7 +33,7 @@ function buildTargetUrl (rOptions, urlConf, interceptOpt, matched) {
targetUrl = urlConf
} else {
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
const URL = url.parse(uri)
uri = URL.path

View File

@ -6,10 +6,13 @@ function getScript (key, script) {
const hash = CryptoJs.SHA256(script).toString(CryptoJs.enc.Base64)
return `
<script crossorigin="anonymous" defer="defer" type="application/javascript"
integrity="sha256-${hash}"
src="${scriptUrl}"></script>
`
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer integrity="sha256-${hash}"></script>
`
}
function getScriptByAbsoluteUrl (scriptUrl) {
return `
<script src="${scriptUrl}" type="application/javascript" crossorigin="anonymous" defer></script>
`
}
module.exports = {
@ -25,11 +28,18 @@ module.exports = {
const scripts = monkey.get(setting.script.dirAbsolutePath)
let tags = getScript('global', scripts.global.script)
for (const key of keys) {
const script = scripts[key]
if (script == null) {
continue
let scriptTag
if (key.indexOf('http:') === 0 || key.indexOf('https:') === 0) {
scriptTag = getScriptByAbsoluteUrl(key)
} else {
const script = scripts[key]
if (script == null) {
continue
}
scriptTag = getScript(key, script.script)
}
const scriptTag = getScript(key, script.script)
tags += '\r\n' + scriptTag
}
res.setHeader('DS-Script-Interceptor', 'true')

View File

@ -83,7 +83,7 @@ module.exports = function createOverWallIntercept (overWallConfig) {
const proxyTarget = domain + '/' + path + '/' + hostname + req.url
// 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
const URL = url.parse(proxy)
rOptions.origional = lodash.cloneDeep(rOptions) // 备份原始请求参数