diff --git a/install.sh b/install.sh index 42de59b..deba19e 100755 --- a/install.sh +++ b/install.sh @@ -388,43 +388,68 @@ fi PROFILED_FILE="/etc/profile.d/clash-for-linux.sh" install_profiled() { - local http_port="${MIXED_PORT:-7890}" - # 兼容你后面可能支持 auto:auto 就先用 7890 - [ "$http_port" = "auto" ] && http_port="7890" + local install_dir="$Install_Dir" - # 只写 IPv4 loopback,避免某些环境 ::1 解析问题 sudo tee "$PROFILED_FILE" >/dev/null </dev/null 2>&1 || true + set -u +fi + +# ===== 默认值(兜底)===== +CLASH_LISTEN_IP="\${CLASH_LISTEN_IP:-127.0.0.1}" +CLASH_HTTP_PORT="\${CLASH_HTTP_PORT:-7890}" +CLASH_SOCKS_PORT="\${CLASH_SOCKS_PORT:-7891}" + +# ===== 开启代理 ===== proxy_on() { - export http_proxy="\$CLASH_HTTP_PROXY" - export https_proxy="\$CLASH_HTTP_PROXY" - export all_proxy="\$CLASH_SOCKS_PROXY" - export HTTP_PROXY="\$CLASH_HTTP_PROXY" - export HTTPS_PROXY="\$CLASH_HTTP_PROXY" - export ALL_PROXY="\$CLASH_SOCKS_PROXY" - echo "[OK] Proxy enabled: \$CLASH_HTTP_PROXY" + export http_proxy="http://\${CLASH_LISTEN_IP}:\${CLASH_HTTP_PORT}" + export https_proxy="http://\${CLASH_LISTEN_IP}:\${CLASH_HTTP_PORT}" + export HTTP_PROXY="http://\${CLASH_LISTEN_IP}:\${CLASH_HTTP_PORT}" + export HTTPS_PROXY="http://\${CLASH_LISTEN_IP}:\${CLASH_HTTP_PORT}" + export all_proxy="socks5://\${CLASH_LISTEN_IP}:\${CLASH_SOCKS_PORT}" + export ALL_PROXY="socks5://\${CLASH_LISTEN_IP}:\${CLASH_SOCKS_PORT}" + export no_proxy="127.0.0.1,localhost,::1" + export NO_PROXY="127.0.0.1,localhost,::1" + echo "[OK] Proxy enabled: http://\${CLASH_LISTEN_IP}:\${CLASH_HTTP_PORT}" } +# ===== 关闭代理 ===== proxy_off() { - unset http_proxy https_proxy all_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY + unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY + unset all_proxy ALL_PROXY no_proxy NO_PROXY echo "[OK] Proxy disabled" } +# ===== 状态 ===== proxy_status() { echo "http_proxy=\${http_proxy:-}" echo "https_proxy=\${https_proxy:-}" echo "all_proxy=\${all_proxy:-}" + echo "CLASH_HTTP_PORT=\${CLASH_HTTP_PORT}" + echo "CLASH_SOCKS_PORT=\${CLASH_SOCKS_PORT}" } EOF sudo chmod 644 "$PROFILED_FILE" + + # ===== 自动写入 bashrc(关键!)===== + local BASHRC_FILE="/root/.bashrc" + local SOURCE_LINE='[ -f /etc/profile.d/clash-for-linux.sh ] && source /etc/profile.d/clash-for-linux.sh' + + if [ -f "$BASHRC_FILE" ]; then + if ! grep -Fq "$SOURCE_LINE" "$BASHRC_FILE"; then + echo "$SOURCE_LINE" >> "$BASHRC_FILE" + fi + fi } install_profiled || true