#!/bin/bash
. /usr/lib/eole/diagnose.sh

EchoGras "*** Services Proxy"
# FIXME: squid tourne en 127.0.0.1
#TestService "proxy" $container_ip_proxy:8080
TestPid "proxy" squid3

USE_E2=$(CreoleGet use_e2guardian)

if [[ ${USE_E2} == "oui" ]]
then
  DAEMON="e2guardian"
else
  DAEMON="dansguardian"
fi

TestProxy() {
    COMMENT=$1
    IP=$2
    printf ".  %${len_pf}s => " "$COMMENT"
    for dnsname in $(CreoleGet serveur_maj); do
        ok=0
        CreoleRun "http_proxy=http://$IP:3128/ wget -q -t2  --connect-timeout=3 --read-timeout=3 --delete-after $dnsname" proxy
        if [ $? -eq 0 ]
        then
            EchoVert "Ok"
            ok=1
            break
        fi
    done
    [ "$ok" = "0" ] && EchoRouge "Erreur"
}

if [ $(CreoleGet activer_filtrage_proxy) = "oui" ];then
    nombre_interfaces=$(CreoleGet nombre_interfaces)
    container_ip_proxy=$(CreoleGet container_ip_proxy)
    container_actif=$(CreoleGet mode_conteneur_actif)
    instance_1_activate=0
    instance_2_activate=0
    # FIXME: tests supplémentaires sans le filtrage ?
    if [ $nombre_interfaces -eq 1 ];then
        if [ "$container_actif" = "oui" ];then
            ## mode conteneur ##
            TestProxy "Filtre web" $(CreoleGet adresse_ip_eth0_proxy_link)
        else
            ## mode non conteneur ##
            TestProxy "Filtre web" $(CreoleGet adresse_ip_eth0)
        fi
        if [ "$container_actif" = "oui" ];then
            ## mode conteneur ##
            IP=$(CreoleGet adresse_ip_eth0_proxy_link)
        else
            ## mode non conteneur ##
            IP=$(CreoleGet adresse_ip_eth0)
        fi
        if [ "$(CreoleGet dansguardian_eth0)" = "1" ]; then
            instance_1_activate=$IP
        elif [ "$(CreoleGet dansguardian_eth0)" = "2" ]; then
            instance_2_activate=$IP
        fi
    else
        for numint in `seq 1 $[nombre_interfaces-1]`; do
            if [ "$container_actif" = "oui" ];then
                ## mode conteneur ##
                IP=$(CreoleGet adresse_ip_eth${numint}_proxy_link)
            else
                ## mode non conteneur ##
                IP=$(CreoleGet adresse_ip_eth$numint)
            fi
            if [ "$(CreoleGet dansguardian_eth$numint)" = "1" ]; then
                instance_1_activate=$IP
            elif [ "$(CreoleGet dansguardian_eth$numint)" = "2" ]; then
                instance_2_activate=$IP
            fi
            nom_zone_eth=$(CreoleGet nom_zone_eth$numint)
            TestProxy "Filtre web $nom_zone_eth ($(CreoleGet nom_machine_eth$numint))" $IP
            if [ "$container_actif" = "non" ];then
                if [ "$(CreoleGet alias_eth$numint)" = "oui" ]; then
                    numalias=0
                    for ip in $(CreoleGet alias_ip_eth$numint); do
                        numalias=$((numalias+1))
                        int="$(CreoleGet nom_zone_eth$numint):$numalias"
                        TestProxy "Filtre web $int" $ip
                    done
                fi
                if [ "$(CreoleGet vlan_eth$numint)" = "oui" ]; then
                    VLAN_ID=($(CreoleGet vlan_id_eth${numint}))
                    VLAN_IP=($(CreoleGet vlan_ip_eth${numint}))
                    NB_VLAN=${#VLAN_ID[*]}
                    for ((id=0; id < $NB_VLAN; id+=1))
                    do
                        int="$nom_zone_eth.${VLAN_ID[id]}"
                        TestProxy "Filtre web $int" "${VLAN_IP[id]}"
                    done
                fi
            fi
        done
      fi
    if [ ! "$instance_1_activate" = "0" ]; then
        MAX=$(CreoleGet maxchildren1)
        printf ".  %${len_pf}s => " "Nb instance 1"
        NB=$(CreoleRun "ss -ltnp 'sport = $instance_1_activate:3128'|grep -o '${DAEMON}'|wc -l" proxy)
        if [ $((NB*100/$MAX)) -lt 90 ]; then
            EchoVert "$NB/$MAX"
        else
            EchoRouge "$NB/$MAX"
        fi
    fi
    if [ ! "$instance_2_activate" = "0" ]; then
        MAX=$(CreoleGet maxchildren2)
        printf ".  %${len_pf}s => " "Nb instances 2"
        NB=$(CreoleRun "ss -ltnp 'sport = $instance_2_activate:3128'|grep -o 'dansguardian'|wc -l" proxy)
        if [ $((NB*100/$MAX)) -lt 90 ]; then
            EchoVert "$NB/$MAX"
        else
            EchoRouge "$NB/$MAX"
        fi
    fi
fi
echo
exit 0
