Proxy gateway CentOS 6


I. Skenario:
Internet Router — eth0:192.168.0.11:cumi.bachem.local:eth1:192.168.10.1 — client

II. Setting Statik IP Address,DNS, etc
# system-config-network-tui

or

-Via CLI, setting ip address eth0
# cd /etc/sysconfig/network-scripts/
# vi ifcfg-eth0

DEVICE=eth0
HWADDR=32:E8:71:6C:BF:E7
NM_CONTROLLED=yes
ONBOOT=yes
IPADDR=192.168.0.11
BOOTPROTO=static
NETMASK=255.255.255.0
TYPE=Ethernet
GATEWAY=192.168.0.1
IPV6INIT=no
USERCTL=no

 

-Setting ip address eth1
# cd /etc/sysconfig/network-scripts/
# vi ifcfg-eth1

DEVICE=eth1
HWADDR=e2:00:34:d8:3b:6f
NM_CONTROLLED=yes
ONBOOT=yes
IPADDR=192.168.10.1
BOOTPROTO=static
NETMASK=255.255.255.0
DNS2=192.168.10.6
TYPE=Ethernet
DNS1=192.168.10.5
IPV6INIT=no
USERCTL=no

 
-Setting DNS:
# vi /etc/resolv.conf

nameserver 192.168.10.5
nameserver 192.168.10.6

 
-Setting Hostname:
# vi /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=cumi.tahubachem.net

 
-Setting /etc/hosts
# vi /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.1	cumi.tahubachem.net	cumi

 
III. Update Server
# yum –y update

IV. Setting NAT/Sebagai Router/Gateway
-Enable IP forwarding
# sysctl -w net.ipv4.ip_forward=1

-To enable it in system startup, edit the file /etc/sysctl.conf
# vi /etc/sysctl.conf
and set
net.ipv4.ip_forward = 1

-Enables packet forwarding by kernel
# echo 1 > /proc/sys/net/ipv4/ip_forward
OR edit /proc/sys/net/ipv4/ip_forward , change 0 to 1 then save

-Setting iptables untuk NAT
# iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
# iptables -I INPUT -i eth1 -p tcp --dport 3128 -j ACCEPT
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# service iptables save
# service iptables restart

III. Install & Setting Squid
-Install Paket Squid 3.1 from default CentOS 6 repo
# yum -y install squid

-Konfigurasi Squid
# cp /etc/squid/squid.conf /etc/squid/squid.conf.asli
# vi /etc/squid/squid.conf

# squid.conf 3.1
# -------------
# OPTIONS FOR AUTHENTICATION
# --------------------------
#auth_param basic program /usr/lib64/squid/squid_ldap_auth -b "dc=tahubachem,dc=local" -f "uid=%s" -h 192.168.10.6
#auth_param basic children 5
#auth_param basic realm Squid proxy-caching web server
#auth_param basic credentialsttl 2 hour
#authenticate_ip_ttl 5 minutes

# ACL
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
#acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
#acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.10.0/24 # RFC1918 possible internal network
#acl localnet src fc00::/7 # RFC 4193 local private network range
#acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
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 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl Safe_ports port 631 # cups
acl Safe_ports port 10000 # webmin
acl Safe_ports port 873 # rsync
acl Safe_ports port 901 # SWAT
acl Safe_ports port 5050 # YM
acl Safe_ports port 110 # POP3
acl Safe_ports port 25 # SMTP
acl Safe_ports port 2095 2096 # webmail from cpanel
acl Safe_ports port 2082 2083 # cpanel
acl CONNECT method CONNECT
acl QUERY urlpath_regex cgi-bin \?

#ACL LDAP auth:
#acl ldapauth proxy_auth REQUIRED
#ACL one ip = one user login
#acl one_ip_access max_user_ip -s 1

# ACL file download
acl downloadfile url_regex -i ftp .001 .002 .003 .004 .005 .006 .007 .008 .009 .010 .3gp .avi .bin .cab .cda .div .divx .deb .doc .docx .exe .flv .gif .gz .ico .iso .jpeg .jpg .midi .mkv .mov .mp3 .mp4 .mpe .mpeg .mpg .msi
acl downloadfile url_regex -i ftp .nrg .ogg .pdf .png .ppt .pptx .qt .ram .rar .raw .rm .rpm .swf .tar .tar.bz2 .tar.gz .tiff .vqf .wav .wmv .xls .xlsx .x-flv .zip

# Recommended minimum Access Permission configuration:
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager

# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
# --------------------------------------------------------------
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access deny all

# Transparent Squid port 3128
# http_port 3128 transparent
http_port 3128 intercept
#No transparent,if using wpad.dat will tell web browser the proxy address & port
#http_port 3128

# We recommend you to use at least the following line.
hierarchy_stoplist cgi-bin ?

# --------------------
# MEMORY CACHE OPTIONS
# --------------------
cache_mem 16 MB
maximum_object_size_in_memory 32 KB
memory_replacement_policy heap GDSF

# ---------------------------
# cache_dir
# ---------------------------
## option cache have a methode for know the configuration,
## normally we use 80% for the /cache all, but never
## give a good performancy, so for the save configuration we
## use 50% for /cache
## x=50% for the /cache in KB
##(ex 50% X 12GB, so we use 6GB=~6,000,000KB)
## y=Average object size (use 13KB)
## z= 256 -> (((x / y) / 256) / 256) * 2 –this the method
## ex, we use 6 GB for /cache (50% ), so:
## 6,000,000 / 13 = 461538.5 / 256 = 1802.9 / 256 = 7 * 2 = 14
## So the row of cache_dir : cache_dir ufs 6000 14 256
# ---------------------------
cache deny QUERY
cache_replacement_policy heap LFUDA
cache_dir aufs /var/spool/squid 6000 14 256
minimum_object_size 0 KB
maximum_object_size 64 MB
cache_swap_low 98
cache_swap_high 99

# LOGFILE OPTIONS
# ---------------
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log
#cache_store_log /var/log/squid/store.log
cache_store_log none
logfile_rotate 10

# OPTIONS FOR TROUBLESHOOTING
# ---------------------------
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

# OPTIONS FOR URL REWRITING
# -------------------------
# Options for squidGuard
#url_rewrite_program /usr/bin/squidGuard -c /etc/squid/squidGuard.conf

# OPTIONS FOR TUNING THE CACHE
# ----------------------------
# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320

refresh_pattern -i \.(gif|png|jpg|jpeg|ico)$ 10080 90% 43200
refresh_pattern -i \.(iso|avi|wav|mp3|mp4|mpeg|mpg|swf|flv|x-flv)$ 10080 90% 43200
refresh_pattern -i \.(deb|rpm|exe|ram|bin|pdf|ppt|pptx|doc|docx|xls|xlsx|tiff)$ 10080 90% 43200
refresh_pattern -i \.(zip|bz2|gz|arj|lha|lzh|tar|tgz|cab|rar)$ 10080 95% 43200
refresh_pattern -i \.(html|htm|css|js|php|asp|aspx) 1440 40% 40320

refresh_pattern -i download.windowsupdate.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i download.microsoft.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i update.microsoft.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i windowsupdate.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i windowsupdate.microsoft.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i ntservicepack.microsoft.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims
refresh_pattern -i wustat.windows.com/.*\.(cab|exe|ms[i|u|f]|asf|wm[v|a]|dat|zip) 4320 80% 43200 reload-into-ims

# TIMEOUTS
# --------
forward_timeout 2 minutes
connect_timeout 1 minute
read_timeout 10 minutes
request_timeout 3 minutes
persistent_request_timeout 1 minutes
client_lifetime 1 day
half_closed_clients off
shutdown_lifetime 10 seconds

# ADMINISTRATIVE PARAMETERS
# -------------------------
cache_mgr triadi80@gmail.com
cache_effective_user squid
visible_hostname cumi.tahubachem.net

# DELAY POOL PARAMETERS
# ---------------------
# 1 byte = 8 bits
# 1 kilobyte (K / Kb) = 2^10 bytes = 1,024 bytes
# 1 megabyte (M / MB) = 2^20 bytes = 1,048,576 bytes
# 1 gigabyte (G / GB) = 2^30 bytes = 1,073,741,824 bytes
# 1 terabyte (T / TB) = 2^40 bytes = 1,099,511,627,776 bytes
# 1 petabyte (P / PB) = 2^50 bytes = 1,125,899,906,842,624 bytes
# 1 exabyte (E / EB) = 2^60 bytes = 1,152,921,504,606,846,976 bytes
# 512 Kbps = 64 Kbytes = 64000 bytes
# 384 Kbps = 48 Kbytes = 48000 bytes
# 256 Kbps = 32 Kbytes = 32000 bytes
# 128 Kbps = 16 Kbytes = 16000 bytes
# 64 Kbps = 8 Kbytes = 8000 bytes

delay_pools 2

# aturan 1, tidak ada pembatasan
delay_class 1 2
delay_parameters 1 -1/-1 -1/-1

# aturan 2,after download 500000 bytes(500KB),download become 15000 bytes/s(15KB/s)
delay_class 2 2
delay_parameters 2 -1/500000 15000/500000

delay_access 2 allow downloadfile
delay_access 2 deny all
delay_access 1 deny downloadfile
delay_access 1 allow all

# OPTIONS INFLUENCING REQUEST FORWARDING
# --------------------------------------
prefer_direct off
always_direct allow QUERY
#always_direct allow ipserver
always_direct deny all

# DNS OPTIONS
# -----------
allow_underscore on
dns_retransmit_interval 2 seconds
dns_timeout 1 minutes
dns_nameservers 192.168.0.1 192.168.1.1
hosts_file /etc/hosts
ignore_unknown_nameservers on
ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024

# MISCELLANEOUS
# -------------
forwarded_for on
uri_whitespace strip

-Set Permision Squid
# chown -R squid:squid /var/log/squid/

-Membuat direktori cache squid, make squid run at startup
# squid -z
# chkconfig squid on
# service squid restart

-Setting iptables Transparent Proxy squid
# iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-port 3128
# service iptables save
# service iptables restart

 
References:
https://guammie.com/donovan/2012/02/15/transparent-squid-3-1-19-on-centos-6-2-vm/
http://www.squid-cache.org/Versions/v3/3.1/cfgman/
http://gilalinux.blogspot.com/2011/01/squid-3110-on-slackware-131.html

Leave a Reply

Your email address will not be published. Required fields are marked *