File: setproxy

package info (click to toggle)
whereami 0.3.36
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: sh: 1,094; perl: 524; makefile: 24
file content (85 lines) | stat: -rwxr-xr-x 1,949 bytes parent folder | download | duplicates (7)
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
#!/bin/bash 

# This script is based on one written Fedor Karpelevitch for "oops"
# to reconfigure the proxy to set the upstream proxy.  I have now
# modified it to also handle "Squid", since I decided to switch
# my local proxy to that instead.

UPSTREAM="$@"

#
# If several proxies are installed we try and deal to all of them
# because it's just too damn much work to figure which one has control!
#

#  The proxies we know how to deal with...
PROXIES="oops squid"
RELOADACTION="force-reload"

modify_conffile() {
  CONFIGURATION="$1"
  shift
  NEWPROXY="$@"
  PROXYCOMMAND="$1"

  if [ "${UPSTREAM}" != "none" ]; then 
    echo "${NEWPROXY}" > ${CONFIGURATION}.N
  fi
  grep -v "^${PROXYCOMMAND} " ${CONFIGURATION} >> ${CONFIGURATION}.N

  mv ${CONFIGURATION}.N ${CONFIGURATION}
}

stopit() {
  echo "Stopping ${PROXY}"
  /etc/init.d/${PROXY} stop
}

startit() {
  echo "Starting ${PROXY}"
  /etc/init.d/${PROXY} start
}

reloadit() {
  echo "Reloading ${PROXY}"
  /etc/init.d/${PROXY} ${RELOADACTION}
}

redirect_oops() {
  echo "Setting OOPS upstream proxy host ($UPSTREAM)" 
  modify_conffile /etc/oops/oops.cfg parent ${UPSTREAM}
}


redirect_squid() {
  PARENT=$1
  TYPE=$2
  # This bit of mucking around lets us handle things in an
  # appropriate way if we just have "setproxy <parent> <port>
  # as we would if we switched from OOPS -> Squid...
  if [ "${TYPE}" = "parent" -o "${TYPE}" = "sibling" ] ; then
    shift
  else
    TYPE=parent
  fi
  PORT=${2:-"3128"}
  ICP_PORT=${3:-"3130"}
  OPTIONS=${4:-"default"}

  echo "Setting Squid upstream proxy host (${PARENT}:${PORT})" 
  modify_conffile /etc/squid/squid.conf cache_peer ${PARENT} ${TYPE} ${PORT} ${ICP_PORT} ${OPTIONS}
}


for PROXY in ${PROXIES} ; do
  if [ -x /usr/sbin/${PROXY} ] ; then
    if [ "$UPSTREAM" = "start" ]; then 
      startit
    elif [ "$UPSTREAM" = "stop" ]; then 
      stopit
    else
      redirect_${PROXY} ${UPSTREAM}
      reloadit
    fi
  fi
done