新增 OPTIONS 请求拦截器。
parent
51961adc96
commit
c3ee313f58
|
@ -50,6 +50,11 @@ module.exports = {
|
||||||
enabled: true
|
enabled: true
|
||||||
},
|
},
|
||||||
intercepts: {
|
intercepts: {
|
||||||
|
'www.nbgzjk.cn': {
|
||||||
|
'/.*': {
|
||||||
|
options: true
|
||||||
|
}
|
||||||
|
},
|
||||||
'github.com': {
|
'github.com': {
|
||||||
'/.*/.*/releases/download/': {
|
'/.*/.*/releases/download/': {
|
||||||
redirect: 'gh.api.99988866.xyz/https://github.com',
|
redirect: 'gh.api.99988866.xyz/https://github.com',
|
||||||
|
@ -226,9 +231,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
whiteList: {
|
whiteList: {
|
||||||
'*.cn': true,
|
|
||||||
'cn.*': true,
|
|
||||||
'*china*': true,
|
|
||||||
'*.dingtalk.com': true,
|
'*.dingtalk.com': true,
|
||||||
'*.apple.com': true,
|
'*.apple.com': true,
|
||||||
'*.microsoft.com': true,
|
'*.microsoft.com': true,
|
||||||
|
@ -263,8 +265,7 @@ module.exports = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mapping: {
|
mapping: {
|
||||||
'*.github.com': 'quad9',
|
'*github*.com': 'quad9',
|
||||||
'*.*github*.com': 'quad9',
|
|
||||||
'*.github.io': 'quad9',
|
'*.github.io': 'quad9',
|
||||||
'*.docker.com': 'quad9',
|
'*.docker.com': 'quad9',
|
||||||
'*.docker*.com': 'quad9',
|
'*.docker*.com': 'quad9',
|
||||||
|
|
|
@ -10,7 +10,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
targets: {
|
targets: {
|
||||||
'*.github.com': true,
|
|
||||||
'*github*.com': true,
|
'*github*.com': true,
|
||||||
'*.wikimedia.org': true,
|
'*.wikimedia.org': true,
|
||||||
'*.v2ex.com': true,
|
'*.v2ex.com': true,
|
||||||
|
|
|
@ -58,11 +58,6 @@ module.exports = {
|
||||||
excludeIpList: {
|
excludeIpList: {
|
||||||
// region 常用国内可访问域名
|
// region 常用国内可访问域名
|
||||||
|
|
||||||
// 中国大陆
|
|
||||||
'*.cn': true,
|
|
||||||
'cn.*': true,
|
|
||||||
'*china*': true,
|
|
||||||
|
|
||||||
// 系统之家
|
// 系统之家
|
||||||
'*.xitongzhijia.net': true,
|
'*.xitongzhijia.net': true,
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
const defaultAllowHeaders = '*'
|
||||||
|
const defaultAllowMethods = 'GET,POST,PUT,DELETE,HEAD,OPTIONS,PATCH' // CONNECT、TRACE被认为是不安全的请求,通常不建议允许跨域
|
||||||
|
|
||||||
|
function readConfig (config, defaultConfig) {
|
||||||
|
if (config) {
|
||||||
|
if (Object.isArray(config)) {
|
||||||
|
config = config.join(',')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
config = defaultConfig
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
requestIntercept (context, interceptOpt, req, res, ssl, next) {
|
||||||
|
const { rOptions, log } = context
|
||||||
|
|
||||||
|
// 不是 OPTIONS 请求,或请求头中不含 origin 时,跳过当前拦截器
|
||||||
|
if (rOptions.method !== 'OPTIONS' || rOptions.headers.origin == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从请求头中获取跨域相关信息;如果不存在,则从配置中获取的值;如果还不存在,则使用默认值
|
||||||
|
const allowHeaders = rOptions.headers['access-control-request-headers'] || readConfig(interceptOpt.optionsAllowHeaders, defaultAllowHeaders)
|
||||||
|
const allowMethods = rOptions.headers['access-control-request-method'] || readConfig(interceptOpt.optionsAllowMethods, defaultAllowMethods)
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
// 允许跨域
|
||||||
|
'Dev-Sidecar-Interceptor': 'options',
|
||||||
|
'Access-Control-Allow-Origin': rOptions.headers.origin,
|
||||||
|
'Access-Control-Allow-Headers': allowHeaders,
|
||||||
|
'Access-Control-Allow-Methods': allowMethods,
|
||||||
|
'Access-Control-Max-Age': interceptOpt.optionsMaxAge > 0 ? interceptOpt.optionsMaxAge : 2592000, // 默认有效一个月
|
||||||
|
Date: new Date().toUTCString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断是否允许
|
||||||
|
if (interceptOpt.optionsCredentials !== false && interceptOpt.optionsCredentials !== 'false') {
|
||||||
|
headers['Access-Control-Allow-Credentials'] = 'true'
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(200, headers)
|
||||||
|
res.end()
|
||||||
|
|
||||||
|
log.info('options intercept:', (rOptions.original || rOptions).url)
|
||||||
|
return true // true代表请求结束
|
||||||
|
},
|
||||||
|
is (interceptOpt) {
|
||||||
|
return !!interceptOpt.options
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
// request interceptor impls
|
// request interceptor impls
|
||||||
|
const OPTIONS = require('./impl/req/OPTIONS.js')
|
||||||
|
|
||||||
const success = require('./impl/req/success')
|
const success = require('./impl/req/success')
|
||||||
const redirect = require('./impl/req/redirect')
|
const redirect = require('./impl/req/redirect')
|
||||||
const abort = require('./impl/req/abort')
|
const abort = require('./impl/req/abort')
|
||||||
|
@ -14,6 +16,7 @@ const script = require('./impl/res/script')
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
// request interceptor impls
|
// request interceptor impls
|
||||||
|
OPTIONS,
|
||||||
success, redirect, abort,
|
success, redirect, abort,
|
||||||
cacheReq,
|
cacheReq,
|
||||||
proxy, sni,
|
proxy, sni,
|
||||||
|
|
Loading…
Reference in New Issue