summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2012-09-09 21:49:21 +0200
committerFlorian Bruhin <me@the-compiler.org>2012-09-09 21:49:21 +0200
commitc266292d1c685e0c67d5480c2043bd9d36716519 (patch)
tree989a5f78664c8592ea1ea71bba78acc099367a68
downloadproxyswitch_polipo-c266292d1c685e0c67d5480c2043bd9d36716519.tar.gz
proxyswitch_polipo-c266292d1c685e0c67d5480c2043bd9d36716519.zip
initial commit
-rw-r--r--proxies.sample2
-rw-r--r--proxyswitch.sh95
2 files changed, 97 insertions, 0 deletions
diff --git a/proxies.sample b/proxies.sample
new file mode 100644
index 0000000..a161125
--- /dev/null
+++ b/proxies.sample
@@ -0,0 +1,2 @@
+proxy1 socks://user:password@hostname:port
+http http://example.com
diff --git a/proxyswitch.sh b/proxyswitch.sh
new file mode 100644
index 0000000..41e84e4
--- /dev/null
+++ b/proxyswitch.sh
@@ -0,0 +1,95 @@
+#!/bin/bash
+# vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :
+
+# Copyright (C) 2012 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 [[ -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 "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
+fi
+
+if [[ $auth ]]; then
+ echo "parentAuthCredentials = \"$auth\" # Added by proxyswitch" >> \
+ "$polipo_conf"
+fi
+
+"$service_file" restart