背景需求,国内云服务器,虚拟机能够更方便得连接学术网站,例如GitHub,docker等 方案1 配置proxychains 优点,简单,傻瓜式,在需要代理的命令前加上proxychains即可 方案2 配置透明代理 为了简化流程,我写了个服务单元,并把脚本上传到对象存储,这样就能保证被墙,用户也不需要麻烦去配置 命令行一键配置 bash <(wget -qO- -o- https://e.oss-cn-beijing.aliyuncs.com/SSR/ssr-systemd.sh) 缺点:启动和关闭都需要执行source ~/.bashrc
云端脚本内容
- #!/bin/bash
-
- cat <<EOF | sudo tee /etc/systemd/system/proxy.service
- [Unit]
- Description=Enable/Disable Global Proxy
- After=network-online.target
-
- [Service]
- Type=oneshot
- RemainAfterExit=true
- ExecStartPre=/bin/bash -c ‘echo "启动成功,执行source ~/.bashrc以立即应用加速!"’
- ExecStart=/bin/bash -c ‘curl -sSL https://e.oss-cn-beijing.aliyuncs.com/SSR/enable_proxy.sh | bash’
- ExecStop=/bin/bash -c ‘curl -sSL https://e.oss-cn-beijing.aliyuncs.com/SSR/disable_proxy.sh | bash’
- ExecStopPost=/bin/bash -c ‘echo "停止成功,执行source ~/.bashrc以立即停止应用加速!"’
-
- [Install]
- WantedBy=multi-user.target
- EOF
-
- systemctl daemon-reload
-
- echo "安装服务成功!"
- echo "执行 ‘systemctl start proxy’ 启动全局代理…"
- echo "执行 ‘systemctl stop proxy’ 停止全局代理…"
- echo "执行 ‘systemctl status proxy’ 查看服务状态…"
复制代码
启动脚本内容:
- #!/bin/bash
-
- # 代理服务器配置
- PROXY_URL="socks5://127.0.0.1:1080"
-
- # 设置环境变量
- echo "Setting environment variables…"
- export http_proxy=$PROXY_URL
- export https_proxy=$PROXY_URL
- export all_proxy=$PROXY_URL
-
- # 将代理环境变量添加到用户的 shell 配置文件
- echo "export http_proxy=$PROXY_URL" >> ~/.bashrc
- echo "export https_proxy=$PROXY_URL" >> ~/.bashrc
- echo "export all_proxy=$PROXY_URL" >> ~/.bashrc
-
- # 配置apt使用代理
- echo "Acquire::http::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
- echo "Acquire::https::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
- echo "Acquire::socks::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
- # 配置wget, git, docker代理
- # wget
- echo "Configuring wget…"
- echo "use_proxy = on" >> ~/.wgetrc
- echo "http_proxy = $PROXY_URL" >> ~/.wgetrc
- echo "https_proxy = $PROXY_URL" >> ~/.wgetrc
-
- # git
- echo "Configuring git…"
- git config –global http.proxy $PROXY_URL
- git config –global https.proxy $PROXY_URL
-
- # Docker
- DOCKER_CONF_DIR="/etc/systemd/system/docker.service.d"
- mkdir -p $DOCKER_CONF_DIR
- echo "[Service]
- Environment="HTTP_PROXY=$PROXY_URL"
- Environment="HTTPS_PROXY=$PROXY_URL"
- " | tee $DOCKER_CONF_DIR/http-proxy.conf
-
- # 重新加载并重启Docker服务
- systemctl daemon-reload
- systemctl restart docker
-
- echo "配置全局加速成功,执行source ~/.bashrc以应用加速!"
复制代码
停止脚本内容:
- #!/bin/bash
-
- # 清除环境变量
- echo "正在清除环境变量…"
- unset http_proxy
- unset https_proxy
- unset all_proxy
- # 从用户的 shell 配置文件中移除代理环境变量
- sed -i ‘/http_proxy/d’ ~/.bashrc
- sed -i ‘/https_proxy/d’ ~/.bashrc
- sed -i ‘/all_proxy/d’ ~/.bashrc
- echo "正在清除应用配置…"
- # 清除wget代理配置
- sed -i ‘/use_proxy = on/d’ ~/.wgetrc
- sed -i ‘/http_proxy/d’ ~/.wgetrc
- sed -i ‘/https_proxy/d’ ~/.wgetrc
-
- # 清除git代理配置
- git config –global –unset http.proxy
- git config –global –unset https.proxy
-
- # 删除Docker代理配置,并重新加载及重启
- DOCKER_CONF_DIR="/etc/systemd/system/docker.service.d"
- sudo rm -f $DOCKER_CONF_DIR/http-proxy.conf
- sudo systemctl daemon-reload
- sudo systemctl restart docker
-
- echo "清除配置成功! 执行 "source ~/.bashrc " 以应用更改"
复制代码
求助各位前辈,目前这种方法无法清除当前连接终端的shell变量 需要手动执行 unset https_proxy unset http_proxy unset all_proxy 才能清除变量,求助优化脚本,能让用户更傻瓜式操作 |