Iptables+squid 多網段透明代理的實現

  首先,還是將我的測試環境介紹一下:
代理服務器:
OS:RedHat Enterprise LINUX AS 4.0 
Proxy Server Version: squid-2.5.STABLE6-3.4E.5 (系統自帶)
網卡設置
Eth0:192.168.0.251 (連接外網)
Eth1:192.168.1.254 (連接內網)
路由器:(主要是用來連接內網的各個子網間的通信,路由器我是用 LINUX 來做的軟路由

OS: OS:RedHat Enterprise LINUX AS 4.0 
網卡設置:
Eth0:192.168.1.1
Eth1:192.168.2.1
客戶端:
Client-1:
OS: Windows 2000 Professional
網卡設置:
IP address: 192.168.1.111
Netmask: 255.255.255.0
Gateway:192.168.1.1
DNS:61.144.56.100
Client-2
OS:Windows XP Professional
IP address: 192.168.2.120
Netmask: 255.255.255.0
Gateway:192.168.2.1
DNS:61.144.56.100
網絡拓樸:

 

本次測試的目的:
1.實現多個網段的透明代理
2.通過 Squid + Iptables 的透明代理實現在客戶端可以用 OE 收發外網郵箱的郵件
二、具體的設置
代理服務器的配置
1.Squid 的配置
http_port 3128
hierarchy_stoplist cgi-bin ?                           
hierarchy_stoplist -i ^https:\\ ?                           
acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi   
acl denyssl urlpath_regex -i ^https:\\                     
no_cache deny QUERY                                 
no_cache deny denyssl
cache_mem 24 MB
cache_swap_low 90 
cache_swap_high 95 
maximum_object_size 4096 KB
maximum_object_size_in_memory 8 KB 
ipcache_size 1024                                
ipcache_low 90                                  
ipcache_high 95                                
fqdncache_size 1024
cache_dir aufs /usr/local/squid/var/cache1 100 16 256            
cache_dir aufs /usr/local/squid/var/Cache2 200 16 256
access_log /usr/local/squid/var/logs/access.log squid  
cache_log /usr/local/squid/var/logs/cache.log  
cache_store_log /usr/local/squid/var/logs/store.log
pid_filename /usr/local/squid/var/logs/squid.pid
ftp_user Squid@ 
ftp_passive on 
refresh_pattern ^ftp:                1440        20%        10080             

refresh_pattern ^gopher:        1440        0%        1440           
refresh_pattern .                0        20%        4320                 
connect_timeout 1 minute
peer_connect_timeout 30 seconds
request_timeout 2 minutes
persistent_request_timeout 1 minute
cache_mgr webmaster@localhost
cache_effective_user squid
cache_effective_group   squid
visible_hostname redhat
/****************       下面四行是實現透明代理的關鍵之處        *****************\
httpd_accel_host virtual                             
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
2.iptables 的配置
以下是我寫的一個腳本:腳本名爲:firewall(當然你也可以根據自己的喜好設置名稱,用 VI 編輯器編譯器好保存,然後將文件的屬性設置爲可執行,用 chmod 777 firewall (這只是一個示例,可根據實際情況更改)命令更改文件的屬性爲可執行文件)
#!/bin/bash
echo “enable ip forwarding”
#打開路由轉發功能
echo “1” > /proc/sys/net/ipv4/ip_forward
#加載 IPTABLES 所需要的模塊
echo “starting iptables rules”
/sbin/modprobe iptable_fileter
/sbin/modprobe ip_tables
/sbin/modprobe iptables_nat
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
#刷新 IPTABLES 的規則鏈
/sbin/iptables –F
/sbin/iptables –X
/sbin/iptables –Z
/sbin/iptables –F –t nat
/sbin/iptables –X –t nat
/sbin/iptables –Z –t nat
#所有從 eth1 進來的,目標端口爲 80 的流量都轉到代理服務器默認的 3128 端口上
/sbin/iptables –t nat –A PREROUTING –i eth1 –p tcp –dport 80 –j REDIRECT –to-ports 3128
#對出站訪問 IP 地址進行僞裝
/sbin/iptables –t nat –A POSTROUTING –s 192.168.1.0/24 –o eth0 –j MASQUERADE
/sbin/iptables –t nat –A POSTROUTING –s 192.168.2.0/24 –o eth0 –j MASQUERADE
代理服務器的路由設置
#在 squid 服務器上添加一條到 192.168.2.0 網絡上的路由,網關爲 192.168.1.1
route add –net 192.168.2.0/24 gw 192.168.1.1
二、路由器上的設置
/********************* 打開路由轉發功能******************\
# echo 1 > /proc/sys/net/ipv4/ip_forward
路由器原來的默認路由爲 0.0.0.0 0.0.0.0 192.168.1.1,將這條路由刪除,添加另外一條默認路由:
route add default gw 192.168.1.254
至些多網段透明代理的設置就算是完成了,現在到客戶端進行測試,可以正常瀏覽網頁,在客戶設置好 OE 可以收發郵件,我是用  sina 和 21cn 的郵箱測試的,163 的郵箱不知道怎麼回事,收發郵件的時候總是會彈出一個認證框,讓重新輸入密碼,但輸入密碼還是不行,懷疑應該是 163 服務器方面的原因。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章