File: init-common.sh

package info (click to toggle)
squid-deb-proxy 0.8.15%2Bnmu1
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 252 kB
  • sloc: sh: 315; ansic: 145; python: 100; makefile: 32
file content (96 lines) | stat: -rw-r--r-- 2,910 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
AVAHIFILE=/etc/avahi/services/squid-deb-proxy.service

concat_file_from_dir() {
      # the .d directory
      DIR="$1"
      # the target file
      FILE="$2"
      # (optional) additional file append to $FILE
      ADDITIONAL_FILE_TO_CAT="$3"
      cat > $FILE <<EOF 
# WARNING: this file is auto-generated from the files in
#          $DIR
#          on squid-deb-proxy (re)start, do NOT edit here
EOF
      if [ -n "$ADDITIONAL_FILE_TO_CAT" ]; then
          cat "$ADDITIONAL_FILE_TO_CAT" >> "$FILE"
      fi

      for f in "$DIR"/*; do
          cat "$f" >> "$FILE"
      done
}

pre_start() {
  if [ -x /usr/sbin/squid ]; then
      SQUID=/usr/sbin/squid
  else
      echo "No squid binary found"
      exit 1
  fi

  # ensure all cache dirs are there
  install -d -o proxy -g proxy -m 750 /var/cache/squid-deb-proxy/
  install -d -o proxy -g proxy -m 750 /var/log/squid-deb-proxy/
  if [ ! -d /var/cache/squid-deb-proxy/00 ]; then
   $SQUID -z -N -f /etc/squid-deb-proxy/squid-deb-proxy.conf
  fi

  # generate pkg blacklist acl file
  PKG_BLACKLIST_DIR=/etc/squid-deb-proxy/pkg-blacklist.d
  PKG_BLACKLIST=/etc/squid-deb-proxy/autogenerated/pkg-blacklist-regexp.acl
  concat_file_from_dir "$PKG_BLACKLIST_DIR" "$PKG_BLACKLIST"
  # postprocess for regexp format
  sed -i -r '/^#/d;/^$/d;s#(.*)#\/\1_.*\.deb$#g' $PKG_BLACKLIST

  # generate mirror file
  MIRROR_DESTDOMAIN_DIR=/etc/squid-deb-proxy/mirror-dstdomain.acl.d
  MIRROR_DESTDOMAIN=/etc/squid-deb-proxy/autogenerated/mirror-dstdomain.acl
  concat_file_from_dir "$MIRROR_DESTDOMAIN_DIR" "$MIRROR_DESTDOMAIN" "/etc/squid-deb-proxy/mirror-dstdomain.acl"

  # generate the allowed-networks file
  ALLOWED_NETWORKS_DIR=/etc/squid-deb-proxy/allowed-networks-src.acl.d
  ALLOWED_NETWORKS=/etc/squid-deb-proxy/autogenerated/allowed-networks-src.acl
  concat_file_from_dir "$ALLOWED_NETWORKS_DIR" "$ALLOWED_NETWORKS" "/etc/squid-deb-proxy/allowed-networks-src.acl"
}

post_start() {
  # create avahi service
  PORT=$(grep http_port /etc/squid-deb-proxy/squid-deb-proxy.conf|cut -d' ' -f2)
  if [ -n "$PORT" ] && [ -d /etc/avahi/services/ ]; then
      (umask 022 && cat > $AVAHIFILE << EOF
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
	<name replace-wildcards="yes">Squid deb proxy on %h</name>
	<service protocol="ipv6">
		<type>_apt_proxy._tcp</type>
		<port>$PORT</port>
	</service>
	<service protocol="ipv4">
		<type>_apt_proxy._tcp</type>
		<port>$PORT</port>
	</service>
</service-group>
EOF
)
  fi
}

post_stop() {
  # remove avahi file again
  rm -f $AVAHIFILE
}

# from the squid debian init script
find_cache_dir () {
        w="     " # space tab
        res=`sed -ne '
                s/^'$1'['"$w"']\+[^'"$w"']\+['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
                t end;
                d;
                :end q' < $CONFIG`
        [ -n "$res" ] || res=$2
        echo "$res"
}