Proxy (a2) |
A:เป็นคำตอบที่ค่อนข้างยาวสักนิดหนึ่งนะครับ แต่เพื่อความ clear จะอธิบายแบบค่อนข้างละเอียดเลย เพื่อ จะได้นำไปใช้งานได้ทันทีครับ ดังนั้นคำถามอื่นที่ไม่ได้ลง เล่มนี้ต้องขอยกไปเล่มหน้าแล้วกันนะครับ : )
การทำ Transparent Proxy นั้นมันจะต้องใช้ทางผ่านของข้อมูล หรือ Gateway ในการกำหนด protocol ชนิดของข้อมูล ไปในทิศทางที่ต้องการจะให้ไป (redirect port) ดังนั้น คุณจะต้องทราบ เสียก่อน ว่า Gateway ที่คุณกำลังใช้อยู่นั้น support ในการ ทำ redirect หรือไม่ ถ้าไม่ก็จัดแจงหาโปรแกรมจำพวก redirect port มาติดตั้งไว้ที่ Gateway เสียก่อน ในหนังสือเล่มนี้จะเป็นเรื่องของ Linux ดังนั้น การทำ Transparent proxy บน OS อื่นจะไม่กล่าวถึงนะครับ
ก่อนอื่นต้องเตรียมเครื่องสำหรับติดตั้ง Linux เพื่อเป็น Gateway ก่อน ส่วนจะลง Linux ตัวไหนก็ตามสบายครับ แต่ขอให้ Kernel เป็น 2.4 ขึ้นไป เพื่อจะใช้ Feature ของ iptables ที่สามารถทำ Redirect port ได้ด้วยตัวของมันเองได้เลย เมื่อเตรียมเครื่องสำหรับทำ Gateway เรียบร้อยแล้ว ก็ทำตาม Step ต่อไปนี้ได้เลยครับ
Step 1 : Compile Kernel ให้ Support Netfilter (iptables)
ก่อนอื่นก็จะต้อง Compile ให้ Kernel support iptables เสียก่อน โดย เข้าไปที่ “/usr/src/linux” และเรียก “make menuconfig” เลื่อน hi-light มาที่ menu “Networking options — >” แล้วกด Enter ก็จะเข้ามาสู่ sub menu ของ Networking options เลื่อน hi-light มาที่ “[ ] Network packet filtering (replaces ipchains)” ทำการเคาะ space bar ให้มี “[*]” ขึ้นเพื่อเป็นการยืนยันขอกว่าจะใช้ iptables กับ kernel นี้ เมื่อ mark เสร็จจะมี sub menu ขึ้นมาอีกอันคือ “IP : Netfilter Configuration — >” ให้เลื่อน hi-light มาที่ sub menu นี้แล้วกด Enter เพื่อเข้าสู่ sub menu นี้ เมื่อเข้ามาแล้วให้ space bar [*] ของทุก menu ย่อย ทั้งหมด ตัวโปรแกรม menuconfig จะมีการเพิ่ม menu มาเอง ให้ enable ให้ครบ หลังจากนั้นให้ออกและ save config ที่พึ่งมีการแก้ไขไป หลังจากนั้นก็เรียกคำสั่ง “make dep” แล้วก็ “make; make install” เพื่อ install kernel ใหม่ พอทำเสร็จพิธีนี้ก็ reboot เครื่องแล้วรอลุ้นว่า kernel จะตกใจหรือไม่ ( kernel panic )
Step 2 : download Proxy program และ install
ผมจะพูดถึง proxy ตัวเก่งที่ใช้กันมานมนานแล้ว คือ squid เจ้าปลาหมึก ส่วน Feature ของเจ้า proxy server ตัวนี้มีเด่นๆ หลาย อย่าง เช่น
• Proxying and caching of HTTP, FTP, and other URL’s
• Proxying for SSL
• Cache hierarchies
• ICP, HTCP, CARP, Cache Digests
• Transparent Caching
• WCCP
• Extensive access controls
• HTTP server acceleration
• SNMP
• Caching of DNS lookups
สามารถ download source ได้ที่ “ http://www.squid-cache.org “ ล่าสุดก็จะเป็น version 2.4 ซึ่งจะ stable สุดตอนนี้ “http://www.squidcache.org/Versions/v2/2.4/squid-2.4.STABLE4-src.tar.gz “ เมื่อ download มาแล้วก็ทำการแตกโปรแกรมด้วยคำสั่ง “ tar xvfz squid-2.4.STABLE4-src.tar.gz “
ต่อจากนี้จะเป็น การ config และ compile โปรแกรม squid proxy server ให้ “cd” เข้าไปใน directory ที่แตก program squid ออกมาแล้วใช้คำสั่งตามนี้
# ./configure -prefix=/squid \
--disable-ident-lookups \
--enable-linux-netfilter \
--enable-delay-pools \
--enable-pool \
--enable-truncate \
- - enable-removal-policies
# make all
# make install
เมื่อทำตามข้างต้นเรียบร้อยแล้ว จะได้โปรแกรม squid proxy server ไว้ที่ “/squid” และ file config จะอยู่ที่ “/squid/etc/squid.conf”
Step 3 :
Config ตัว proxy Server
หลักจริงๆ ของการ Config Transparent proxy ด้วย Squid นั้นจะใช้ iptables ในการ redirect port 80 (http port) เพื่อให้ชี้มาที่ port 8080 ซึ่งเป็น port proxy มาตรฐาน (มาตรฐานคนไทย) เมื่อใช้ iptables ในการชี้มาเสร็จแล้วก็จะต้อง add config นี้ลงไปใน config ของ squid เพื่อให้ squid ยอมรับการทำ redirect port
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
Step4 : การกำหนด Bandwidth ในการ download ข้อมูล
การกำหนด Bandwidth ในการ download โปรแกรม หรือการใช้ข้อมูลผ่าน squid นั้นจะใช้ Feature ที่ชื่อว่า Delay Pools ซึ่งตอนแรกที่เราได้ทำการ configure และ install ไปนั้น เราได้ทำไปแล้วซึ่งจะแบ่งการกำหนดออกเป็น 3 ลักษณะดังนี้
แบบแรก : การ กำหนด การ download จาก ตัวอักษรหรือคำ จาก website
acl magic_words url_regx –i .exe
delay_pools 1
delay_class 1 1
delay_parameters 1 4000/4000
delay_access 1 allow magic_words
อธิบายได้ดังนี้
magic_words เป็นตัวแปลบอกว่าถ้ามีนามสกุล .exe (ส่วน –i คือเป็นตัวบอกว่าให้เป็น case-insensitive) ให้ download มาด้วยความเร็วไม่เกิน 32Kbit/sec ก็ประมาณ 4KByte/sec 32Kbit/sec มาจาก 4000 * 8 ถ้าต้องการจะหา ความเร็วที่ต้องการจะให้ download ไม่เกินสามารถ หาได้ด้วยวิธีการคำนวณแบบนี้ สมมติ ให้ต้องการให้ download ไม่เกิน 2KByte/sec คือ (2000*8)/8 = 2000 (จะแสดงวิธีคิดให้ดูทำไมล่ะนี่ ?) ก็คือนำเอาจำนวน KByte/sec ที่ต้องการใส่เข้าไปเลยนะครับ :P~
แบบสอง : กำหนดให้ ขนาด Bandwidth ให้กับคนที่ใช้ภายใน network
acl a_ll src 0.0.0.0/0.0.0.0
delay_pool_count 1
delay_class 1 2
delay_parameters 1 12500/12500 2500/2500
delay_access 1 allow a_ll
อธิบายได้ดังนี้
จากตัวอย่างข้างต้นสมมติว่าคุณมี Lease Line ขนาด 128Kbit/sec และต้องการจะเหลือไว้สักนิดหนึ่งสำหรับ SMTP server ซึ่งเป็น IP จริงไม่ได้ต่อผ่านกับ gateway ตัวนี้ และ ให้ใช้สำหรับ NAT ภายในองค์กร 100Kbit/sec เพื่อให้ทุกคน share กันใช้ เท่าๆ กันและต้องการจะให้แต่ละคนใช้ bandwidth ได้ไม่เกิน 2.5KByte/sec (สังเกตดีๆ นะครับหน่วยไม่เหมือนกัน เพื่อง่ายต่อการเข้าใจควรตั้งใจอ่านทุกตัวอักษรครับ)
แบบสาม : กำหนดขนาด Bandwidth ให้กับ group network
acl a_ll src 0.0.0.0/0.0.0.0
delay_pool_count 1
delay_class 1 3
delay_parameters 1 56000/56000 18750/18750 500/500
delay_access 1 allow all
อธิบายได้ดังนี้
ถ้าคุณมี Lease Line ขนาด 512Kbit/sec และต้องการ เหลือ bandwidth สำหรับ SMTP ไว้ 8KByte/sec ใช้ภายใน network ไม่เกิน 448Kbit/sec และ แบ่งให้แต่ละ class ไม่เกิน 150Kbit/sec และให้แต่ละ IP ไม่เกิน 4kbit/sec (หน่วยไม่ผิดนะครับ ถ้าเป็น KByte เอา 8 หารจะเป็น Kbit ครับ)
เพื่อป้องกันความสับสน ผมจะเอาตัวอย่าง proxy ที่ผมใช้ภายใน Office เป็นตัวอย่างครับ ดังนี้
ตัวอย่าง squid.conf
#/squid/etc/squid.conf
# IP ของเครื่อง gateway ที่ใช้คือ 10.20.1.181
# eth1 10.20.1.181
# กำหนด port เพื่อใช้ในการ connect proxy http_port 8080
# กำหนด user ที่ใช้ในการ control cache squid ห้ามใช้ root เป็นอันขาด (เพราะมันจะใช้งานไม่ได้)
cache_effective_user squid
cache_effective_group squid
# Cache objects no larger than this size maximum_object_size 40960 KB
# Use this amount of memory (1/3rd of total memory).
# Note squid will use MORE than this for other tasks
cache_mem 16 MB
# กำหนดขนาด cache ที่นี้กำหนดให้ขนาด 4GB cache_dir ufs /squid/cache 4000 16 256
# กำหนดการ fake header
forwarded_for off
# What to do when a user aborts a download.
# If the transfer has less than the “quick_abort_min” value, it will continue to download
# the data. If it has more than the “quick_abort_max” value, it will abort the download.
# If more than “quick_abort_pct” percentage of the download has occurred, it won’t abort
# the download either. These values are the default settings.
quick_abort_min 16 KB
quick_abort_max 16 KB
quick_abort_pct 95
# กำหนด คำกับ website ที่ไม่ต้องการให้ cache
acl QUERY urlpath_regex cgi-bin \?
acl website dst 10.20.2.14/32
acl PHP url_regex .php?
no_cache deny website
no_cache deny QUERY
no_cache deny PHP
# Define some access control list (acl) “aliases”
acl Safe_ports port 80 21 443 563 591 70 210 1025-65535
acl manager proto cache_object
acl all src 0.0.0.0/0.0.0.0
acl allowed_hosts src 10.20.0.0/255.255.0.0
acl localhost src 127.0.0.1/255.255.255.255
acl SSL_ports port 443 563
acl CONNECT method CONNECT
# Generate some rules based on the aliases we just made
# This prevents access from those who shouldn’t have it
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow allowed_hosts
http_access deny all
# Name to appear on error documents
cache_mgr yourname@yourdomain.com
# Append this domain to non-resolvable websites before giving up
append_domain .uoregon.edu
# Transparent proxy settings. Requires iptabes to be
# set up for transparent proxy as well (see below).
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
httpd_accel_single_host off
acl a_ll src 0.0.0.0/0.0.0.0
delay_pool_count 1
delay_class 1 2
delay_parameters 1 12500/12500 2500/2500
delay_access 1 allow a_ll
# หลอก header ไม่ให้ show X-Forward
anonymize_headers deny From Referer Server
anonymize_headers deny User-Agent WWW-Authenticate Link
fake_user_agent Thailand_ChaiYoo/1.0
Step 5 : ทำการ transparent ด้วย iptables
ทำการ redirect port 80 ที่ผ่าน Gateway ทั้งหมดไป ที่ port 8080 เพื่อบังคับให้ทุกคนใช้ proxy ครับ ด้วยคำสั่งดังนี้เลย
# iptables -t nat -A PREROUTING -i eth0 -p tcp —dport 80 -j REDIRECT -- to-port 8080
เมื่อทำตาม ปกติเรียบร้อยแล้ว ก็ทำการทดสอบใช้โดยเรียก IE หรือ mozilla เพื่อ เรียกดู website ส่วนที่ Gateway นั้น ก็ “tail-f /squid/logs/access.log” เพื่อดูการเปลี่ยนแปลงของ log ครับ ทดลองดูกันนะครับ ขอให้โชคดี : )