代理服务之squid+sarg

代理服务器软件

最近有个需求要用到代理服务器,搜索了一番,用得比较多的是windows平台上的ccproxy,不过反应软件不太靠谱,软件经常会崩溃,比较靠谱的是linux平台下的squid。其实还有比较优秀的代理软件如轻量级的tinyproxy,privoxy,还有重量级的vanish和nginx。轻量级的软件不支持缓存,故选择squid。

Varnish与Nginx缓存服务器

1、高性能缓存服务器Varnish
Varnish是一款高性能的、开源的方向代理服务器和缓存器。挪威最大的在线报纸Verdens Gang使用了3台Varnish代替了原来的12台Squid,性能更好!

Varnish与Squid的对比如下:

(1)优点

Varnish具有更好的稳定性、更快的访问速度、更多的并发连接支持数,可以通过管理端口来管理缓存。

(2)缺点

1)在高并发状态下,Varnish消耗更多的CPU、I/O和内存资源。

2)Varnish进程一旦挂起、崩溃或者重启,缓存的数据会从内存中释放,此时所有的请求都会转发到后端服务器,给后端服务器造成很大压力。

2、轻量级缓存服务器Nginx

Nginx支持类似Squid的缓存功能,把URL以及相关信息当成key,用MD5编码Hash后把数据文件保存在硬盘上。

Nginx只能为只当的URL或者状态码设置过期时间,并不支持类似Squid的purge命令来手动清除指定的缓存页面。可以通过第三方的ngx_cache_purge来清除指定的URL缓存。

Nginx的缓存加速功能是由proxy_cache和fastcgi_cache两个功能模块完成的。

Nginx缓存加速的特点如下:
1)缓存功能十分稳点,运行速度不逊于Squid。
2)对多核CPU的利用率比其他的开源软件要好。
3)支持高并发请求数,能同时承受更多的访问请求。

代理基本类型

根据实现的方式不同,squid代理服务可分为传统代理和透明代理两个常见的代理服务。

1)传统代理
也就是普通的代理服务,首先必须在客户机的浏览器、QQ聊天工具、下载软件等程序中手动设置代理服务器的地址和端口,然后才能使用代理服务来访问网络。对于网页浏览器,访问网站时的域名解析请求也会发送给指定的代理服务器。

2)透明代理
提供与传统代理相同的功能和服务,其区别在于客户机不需要指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向,实际上仍然交给代理服务器来处理。重定向的过程对客户机来说时是“透明”的,用户甚至并不知道自己在使用代理服务,所以称为“透明代理”。使用透明代理时,网页浏览器访问网站时的域名解析请求将优先发给DNS服务器。

在实际应用中,传统代理多见于Internet环境,如为QQ程序使用代理可以隐藏本机真实IP地址,为下载工具使用多个代理可以规避服务器的并发连接限制。而透明带多见于局域网环境,如在Linux网关中启用透明代理后,局域网主机无需进行额外的设置就可以享受更好的上网速度。

搭建Squid传统代理服务器

安装

到官网下载最新的squid源码包
解压:
tar zxf squid-5.0.5.tar.gz

安装编译环境:
yum install gcc gcc-c++ make perl-devel -y

编译:
cd squid-5.0.5

./configure \

# 把软件安装到指定位置
--prefix=/app/squid-5.0.5 \

# 可以在规则中设置为直接通过客户端MAC进行管理,防止客户端使用IP欺骗
--enable-arp-acl \

# 使用内核过滤
--enable-linux-netfilter \

# 支持透明模式
--enable-linux-tproxy \

# 异步I/O,提升存储性能
--enable-async-io=100 \

# 错误信息的显示语言
--enable-err-language="Simplify_Chinese" \

# 允许URL中有下划线
--enable-underscore \

# 使用poll()模式,提升性能
--enable-poll \

# 使用GNU正则表达式
--enable-gnuregex

安装:
make && make install

创建squid用户:
useradd -M -s /sbin/nologin squid

修改属主和属组:
chwon -R squid:squid /app/squid-5.0.5

创建软连接:
cd /app

ln -s squid-5.0.5 squid

更改配置文件

vim /app/squid/etc/squid.conf

1
2
3
4
5
6
7
8
9
http_access allow all
http_port 3128
visible_hostname 192.168.1.200 # 指定可见主机名
cache_mem 64 MB
cache_swap_low 80
cache_swap_high 97
cache_dir ufs /app/squid/var/cache/squid 512 16 256 # 配置硬盘缓存,打开缓存目录512M,其中一级目录16个,二级256个
cache_effective_user squid
cache_effective_group squid

检查配置文件:
squid -k parse

重新加载配置文件:
squid -k rec

初始化缓存目录:
squid -zX

启动

添加squid.service:
vim /usr/lib/systemd/system/squid.service

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=squid
Wants=network-online.target
After=network-online.target

[Service]
Type=forking
PIDFile=/app/squid/var/run/squid.pid
ExecStart=/app/squid/sbin/squid

[Install]
WantedBy=multi-user.target

重载systemctl:
systemctl daemon-reload

开启squid:
systemctl start squid

查看监听端口:
ss -tunlp | grep squid

服务器多端口设置

使用docker实现多端口

从官方网站或者第三方docker镜像启动后获取默认squid配置,存放到/software目录下:
docker cp xxxx:/etc/squid/squid.conf /software

xxx为容器ID

导出排除注释的配置:
egrep -v "^#|^$" squid.conf > squid.conf.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
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
http_access allow localhost manager
http_access deny manager
http_access deny to_localhost
http_access allow localnet
http_access allow localhost
http_access allow all
http_port 3128
cache_dir ufs /var/spool/squid 100 16 256
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 (Release|Packages(.gz)*)$ 0 20% 2880
refresh_pattern . 0 20% 4320

注释掉http_access deny !Safe_ports和http_access deny CONNECT !SSL_ports,http_access allow all这样就相当于无限制访问

建立文件夹以存放docker的日志和缓存:
mkdir -p /app/docker-squid/{logs,data,etc}

把配置文件拷贝到etc目录下:
cp /software/squid.conf.1 /app/docker-squid/etc/squid.conf

新建docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
version: '2'
services:
squid-all:
container_name: squid-all
restart: always
image: datadog/squid
ports:
- 3128:3128
volumes:
- /app/docker-squid/logs:/var/log/squid
- /app/docker-squid/data:/var/spool/squid
- /app/docker-squid/etc/squid.conf:/etc/squid/squid.conf

使用的是hub.docker.com上的第三方镜像

使用docker创建容器:
docker-compose -f docker-compose.yml -d up

测试OK

使用dstdomain设置访问网页白名单

配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
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 Safe_ports port 8080 # qyweixin client login
acl CONNECT method CONNECT
acl whitelist dstdomain "/app/squid/etc/whitelist"
http_access deny !whitelist
#http_access deny !Safe_ports
#http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access deny to_localhost
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 3129
cache_mem 64 MB
cache_swap_low 80
cache_swap_high 97
cache_dir ufs /app/squid/var/cache/squid 512 16 256
cache_effective_user squid
cache_effective_group squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
refresh_pattern . 0 20% 4320

增加了:
acl whitelist dstdomain "/app/squid/etc/whitelist" http_access deny !whitelist

通过whitelist文件设置白名单,禁止白名单外的地址访问
whitelist设置的白名单地址可以是全域名地址,也可以是通配:
.qq.com
.jiandaoyun.com
.tbk.cn
.jdycdn.com
.qpic.cn
.qlogo.cn
.就是代表通配

squid日志分析

安装apache

yum install -y httpd

systemctl enable httpd

systemctl start httpd

检查端口:
ss -tunlp | grep httpd

测试:
浏览器访问http://192.168.1.200(服务器地址)

安装sarg

tar zxf sarg-2.4.0.tar.gz

cd sarg-2.4.0

./configure \

--prefix=/app/sarg-2.4.0

enable-extraprotection

编译安装:
make && make install

创建软连接
cd /app

ln -s sarg-2.4.0 sarg

修改配置

vi /app/sarg/etc/sarg/sarg.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
access_log /app/squid/var/logs/access.log  # squid的访问日志位置
title "Squid User Access Reports" # 网页标题
output_dir /var/www/html/squid # 分析报告的存放位置
user_ip no # 不使用IP代替用户ID
topuser_sort_field BYTES reverse # 升序排列
user_sort_field BYTES reverse
exclude_hosts /app/sarg/noreport # 设置不生成报告的主机
overwrite_report no
mail_utility mailx # 指定发邮件命令
charset UTF-8
weekdays 0-6 # 指定top排序星期周期
hours 7-12,14,16,18-20 # 指定top排序时间周期
www_document_root /var/www/html # 网页根目录

建立不生成报告的主机列表文件
touch /app/sarg/noreport

按小时生成squid流量报表

新建一个sh文件:
vim /root/squid-hours.sh

1
2
3
4
5
6
7
8
#!/bin/bash
Date=$(date -d "1 hours ago" +"%y%m%d%H")
/app/squid/sbin/squid -k rotate
sleep 10
Squiddir='/app/squid/var/logs'
Access='access.log.*'
/bin/mv ${Squiddir}/${Access} ${Squiddir}/access${Date}.log
/app/sarg/bin/sarg -o /var/www/html/hours -l ${Squiddir}/access${Date}.log

创建计划任务执行sarg记录,只在上班时间执行。
crontab -e

0 9,10,11,12,13,14,15,16,17,18,19 * * * /root/sarg-hour.sh

验证访问

http://192.168.1.200/squid-reports/

ss5配置

squid只支持http和https类型的代理,某些应用需要用到socks5代理,这时需要配置上ss5

安装环境:
yum install pam-devel openldap-devel openssl-devel

yum -y install gcc automake make pam-devel openldap-devel cyrus-sasl-devel

下载ss5:
wget http://downloads.sourceforge.net/project/ss5/ss5/3.8.9-8/ss5-3.8.9-8.tar.gz

安装:
tar -xzvf ss5-3.8.9-8.tar.gz

cd ss5-3.8.9

./configure

make

make install

配置无用户认证方式
vim /etc/opt/ss5/ss5.conf

1
2
auth    0.0.0.0/0               -               -
permit - 0.0.0.0/0 - 0.0.0.0/0 - - - - -

配置用户认证方式

1
2
3
4
5
auth    0.0.0.0/0               -               u
permit - 0.0.0.0/0 - 0.0.0.0/0 - - - - -

cat /etc/opt/ss5/ss5.passwd
test test

启动服务:
chmod a+x /etc/init.d/ss5

systemctl start ss5

查看端口:
ss -tunlp | grep ss5

分享到