Skip to content

Commit

Permalink
bugfix: 修复 linux 下,启用系统代理失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Sep 21, 2024
1 parent 8f1ab61 commit f22e792
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 20 additions & 25 deletions packages/core/src/shell/scripts/set-system-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function _winUnsetProxy (exec, setEnv) {
}
})
} catch (e) {
log.error('启动系统代理失败:', e)
log.error('关闭系统代理失败:', e)
}
}

Expand Down Expand Up @@ -82,19 +82,17 @@ async function _winSetProxy (exec, ip, port, setEnv) {
const executor = {
async windows (exec, params = {}) {
const { ip, port, setEnv } = params
if (ip == null) {
// 清空代理
log.info('关闭windows系统代理')
return _winUnsetProxy(exec, setEnv)
} else {
// 设置代理
if (ip != null) { // 设置代理
log.info('设置windows系统代理:', ip, port, setEnv)
return _winSetProxy(exec, ip, port, setEnv)
} else { // 关闭代理
log.info('关闭windows系统代理')
return _winUnsetProxy(exec, setEnv)
}
},
async linux (exec, params = {}) {
const { ip, port } = params
if (ip != null) {
if (ip != null) { // 设置代理
// 延迟加载config
loadConfig()

Expand All @@ -103,25 +101,22 @@ const executor = {
// https
const setProxyCmd = [
'gsettings set org.gnome.system.proxy mode manual',
'gsettings set org.gnome.system.proxy.https enabled true',
`gsettings set org.gnome.system.proxy.https host ${ip}`,
`gsettings set org.gnome.system.proxy.https port ${port}`
]
// http
if (config.get().proxy.proxyHttp) {
setProxyCmd.push('gsettings set org.gnome.system.proxy.http enabled true')
setProxyCmd.push(`gsettings set org.gnome.system.proxy.http host ${ip}`)
setProxyCmd.push(`gsettings set org.gnome.system.proxy.http port ${port}`)
} else {
setProxyCmd.push('gsettings set org.gnome.system.proxy.http enabled false')
setProxyCmd.push("gsettings set org.gnome.system.proxy.http host ''")
setProxyCmd.push('gsettings set org.gnome.system.proxy.http port 0')
}
// ignore-hosts
// setProxyCmd.push(`gsettings set org.gnome.system.proxy ignore-hosts "${local}"`)

await exec(setProxyCmd)
} else {
} else { // 关闭代理
const setProxyCmd = [
'gsettings set org.gnome.system.proxy mode none'
]
Expand All @@ -134,19 +129,7 @@ const executor = {
wifiAdaptor = wifiAdaptor.trim()
wifiAdaptor = wifiAdaptor.substring(wifiAdaptor.indexOf(' ')).trim()
const { ip, port } = params
if (ip == null) {
// https
await exec(`networksetup -setsecurewebproxystate '${wifiAdaptor}' off`)
// http
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`)

// const removeEnv = `
// sed -ie '/export http_proxy/d' ~/.zshrc
// sed -ie '/export https_proxy/d' ~/.zshrc
// source ~/.zshrc
// `
// await exec(removeEnv)
} else {
if (ip != null) { // 设置代理
// 延迟加载config
loadConfig()

Expand All @@ -166,6 +149,18 @@ const executor = {
// source ~/.zshrc
// `
// await exec(setEnv)
} else { // 关闭代理
// https
await exec(`networksetup -setsecurewebproxystate '${wifiAdaptor}' off`)
// http
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`)

// const removeEnv = `
// sed -ie '/export http_proxy/d' ~/.zshrc
// sed -ie '/export https_proxy/d' ~/.zshrc
// source ~/.zshrc
// `
// await exec(removeEnv)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shell/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function _childExec (composeCmds, options = {}) {
childProcess.exec(composeCmds, options, function (error, stdout, stderr) {
if (error) {
if (options.printErrorLog !== false) {
log.error('cmd 命令执行错误:\n==============================\ncommands:', composeCmds, '\n error:', error, '\n stdout:', stdout, '\n stderr:', stderr, '\n==============================')
log.error('cmd 命令执行错误:\n===>\ncommands:', composeCmds, '\n error:', error, '\n stdout:', stdout, '\n stderr:', stderr, '\n<===')
}
reject(new Error(stderr))
} else {
Expand Down

0 comments on commit f22e792

Please sign in to comment.