Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy_on后只对当前ssh窗口生效 #44

Open
unita-admin opened this issue Oct 30, 2024 · 1 comment
Open

proxy_on后只对当前ssh窗口生效 #44

unita-admin opened this issue Oct 30, 2024 · 1 comment

Comments

@unita-admin
Copy link

No description provided.

@i-bara
Copy link

i-bara commented Jan 1, 2025

因为 proxy_on 的原理是通过 export 命令修改环境变量,自然而然地,只对当前环境有效或者其子进程有效。

环境变量的作用范围不仅包括当前的 Shell 环境,还包括所有由这个 Shell 启动的子进程。

如果您不希望每一次启动都输入 proxy_on,可以键入以下命令(如果要使用选择代理功能,还需要把 api_urlSecret 修改为自己的,参考 https://github.com/Elegycloud/clash-for-linux-backup/issues/40#issuecomment-2136643937):

# ====== 开启代理 ======
cat>>~/.bashrc<<EOF

# 开启系统代理
function proxy_on() {
	export http_proxy=http://127.0.0.1:7880
	export https_proxy=http://127.0.0.1:7880
	export all_proxy=socks5://127.0.0.1:7880
	export no_proxy=127.0.0.1,localhost
	export HTTP_PROXY=http://127.0.0.1:7880
	export HTTPS_PROXY=http://127.0.0.1:7880
	export ALL_PROXY=socks5://127.0.0.1:7880
	export NO_PROXY=127.0.0.1,localhost
	echo -e "\033[32m[√] 已开启代理\033[0m"
}

# 关闭系统代理
function proxy_off(){
	unset http_proxy
	unset https_proxy
	unset all_proxy
	unset no_proxy
	unset HTTP_PROXY
	unset HTTPS_PROXY
	unset ALL_PROXY
	unset NO_PROXY
	echo -e "\033[31m[×] 已关闭代理\033[0m"
}

proxy_on

api_url="http://localhost:9090"
Secret=""

# 获取Clash代理节点列表
get_proxy_list() {
    # 使用您的命令获取代理节点列表
    # ============= 修改此处 =============
    proxies=\$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer \${Secret}" \$api_url/proxies | jq -c '.proxies.GLOBAL.all')
}

# 选择代理节点
select_proxy() {
    local mode=\$1
    if [ -z \$mode ]; then
    mode="Proxy"
    fi
    get_proxy_list
    echo "========== 代理节点列表 =========="
    i=1
    # ============= 修改此处 =============
    echo "\$proxies" | jq -r '.[]' | while IFS= read -r proxy; do
        echo "\$i. \$proxy"
        i=\$((i+1))
    done
    echo "==================================="
    read -p "请选择代理节点(输入编号):" proxy_index
    proxy=\$(echo "\$proxies" | jq -r ".[\$((proxy_index-1))]")
    # ============= 修改结束 =============
    if [[ -n \$proxy ]]; then
        # 更新Clash的代理节点设置
        curl -X PUT -s "\$api_url/proxies/\$mode" -H "Content-Type: application/json" -H "Authorization: Bearer \${Secret}" --data "{\"name\":\"\$proxy\"}" > /dev/null

        echo "代理节点已更新为:\$proxy"
    else
        echo "无效的选择!"
    fi
}
EOF

source ~/.bashrc

这样,每次登录时都会自动地执行 proxy_on 命令。同时,可以使用 proxy_onproxy_offselect_proxy 来完成开启代理、关闭代理、选择代理的操作。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants