Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> 更多Linux

Squid + iptables:打造零設定上網的透明代理

透明代理的價值在於「使用者不必改任何設定」。瀏覽器裡不用填代理位址,網路流量卻已經被 Squid 攔下、快取、加速。本文以一台雙網卡 Linux 主機為例,把 Squid 的編譯安裝與 iptables 的轉發規則完整串起來。

一、環境與軟體

硬體:一台普通相容主機,兩張網卡(eth0 對外、eth1 對內)。
軟體:Linux 7.1 搭配 squid-2.3.STABLE4 原始碼。

二、編譯與安裝 Squid

官方下載點為 https://www.squid-cache.org。取得原始碼後依序執行:

[root@www root]# tar xzvf squid-2.3.STABLE4-src.tar.gz
[root@www root]# cd squid-2.3.STABLE4
[root@www squid-2.3.STABLE4]# ./configure --prefix=/usr/local/squid --enable-ipf-transparent
[root@www squid-2.3.STABLE4]# make all
[root@www squid-2.3.STABLE4]# make install

其中 --prefix 決定安裝位置,--enable-ipf-transparent 是透明代理的關鍵開關,少了它後面的重導向就不會生效。跑完這五步,程式會落在 /usr/local/squid。

三、建立快取目錄與調整擁有者

[root@www squid-2.3.STABLE4]# cd /usr/local/squid
[root@www squid]# mkdir cache
[root@www squid]# cd ..
[root@www local]# chown nobody.nobody -R squid

四、初始化 swap 目錄

切換成 nobody 身分,用 -z 建立交換目錄結構:

[root@www local]# su nobody
[nobody@www local]$ cd /usr/local/squid/bin
[nobody@www local]$ ./squid -z

五、squid.conf 的關鍵幾行

打開 /usr/local/squid/etc/squid.conf,確認下列設定存在(部分版本預設沒有 httpd_accel_host virtual 這一行,必須自行補上):

httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
cache_effective_user nobody
cache_effective_group nobody
http_access allow all
cache_dir ufs /usr/local/squid/cache 100 16 256

啟動並確認:

[root@www local]# /usr/local/squid/bin/squid
[root@www local]# ps ax

正常的時候,除了 Squid 主程序,還會看到 unlinkd 之類的子程序,並且 3128(TCP)與 3130(UDP)進入監聽狀態:

tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:3130 0.0.0.0:*

若 Squid 起不來,先檢查主機名稱與 DNS 設定是否正確,這是最常見的失敗原因。

讓開機自動啟動

編輯 /etc/rc.d/rc.local,在最後一行加上:

su nobody -c "/usr/local/squid/bin/squid"

此時若手動在 IE 填入 192.168.0.101:3128,已經可以當普通代理使用;但這還不是透明代理。

六、iptables 才是透明代理的靈魂

  1. 用 linuxconf 打開 enable routing,並在 modules 裡載入 firewall 相關設定。
  2. 執行 setup,在 System services 中把 ipchains 取消勾選(2.4 內核改用 iptables)。
  3. 在 /etc/rc.d/ 建立 firewall 腳本,chmod u+x firewall,再把 /etc/rc.d/firewall 寫進 rc.local。

腳本核心只有兩條規則,一條把內網送出的 80 埠流量重導向到 3128,一條做位址轉換:

modprobe ip_tables
modprobe ip_nat_ftp
modprobe ip_conntrack
modprobe ip_conntrack_ftp
echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -A POSTROUTING -s 172.16.0.0/23 -o eth0 -j SNAT --to 211.141.67.10

若再加上防火牆策略,可以參考下面的完整版本:

#!/bin/sh
echo "Enable IP Forwarding..."
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "Starting iptables rules..."
/sbin/modprobe iptable_filter
/sbin/modprobe ip_tables
/sbin/modprobe iptable_nat

/sbin/iptables -F -t nat
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD DROP

/sbin/iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128
/sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to a.b.c.d
/sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

這裡 eth1 是內網網卡、eth0 是外網網卡;內網段為 172.16.0.0/23,eth0 上的公開位址為 211.141.67.10。設定完成後,Client 端只要把閘道指向這台主機、DNS 填妥,就能直接上網,完全不需要知道代理的存在。

七、加上使用者認證

重要前提:透明代理與使用者認證無法並存。認證需要瀏覽器跳出帳密視窗,而透明代理攔下的連線沒有這個互動機會,兩者同時啟用會讓認證失效。要認證就必須退回「手動設定代理」的模式。

以 NCSA 模組為例(假設原始碼在 /tmp/squid-2.3.STABLE4):

# cd /tmp/squid-2.3.STABLE4/auth_modules/NCSA
# make ncsa_auth
# cp ncsa_auth /usr/local/squid/bin

接著借用 Apache 的 htpasswd 產生密碼檔:

# cd /var/www/bin
# ./htpasswd -c /usr/local/squid/etc/passwd test
New password:
Re-type new password:
Adding password for user test

新增其他帳號時去掉 -c 即可:

# htpasswd /usr/local/squid/etc/passwd newuser

然後在 squid.conf 註冊認證程式與 ACL:

authenticate_program /usr/local/squid/bin/ncsa_auth /usr/local/squid/etc/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

REQUIRED 代表「任何通過密碼檔驗證的合法使用者」。最後一點常被忽略:http_access 的順序有意義,需要認證的類別必須寫在最前面。

# 錯誤
http_access allow auth_user all manager
# 正確
http_access allow auth_user manager all

順序寫錯,Squid 會先被前面那條放行規則攔截,認證永遠不會被觸發。

Copyright © Linux教程網 All Rights Reserved