Skip to content

Commit

Permalink
feature: MacOS 也支持系统代理排除域名功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Oct 1, 2024
1 parent 33ccf67 commit 59aead2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
35 changes: 24 additions & 11 deletions packages/core/src/shell/scripts/set-system-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,11 @@ function getDomesticDomainAllowList () {
}
}

async function _winSetProxy (exec, ip, port, setEnv) {
// 延迟加载config
loadConfig()

function getProxyExcludeIpStr (split) {
let excludeIpStr = ''
for (const ip in config.get().proxy.excludeIpList) {
if (config.get().proxy.excludeIpList[ip] === true) {
excludeIpStr += ip + ';'
excludeIpStr += ip + split
}
}

Expand All @@ -197,7 +194,7 @@ async function _winSetProxy (exec, ip, port, setEnv) {
try {
let domesticDomainAllowList = getDomesticDomainAllowList()
if (domesticDomainAllowList) {
domesticDomainAllowList = (domesticDomainAllowList + '\n').replaceAll(/[\r\n]+/g, '\n').replaceAll(/[^\n]*[^*.a-zA-Z\d-\n]+[^\n]*\r?\n/g, '').replaceAll(/\s*\n+\s*/g, ';')
domesticDomainAllowList = (domesticDomainAllowList + '\n').replaceAll(/[\r\n]+/g, '\n').replaceAll(/[^\n]*[^*.a-zA-Z\d-\n]+[^\n]*\r?\n/g, '').trim().replaceAll(/\s*\n+\s*/g, split)
if (domesticDomainAllowList) {
excludeIpStr += domesticDomainAllowList
log.info('系统代理排除列表拼接国内域名')
Expand All @@ -210,6 +207,15 @@ async function _winSetProxy (exec, ip, port, setEnv) {
}
}

log.debug('系统代理排除域名(excludeIpStr):', excludeIpStr)

return excludeIpStr
}

async function _winSetProxy (exec, ip, port, setEnv) {
// 延迟加载config
loadConfig()

const proxyPath = extraPath.getProxyExePath()
const execFun = 'global'

Expand All @@ -220,6 +226,9 @@ async function _winSetProxy (exec, ip, port, setEnv) {
proxyAddr = `http=http://${ip}:${port};` + proxyAddr
}

// 读取排除域名
const excludeIpStr = getProxyExcludeIpStr(';')
// 设置代理,同时设置排除域名
log.info(`执行“设置系统代理”的程序: ${proxyPath} ${execFun} ${proxyAddr} ......(省略排除IP列表)`)
await execFile(proxyPath, [execFun, proxyAddr, excludeIpStr])

Expand Down Expand Up @@ -298,14 +307,18 @@ const executor = {
loadConfig()

// https
await exec(`networksetup -setsecurewebproxy '${wifiAdaptor}' ${ip} ${port}`)
await exec(`networksetup -setsecurewebproxy "${wifiAdaptor}" ${ip} ${port}`)
// http
if (config.get().proxy.proxyHttp) {
await exec(`networksetup -setwebproxy '${wifiAdaptor}' ${ip} ${port}`)
await exec(`networksetup -setwebproxy "${wifiAdaptor}" ${ip} ${port}`)
} else {
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`)
await exec(`networksetup -setwebproxystate "${wifiAdaptor}" off`)
}

// 设置排除域名
const excludeIpStr = getProxyExcludeIpStr('" "')
await exec(`networksetup -setproxybypassdomains "${wifiAdaptor}" "${excludeIpStr}"`)

// const setEnv = `cat <<ENDOF >> ~/.zshrc
// export http_proxy="http://${ip}:${port}"
// export https_proxy="http://${ip}:${port}"
Expand All @@ -315,9 +328,9 @@ const executor = {
// await exec(setEnv)
} else { // 关闭代理
// https
await exec(`networksetup -setsecurewebproxystate '${wifiAdaptor}' off`)
await exec(`networksetup -setsecurewebproxystate "${wifiAdaptor}" off`)
// http
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`)
await exec(`networksetup -setwebproxystate "${wifiAdaptor}" off`)

// const removeEnv = `
// sed -ie '/export http_proxy/d' ~/.zshrc
Expand Down
30 changes: 16 additions & 14 deletions packages/gui/src/view/pages/proxy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@
</a-form-item>

<!-- 以下功能仅windows支持,mac和linux暂不支持 -->
<div v-if="isWindows()">
<a-form-item label="设置环境变量" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.proxy.setEnv" >
是否同时修改<code>HTTPS_PROXY</code>环境变量(不好用,不建议勾选)
</a-checkbox>
<div class="form-help">
当发现某些应用并没有走加速通道或加速报错时,可尝试勾选此选项,并重新开启系统代理开关<br/>
注意:当前已打开的命令行并不会实时生效,需要重新打开一个新的命令行窗口
</div>
</a-form-item>
<a-form-item label="设置loopback" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-button @click="loopbackVisible=true">去设置</a-button>
<div class="form-help">解决<code>OneNote</code>、<code>MicrosoftStore</code>、<code>Outlook</code>等<code>UWP应用</code>开启代理后无法访问网络的问题</div>
</a-form-item>
<a-form-item v-if="isWindows()" label="设置环境变量" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.proxy.setEnv" >
是否同时修改<code>HTTPS_PROXY</code>环境变量(不好用,不建议勾选)
</a-checkbox>
<div class="form-help">
当发现某些应用并没有走加速通道或加速报错时,可尝试勾选此选项,并重新开启系统代理开关<br/>
注意:当前已打开的命令行并不会实时生效,需要重新打开一个新的命令行窗口
</div>
</a-form-item>
<a-form-item v-if="isWindows()" label="设置loopback" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-button @click="loopbackVisible=true">去设置</a-button>
<div class="form-help">解决<code>OneNote</code>、<code>MicrosoftStore</code>、<code>Outlook</code>等<code>UWP应用</code>开启代理后无法访问网络的问题</div>
</a-form-item>

<!-- 以下功能仅windows和macos支持,linux暂不支持 -->
<div v-if="isWindows() || isMac()">
<hr/>
<a-form-item label="排除国内域名" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.proxy.excludeDomesticDomainAllowList" >
Expand Down

0 comments on commit 59aead2

Please sign in to comment.