实现squid+iptables+dhcp的透明代理

最近应公司的要求,寻求合适的网络解决方式,解决酒店客房带宽不够用的问题,经过一两个月研究与学习,决定采用squid+iptables+dhcp的方式。Squid不仅免费且带缓存功能,iptables防护功能是目前软件防火墙中最好的。在此写出来与大家分享。此次能配成功透明代理在这里要感谢51CTOlinux群的北京--你大爷及其他的群友。

我的系统平台:red hat enterprise Linux 5

系统一般都已经安装好了squid2.6 iptables  dhcp需自行安装。如都没有安装,安装包可以在光盘的server中找。

squidiptable+dhcpd的具体安装我在此不做太多说明,一般看看它们的说明即可。我公司用一台机器作为代理上网。eth0连接内部网,eth1连接外部网。下面把我的配置写下来。

iptable的配置,在/etc/rc.d/目录下用touch命令建立firewall文件,执行chmod u+x firewll以更改文件属性,编辑/etc/rc.d/rc.local文件,在末尾加上/etc/rc.d/firewall以确保开机时能自动执行该脚本。

modprobe ip_tables

modprobe iptable_nat

modprobe ip_nat_ftp

modprobe ip_conntrack_ftp

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -F

iptables -X

iptables -t nat -F

iptables -t nat -X

iptables -t nat -A PREROUTING -i eth0 -p tcp -s 192.168.1.0/24 --dport 80 -j REDIRECT --to-ports 3128

iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

#iptables -t nat -A PREROUTING -s 59.175.177.118 -p tcp --dport 80 -j ACCEPT

#iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 59.175.177.118

#iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j SNAT --to 59.175.177.118

说明192.168.1.0/24 是内网的网段  59.175.177.118是外网IP地址。

squid的配置

# WELCOME TO SQUID 2.6.STABLE21

http_port 192.168.1.10:3128 transparent

acl all src 0.0.0.0/0.0.0.0

acl manager proto cache_object

acl localhost src 127.0.0.1/255.255.255.255

acl to_localhost dst 127.0.0.0/8

acl SSL_ports port 443          #https

acl Safe_ports port 53          #dns

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

acl mynet src 192.168.1.0/255.255.255.0

http_access allow mynet

http_access deny all

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost

http_access allow all

http_reply_access allow all

icp_access allow all

hierarchy_stoplist cgi-bin ?

cache_mem 512 MB

cache_dir ufs /var/spool/squid 204800 16 256

maximum_object_size 32768 KB

cache_swap_low 75

cache_swap_high 95

access_log /var/log/squid/access.log squid

cache_log /var/log/squid/cache.log squid

cache_store_log /var/log/squid/store.log squid

emulate_httpd_log on

pid_filename /var/run/squid.pid

acl QUERY urlpath_regex cgi-bin \?

cache deny QUERY

refresh_pattern ^ftp:                   1440   20%   10080

refresh_pattern ^gopher:    1440   0%    1440

refresh_pattern .                0        20%   4320

acl apache rep_header Server ^Apache

broken_vary_encoding allow apache

cache_mgr root

cache_effective_user squid

cache_effective_group squid

visible_hostname 192.168.1.10

udp_incoming_address 0.0.0.0

# udp_outgoing_address 255.255.255.255

dns_nameservers 202.103.24.68 202.103.0.68

coredump_dir /var/spool/squid

 

这个酌配置是最后的正确配置

 

 

 

dhcpd的配置:

ddns-update-style interim;

ignore client-updates;

 

subnet 192.168.1.0 netmask 255.255.255.0 {

 

# --- default gateway

   option routers                          192.168.1.10;

   option subnet-mask                  255.255.255.0;

 

   option nis-domain           "domain.org";

   option domain-name                 "domain.org";

   option domain-name-servers      202.103.24.68;

 

   option time-offset            -18000;        # Eastern Standard Time

# option ntp-servers           192.168.1.10;

   option netbios-name-servers       192.168.1.10;

# --- Selects point-to-point node (default is hybrid). Don't change this unless

# -- you understand Netbios very well

# option netbios-node-type 2;

 

   range dynamic-bootp 192.168.1.100 192.168.1.200;

   default-lease-time 21600;

   max-lease-time 43200;

 

   # we want the nameserver to appear at a fixed address

   host ns {

            next-server marvin.redhat.com;

            hardware ethernet 00:E0:B1:0C:22:E4;

            fixed-address 192.168.1.10;

   }

此时透明代理可以实现,把客户端自动获取IP就可以上网了,客户端浏览器不需要设置代理,但测试的结果是客户端上不了,在客户端浏览器上填上代理服务器地址,可以上正常上网。

后来我在代理服务器上按照网上的资料运行setup,重新配置防火墙,在自定义端口那里写了53,然后重启iptables,重新运行自己的firewall脚本问题解决,squid都没有重启,成功!也就是说只是在代理服务器上面增开53端口。因为没有启动bind服务,用nmap扫描服务器并没有发现53端口开放。但运行了不到一个小时故障依旧出现了,此时我就将配置文件发给51CTOlinux群的北京­-你大爷.第二天也就今天我把服务器连接到光纤上,客户端浏览器不设置代理IP,测试近三个小时一切正常。后来将服务器放到办公室就不行了,非要在客户端浏览器设置代理IP,才能上网。反反复复的测试,我总结出一条问题,就是在已有的网络中再做代理很容易出问题,问题出现了也很难找出原因。那试以后大家做代理试验的时候多多用心。就写到这里。

                              

我的51cto账号a316591296  QQ316591296 欢迎与大家一起分享交流。