Squid代理:APT、PyPI和Docker的内网穿透解决方案

4 篇文章 0 订阅
2 篇文章 0 订阅

如果你是在内网环境,并且你知道一台服务器可以链接外网,可以通过Squid代理的方式更新apt、pypi、docker源。

你可以通过在服务器A(172.16.16.122,可上外网)上设置代理服务器来实现服务器B通过服务器A访问外部APT源的需求。以下是具体步骤:

内网环境下

1.在可以访问外网的服务器A上

  1. 安装代理服务

    假设你使用的是Squid作为代理服务器,因为它是Linux下常用的代理服务器软件之一。

    在服务器A上安装Squid:

    sudo apt update
    sudo apt install squid
    
  2. 配置Squid代理服务器

    编辑Squid的配置文件/etc/squid/squid.conf,使其允许服务器B的访问请求:

    sudo vim /etc/squid/squid.conf
    

    在文件中添加或修改以下行:

    http_access allow all
    

    这将允许所有的HTTP请求通过代理,你也可以设置更细粒度的访问控制。

    保存并关闭文件后,重启Squid服务以应用新的配置:

    sudo systemctl restart squid
    

2.在其他无法访问外网的B服务器下

对于APT:

你已经得到了关于如何通过代理服务器进行APT更新的说明,这里我再概述一遍:

  1. 在服务器A上安装并配置好Squid代理服务器。

  2. 在服务器B上配置APT代理,创建或编辑/etc/apt/apt.conf.d/02proxy文件,并添加:

    Acquire::http::Proxy "http://172.16.16.122:3128";
    Acquire::https::Proxy "https://172.16.16.122:3128";
    

注意,HTTPS代理可能需要Squid进行额外的SSL配置。

对于PyPI:

对于Python包管理器,你需要配置pip以使用代理。在服务器B上,你可以为pip添加代理设置:

  1. 在用户主目录下创建或编辑.pip/pip.conf(对于全局配置则是/etc/pip.conf),添加:

    [global]
    proxy = http://172.16.16.122:3128
    

    如果你需要通过HTTPS使用代理,确保Squid配置支持SSL,并使用相应的https://代理地址。

对于Docker:

对于Docker,需要在服务器B上配置Docker守护程序以使用HTTP/HTTPS代理:

  1. 创建或编辑/etc/systemd/system/docker.service.d/http-proxy.conf文件,添加:

    [Service]
    Environment="HTTP_PROXY=http://172.16.16.122:3128/"
    Environment="HTTPS_PROXY=http://172.16.16.122:3128/"
    Environment="NO_PROXY=localhost,127.0.0.1"
    
  2. 重新加载Daemon配置,重启Docker服务:

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

这样配置后,服务器B的Docker守护程序会通过服务器A的Squid代理进行外网访问。把这个几个配置给我封装成一个自动化执行的shell脚本。

配置封装成一个自动化执行的shell脚本proxy_setup.sh

#!/bin/bash

# 变量定义
TODAY=$(date +%Y-%m-%d)
PROXY_SERVER_IP="172.16.16.122"
PROXY_PORT="3128"
SQUID_CONFIG_FILE="/etc/squid/squid.conf"
DOCKER_PROXY_CONFIG_FILE="/etc/systemd/system/docker.service.d/http-proxy.conf"
APT_PROXY_CONFIG_FILE="/etc/apt/apt.conf.d/02proxy"
PIP_CONFIG_FILE="/etc/pip.conf"

# 打印帮助信息
print_help() {
    echo "Usage:"
    echo "./proxy_setup.sh Squid    # 安装并配置Squid"
    echo "./proxy_setup.sh apt      # 配置APT代理"
    echo "./proxy_setup.sh pip      # 配置Pip代理"
    echo "./proxy_setup.sh docker   # 配置Docker代理"
    echo "./proxy_setup.sh all      # 配置APT、Pip和Docker代理"
    exit 0
}

# 备份并清空配置文件的函数
backup_and_clear_config() {
    local config_file=$1
    if [[ -s $config_file ]]; then
        local backup_file="${config_file}.${TODAY}.bak"
        echo "Backing up and clearing $config_file to $backup_file"
        sudo cp $config_file $backup_file
        echo '' | sudo tee $config_file > /dev/null
    fi
}

# 安装Squid代理服务器
function install_squid_on_server_a() {
    echo "Installing and configuring Squid..."
    sudo apt update
    sudo apt install -y squid
}

# 配置Squid代理服务器
function configure_squid() {
    backup_and_clear_config ${SQUID_CONFIG_FILE}
    echo "http_access allow all" | sudo tee -a ${SQUID_CONFIG_FILE}
    sudo systemctl restart squid
}

# 在服务器B上配置APT代理
function configure_apt_proxy() {
    backup_and_clear_config ${APT_PROXY_CONFIG_FILE}
    echo "Acquire::http::Proxy \"http://${PROXY_SERVER_IP}:${PROXY_PORT}\";" | sudo tee ${APT_PROXY_CONFIG_FILE}
    echo "Acquire::https::Proxy \"http://${PROXY_SERVER_IP}:${PROXY_PORT}\";" | sudo tee -a ${APT_PROXY_CONFIG_FILE}
}

# 在服务器B上配置Pip代理
function configure_pip_proxy() {
    local pip_dir=$(dirname ${PIP_CONFIG_FILE})
    mkdir -p ${pip_dir}
    backup_and_clear_config ${PIP_CONFIG_FILE}
    echo "[global]" | sudo tee ${PIP_CONFIG_FILE}
    echo "proxy = http://${PROXY_SERVER_IP}:${PROXY_PORT}" | sudo tee -a ${PIP_CONFIG_FILE}
}

# 在服务器B上配置Docker代理
function configure_docker_proxy() {
    local docker_dir=$(dirname ${DOCKER_PROXY_CONFIG_FILE})
    mkdir -p ${docker_dir}
    backup_and_clear_config ${DOCKER_PROXY_CONFIG_FILE}
    echo "[Service]" | sudo tee ${DOCKER_PROXY_CONFIG_FILE}
    echo "Environment=\"HTTP_PROXY=http://${PROXY_SERVER_IP}:${PROXY_PORT}/\"" | sudo tee -a ${DOCKER_PROXY_CONFIG_FILE}
    echo "Environment=\"HTTPS_PROXY=http://${PROXY_SERVER_IP}:${PROXY_PORT}/\"" | sudo tee -a ${DOCKER_PROXY_CONFIG_FILE}
    echo "Environment=\"NO_PROXY=localhost,127.0.0.1\"" | sudo tee -a ${DOCKER_PROXY_CONFIG_FILE}

    sudo systemctl daemon-reload
    sudo systemctl restart docker
}

# 检查传入参数并执行对应的函数
case "$1" in
    Squid)
        install_squid_on_server_a
        configure_squid
        ;;
    apt)
        configure_apt_proxy
        ;;
    pip)
        configure_pip_proxy
        ;;
    docker)
        configure_docker_proxy
        ;;
    all)
        configure_apt_proxy
        configure_pip_proxy
        configure_docker_proxy
        ;;
    -h|--help)
        print_help
        ;;
    *)
        echo "Invalid option: $1"
        echo "Use -h or --help to get the usage information."
        exit 1
        ;;
esac

echo "Proxy setup is complete."

将此脚本保存为文件后(例如 proxy_setup.sh),您可以传递参数来指定要执行的动作。例如:

chmod +x proxy_setup.sh
sudo ./proxy_setup.sh Squid    # 安装并配置Squid 
sudo ./proxy_setup.sh apt      # 配置APT代理
sudo ./proxy_setup.sh pip      # 配置Pip代理
sudo ./proxy_setup.sh docker   # 配置Docker代理
sudo ./proxy_setup.sh all      # 配置APT、Pip和Docker代理
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星谐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值