bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
Dịch vụ squid server
1. DỊCH VỤ SQUID PROXY
(Phùng Tấn Phát – Trần Quốc Khải)
-------------I.
CƠ BẢN VỀ DỊCH VỤ SQUID PROXY
1.1. Giới thiệu:
Squid là một proxy server, khả năng của squid là tiết kiệm băng thong
(bandwidth), cải tiến việc bảo mật, tăng tốc độ truy cập web cho người sử dụng
và trở thành một trong những proxy phổ biến được nhiều người biết đến. Hiện
nay, trên thị trường có rất nhiều chương trình proxy-server nhưng chúng lại có
hai nhược điểm, thứ nhất là phải trả tiền để sử dụng, thứ hai là hầu hết không hỗ
trợ ICP (ICP được sử dụng để cập nhật những thay đổi về nội dung của những
URL sẵn có trong cache – là nơi lưu trữ những trang web mà bạn đã từng đi
qua). Squid là sự lựa chọn tốt
nhất cho một proxy-cache server,
squid đáp ứng hai yêu cầu của
chúng ta là sử dụng miễn phí và
có thể sử dụng đặc trưng ICP.
Squid đưa ra kỹ thuật lưu
trữ ở cấp độ cao của các web
client, đồng thời hỗ trợ các dịch
vụ thông thường như FTP,
Gopher và HTTP. Squid lưu trữ
thông tin mới nhất của các dịch
vụ trên trong RAM, quản lý một
cơ sở dữ liệu lớn của các thông tin trên đĩa, có một kỹ thuật điều khiển truy cập
phức tạp, hỗ trợ giao thức SSL cho các kết nối bảo mật thông qua proxy. Hơn
nữa, squid có thể liên kết với các cache của các proxy server khác trong việc sắp
xếp lưu trữ các trang web một cách hợp lý.
1.2 Giao thức hỗ trợ trên Squid Proxy
Squid proxy hỗ trợ những giao thức sau:
- Proxying and caching of HTTP, FTP, and other URLs.
- Proxying for SSL.
- Cachinh hierarchies.
1
2. - ICP, HTTP, CARP, Cache Digests.
- Transparent caching.
- WCCP- Web Cache Communication Protocol
- Extensive access controls
- HTTP server acceleration
- SNMP
- Caching of DNS lookups.
1.3 Những điểm tối ưu mà Proxy mang lại:
Squid có khả năng chia sẽ dữ liệu giữa những cache với nhau.Việc chia sẻ
này mang lại những lợi ích như:
- User Base: Nếu số lượng client truy cập internet thông qua proxy
càngnhiều thì khả năng một đối tượng nào đó được yêu cầu 2 lần sẽ cao hơn.
- Giảm tải truy xuất cho đường truyền.
- Disk space: nếu bạn chuyển cân bằng giữa các cache với nhau sẽ tránh
được việc sao lại dữ liệu đã lưu. Dó đó dung lượng đĩa cứng dành cho việc lưu
trữ cache sẽ giảm.
1.4 Hệ thống file và thư mục của Squid Proxy:
- /usr/sbin: Lưu những thư viện của squid.
- /etc/squid: Lưu các tập tin cấu hình chính squid.
- /usr/local/squid: thư mục cài đặt squid
- /usr/local/squid/bin: thư mục lưu binary squid và những tool được hỗ trợ.
- /usr/local/squid/cache: thư mục lưu những dữ liệu được cache. Đây là thư
mục mặc định, bạn có thể thay đổi vị trí thư mục này.
- /usr/local/squid/etc: những tập tin cấu hình squid nằm trong thư mục này.
- /usr/local/squid/src: thư mục lưu source code squid được download từ net.
- /var/log/squid: Lưu các tập tin log của squid.
II. Nguyên tắc hoạt động
Squid xác định những yêu cầu từ client và quyết định đáp ứng hay không
đáp ứng, nếu yêu cầu được đáp ứng, nó sẽ kết nối với server thật thay cho client
2
3. và tiếp tục chuyển tiếp đến những yêu cầu từ client đến server, cũng như đáp
ứng những yêu cầu của server đến client. Vì vậy squid proxy giống cầu nối
trung gian giữa server và client.
Bên cạnh việc chuyển tiếp các yêu cầu từ phía client, nó cũng sẽ đồng
thời lưu lại trên đĩa những dữ liệu được trả về từ Internet Server – gọi là caching
(thường là nội dung các trang Web, các tập tin…). Nếu trong thời gian hiệu lực
mà một hay nhiều client cùng yêu cầu một nội dung thì squid proxy sẽ ngay lập
tức đáp ứng lại những yêu cầu từ phía client.
III. Cấu hình Squid
* Cài đặt squid:
apt-get install squid
* Cấu hình:
File cấu hình chính của Squid là squid.conf ( /etc/squid/squid.conf ) , mọi thay
đổi ở file này bạn cần restart lại squid để có hiệu lực.
để restart squid chúng ta dùng lệnh
/etc/init.d/squid restart
a.Hạn chế truy cập web theo thời gian:
Mở file squid.conf ra
# Add this to the bottom of the ACL section of squid.conf
acl home_network src 192.168.1.0/24
acl allow_hours time M T W H F 9:00-17:00
acl RestrictedHost src 192.168.1.23
# Add this at the top of the http_access section of squid.conf
http_access deny RestrictedHost
http_access allow home_network allow_hours
Nếu bạn có các dòng trên trong file squid.conf thì các máy trong mạng nội bộ
chỉ truy cập được vào mạng vào các ngày thứ 3 đến thứ 6 và trong thời gian 9h
đến 17h.
Trừ máy 192.168.1.23, vì bị cấm tất cả.
3
4. M, T, W, H, F là viết tắt các ngày trong tuần ( chủ nhật : S, thứ bảy : A)
Có thể dùng D để thay bằng các ngày trong tuần ( M - F )
b.Hạn chế truy cập các website được ấn định trước :
Đầu tiên tạo 2 file ở trong thư mục /etc/squid là allowed-sites.squid và
restricted-sites.squid để chứa tên các domain cho phép truy cập và cấm truy cập.
# File: /squid/allowed-sites.squid
www.vickifcvn.net
fotech.org
# File: /etc/squid/restricted-sites.squid
www.sex.com
xxx.net
Nếu bạn muốn trong thời gian làm việc, cấm vào một số site và cho phép vào
một số site nó thì :
Mở file squid.conf ra :
# Add this to the bottom of the ACL section of squid.conf
acl home_network src 192.168.1.0/24
acl work_hours time M T W H F 9:00-17:00
acl GoodSites dstdomain "/etc/squid/allowed-sites.squid"
acl BadSites dstdomain "/etc/squid/restricted-sites.squid"
# Add this at the top of the http_access section of squid.conf
http_access deny BadSites
http_access allow home_network work_hours GoodSites
Bạn có thể dùng url_regex để hạn chế vào các domain name được ấn định:
# Add this to the bottom of the ACL section of squid.conf
acl domainname url_regex -i ^http://www
# Add this at the top of the http_access section of squid.conf
http_access deny domainname
4
5. c.Hạn chế truy cập web theo địa chỉ ip :
Bạn có thể tạo một access control list để hạn chế truy cập web trên một mạng
nào đó.
# Add this to the bottom of the ACL section of squid.conf
acl home_network src 192.168.1.0/255.255.255.0
acl localhost dst 127.0.0.0/8
# Add this at the top of the http_access section of squid.conf
http_access allow home_network
http_access allow localhost
d.Hạn chế nội dung các file download:
Tạo một file chứ nội dung các phần đuôi của các tập tin mà bạn muốn hạn chế là
blocks.files.acl nằm trong /etc/squid
.[Ee][Xx][Ee]$
.[Aa][Vv][Ii]$
.[Mm][Pp][Gg]$
.[Mm][Pp][Ee][Gg]$
.[Mm][Pp]3$
Thêm vào squid.conf dòng
# Add this to the bottom of the ACL section of squid.conf
acl blockfiles urlpath_regex “/etc/squid/blocks.files.acl”
# Add this at the top of the http_access section of squid.conf
http_access deny blockfiles
e.Hạn chế truy cập theo cổng :
vd
# Add this to the bottom of the ACL section of squid.conf
acl httpports port 80 8000-8010 8080
5
6. # Add this at the top of the http_access section of squid.conf
http_access allow httpports
Các bạn nên duy trì và allow một số cổng. Dưới đây là một số cổng được cho là
an toàn ( mặc định trong file squid.conf )
# Add this to the bottom of the ACL section of squid.conf
acl safe_ports port 80 # http
acl safe_ports port 21 # ftp
acl safe_ports port 443 563 # https snews
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 480 # gss-http
acl safe_ports port 591 # filemaker
acl safe_ports port 777 # multiling http
# Add this at the top of the http_access section of squid.conf
http_access deny !safe_ports
f.Hạn chế truy cập theo giao thức :
# Add this to the bottom of the ACL section of squid.conf
acl FTP proto FTP
# Add this at the top of the http_access section of squid.conf
http_access deny FTP
g.Dùng NCSA kiểm định password:
đầu tiên tạo một file password,vd /etc/squid/squid_passwd
touch /etc/squid/squid_passwd
chmod o+r /etc/squid/squid_passwd
6
7. Dùng htpasswd để add user vào pass vào file squid_passwd đó
htpasswd /etc/squid/squid_passwd vicki
New password:
Re-type new password:
Adding password for user vicki
giờ thêm vào file squid.conf
# Add this to the auth_param section of squid.conf
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_passwd
# Add this to the bottom of the ACL section of squid.conf
acl ncsa_users proxy_auth REQUIRED
# Add this at the top of the http_access section of squid.conf
http_access allow ncsa_users
Như vậy phải dùng user và pass được add vào file squid_passwd mới có thể truy
cập vào internet được.
Cũng có thể kết hợp thời gian để bắt kiểm định pass vào mốc thời gian nhất định
( tham khảo đầu bài )
Nếu bạn muốn mọi deny đều có một thông báo cho người dùng biết thì :
Tạo một file deny.php với nội dung tùy bạn, đặt vào /var/www chẳng hạn.
acl blacklist url_regex yahoo.com vietnamnet.vn
http_access deny blacklist
deny_info domain/deny.php blacklist
7
8. PHỤ LỤC
MỘT SỐ VÍ DỤ KHÁC VỀ ACL SQUID
Hạn chế truy cập web theo thời gian và cấm các IP chỉ định đi web
acl home_network src 192.168.1.0/24
acl allow_hours time M T W H F 9:00-17:00
acl RestrictedHost src 192.168.1.200
http_access deny RestrictedHost
http_access allow home_network allow_hours
Hạn chế truy cập web theo thời gian và loại trừ IP của các sếp
acl home_network src 192.168.1.0/24
acl allow_hours time M T W H F 9:00-17:00
acl AllowHost src "/etc/squid/AllowHost.txt"
http_access allow AllowHost
http_access allow home_network allow_hours
# vi /etc/squid/AllowHost.txt
192.168.1.100
192.168.1.200
Tự động redirect sang website ấn định trước khi truy cập những website ko được phép
acl home_network src 192.168.1.0/24
acl denywebsite dstdom_regex "/etc/squid/denywebsite.txt"
http_access deny denywebsite
deny_info http://vnlamp-test.vn/deny.html denywebsite >> tất nhiên đã cấu hình dns & httpd
#deny_info http://www.google.com.vn denywebsite >> redirect tới google
# deny_info TCP_RESET denywebsite >> reset the TCP connection (blank page)
http_access allow home_network
# vi /var/www/html/deny.html
"This site has restricted by administrator"
Hạn chế tên tập tin download
acl home_network src 192.168.1.0/24
acl blockfiles urlpath_regex "/etc/squid/block.files.acl"
http_access deny blockfiles
http_access allow home_network
hoặc
acl home_network src 192.168.1.0/24
acl denyfiletypes url_regex -i .mp3$ .mpg$ .mpeg$ .mp2$ .avi$ .wmv$ .wma$ .exe$
http_access deny denyfiletypes
http_access allow home_network
Dùng htpasswd để add user và pass vào file squid_passwd mới tạo
# htpasswd /etc/squid/squid_passwd u1
New password:
8
9. Re-type new password:
Adding password for user u1
# Edit file squid.conf
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_passwd
acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users
Cho phép một range IP chỉ truy cập vào những trang web cố định
acl allow_domains dstdomain url_regex "/etc/squid/local/access-local.txt"
acl ip-local src "/etc/squid/ip-local.txt"
http_access deny ip-local !allow_domains
http_access allow ip-local
# vi /etc/squid/local/access-local.txt
.vnexpress.net
.tuoitre.com.vn
# vi /etc/squid/local/ip-local.txt
192.168.1.0/24
Cấm truy cập vào yahoo
acl aclyahoo dstdomain pager.yahoo.com
acl aclyahoo dstdomain shttp.msg.yahoo.com
acl aclyahoo dstdomain update.pager.yahoo.com
acl aclyahoo dstdomain scsa.yahoo.com
acl aclyahoo dstdomain msg.yahoo.com
http_access deny aclyahoo
Cấm download file có dung lượng >=10MB
acl home_network src 192.168.2.0/24
acl denywebsite dstdom_regex "/etc/squid/denywebsite.txt"
http_access deny denywebsite
deny_info http://www.google.com.vn denywebsite
reply_body_max_size 10000000 allow home_network
http_access allow home_network
http_access deny all
icp_access allow all
Cấu hình Squid Proxy Server để điều khiển băng thông
Bước 1: Thêm vào trong file cấu hình squid một số phần như sau:
# vi /etc/squid/squid.conf
# Add Control Bandwith
acl ip src "/etc/squid/ip.txt" # ip trong file ip.txt bị giới hạn
acl all src 0.0.0.0/0.0.0.0
# Add Control Bandwith
delay_pools 1
delay_class 1 2
delay_access 1 allow ip
9
10. delay_access 1 deny all
delay_parameters 1 -1/-1 15000/15000
Với delay_parameters 1 -1/-1 15000/15000 ta sẽ giới hạn băng thông cho các client không thể
vượt quá 15000 tương đương 15Kbps
Bước 2: Tạo file ip.txt như sau:
# vi /etc/squid/ip.txt
192.168.1.33
192.168.1.34
Vậy các IP trong file ip.txt sẽ có băng thông tối đa là 15Kbps
# Block online streaming of Audio/Video
acl BlockExt url_regex -i .mp3$ .asx$ .wma$ .wmv$ .avi$ .mpeg$ .mpg$ .qt$ .ram$
.rm$ .iso$ .wav$ .exe$
acl webRadioReq1 req_mime_type -i ^video/x-ms-asf$
acl webRadioReq2 req_mime_type -i ^application/vnd.ms.wms-hdr.asfv1$
acl webRadioReq3 req_mime_type -i ^application/x-mms-framed$
acl webRadioRep1 rep_mime_type -i ^video/x-ms-asf$
acl webRadioRep2 rep_mime_type -i ^application/vnd.ms.wms-hdr.asfv1$
acl webRadioRep3 rep_mime_type -i ^application/x-mms-framed$
acl WMP browser Windows-Media-Player/*
http_access deny BlockExt !UtentiGold
http_access deny WMP all
http_access deny webRadioReq1 all
http_access deny webRadioReq2 all
http_access deny webRadioReq3 all
http_reply_access deny webRadioRep1 all
http_reply_access deny webRadioRep2 all
http_reply_access deny webRadioRep3 all
( 1. Added the extensions .wma and .wmv at the following line, so it's impossible to
dowlonload these file types acl BloccoExt url_regex -i .mp3$ .asx$ .wma$ .wmv$
.avi$ .mpeg$ .mpg$ .qt$ .ram$ .rm$ .iso$ .wav$ .exe$
2. Created acl for the other two 2 mime types (maybe not necessary) acl webRadioReq1
req_mime_type -i ^video/x-ms-asf$ acl webRadioReq2 req_mime_type -i
^application/vnd.ms.wms-hdr.asfv1$ acl webRadioReq3 req_mime_type -i ^application/xmms-framed$ acl webRadioRep1 rep_mime_type -i ^video/x-ms-asf$ acl webRadioRep2
rep_mime_type -i ^application/vnd.ms.wms-hdr.asfv1$ acl webRadioRep3 rep_mime_type -i
10
11. ^application/x-mms-framed$
3. Added this too... acl WMP browser Windows-Media-Player/*
4. Created following rules http_access deny BloccoExt !UtentiGold http_access deny WMP !
UtentiGold http_access deny webRadioReq1 !UtentiGold http_access deny webRadioReq2 !
UtentiGold http_access deny webRadioReq3 !UtentiGold
http_reply_access deny webRadioRep1 !UtentiGold http_reply_access deny webRadioRep2 !
UtentiGold http_reply_access deny webRadioRep3 !UtentiGold
Notes:UtentiGold is an ip list of privileged users (CEO, ecc.)
In this mode, if I'm not a privileged user, I'm not able to open web radio (example
http://bbwms.libero.it/lifegate) with IE6 and WMP9, clicking on a link or typing the url )
11