如何使用docker-compose搭建Squid服务

前言

dockerdocker-compose环境的安装就不再赘述了,随便搜索一下就一大堆。
这边简单给个Centos7.9下的Docker环境部署脚本提供参考。

#!/bin/bash
echo "Installing Docker ..."
echo "Install Docker Running~~~"
sh <(curl -fsSL https://get.docker.com)
echo "Installing Docker-Compose ..."
echo "Download Docker-compose ..."

yum install -y epel-release git curl python-pip
# 获取最新版本号
latest_version=$(curl -Ls https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
# 拼接下载链接
download_url="https://github.com/docker/compose/releases/download/${latest_version}/docker-compose-$(uname -s)-$(uname -m)"
# 下载执行文件
curl -L $download_url -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
clear
docker-compose --version
echo "Docker-conpose Install Success!"

开始教程

1.因为basic认证需要使用htpasswd创建账号密码,所以要先安装对应工具

    `yum install -y httpd-tools || apt-get install -y httpd-tools || apk add apache2-utils`

2.先创建并进入对应目录

mkdir squid && cd squid

3.创建Dockerfile文件

# 生成 Dockerfile
cat <<EOF > Dockerfile
FROM alpine:latest as run
RUN apk --no-cache add squid apache2-utils curl
RUN mkdir -p /var/spool/squid && squid -z
COPY squid.conf /etc/squid/squid.conf
EXPOSE 3128
CMD ["squid", "-N"]
EOF

4.配置网站黑名单

# 配置网站黑名单
echo "" >> blocked_sites

5.创建squid的配置文件 squid.conf

cat <<EOF > squid.conf
# Squid 基本配置
http_port 3128                 # 监听端口号为 3128
cache deny all                 # 禁止缓存
forwarded_for delete           # 删除请求中的 "X-Forwarded-For" 头部信息
request_header_access X-Forwarded-For deny all  # 拒绝所有请求中的 "X-Forwarded-For" 头部信息
request_header_access Via deny all              # 拒绝所有请求中的 "Via" 头部信息
request_header_access Forwarded-For deny all   # 拒绝所有请求中的 "Forwarded-For" 头部信息
request_header_access Server deny all          # 拒绝所有请求中的 "Server" 头部信息
request_header_access Link deny all            # 拒绝所有请求中的 "Link" 头部信息
request_header_access X-Request-ID deny all    # 拒绝所有请求中的 "X-Request-ID" 头部信息
visible_hostname squid                        # 设置可见主机名为 "squid"
coredump_dir /var/cache/squid                 # 设置核心转储目录
access_log /dev/stdout                        # 设置访问日志输出到标准输出
cache_store_log none                          # 禁用缓存存储日志
pid_filename /var/run/squid/squid.pid        # 设置 PID 文件路径
dns_nameservers 8.8.8.8 8.8.4.4              # 配置 DNS 服务器

# 安全端口配置
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

acl blocked_sites dstdomain /etc/squid/blocked_sites  # 设置黑名单 填入文件中的网址不可访问,一行一个网址
http_access deny blocked_sites # 拒绝黑名单访问

# 基本认证配置 默认从passwd内读取认证信息
# 选项1 从passwd文件读取认证信息
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd  # 设置 basic 认证程序

# 选项2 直接使用配置内的认证信息
# auth_param basic program /usr/lib/squid/basic_ncsa_auth  # 设置 basic 认证程序
# auth_param basic user "$USERNAME" password "$PASSWORD"

auth_param basic children 5                                                # 启动 5 个 basic 认证子进程
auth_param basic credentialsttl 2 hours                                   # 设置凭证有效期为 2 小时
# auth_param basic realm Squid Basic Authentication                        # 设置基本认证域
auth_param basic realm proxy                                             # 设置基本认证域
auth_param basic casesensitive off                                      # 设置用户名不区分大小写

acl localip1 localip $IP
tcp_outgoing_address $IP localip1
acl auth_users1 proxy_auth REQUIRED
http_access allow auth_users1 localip1

#-------------------------------------#

# acl访问控制策略
http_access allow localhost manager
http_access deny manager
http_access allow localhost
http_access deny all
# 缓存配置

coredump_dir /var/spool/squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern \/(Packages|Sources)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern \/Release(|\.gpg)$ 0 0% 0 refresh-ims
refresh_pattern \/InRelease$ 0 0% 0 refresh-ims
refresh_pattern \/(Translation-.*)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern .               0       20%     4320
logfile_rotate 0

6.当前目录下生成账号密码文件passwd

htpasswd -bc passwd $USERNAME $PASSWORD  # 创建账号密码 $USERNAME  $PASSWORD 是你的账号和密码

多个账号密码执行多次,但是我没有测试过多账户。我只试过2个IP用同一个账号密码

  1. 创建docker-compose.yml文件
# 生成 docker-compose.yml 文件
cat <<EOF > docker-compose.yml
version: '3'
services:
  squid:
    build:
      context: .
      target: run
    container_name: squid
    image: squid-proxy:latest
    ports:
      - "$PORT:3128"
    volumes:
      - ./squid.conf:/etc/squid/squid.conf
      - ./passwd:/etc/squid/passwd
      - ./blocked_sites:/etc/squid/blocked_sites
    command: sh -c "squid && tail -f /dev/null"
    tty: true
    restart: unless-stopped
    network_mode: host
EOF

8.构建镜像,启动服务

# 构建镜像
docker-compose build
# 启动容器
docker-compose up -d

9.测试

curl -v -x "http://$USERNAME:$PASSWORD@$IP:$PORT" -L "https://icanhazip.com/"

结束

说说遇到的问题:

  1. alpine镜像命令行阻塞问题:
    开始测试的时候,镜像模板选ubuntu的时候运行的好好的,后面为了减小镜像大小换成alpine发现docker-compose up -d 后容器不起来,一直处于Restarting。 后面测试发现是docker-compose运行alpine时没有阻塞住,运行完直接结束退出了,后面把启动命令从 “squid -N”换成了sh -c “squid && tail -f /dev/null” 阻塞住就好了。
  2. docker网卡模式问题:
    前面测试squid服务的时候eth0网卡配置的是dhcp的单IP,squid容器服务是正常的;后面给eth0网卡加了几个副IP以后重新生成配置运行就不正常了。tcpping几个副IP的端口一个都不通。经过排查发现是docker默认网卡模式的问题。默认的是桥接。没办法正常使用网卡的多个IP。后面直接把网卡模式改成了host仅主机模式就通了。

最后的最后
嗯,测试过单网卡多副IP是可以用的
给出一键配置脚本,请自行调整后使用。

#!/bin/bash

# 定义变量
USERNAME=squid
PASSWORD=Whoami
PORT=3128
WORKSPACE=squid
NETWORK_DEVICE=eth0

# 创建工作空间目录
mkdir $WORKSPACE
cd $WORKSPACE || exit

# 安装对应工具
yum install -y httpd-tools || apt-get install -y httpd-tools || apk add apache2-utils

# 生成 Dockerfile
cat <<EOF > Dockerfile
FROM alpine:latest as run
RUN apk --no-cache add squid apache2-utils curl
RUN mkdir -p /var/spool/squid && squid -z
COPY squid.conf /etc/squid/squid.conf
EXPOSE 3128
CMD ["squid", "-N"]
EOF

# 配置网站黑名单
echo "" >> blocked_sites

# 生成 squid.conf 文件
# 生成前半部分的配置
cat <<EOF > squid.conf
# Squid 基本配置
http_port 3128                 # 监听端口号为 3128
cache deny all                 # 禁止缓存
forwarded_for delete           # 删除请求中的 "X-Forwarded-For" 头部信息
request_header_access X-Forwarded-For deny all  # 拒绝所有请求中的 "X-Forwarded-For" 头部信息
request_header_access Via deny all              # 拒绝所有请求中的 "Via" 头部信息
request_header_access Forwarded-For deny all   # 拒绝所有请求中的 "Forwarded-For" 头部信息
request_header_access Server deny all          # 拒绝所有请求中的 "Server" 头部信息
request_header_access Link deny all            # 拒绝所有请求中的 "Link" 头部信息
request_header_access X-Request-ID deny all    # 拒绝所有请求中的 "X-Request-ID" 头部信息
visible_hostname squid                        # 设置可见主机名为 "squid"
coredump_dir /var/cache/squid                 # 设置核心转储目录
access_log /dev/stdout                        # 设置访问日志输出到标准输出
cache_store_log none                          # 禁用缓存存储日志
pid_filename /var/run/squid/squid.pid        # 设置 PID 文件路径
dns_nameservers 8.8.8.8 8.8.4.4              # 配置 DNS 服务器

# 安全端口配置
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

acl blocked_sites dstdomain /etc/squid/blocked_sites  # 设置黑名单 填入文件中的网址不可访问,一行一个网址
http_access deny blocked_sites # 拒绝黑名单访问

# 基本认证配置 默认从passwd内读取认证信息
# 选项1 从passwd文件读取认证信息
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd  # 设置 basic 认证程序

# 选项2 直接使用配置内的认证信息
# auth_param basic program /usr/lib/squid/basic_ncsa_auth  # 设置 basic 认证程序
# auth_param basic user "$USERNAME" password "$PASSWORD"

auth_param basic children 5                                                # 启动 5 个 basic 认证子进程
auth_param basic credentialsttl 2 hours                                   # 设置凭证有效期为 2 小时
# auth_param basic realm Squid Basic Authentication                        # 设置基本认证域
auth_param basic realm proxy                                             # 设置基本认证域
auth_param basic casesensitive off                                      # 设置用户名不区分大小写
#-------------------------------------#
EOF

# 生成 代{过}{滤}理ip部分的配置
# 根据 IP 列表添加代{过}{滤}理配置并使用htpasswd设置账号密码
# 使用htpasswd 和直接将账号密码写入的区别在于htpasswd生成的密码是加密的
# 获取网卡 eth0 的所有 IP 地址
IP_LIST=$(ip addr show dev $NETWORK_DEVICE | grep "inet " | awk '{print $2}' | cut -d '/' -f 1)
IP_COUNT=$(ip addr show dev $NETWORK_DEVICE | grep "inet " | awk '{print $2}' | wc -l)
echo "" >> squid.conf
echo "# 代{过}{滤}理ip配置" >> squid.conf
i=1
for IP in $IP_LIST; do
    echo "acl localip$i localip $IP" >> squid.conf
    echo "tcp_outgoing_address $IP localip$i" >> squid.conf
    echo "# 创建一个代{过}{滤}理身份验证 ACL" >> squid.conf
    echo "acl auth_users$i proxy_auth REQUIRED" >> squid.conf
    echo "# 允许身份验证后的访问" >> squid.conf
    echo "http_access allow auth_users$i localip$i" >> squid.conf
    echo "" >> squid.conf
    htpasswd -b -c passwd $USERNAME $PASSWORD  # 创建账号密码
    ((i++))
done

# 生成后半部分squid配置
cat <<EOF >> squid.conf

# 代{过}{滤}理服务器配置
#cache_peer proxy1.example.com parent 3128 0 no-query   # 添加代{过}{滤}理服务器 1
#cache_peer proxy2.example.com parent 3128 0 no-query   # 添加代{过}{滤}理服务器 2
#-------------------------------------#

# acl访问控制策略
http_access allow localhost manager
http_access deny manager
http_access allow localhost
http_access deny all
# 缓存配置

coredump_dir /var/spool/squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern \/(Packages|Sources)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern \/Release(|\.gpg)$ 0 0% 0 refresh-ims
refresh_pattern \/InRelease$ 0 0% 0 refresh-ims
refresh_pattern \/(Translation-.*)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
refresh_pattern .               0       20%     4320
logfile_rotate 0
EOF

# 生成 docker-compose.yml 文件
cat <<EOF > docker-compose.yml
version: '3'
services:
  squid:
    build:
      context: .
      target: run
    container_name: squid
    image: squid-proxy:latest
    ports:
      - "$PORT:3128"
    volumes:
      - ./squid.conf:/etc/squid/squid.conf
      - ./passwd:/etc/squid/passwd
      - ./blocked_sites:/etc/squid/blocked_sites
    command: sh -c "squid && tail -f /dev/null"
    tty: true
    restart: unless-stopped
    network_mode: host
EOF

clear
# 构建镜像
docker-compose build
docker-compose up -d
echo "一键生成配置完成!配置代{过}{滤}理ip $IP_COUNT个"
echo "使用 docker-compose up -d 启动并运行服务"
echo "启动服务中"
echo "使用 docker-compose down 停止服务"
echo "代{过}{滤}理ip测试结果存储在success和error中"
sleep 1
for IP in $IP_LIST; do
    curl -v -x "http://$USERNAME:$PASSWORD@$IP:$PORT" -L "https://icanhazip.com/" 1>>success 2>>error
done
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞9赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容