e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

其实之前配过一个squid,只是由于太懒,网上随便搜了一个教程,用了默认端口并且没有添加用户认证。某天不幸的被爬虫扫到,被用来发了半个月的垃圾邮件。。直到有一天登录邮箱,看到了一大坨警告邮件,才意识到问题的严重。惊了个呆之后,赶紧重配一遍-.-

我这里是用squid配置了一个带用户认证的普通代理。

安装

安装过程十分简便,只需要安装一下squid,一条命令搞定。我这里装的是squid3.3。

yum install squid
  • 1
rpm -qa | grep squid
squid-3.3.8-12.el7_0.x86_64
  • 1
  • 2

配置

修改squid的配置文件 /etc/squid/squid.conf

主要就是配置一下端口,缓存,日志和访问规则。

http_port 3712
cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
http_access allow all
visible_hostname squid.chao
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

初始化

在第一次启动之前或者修改了cache路径之后,需要重新初始化cache目录。

squid -z
  • 1

启动

systemctl start squid
  • 1

使用

在浏览器中修改代理配置即可。

在windows中:

Internet选项 -> 连接 -> 局域网连接 -> 代理服务器

在macOSX中:

Safari -> 偏好设置 -> 代理 -> Web代理

然后输入你的代理地址和端口,就可以正常工作了。

测试

我从网上看到一个非常简单的方法,可以用来快速测试你的代理是否正常工作。首先打开百度,然后搜索ip。如果出来的是你代理的那台机器的ip,那么恭喜你,一大波垃圾邮件即将赶来。

添加用户认证

为了防止我们的代理被爬虫扫到并且被用于不法用途,我们非常有必要为我们的squid添加用户认证。事实上我刚配的代理,没过多久就已然被扫到。

我从我的access.log里面看到。然而此时我还没有添加任何认证机制,幸好我没使用认端口,不然我的邮箱又会收到一大坨告警邮件了。。

1439106533.703      0 89.102.9.196 TCP_DENIED/403 3739 GET http://www2.praguerentacar.com/proxy/detectproxy.php - HIER_NONE/- text/html
1439106539.302      0 89.102.9.196 TCP_DENIED/403 3724 GET http://www2.intimnosti.cz/proxy/detectproxy.php - HIER_NONE/- text/html
1439106544.881      0 89.102.9.196 TCP_DENIED/403 3706 GET http://93.185.96.50/proxy/detectproxy.php - HIER_NONE/- text/html
1439106550.453      0 89.102.9.196 TCP_DENIED/403 3712 GET http://www2.nuabi.com/proxy/detectproxy.php - HIER_NONE/- text/html
  • 1
  • 2
  • 3
  • 4

我们这里通过ncsa认证模块来为我们的squid添加认证。为什么我选择ncsa呢,因为我从网上搜到的大多用了这个方式。。

首先我们得配置我们的访问用户的账户信息。最后一个参数是用户名,可以替换成任何你喜欢的名字~

htpasswd -c /etc/squid/passwd chao
  • 1

如果找不到htpasswd,就先装个Apache。yum install httpd。 然后就能使用htpasswd了。

有了帐户文件之后,我们重新配置我们的squid。在squid.conf里面,把

http_access allow all
  • 1

改成

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 5
auth_param basic realm chao's squid server
auth_param basic credentialsttl 2 hours
acl myacl proxy_auth REQUIRED
http_access allow myacl
http_access deny all
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

最后重启squid就可以啦。

systemctl restart squid
  • 1
  • 2

现在当你使用代理访问页面的时候,会先弹出一个提示框,让你输入用户名密码。然后就可以继续访问了。


配置说明

有些人可能一开始对原理以及配置的详细介绍并不感冒,只是想让代理先跑起来再说。至少我本人就是这样子的。。所以,我把这块放在了最后。

现在,就开始介绍一下上面配置的具体内容。当然参考官网会更加详细准确。

http_port 3712 这个指定了我们代理的端口。

cache_mem 64 MB 内存中的缓存大小

cache_dir ufs /var/spool/squid 100 16 256 缓存文件夹,默认是只在内存中进行缓存的。这里指定缓存大小为100M,第一层子目录为16个,第二层为256。

maximum_object_size 4 MB 最大被缓存文件大小,这个配合上面的cache_dir使用,只作用于缓存到磁盘的文件。

access_log /var/log/squid/access.log 访问日志

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd 指定认证程序以及账户文件

auth_param basic children 5 认证程序同时跑的个数

auth_param basic realm chao’s squid server 客户端在使用代理时,输入密码时弹出来的提示框中的描述文字。

auth_param basic credentialsttl 2 hours 认证的持续时间

acl myacl proxy_auth REQUIRED 对myacl使用外部程序进行认证

http_access allow myacl 允许myacl中的成员访问

http_access deny all 拒绝所有其它访问

visible_hostname squid.chao 代理机名字


附上全自动脚本

一、######### Local shell

# sshpass 免于ssh首次登录远程机器需要交互输入密码的繁琐。 
# sshpass -p SERVER_PASSWD ssh/scp params command
# sshpass :-p 远程服务器密码
# ssh: -o "StrictHostKeyChecking no"   默认准许登录密钥认证
sshpass -p "SERVER_PASSWD" ssh -o "StrictHostKeyChecking no" -p SERVER_SSH_PORT SERVER_USER@SERVER_IP 'curl "http://zhangzhipeng2023.cn/squid/install-squid.sh" | bash'
  • 1
  • 2
  • 3
  • 4
  • 5

二、####### install-squid.sh (remote)

# 1.  静默安装squid, -y:默认yes
yum install -y squid
# 2.  下载并替换本地squid配置文件, -O:文件保存路径
wget -O "/etc/squid/squid.conf" "http://xxx.com/squid/squid.conf"
# 3.  检查参数
squid -k parse
# 4.  初始化缓存
squid -z
# 5.  htpasswd 生成squid 代理 用户名、密码, -c:创建密码文件,-b 使用参数传递设置密码
htpasswd -c -b /usr/lib/squid/passwd PROXY_USER PROXY_PASSWD
# 6.  启动squid服务
service squid restart
# 7.  开机启动
####---
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三、####### squid.conf(remote)

# 
# Recommended minimum configuration:
#
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
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

#
# Recommended minimum Access Permission configuration:
#
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager

# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
# http_access allow all
# 配置:代理账号 密码,授权用户
auth_param basic program /usr/lib/squid/ncsa_auth /usr/lib/squid/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
cache_mem 96 MB
cache_swap_low 90
cache_swap_high 95
cache_dir ufs /tmp/squid 100 16 256
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

# 配置高匿,不允许设置任何多余头信息,保持原请求header。
request_header_access Via deny all
request_header_access X-Forwarded-For deny all
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

转自:http://www.cnblogs.com/riversouther/p/4717720.html

posted on 2018-09-15 19:07  纯黑Se丶  阅读(163)  评论(0编辑  收藏  举报