vps交流

[美国VPS] Linux使用代理


背景需求,国内云服务器,虚拟机能够更方便得连接学术网站,例如GitHub,docker等
方案1 配置proxychains
优点,简单,傻瓜式,在需要代理的命令前加上proxychains即可
方案2 配置透明代理
为了简化流程,我写了个服务单元,并把脚本上传到对象存储,这样就能保证被墙,用户也不需要麻烦去配置
命令行一键配置
bash <(wget -qO- -o- https://e.oss-cn-beijing.aliyuncs.com/SSR/ssr-systemd.sh)
缺点:启动和关闭都需要执行source ~/.bashrc

云端脚本内容

  1. #!/bin/bash
  2. cat <<EOF | sudo tee /etc/systemd/system/proxy.service
  3. [Unit]
  4. Description=Enable/Disable Global Proxy
  5. After=network-online.target
  6. [Service]
  7. Type=oneshot
  8. RemainAfterExit=true
  9. ExecStartPre=/bin/bash -c ‘echo "启动成功,执行source ~/.bashrc以立即应用加速!"’
  10. ExecStart=/bin/bash -c ‘curl -sSL https://e.oss-cn-beijing.aliyuncs.com/SSR/enable_proxy.sh | bash’
  11. ExecStop=/bin/bash -c ‘curl -sSL https://e.oss-cn-beijing.aliyuncs.com/SSR/disable_proxy.sh | bash’
  12. ExecStopPost=/bin/bash -c ‘echo "停止成功,执行source ~/.bashrc以立即停止应用加速!"’
  13. [Install]
  14. WantedBy=multi-user.target
  15. EOF
  16. systemctl daemon-reload
  17. echo "安装服务成功!"
  18. echo "执行 ‘systemctl start proxy’ 启动全局代理…"
  19. echo "执行 ‘systemctl stop proxy’ 停止全局代理…"
  20. echo "执行 ‘systemctl status proxy’ 查看服务状态…"

复制代码

启动脚本内容:

  1. #!/bin/bash
  2. # 代理服务器配置
  3. PROXY_URL="socks5://127.0.0.1:1080"
  4. # 设置环境变量
  5. echo "Setting environment variables…"
  6. export http_proxy=$PROXY_URL
  7. export https_proxy=$PROXY_URL
  8. export all_proxy=$PROXY_URL
  9. # 将代理环境变量添加到用户的 shell 配置文件
  10. echo "export http_proxy=$PROXY_URL" >> ~/.bashrc
  11. echo "export https_proxy=$PROXY_URL" >> ~/.bashrc
  12. echo "export all_proxy=$PROXY_URL" >> ~/.bashrc
  13. # 配置apt使用代理
  14. echo "Acquire::http::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
  15. echo "Acquire::https::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
  16. echo "Acquire::socks::Proxy "$PROXY_URL";" >>/etc/apt/apt.conf.d/proxy.conf
  17. # 配置wget, git, docker代理
  18. # wget
  19. echo "Configuring wget…"
  20. echo "use_proxy = on" >> ~/.wgetrc
  21. echo "http_proxy = $PROXY_URL" >> ~/.wgetrc
  22. echo "https_proxy = $PROXY_URL" >> ~/.wgetrc
  23. # git
  24. echo "Configuring git…"
  25. git config –global http.proxy $PROXY_URL
  26. git config –global https.proxy $PROXY_URL
  27. # Docker
  28. DOCKER_CONF_DIR="/etc/systemd/system/docker.service.d"
  29. mkdir -p $DOCKER_CONF_DIR
  30. echo "[Service]
  31. Environment="HTTP_PROXY=$PROXY_URL"
  32. Environment="HTTPS_PROXY=$PROXY_URL"
  33. " | tee $DOCKER_CONF_DIR/http-proxy.conf
  34. # 重新加载并重启Docker服务
  35. systemctl daemon-reload
  36. systemctl restart docker
  37. echo "配置全局加速成功,执行source ~/.bashrc以应用加速!"

复制代码

停止脚本内容:

  1. #!/bin/bash
  2. # 清除环境变量
  3. echo "正在清除环境变量…"
  4. unset http_proxy
  5. unset https_proxy
  6. unset all_proxy
  7. # 从用户的 shell 配置文件中移除代理环境变量
  8. sed -i ‘/http_proxy/d’ ~/.bashrc
  9. sed -i ‘/https_proxy/d’ ~/.bashrc
  10. sed -i ‘/all_proxy/d’ ~/.bashrc
  11. echo "正在清除应用配置…"
  12. # 清除wget代理配置
  13. sed -i ‘/use_proxy = on/d’ ~/.wgetrc
  14. sed -i ‘/http_proxy/d’ ~/.wgetrc
  15. sed -i ‘/https_proxy/d’ ~/.wgetrc
  16. # 清除git代理配置
  17. git config –global –unset http.proxy
  18. git config –global –unset https.proxy
  19. # 删除Docker代理配置,并重新加载及重启
  20. DOCKER_CONF_DIR="/etc/systemd/system/docker.service.d"
  21. sudo rm -f $DOCKER_CONF_DIR/http-proxy.conf
  22. sudo systemctl daemon-reload
  23. sudo systemctl restart docker
  24. echo "清除配置成功! 执行 "source ~/.bashrc " 以应用更改"

复制代码

求助各位前辈,目前这种方法无法清除当前连接终端的shell变量
需要手动执行
unset https_proxy
unset http_proxy
unset all_proxy
才能清除变量,求助优化脚本,能让用户更傻瓜式操作

这谁啊,还用oss呢

HOH 发表于 2024-8-9 17:06
这谁啊,还用oss呢

方便,systemctld可以直接读取oss的启动脚本

很赞!
已学习