#!/bin/bash
# Script um die cache_peer funktion von Squid upzudaten.
# V1.0
# 13.10.2010
# Stoeckholzer, Haas

if [ ${#1} == 0 ];
then
echo "USAGE: write_conf_file updatetime"
exit 0
fi

while [ "1" == "1" ] ; do

sleep $1
zero="1"

while [ $zero == "1" ] ; do

# Zaehlvariable einlesen
id=$(cat /var/vpn/vpnid)

# id inkrementieren
let id=$id+1

# 255 Overflow verhindern
if [ $id -gt 254 ]
then id=0
fi

# Variable id fuer naechsten Durchlauf speichern
echo $id > /var/vpn/vpnid

# id HEX wandeln
hex=$(echo "obase=16; $id" |bc)

# Abfrage ob Strinlaenge von hex kleiner als 2
if [ ${#hex} -lt 2  ];
then 	hex=0$hex
fi

# Login-Daten aus Datenbank holen
credentials=$(/home/christoph/squid_dynamic/squidy $hex)
# Abfrage ob Datensatz leer :

if [ ${#credentials} -gt 10 ];
then
	zero="0"
fi
echo "Schleife zero: $zero"
echo "Schleife id: $id"
echo "Schleife hex: $hex"
done
echo "USING zero: $zero"
echo "USING id: $id"
echo "USING hex: $hex"
echo "USING credentials: $credentials"

sudo killall -9 unlinkd
sudo killall -9 squid

sudo rm /etc/squid3/squid.conf
######################################################################################

# Config- Datei schreiben.
sudo echo -e -n "
# Squid config by h44z

#  TAG: http_port
#   Usage:  port
#       hostname:port
#       1.2.3.4:port
http_port 10.8.0.1:3128 transparent
http_port 10.8.0.1:8080
#https_port 10.8.0.1:8181 key=/etc/apache2/ssl/apache.pem


visible_hostname vpnrack

#  TAG: icp_port
icp_port 0

#  TAG: no_cache
acl QUERY urlpath_regex cgi-bin \\?
no_cache deny QUERY

#  TAG: cache_mem   (bytes)
cache_mem 32 MB

#  TAG: cache_dir
cache_dir ufs /var/cache/squid 100 16 256

#  TAG: cache_access_log
cache_access_log /var/log/squid/access.log

#  TAG: cache_log
cache_log /var/log/squid/cache.log

#  TAG: cache_store_log
cache_store_log /var/log/squid/store.log

#  TAG: emulate_httpd_log       on|off
emulate_httpd_log on

#  TAG: pid_filename
pid_filename /var/run/squid.pid

#  TAG: cache_mgr
cache_mgr christoph.haas2@students.htlinn.ac.at
#blubber

http_access allow all
acl https port 443
http_access allow https


# TAG: http_reply_access
# Allow replies to client requests. This is complementary to http_access.
#http_reply_access allow all

cache_peer 192.168.195.101 parent 8080 7 no-query default no-digest login=$credentials

never_direct allow all
" > /etc/squid3/squid.conf
echo "Written credentials: $credentials"
######################################################################################
#restart squid
sudo squid -D -YC -f /etc/squid3/squid.conf

done
