Add proxy toggle script for easy proxy management
- Add proxy-toggle.sh script with full proxy control functionality - Support temporary and permanent proxy enable/disable - Add proxy status checking and connection testing - Include convenient aliases: proxy-on, proxy-off, proxy-toggle, etc. - Automatic IP display when switching proxy states - Configuration file management for persistent proxy settings
This commit is contained in:
parent
7fe25f26c9
commit
8861d81371
|
|
@ -240,3 +240,12 @@ alias users='users'
|
|||
alias w='w'
|
||||
alias who='who'
|
||||
alias last='last -n 10'
|
||||
|
||||
# Proxy Management
|
||||
alias proxy-on='/root/mgmt/scripts/utilities/proxy-toggle.sh on'
|
||||
alias proxy-off='/root/mgmt/scripts/utilities/proxy-toggle.sh off'
|
||||
alias proxy-toggle='/root/mgmt/scripts/utilities/proxy-toggle.sh toggle'
|
||||
alias proxy-enable='/root/mgmt/scripts/utilities/proxy-toggle.sh enable'
|
||||
alias proxy-disable='/root/mgmt/scripts/utilities/proxy-toggle.sh disable'
|
||||
alias proxy-status='/root/mgmt/scripts/utilities/proxy-toggle.sh status'
|
||||
alias proxy-test='/root/mgmt/scripts/utilities/proxy-toggle.sh test'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,304 @@
|
|||
#!/bin/bash
|
||||
|
||||
# 代理开关脚本
|
||||
# 用于一键开启/关闭 istoreos.tailnet-68f9.ts.net:1082 代理
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
# 代理配置
|
||||
PROXY_HOST="istoreos.tailnet-68f9.ts.net"
|
||||
PROXY_PORT="1082"
|
||||
PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}"
|
||||
|
||||
# 配置文件路径
|
||||
PROXY_ENV_FILE="/root/mgmt/configuration/proxy.env"
|
||||
SHELL_RC_FILE="$HOME/.zshrc"
|
||||
BASH_RC_FILE="$HOME/.bashrc"
|
||||
|
||||
# 日志函数
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查代理状态
|
||||
check_proxy_status() {
|
||||
if [[ -n "${http_proxy:-}" ]] || [[ -n "${HTTP_PROXY:-}" ]]; then
|
||||
echo "on"
|
||||
else
|
||||
echo "off"
|
||||
fi
|
||||
}
|
||||
|
||||
# 测试代理连接
|
||||
test_proxy() {
|
||||
log_info "测试代理连接..."
|
||||
if curl -s --connect-timeout 5 --proxy "$PROXY_URL" https://httpbin.org/ip >/dev/null 2>&1; then
|
||||
log_success "代理连接正常"
|
||||
return 0
|
||||
else
|
||||
log_error "代理连接失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 开启代理
|
||||
enable_proxy() {
|
||||
log_info "开启代理..."
|
||||
|
||||
# 设置环境变量
|
||||
export http_proxy="$PROXY_URL"
|
||||
export https_proxy="$PROXY_URL"
|
||||
export HTTP_PROXY="$PROXY_URL"
|
||||
export HTTPS_PROXY="$PROXY_URL"
|
||||
export no_proxy="localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net"
|
||||
export NO_PROXY="localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net"
|
||||
export ALL_PROXY="$PROXY_URL"
|
||||
export all_proxy="$PROXY_URL"
|
||||
|
||||
# 测试连接
|
||||
if test_proxy; then
|
||||
log_success "代理已开启: $PROXY_URL"
|
||||
|
||||
# 显示当前IP
|
||||
local current_ip=$(curl -s --connect-timeout 5 --proxy "$PROXY_URL" https://httpbin.org/ip | jq -r .origin 2>/dev/null || echo "未知")
|
||||
log_info "当前IP: $current_ip"
|
||||
|
||||
return 0
|
||||
else
|
||||
log_error "代理开启失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 关闭代理
|
||||
disable_proxy() {
|
||||
log_info "关闭代理..."
|
||||
|
||||
# 清除环境变量
|
||||
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
|
||||
unset no_proxy NO_PROXY ALL_PROXY all_proxy
|
||||
|
||||
log_success "代理已关闭"
|
||||
|
||||
# 显示当前IP
|
||||
local current_ip=$(curl -s --connect-timeout 5 https://httpbin.org/ip | jq -r .origin 2>/dev/null || echo "未知")
|
||||
log_info "当前IP: $current_ip"
|
||||
}
|
||||
|
||||
# 切换代理状态
|
||||
toggle_proxy() {
|
||||
local current_status=$(check_proxy_status)
|
||||
|
||||
if [[ "$current_status" == "on" ]]; then
|
||||
disable_proxy
|
||||
else
|
||||
enable_proxy
|
||||
fi
|
||||
}
|
||||
|
||||
# 永久开启代理(写入配置文件)
|
||||
enable_proxy_permanent() {
|
||||
log_info "永久开启代理..."
|
||||
|
||||
# 创建代理环境文件
|
||||
cat > "$PROXY_ENV_FILE" << EOF
|
||||
# Proxy Configuration for ${PROXY_HOST}:${PROXY_PORT}
|
||||
# This file contains proxy environment variables for the management system
|
||||
|
||||
# HTTP/HTTPS Proxy Settings
|
||||
export http_proxy=${PROXY_URL}
|
||||
export https_proxy=${PROXY_URL}
|
||||
export HTTP_PROXY=${PROXY_URL}
|
||||
export HTTPS_PROXY=${PROXY_URL}
|
||||
|
||||
# No Proxy Settings (local networks and services)
|
||||
export no_proxy=localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net
|
||||
export NO_PROXY=localhost,127.0.0.1,::1,.local,.tailnet-68f9.ts.net
|
||||
|
||||
# Additional proxy settings for various tools
|
||||
export ALL_PROXY=${PROXY_URL}
|
||||
export all_proxy=${PROXY_URL}
|
||||
|
||||
# Docker proxy settings
|
||||
export DOCKER_BUILDKIT=1
|
||||
export BUILDKIT_PROGRESS=plain
|
||||
|
||||
# Git proxy settings
|
||||
export GIT_HTTP_PROXY=${PROXY_URL}
|
||||
export GIT_HTTPS_PROXY=${PROXY_URL}
|
||||
|
||||
# Curl proxy settings
|
||||
export CURL_PROXY=${PROXY_URL}
|
||||
|
||||
# Wget proxy settings
|
||||
export WGET_PROXY=${PROXY_URL}
|
||||
EOF
|
||||
|
||||
# 在 shell 配置文件中加载代理配置
|
||||
local shell_files=("$SHELL_RC_FILE" "$BASH_RC_FILE")
|
||||
|
||||
for shell_file in "${shell_files[@]}"; do
|
||||
if [[ -f "$shell_file" ]]; then
|
||||
# 检查是否已经加载了代理配置
|
||||
if ! grep -q "source.*proxy.env" "$shell_file"; then
|
||||
log_info "在 $shell_file 中添加代理配置加载..."
|
||||
echo "" >> "$shell_file"
|
||||
echo "# Load proxy configuration if exists" >> "$shell_file"
|
||||
echo "if [[ -f $PROXY_ENV_FILE ]]; then" >> "$shell_file"
|
||||
echo " source $PROXY_ENV_FILE" >> "$shell_file"
|
||||
echo "fi" >> "$shell_file"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# 立即加载配置
|
||||
if [[ -f "$PROXY_ENV_FILE" ]]; then
|
||||
source "$PROXY_ENV_FILE"
|
||||
fi
|
||||
|
||||
if test_proxy; then
|
||||
log_success "代理已永久开启"
|
||||
log_info "配置已保存到: $PROXY_ENV_FILE"
|
||||
log_info "请重新登录或运行: source ~/.zshrc"
|
||||
else
|
||||
log_error "代理永久开启失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 永久关闭代理(从配置文件移除)
|
||||
disable_proxy_permanent() {
|
||||
log_info "永久关闭代理..."
|
||||
|
||||
# 备份现有配置
|
||||
if [[ -f "$PROXY_ENV_FILE" ]]; then
|
||||
cp "$PROXY_ENV_FILE" "${PROXY_ENV_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||
rm -f "$PROXY_ENV_FILE"
|
||||
fi
|
||||
|
||||
# 从 shell 配置文件中移除代理配置加载
|
||||
local shell_files=("$SHELL_RC_FILE" "$BASH_RC_FILE")
|
||||
|
||||
for shell_file in "${shell_files[@]}"; do
|
||||
if [[ -f "$shell_file" ]]; then
|
||||
# 移除代理配置加载行
|
||||
if grep -q "source.*proxy.env" "$shell_file"; then
|
||||
log_info "从 $shell_file 中移除代理配置加载..."
|
||||
sed -i '/# Load proxy configuration if exists/,/^fi$/d' "$shell_file"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# 立即清除环境变量
|
||||
disable_proxy
|
||||
|
||||
log_success "代理已永久关闭"
|
||||
log_info "请重新登录或运行: source ~/.zshrc"
|
||||
}
|
||||
|
||||
# 显示代理状态
|
||||
show_status() {
|
||||
local current_status=$(check_proxy_status)
|
||||
|
||||
echo ""
|
||||
log_info "=== 代理状态 ==="
|
||||
|
||||
if [[ "$current_status" == "on" ]]; then
|
||||
log_success "代理状态: 开启"
|
||||
log_info "代理地址: $PROXY_URL"
|
||||
|
||||
# 显示当前IP
|
||||
local current_ip=$(curl -s --connect-timeout 5 --proxy "$PROXY_URL" https://httpbin.org/ip | jq -r .origin 2>/dev/null || echo "未知")
|
||||
log_info "当前IP: $current_ip"
|
||||
else
|
||||
log_warning "代理状态: 关闭"
|
||||
|
||||
# 显示当前IP
|
||||
local current_ip=$(curl -s --connect-timeout 5 https://httpbin.org/ip | jq -r .origin 2>/dev/null || echo "未知")
|
||||
log_info "当前IP: $current_ip"
|
||||
fi
|
||||
|
||||
# 检查配置文件状态
|
||||
if [[ -f "$PROXY_ENV_FILE" ]]; then
|
||||
log_info "配置文件: 存在 ($PROXY_ENV_FILE)"
|
||||
else
|
||||
log_info "配置文件: 不存在"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 显示帮助信息
|
||||
show_help() {
|
||||
echo "代理开关脚本 - 管理 istoreos.tailnet-68f9.ts.net:1082 代理"
|
||||
echo ""
|
||||
echo "用法: $0 [命令]"
|
||||
echo ""
|
||||
echo "命令:"
|
||||
echo " on - 临时开启代理(仅当前会话)"
|
||||
echo " off - 临时关闭代理(仅当前会话)"
|
||||
echo " toggle - 切换代理状态"
|
||||
echo " enable - 永久开启代理(写入配置文件)"
|
||||
echo " disable - 永久关闭代理(从配置文件移除)"
|
||||
echo " status - 显示代理状态"
|
||||
echo " test - 测试代理连接"
|
||||
echo " help - 显示此帮助信息"
|
||||
echo ""
|
||||
echo "示例:"
|
||||
echo " $0 on # 临时开启代理"
|
||||
echo " $0 enable # 永久开启代理"
|
||||
echo " $0 status # 查看代理状态"
|
||||
echo " $0 toggle # 切换代理状态"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
case "${1:-help}" in
|
||||
"on")
|
||||
enable_proxy
|
||||
;;
|
||||
"off")
|
||||
disable_proxy
|
||||
;;
|
||||
"toggle")
|
||||
toggle_proxy
|
||||
;;
|
||||
"enable")
|
||||
enable_proxy_permanent
|
||||
;;
|
||||
"disable")
|
||||
disable_proxy_permanent
|
||||
;;
|
||||
"status")
|
||||
show_status
|
||||
;;
|
||||
"test")
|
||||
test_proxy
|
||||
;;
|
||||
"help"|*)
|
||||
show_help
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Loading…
Reference in New Issue