summaryrefslogtreecommitdiffstats
path: root/proxyswitch.sh
blob: 0f3e30a4f87419a14a1e69422f223bb453f263ce (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :

# Copyright (C) 2012-2013 Florian Bruhin <me@the-compiler.org>

# proxyswitch-polipo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# proxyswitch-polipo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with proxyswitch-polipo  If not, see <http://www.gnu.org/licenses/>.

polipo_conf=/etc/polipo/config
if [[ -z "$1" ]]; then
    echo "Usage: $0 <proxy-name>"
    exit 1
fi
if [[ $UID != 0 ]]; then
    echo "Error: this tool needs to be started with root rights."
    exit 1
fi
if [[ ! -f "$polipo_conf" ]]; then
    echo "Error: polipo config not found!"
    echo "Is polipo really installed?"
    exit 1
fi
if which systemctl &>/dev/null; then
    systemd=1
elif [[ -f /etc/rc.d/polipo ]]; then
    service_file="/etc/rc.d/polipo"
elif [[ -f /etc/init.d/polipo ]]; then
    service_file="/etc/init.d/polipo"
else
    echo "Error: Neither /etc/rc.d/polipo nor /etc/init.d/polipo exist."
    echo "Also systemd is not installed."
    echo "Is polipo really installed?"
    exit 1
fi

declare -A proxies
while read name url; do
    proxies["$name"]="$url"
done < ./proxies

proxy="${proxies["$1"]}"

if [[ $proxy == *://* ]]; then # Protocol in proxy-url
    protocol="${proxy%%://*}"
    proxy="${proxy#*://}"
else
    protocol=http
fi

if [[ $proxy == *@* ]]; then # User/password before host
    auth="${proxy%@*}"
    host="${proxy##*@}"
else
    host="${proxy}"
    auth=
fi
sed -i "
    # Delete old lines added by this script
    /# Added by proxyswitch\$/d;
    # Comment old proxy-lines in the config
    s/^parentAuthCredentials/# &/;
    s/parentProxy/# &/;
    s/socksParentProxy/# &/;
    s/socksProxyType/# &/;
    " "$polipo_conf"
if [[ $protocol == http ]]; then
    echo "parentProxy = \"$host\" # Added by proxyswitch" >> "$polipo_conf"
elif [[ $protocol == socks* ]]; then
    echo "socksParentProxy = \"$host\" # Added by proxyswitch" >> \
        "$polipo_conf"
    case $protocol in
        socks|socks5)
            echo "socksProxyType = socks5 # Added by proxyswitch" >> \
                "$polipo_conf"
            ;;
        socks4)
            echo "socksProxyType = socks # Added by proxyswitch" >> \
                "$polipo_conf"
            ;;
    esac
elif [[ $protocol == direct ]]; then
    true # Do nothing
else
    echo "Error: $protocol is an unknown protocol!"
    exit 1
fi

if [[ $auth ]]; then
    echo "parentAuthCredentials = \"$auth\" # Added by proxyswitch" >> \
        "$polipo_conf"
fi

if [[ $systemd ]]; then
    systemctl restart polipo
    status=$?
else
    "$service_file" restart
    status=$?
fi

if ((status != 0)); then
    echo "Error: restarting failed" >&2
    exit $status
fi