Raspberry Pi come proxy cache trasparente

Se avete configurato il votro Raspberry come router o bridge di rete wifi-ethernet (vedi articolo Raspberry come bridge wifi to ethernet e file server samba), allora possiamo aggiungere facilmente un proxy http trasparente ai servizi che il vostro Raspberry già eroga.

1) installare squid e configurare:

sudo apt-get install squid3 squid3-common

editiamo

sudo nano /etc/squid/squid.conf

e inseriamo un configurazione tipo

http_port 3128 intercept

acl localnet src 192.168.0.0/16 # 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 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 1025-65535  # unregistered ports
acl CONNECT method CONNECT

http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow manager localhost
http_access deny manager

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

http_access allow localnet
http_access allow localhost
http_access deny all

coredump_dir /var/cache/squid
cache_dir ufs /var/cache/squid 10000 16 256

logfile_rotate 3

refresh_pattern (/cgi-bin/|\?) 0 0% 0
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i \.(gif|png|jpg|jpeg|ico)$ 10080 90% 43200 override-expire ignore-no-store ignore-private
refresh_pattern -i \.(ts|avi|wav|mp3|mp4|mpeg|swf|flv|x-flv)$ 43200 90% 432000 override-expire ignore-no-store ignore-private
refresh_pattern -i \.index.(html|htm)$ 0 40% 10080
refresh_pattern -i \.(html|htm|css|js)$ 1440 40% 40320
refresh_pattern . 0 40% 40320

# ANONYMOUS PROXY
forwarded_for off
request_header_access Allow allow all
request_header_access Authorization allow all
request_header_access WWW-Authenticate allow all
request_header_access Proxy-Authorization allow all
request_header_access Proxy-Authenticate allow all
request_header_access Cache-Control allow all
request_header_access Content-Encoding allow all
request_header_access Content-Length allow all
request_header_access Content-Type allow all
request_header_access Date allow all
request_header_access Expires allow all
request_header_access Host allow all
request_header_access If-Modified-Since allow all
request_header_access Last-Modified allow all
request_header_access Location allow all
request_header_access Pragma allow all
request_header_access Accept allow all
request_header_access Accept-Charset allow all
request_header_access Accept-Encoding allow all
request_header_access Accept-Language allow all
request_header_access Content-Language allow all
request_header_access Mime-Version allow all
request_header_access Retry-After allow all
request_header_access Title allow all
request_header_access Connection allow all
request_header_access Proxy-Connection allow all
request_header_access User-Agent allow all
request_header_access Cookie allow all
request_header_access All deny all

 

se la directory cache non esiste la si può creare:

sudo mkdir /var/cache/squid

sudo chown -R proxy:proxy /var/cache/squid

per essere sicuri che la struttura della cache sia inizializzata eseguiamo:

sudo squid -z

sudo service squid restart

 

2) configuriamo il kernel al fine di redirigere il traffico http verso il proxy

editiamo

sudo nano /etc/rc.local

e inseriamo

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128

in questo modo verrà eseguito ad ogni avvio. Riavviamo il Raspberry.

Tutti i dispositivi a cui il Raspberry eroga una connessione internet useranno in modo automatico e trasparente la cache del proxy.

Ultimo aggiornamento (Lunedì 15 Gennaio 2018 17:01)