Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 在 script 拦截器配置相对地址和绝对地址时,自动生成一些相关的辅助拦截配置,避免相对地址和绝对地址引起的跨域问题和脚本 content-type 限制问题。 #298

Merged

Conversation

wangliang181230
Copy link
Contributor

@wangliang181230 wangliang181230 commented Apr 16, 2024

Ⅰ. 描述此PR的作用:

feature: 在 script 拦截器配置相对地址和绝对地址时,自动生成一些相关的辅助拦截配置,解决以下问题:

  1. 避免跨域问题
  2. 避免脚本 content-type 限制问题
  3. 避免绝对地址在接收代理请求时,由于请求头包含了源站点的一些信息而被拦截或重定向

Ⅱ. 原理说明:

1. 当使用script配置绝对地址时,将会生成相对的伪脚本地址,并为该伪脚本地址生成代理配置,如下:

原配置:

{
  "github.com": {
    "^(/[^/.]{1,30}){2}([/?].*)?$": {
      "script": [
        "https://raw.githubusercontent.com/docmirror/dev-sidecar/scripts/github/monkey.js"
      ],
      "desc": "加载DS仓库中的Github油猴脚本,加速clone和文件下载等。"
    }
  }
}

自动替换为:

{
  "github.com": {
    "^(/[^/.]{1,30}){2}([/?].*)?$": {
      "script": [
        // 绝对地址替换为相对地址,避免跨域问题
        "/____ds_script_proxy____/https_raw_githubusercontent_com_docmirror_dev_sidecar_scripts_github_monkey.js"
      ],
      "desc": "加载DS仓库中的Github油猴脚本,加速clone和文件下载等。"
    },
    "^/____ds_script_proxy____/https_raw_githubusercontent_com_docmirror_dev_sidecar_scripts_github_monkey\\.js$": {
      "proxy": "https://raw.githubusercontent.com/docmirror/dev-sidecar/scripts/github/monkey.js",
      "requestReplace": {
        "headers": {
          "host": "[remove]",
          "referer": "[remove]",
          "cookie": "[remove]"
        }
      },
      "responseReplace": {
        "headers": {
          "content-type": "application/javascript; charset=utf-8",
          "set-cookie": "[remove]",
          "server": "[remove]"
        }
      },
      "cacheDays": 7,
      "desc": "为伪脚本文件设置代理地址,并设置响应头 `content-type: 'application/javascript; charset=utf-8'`,同时缓存7天。"
    }
  }
}

2. 当使用script配置相对地址时,将会生成响应头替换配置,如下:

原配置:

{
  "github.com": {
    "^(/[^/.]{1,30}){2}([/?].*)?$": {
      "script": [
        "/docmirror/dev-sidecar/raw/scripts/github/monkey.js"
      ],
      "desc": "加载DS仓库中的Github油猴脚本,加速clone和文件下载等。"
    }
  }
}

自动增加响应头替换配置:

{
  "github.com": {
    "^(/[^/.]{1,30}){2}([/?].*)?$": {
      "script": [
        "/docmirror/dev-sidecar/raw/scripts/github/monkey.js" // 相对地址不变
      ],
      "desc": "加载DS仓库中的Github油猴脚本,加速clone和文件下载等。"
    },
    "/docmirror/dev-sidecar/raw/scripts/github/monkey.js": {
      "responseReplace": {
        "headers": {
          "content-type": "application/javascript; charset=utf-8"
        }
      },
      "cacheDays": 7,
      "desc": "为脚本设置响应头 `content-type: 'application/javascript; charset=utf-8'`,同时缓存7天。"
    }
  }
}

Ⅲ. 此PR解决了什么问题?

降低了新特性 #294 配置复杂性。

@wangliang181230 wangliang181230 merged commit 35ca94e into docmirror:master Apr 16, 2024
@wangliang181230 wangliang181230 deleted the optimize-script-config branch April 16, 2024 05:34
@wangliang181230 wangliang181230 added this to the 1.8.1 milestone Apr 18, 2024
@wangliang181230 wangliang181230 added the Feature 新特性PR label Apr 18, 2024
@wangliang181230 wangliang181230 changed the title feature: 在 script 拦截器配置相对地址和绝对地址时,自动生成一些相关的辅助拦截配置,避免相对地址和绝对地址引起的跨域问题和脚本内容类型限制问题。 feature: 在 script 拦截器配置相对地址和绝对地址时,自动生成一些相关的辅助拦截配置,避免相对地址和绝对地址引起的跨域问题和脚本 content-type 限制问题。 Apr 18, 2024
@wangliang181230 wangliang181230 added v1.8.1 1.8.1版本 and removed v1.8.1 1.8.1版本 labels Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature 新特性PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant