const result = {}
Object.keys(config).map((key) => {
const value = config[key] const ipMatch = (value.receiver || '').match(/://(.*?):d/)
const ip = ipMatch && ipMatch[1] result[key] = {
ip,
host: value.host || defaultHost,
port: value.port || '8391'
}
})
// 设置 remote
if (rdConfig) {
const ipMatch = (rdConfig.receiver || '').match(/://(.*?):d/)
const ip = ipMatch && ipMatch[1] result.remote = {
ip,
host: rdConfig.host || defaultHost,
port: rdConfig.port || '8391'
}
}
// 设置 local
result.local = {
ip: 'localhost',
host: 'localhost',
port: localProxyPort
}
return result
}
function setProxy (proxyTable) {
const result = {}
Object.keys(proxyTable).forEach((api) => {
let type = proxyTable[api] const isCustomType = typeof type === 'string' && !/^http/.test(type)
if (isCustomType && type !== 'remote' && type !== 'local' && !finalConfig[type]) {
throw new TypeError(`代理类型${type}不正确,请提供 http 或 https 类型的接口,或者指定正确的 release 机器名称`)
}
if (type === 'remote' && !finalConfig.remote) {
type = 'local'
}
if (isCustomType) {
if (isAllRemote && type !== 'remote') {
type = 'remote'
}
if (isAllLocal && type !== 'local') {
type = 'local'
}
}
const targetConfig = finalConfig[type] let target = type
if (targetConfig) {
target = {
target: `http://${useHost ? targetConfig.host : targetConfig.ip}:${targetConfig.port}`,
// 使用 host 时需要转换,其他不需要转换
headers: {
host: `${targetConfig.host}:${targetConfig.port}`
}
}
}
result[api] = target
})
return result
}
return {
proxyTable: setProxy(proxy.proxyTable),
host: remote.host || defaultHost
}
}
用法
用法中需要配置两种指向:系统 host 和浏览器代理 Host。
之所以要两种 host, 本质上是因为接口使用的域名
和我们的本地访问的域名是相同的,同一域名无法指向两个地址,所以相当于对浏览器端进行了拦截。
系统 host 推荐使用 switchHost 进行切换,浏览器推荐使用 whistle 进行切换。
本地开发
host 配置:无
whistle 配置:默认的域名
127.0.0.1 dev.example.com
启动命令:
npm run dev
npm run dev --allLocal注: 此时 proxyTable 中配置的 remote 全部转换为 local,在 allLocal 参数时将所有自定义类型转换为 local










