mirror of
https://github.com/EtherDream/jsproxy
synced 2024-11-22 18:50:48 +00:00
78 lines
2.0 KiB
Plaintext
78 lines
2.0 KiB
Plaintext
# 标记客户端是否已安装 Service Worker
|
||
# 0:请求任何路径,都返回 SW 安装页面(www/__setup.html)
|
||
# 1:正常反向代理
|
||
set $_hasSw '0';
|
||
|
||
# 标记资源的协议
|
||
# 0:HTTP
|
||
# 1:HTTPS
|
||
set $_isHttp '0';
|
||
|
||
# 标记是否为 CORS 请求
|
||
# 0:不转发 Origin 头
|
||
# 1:调整并转发 Origin 头
|
||
set $_hasCors '0';
|
||
|
||
# 记录资源的端口号
|
||
set $_port '';
|
||
|
||
set $_ref '';
|
||
set $_ori $http_origin;
|
||
|
||
set $_acao '';
|
||
|
||
# 获取并删除 flag 参数
|
||
# 参数格式: isHttp .. port
|
||
if ($args ~
|
||
(?<_pre>.*?)&flag__=(?<_hasSw>.)(?<_isHttp>.)(?<_hasCors>.)(?<_port>\d*)(?<_post>.*)
|
||
) {
|
||
set $args $_pre$_post;
|
||
set $_js 1;
|
||
}
|
||
|
||
# 调整 Referer 头
|
||
# TODO:未考虑协议和端口,下面的 cors 也有这问题
|
||
if ($http_referer ~ ^https://(?<_vhost>[^/]+)(?<_path>.*)) {
|
||
set $_ref https://$_vhost_to_rhost$_path;
|
||
set $_acao https://$_vhost;
|
||
}
|
||
|
||
if ($_ori) {
|
||
set $_acao $_ori;
|
||
}
|
||
if ($_acao = '') {
|
||
set $_acao '*';
|
||
}
|
||
|
||
# ServiceWorker 的 fetch 强制 cors 模式,
|
||
# 所以需要该标记,标识原始请求是否为 cors
|
||
if ($_hasCors = '0') {
|
||
set $_ori '';
|
||
}
|
||
if ($_ori ~ ^https://(?<_vhost>.*)) {
|
||
set $_ori https://$_vhost_to_rhost;
|
||
}
|
||
|
||
proxy_set_header Origin $_ori;
|
||
proxy_set_header Referer $_ref;
|
||
proxy_set_header Upgrade $http_upgrade;
|
||
proxy_set_header Connection $http_connection;
|
||
|
||
# CSP 可能导致注入页面资源无法加载
|
||
more_clear_headers
|
||
content-security-policy
|
||
content-security-policy-report-only
|
||
expect-ct
|
||
x-frame-options
|
||
;
|
||
|
||
more_set_headers
|
||
'access-control-allow-credentials: true'
|
||
'access-control-allow-origin: $_acao'
|
||
'strict-transport-security: max-age=99999999; includeSubDomains; preload'
|
||
;
|
||
|
||
# 重定向调整
|
||
# 直接用 return 指令返回状态码,会丢失其他的头,比如 set-cookie
|
||
header_filter_by_lua_file ../proc-redir.lua;
|