文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>squid 2.6 + iptables 实现透明代理与DNS转发

squid 2.6 + iptables 实现透明代理与DNS转发

时间:2007-03-09  来源:xiaoman

squid 2.6 + iptables 实现透明代理

    系统:
        CentOS 4.4

    软件:
        squid-2.6.STABLE10 + iptables(CentOS自带安装,rpm包)
 
    安装squid:
        ./configure --prefix=/path/to/squid    --enable-arp-acl                                  --enable-linux-netfilter
        make all
        make install

    编辑配置文件:
        http_port 192.168.1.1:3128
        在
            acl Safe_ports port 777         # multiling http
            acl CONNECT method CONNECT
        后加入以下设置指令:
            #------------------------------------#
            #          Host Definition           #
            #------------------------------------#
            acl myhost src 192.168.1.0/24
       

        在
            http_access deny CONNECT !SSL_ports
        后加入以下设置指令:
            http_access allow myhost
        如果要加入一些限制一定刻把这些限制加在以上语句的前面。。。。


        在文件最后加入以下设置指令:
            #----------------------------------#
            #         Custom Settings          #
            #----------------------------------#
            connect_timeout 2 minutes
            visible_hostname hostname(你的主机名称)
            cache_effective_user proxy-user
            cache_store_log none
                                                                                                                                                                 


    添加缓存目录所有者,并设置好权限:
    追加用户:
        adduser user-proxy -d /dev/null -s /sbin/nologin
        chown user-proxy:user-proxy -R /path/to/squid/var

    squid -z 创建好缓存目录


       第一次最好以以下命令起动,看squid是否能够正常工作:
       /path/to/squid/sbin/squid -N -d1
        看是否有错。。。。


    测试:
        telnet  host-ip   squid-port(3128)  or netstat -an | grep 3128
        如果配置参数里指定:http_port host-ip:squid-port 那么在本机telnet时,host-ip一定不能为localhost,因为squid并没有监听此地址,它只监听http_port中指定的地址(如果有)。   
        squidclient -h host-ip http://www.squid-cache.org,
        如果成功则会检索网页内容如果测试成功则按Ctrl+c中止squid

   
    正常起动,关闭, 重新配置squid的命令:
        起动:
            squid -sD    会在后台运行,如果有-s参数,可squid会将日志发送给系统syslogd,
            所以要在/etc/syslog.conf中加入local4.*        /var/squid.log以让syslogd接收squid的日志
        关闭:
            squid -k shutdown  or  squid -k interrupt  
            or 
            kill -TERM `head /path/to/squid.pid`
        重新配置:
            squid -k reconfigure


    创建随系统启动脚本:
        vi /etc/init.d/squid
        #!/bin/bash
        #
        # squid Web proxy
        #
        # chkconfig: 2345 86 14
        # description: Squid is a Web proxy daemon.
        #

        start() {
                /var/squid/sbin/squid -sD > /dev/null 2>&1
                echo "Starting Squid..."
        }

        stop() {
                /var/squid/sbin/squid -k shutdown >/dev/null 2>&1
                echo "Stop Squid..."

        }

        case "$1" in
                start)
                        start
                        ;;
                stop)
                        stop
                        ;;
                restart)
                        stop
                        start
                        ;;
                *)
                        echo "Usage:`basename $0` {start|stop|restart}"
                        exit 1
        esac

       
       chmod +x /etc/init.d/squid
       chkconfig --add squid
       chkconfig squid on
       service squid start
        或者:
        ln -s /etc/init.d/squid /etc/rc2.d/S86squid
        ln -s /etc/init.d/squid /etc/rc3.d/S86squid
        ln -s /etc/init.d/squid /etc/rc4.d/S86squid
        ln -s /etc/init.d/squid /etc/rc5.d/S86squid

        ln -s /etc/init.d/squid /etc/rc0.d/K14squid
        ln -s /etc/init.d/squid /etc/rc6.d/K14squid


    squid+iptables(单网卡)作透明代理,(etho==192.168.1.1能上网,并以代理服务器的地址作为客户端的DNS,这样就只要更改服务器的设置就能改变DNS设置:
    squid:
        修改
            http_port 192.168.1.1:3128 -> http_port 192.168.1.1:3128 transparent

        在文件最后的设置指令中加入:
            always_direct allow all
       
    iptables与CentOS:
        echo 1 > /proc/sys/net/ipv4/ip_forward    //加入路由转发
        可以修改:
            vi /etc/sysctl.conf
            net.ipv4.ip_forward = 0 ->  net.ipv4.ip_forward = 1 //系统起动时自动设置为1

        在iptables规则链最前面加入规则:
            *nat
            :PREROUTING ACCEPT [54:7159]
            :POSTROUTING ACCEPT [1:100]
            :OUTPUT ACCEPT [1:100]
            -A PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
            -A PREROUTING -i eth0 -s 192.168.1.0/24 -p udp --dport 53 -j DNAT --to dns-ip-address:53
            -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE    
            or   
            -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 192.168.1.1
            COMMIT

        也可使用以下命令:
            iptables -t nat -A PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128   //只对内网用户作代理转发,外网可以连接到此代理机上web服务的80号端口
            iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  
            or
            iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 192.168.1.1
            当使用ppp0等接口地址动态变更时使用MASQUERADE,使用静态地址作为伪装时最好使用SNAT,这样性能要好一点。。。
       
        在CentOS,redhat系统中可以执行以下命令:
            /etc/init.d/iptables save    //将以上两条规则保存到防火墙规则链中,再重起:service iptables restart
        其它系统可以使用:
            iptables-save,iptables-restore保存与恢复规则链

        DNS转发:
        在iptables规则链中加入规则:
            -A PREROUTING -i eth0 -s 192.168.1.0/24 -p udp --dport 53 -j DNAT --to dns-ip-address:53  //要加入到上面两条规则链中

        or
       
            iptables -t filter -A PREROUTING -i eth0 -s 192.168.1.0/24 -p udp --dport 53 -j DNAT --to dns-ip-address:53
            iptables -t filter -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MSQUERADE
            /etc/init.d/iptables save

        iptables 要开放 dns端口 ,否则客户端不能执行DNS解析,因为被iptables给干掉了,在做透明 代理时要注意,其中装有squid+iptables的linux机器一定要注意防火墙的设置,要开放dns(53),(squid)3128端口, 如下(我的CentOS的规则链摘下来的,etc/sysconfig/iptables):
            -A RH-Firewall-1-INPUT -s 192.168.1.0/24 -p tcp -m state --state      NEW -m tcp --dport 3128 -j ACCEPT
            -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport      53 -j ACCEPT
            -A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport      53 -j ACCEPT

        or
            iptables -t filter -A input -s 192.168.1.0/24 -p tcp -m state --state NEW -m tcp --dport 3128 -j ACCEPT
            iptables -t filter -A input -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT
            iptables -t filter -A input -p udp -m state --state NEW -m udp --dport 53 -j ACCEPT
            /etc/init.d/iptables save           
           
           
    重起squid,iptables:
        /etc/init.d/squid restart   //netstat -an | grep 3128查看是否启动,如果不能成功起动则可以直接使用/path/to/squid起动。。。。:)       
        service iptables restart     or        /etc/init.d/iptables restart


    开放某个应用(如:skype):
        acl skype dstdom_regex [0-9]$  //匹配所有带有数字的地址              
        ...
        http_access allow skype //将此规则最好放到自定义规则的最前面。。。。
   

    常规设置:
    acl myacl src  "/path/to/acl/filename"  可以将长的源地址放置在文件里,每个地址一行
    acl myacl2 arp  "/path/to/acl/filename" 放置网卡的物理地址到一个文件里,每个地址一行


        .
        .
        .
        .


    注意:
      1.当更改过配置文件后最好验证一下配置文件:squid -k parse
      2.记得创建缓存目录,而且要设置好 var的目录权限为cache_effective_user参数中所指定的用户
      3.如果有防火墙则需要开放3128端口
      4.注意squid的规则匹配,有点奇怪。。。。好像是规则组合。。。把所有已匹配的规则组合起来。
      5.出于安全问题iptables 不能像转发80端口那样转发443到3128
     


相关阅读 更多 +
排行榜 更多 +
拔草大作战

拔草大作战

休闲益智 下载
依拉英语

依拉英语

学习教育 下载
自然灾难模拟器

自然灾难模拟器

角色扮演 下载