Español  

Squid-openwrt-pares: Diferència entre les revisions

De Guifi.net - Wiki Català

(Conmutació de proxys amb OpenWrt i Squid)
 
(Hi ha 7 revisions intermèdies sense mostrar fetes per 2 usuaris)
Línia 43: Línia 43:
 
Reset del fitxer de configuració
 
Reset del fitxer de configuració
 
  root@OpenWrt:~# echo > /etc/squid/squid.conf
 
  root@OpenWrt:~# echo > /etc/squid/squid.conf
 +
  
 
Editem el fitxer de configuració amb aquests paràmetres
 
Editem el fitxer de configuració amb aquests paràmetres
 
  root@OpenWrt:~# vi /etc/squid/squid.conf
 
  root@OpenWrt:~# vi /etc/squid/squid.conf
  
 +
===Fitxer de configuració===
 
  http_port 3128 transparent
 
  http_port 3128 transparent
 
  hierarchy_stoplist cgi-bin ?
 
  hierarchy_stoplist cgi-bin ?
Línia 115: Línia 117:
  
  
'''Script d'inicilització:'''
+
===Script d'inicilització:===
  
  
Línia 126: Línia 128:
 
  NAME=squid
 
  NAME=squid
 
  DESC="Squiq web proxy"
 
  DESC="Squiq web proxy"
 
 
  #If lan
 
  #If lan
 
  LAN="br-lan"
 
  LAN="br-lan"
 
 
  #Ruta on hi ha l'iptables
 
  #Ruta on hi ha l'iptables
 
  IPT=/usr/sbin/iptables
 
  IPT=/usr/sbin/iptables
   
+
  LOGPATH=/var/log
 +
PIDPATH=/var/log/squid.pid
 
  #Rang de la xarxa local
 
  #Rang de la xarxa local
 
  LOCAL_LAN="192.168.1.0/24"
 
  LOCAL_LAN="192.168.1.0/24"
 
 
  SQUID_SERVER="127.0.0.1"
 
  SQUID_SERVER="127.0.0.1"
 
  SQUID_PORT="3128"
 
  SQUID_PORT="3128"
 
   
 
   
case "$1" in
 
start)
 
 
   
 
   
  # if [ -e /var/log/$NAME.pid ]; then
+
  # Obrim port a iptables
#  echo "$DESC: $NAME ja iniciat."
+
#  exit 0
+
#  fi
+
+
echo -n "Iniciant $DESC: $NAME"
+
 
  $IPT -A INPUT -i $LAN -p tcp --dport $SQUID_PORT -j ACCEPT
 
  $IPT -A INPUT -i $LAN -p tcp --dport $SQUID_PORT -j ACCEPT
 
   
 
   
  #Només si volem proxy trasparent
+
  # mirem si existeix $LOGPATH
  #$IPT -t nat -A PREROUTING -s $LOCAL_LAN -p tcp --dport 80 -j DNAT --to $SQUID_SERVER:$SQUID_PORT
+
  test -d $LOGPATH || mkdir -p $LOGPATH || chmod 744 $LOGPATH
  #$IPT -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT
+
 
   
 
   
  test -d /var/log || mkdir -p /var/log/
+
  case "$1" in
 +
start)
 
   
 
   
#Molt perillós, cal millorar-ho
+
  # Comprovant que no estigui iniciat.
#chmod 744 /var/log -R
+
  if [ -e $PIDPATH ]; then
/usr/sbin/$NAME -d 9
+
      echo "$DESC: $NAME ja iniciat."
test -d /var/log || mkdir -p /var/log/
+
      exit 0
 +
  fi
 
   
 
   
#test -e /var/run/$NAME.pid || touch /var/run/$NAME.pid
+
  echo -n "Iniciant $DESC: $NAME"
#/usr/sbin/$NAME
+
     
echo "."
+
  $NAME -d 9
 +
     
 +
        echo "."
 
  ;;
 
  ;;
 
   
 
   
 
  stop)
 
  stop)
 
   
 
   
# if [ ! -e /var/run/$NAME.pid ]; then
+
    if [ ! -e $PIDPATH ]; then
#    echo "$DESC: $NAME no esta funcionant."
+
        echo "$DESC: $NAME no esta funcionant."
#    exit 0
+
        exit 0
  # fi
+
    fi
 +
   
 +
    echo -n "Parant $DESC: $NAME"
 +
   
 +
    $NAME -k shutdown
 
   
 
   
echo -n "Stopping $DESC: $NAME"
+
    #és molt lent a apagar, esperem 5 segons
#si volem anar a sac
+
    sleep 5
#killall -9 $NAME
+
rm -f $PIDPATH
#si volem ser una mica més nets
+
    echo "."
/usr/sbin/$NAME -k shutdown
+
#és molt lent a apagar, esperem 5 segons
+
sleep 5
+
rm -f /var/run/$NAME.pid
+
echo "."
+
 
  ;;
 
  ;;
 
   
 
   
  restart|force-reload)
+
  restart)
 
   
 
   
if [ ! -e /var/run/$NAME.pid ]; then
+
      if [ ! -e $PIDPATH ]; then
$0 start
+
      $0 start
exit 0
+
      exit 0
fi
+
      fi
echo -n "Restart $DESC: $NAME"
+
     
 +
      echo -n "Restart $DESC: $NAME"
 +
     
 +
      $NAME -k shutdown
 +
      sleep 15
 +
      rm -f $PIDPATH
 +
 
 +
  count=1
 +
     
 +
      while [ -e $PIDPATH ]
 +
      do
 +
$NAME -k shutdown
 +
sleep 10
 +
rm -f $PIDPATH
 +
count=$((count+1))
 +
 
 +
if [ $count -eq 4 ]; then
 +
echo "no puc matar el procés intenta-ho amb force-reload"
 +
exit 1
 +
fi
 +
      done
 +
     
 +
     
 +
      if [ ! -e $PIDPATH ]; then
 +
$0 start
 +
exit 0
 +
      fi
 +
 
 +
      echo -n "Iniciant $DESC: $NAME"
 +
      $NAME -d 9
 +
     
 +
      echo "."
 +
;;
 
   
 
   
  /usr/sbin/$NAME -k shutdown
+
  force-reload)
sleep 5
+
     
rm -f /var/run/$NAME.pid
+
      if [ ! -e $PIDPATH ]; then
$IPT -A INPUT -i $LAN -p tcp --dport $SQUID_PORT -j ACCEPT
+
$0 start
#Trasparent
+
exit 0
#$IPT -t nat -A PREROUTING -s $LOCAL_LAN -p tcp --dport 80 -j DNAT --to $SQUID_SERVER:$SQUID_PORT
+
      fi
#$IPT -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT
+
     
test -d /var/log || mkdir -p /var/log/
+
      echo -n "Restart $DESC: $NAME"
#perill
+
     
#chmod 755 /var/log -R
+
     
/usr/sbin/$NAME -d 9
+
      $NAME -k shutdown
test -d /var/log || mkdir -p /var/log/
+
      sleep 10
sleep 5
+
      rm -f $PIDPATH
echo "."
+
      count=1
 +
     
 +
      while [ -e $PIDPATH ]
 +
      do
 +
killall -9 $NAME
 +
sleep 10
 +
rm -f $PIDPATH
 +
  count=$((count+1))
 +
 
 +
if [ $count -eq 4 ]; then
 +
echo "no puc matar el procés"
 +
exit 1
 +
fi
 +
      done
 +
     
 +
     
 +
      if [ ! -e $PIDPATH ]; then
 +
$0 start
 +
exit 0
 +
      fi
 
  ;;
 
  ;;
 
   
 
   
 
  *)
 
  *)
 
   
 
   
N=/etc/init.d/$NAME
+
    N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
+
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
+
    ;;
echo -n "Restarting $DESC: $NAME"
+
/usr/sbin/$NAME -k shutdown
+
sleep 25
+
rm -f /var/run/$NAME.pid
+
+
#Regles d'iptables
+
$IPT -A INPUT -i $LAN -p tcp --dport $SQUID_PORT -j ACCEPT
+
$IPT -t nat -A PREROUTING -s $LOCAL_LAN -p tcp --dport 80 -j DNAT --to $SQUID_SERVER:$SQUID_PORT
+
$IPT -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT
+
test -d /var/log || mkdir -p /var/log/
+
                                                               
+
/usr/sbin/$NAME -d 9
+
test -d /var/log || mkdir -p /var/log/
+
sleep 5
+
echo "."
+
;;
+
 
   
 
   
 
  esac
 
  esac
 
  exit 0
 
  exit 0
  
root@OpenWrt:# chmod 755 /etc/init.d/squid
 
  
  root@OpenWrt:# ln -s /etc/init.d/squid /etc/rc.d/S99squid
+
  root@OpenWrt:/etc/init.d# chmod 755 /etc/init.d/squid
 +
 
 +
root@OpenWrt:/etc/init.d# ln -s /etc/init.d/squid /etc/rc.d/S99squid
 +
 
 +
 
 +
===Problemes típics===
 +
 
 +
- Si no va mira quin gateway tens (route i mirar el default) i que tinguis routing cap al parent server de l'squid. Prova de fer un ping a veure si et respon, a vegades hi ha nodes caiguts que fan que no puguis arribar al proxy.
 +
 
 +
 
 +
 +
===Comandes bàsiques d'squid===
 +
 
 +
Aturar
 +
root@OpenWrt:/#squid -k shutdown
 +
 
 +
Engegar amb debug
 +
root@OpenWrt:/#squid -d 9
 +
 
 +
Debug + no comprovar dns (per si no tenim routing dns)
 +
root@OpenWrt:/#squid -d 9 -D
 +
 
 +
[[es:Squid-openwrt-padres]]

Revisió de 21:10, 28 juny 2012

Conmutació de proxys amb OpenWrt i Squid

Amb aquest document es pretén explicar com configurar un proxy perquè es validi amb altres proxys de la xarxa.

Nota: Aquí està explicat per OpenWrt però hauria de ser idèntic (menys els paquets) amb qualsevol linux.

Esquema

Navegador ----> Router OpenWrt -----> Proxy guifi extern -----> Internet

Avantatges, usos, inconvenient i perills

Avantatges i usos:

  • Si un proxy deixa d'estar operatiu no cal modificar la configuració dels navegadors.
  • No demana contrasenyes de proxy als navegadors.
  • Si tenim un proxy amb Squid i ens cau la nostra línia podem redirigir el nostre transit a altres proxys mentre fem el manteniment


Inconvenients i perills:

  • Necessitem un router capaç de gestionar-ho.
  • Hem d'afegir un proxy a la nostra xarxa amb els manteniments que això suposa.


Configuració als navegadors

Als navegadors de la nostra xarxa haurem de configurar la ip del ROUTER i no la del proxy de guifi extern.

Si el nostre Squid té la ip 192.168.1.5 hem de configurar als navegadors 192.168.1.5:3128

Squid

Paquets necessaris:

root@OpenWrt: opkg update
root@OpenWrt: opkg install squid iptables-mod-nat-extra


Configuració d'Squid:

Fem una còpia de seguretat del fitxer de configuració original

root@OpenWrt:~# cp /etc/squid/squid.conf /etc/squid/squid.conf.original

Reset del fitxer de configuració

root@OpenWrt:~# echo > /etc/squid/squid.conf


Editem el fitxer de configuració amb aquests paràmetres

root@OpenWrt:~# vi /etc/squid/squid.conf

Fitxer de configuració

http_port 3128 transparent
hierarchy_stoplist cgi-bin ?
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
acl apache rep_header Server ^Apache
access_log /var/log/access.log squid
hosts_file /etc/hosts
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
#només permetem connexions des del rang local
acl all src 192.168.1.0/255.255.255.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563 # https, snews
acl SSL_ports port 873 # rsync
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 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 873 # rsync
acl Safe_ports port 901 # SWAT
acl purge method PURGE
acl CONNECT method CONNECT
http_access allow all
http_access allow manager all
http_access allow localhost
http_reply_access allow all
icp_access allow all
visible_hostname guifi-proxy-local
# always_direct allow all
nonhierarchical_direct off
# si volem que primer usi els parents "prefer_direct off"
prefer_direct off
coredump_dir /var/spool/squid
# No volem cache (els openwrt solen tenir poc espai!)
cache deny all
# Per si crea cache que ho faci a null 
cache_dir null /dev/nul
#si creieu que us podeu quedar sense espai podeu posar aquestes rutes a /dev/nul també
cache_store_log /var/log/store.log
cache_log /var/log/cache.log
pid_filename /var/log/squid.pid
 
#A partir d'aquí proxys pares de guifi, cal mirar quins et van millor i posar-hi el teu USUARI:CONTRASSENYA
jo ho tinc apuntat així al fitxer de configuració per saber quin és quin.

#10.138.27.163   BCNSettProxy    ONO - PING 23ms - 11.66Mb / 4.85Mb      -       8 salts
cache_peer 10.138.27.163 parent 3128  3128  proxy-only no-query no-digest  default login=USUARI:CONTRASSENYA	

#10.139.37.194   Vallcarca       Telefonica - PING 32ms - 10Mb/5Mb       -       3 salts
cache_peer 10.139.92.131 parent 3128  3128  proxy-only no-query no-digest  default login=USUARI:CONTRASSENYA
		
#10.138.27.98    BCNAragoOlostSrvr1   Jazztel - PING 45ms - 9Mb / 2Mb    -       6 salts
cache_peer 10.35.125.163 parent 3128  3128  proxy-only no-query no-digest  default login=USUARI:CONTRASSENYA

#10.139.36.66    agricultura 3128  ONO - PING 34ms - 7.79Mb / 4.48Mb     -       9 salts
cache_peer 10.139.36.66 parent 3128  3128  proxy-only no-query no-digest  default login=USUARI:CONTRASSENYA


Script d'inicilització:

AJUDA: Cal millorar-lo!

root@OpenWrt:~# vi /etc/init.d/squid
#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=squid
DESC="Squiq web proxy"
#If lan
LAN="br-lan"
#Ruta on hi ha l'iptables
IPT=/usr/sbin/iptables
LOGPATH=/var/log
PIDPATH=/var/log/squid.pid
#Rang de la xarxa local
LOCAL_LAN="192.168.1.0/24"
SQUID_SERVER="127.0.0.1"
SQUID_PORT="3128"


# Obrim port a iptables
$IPT -A INPUT -i $LAN -p tcp --dport $SQUID_PORT -j ACCEPT

# mirem si existeix $LOGPATH
test -d $LOGPATH || mkdir -p $LOGPATH  || chmod 744 $LOGPATH

case "$1" in
start)

 # Comprovant que no estigui iniciat.
 if [ -e $PIDPATH ]; then
     echo "$DESC: $NAME ja iniciat."
     exit 0
  fi

  echo -n "Iniciant $DESC: $NAME"
     
  $NAME -d 9
     
        echo "."
;;

stop)

   if [ ! -e $PIDPATH ]; then
        echo "$DESC: $NAME no esta funcionant."
        exit 0
   fi

   echo -n "Parant $DESC: $NAME"

   $NAME -k shutdown

   #és molt lent a apagar, esperem 5 segons
   sleep 5
	rm -f $PIDPATH
   echo "."
;;

restart)

     if [ ! -e $PIDPATH ]; then
      $0 start
      exit 0
     fi
     
     echo -n "Restart $DESC: $NAME"
     
     $NAME -k shutdown
     sleep 15
     rm -f $PIDPATH
	  
	  count=1
     
     while [ -e $PIDPATH ]
     do
		$NAME -k shutdown
		sleep 10
		rm -f $PIDPATH
		count=$((count+1))
  
		if [ $count -eq 4 ]; then
			echo "no puc matar el procés intenta-ho amb force-reload"
			exit 1
		fi
     done
     
     
     if [ ! -e $PIDPATH ]; then
		$0 start
		exit 0
     fi
	  
     echo -n "Iniciant $DESC: $NAME"
     $NAME -d 9
     
     echo "."
;;

force-reload)
     
     if [ ! -e $PIDPATH ]; then
		$0 start
		exit 0
     fi
     
     echo -n "Restart $DESC: $NAME"
     
     
     $NAME -k shutdown
     sleep 10
     rm -f $PIDPATH
     count=1
     
     while [ -e $PIDPATH ]
     do
		killall -9 $NAME
		sleep 10
		rm -f $PIDPATH
 		count=$((count+1))
  
		if [ $count -eq 4 ]; then
			echo "no puc matar el procés"
			exit 1
		fi
     done
     
     
     if [ ! -e $PIDPATH ]; then
		$0 start
		exit 0
     fi
;;

*)

   N=/etc/init.d/$NAME
   echo "Usage: $N {start|stop|restart|force-reload}" >&2
   ;;

esac
exit 0


root@OpenWrt:/etc/init.d# chmod 755 /etc/init.d/squid
root@OpenWrt:/etc/init.d# ln -s /etc/init.d/squid /etc/rc.d/S99squid


Problemes típics

- Si no va mira quin gateway tens (route i mirar el default) i que tinguis routing cap al parent server de l'squid. Prova de fer un ping a veure si et respon, a vegades hi ha nodes caiguts que fan que no puguis arribar al proxy.


Comandes bàsiques d'squid

Aturar

root@OpenWrt:/#squid -k shutdown 

Engegar amb debug

root@OpenWrt:/#squid -d 9 

Debug + no comprovar dns (per si no tenim routing dns)

root@OpenWrt:/#squid -d 9 -D
Eines de l'usuari