|
#!/usr/bin/env bash |
|
|
|
# Deal with some proxy pain for Linux apps! Note: |
|
# - Default is let desktop env manage proxy env |
|
# - However, GNOME does't always set the proxy enviroment when an application is |
|
# executed via a .desktop file #sadpanda |
|
# - This tries to help pull org.gnome.proxy settings into http_proxy, etc |
|
# - `gsettings list-recursively org.gnome.system.proxy` |
|
|
|
set -e |
|
|
|
function un_single_quote() { |
|
s="$1" |
|
s=${s%\'} |
|
s=${s#\'} |
|
echo "$s" |
|
} |
|
|
|
function proxy_on() { |
|
# http org.gnome.system.proxy.http |
|
http_host="$(un_single_quote "$(gsettings get org.gnome.system.proxy.http host)")" |
|
http_port="$(un_single_quote "$(gsettings get org.gnome.system.proxy.http port)")" |
|
export http_proxy="http://$http_host:$http_port" |
|
export HTTP_PROXY=$http_proxy |
|
# org.gnome.system.proxy.https |
|
https_host="$(un_single_quote "$(gsettings get org.gnome.system.proxy.https host)")" |
|
https_port="$(un_single_quote "$(gsettings get org.gnome.system.proxy.https port)")" |
|
export https_proxy="https://$https_host:$https_port" |
|
export HTTPS_PROXY=$https_proxy |
|
# org.gnome.system.proxy.ftp |
|
ftp_host="$(un_single_quote "$(gsettings get org.gnome.system.proxy.ftp host)")" |
|
ftp_port="$(un_single_quote "$(gsettings get org.gnome.system.proxy.ftp port)")" |
|
export ftp_proxy="ftp://$ftp_host:$ftp_port" |
|
export FTP_PROXY=$ftp_proxy |
|
# socks org.gnome.system.proxy.socks |
|
# TODO: not proccessed for now |
|
# org.gnome.system.proxy ignore-hosts |
|
ignore_hosts="$(gsettings get org.gnome.system.proxy ignore-hosts)" |
|
no_proxy="${ignore_hosts:1:-1}" |
|
no_proxy="${no_proxy// /}" |
|
no_proxy="${no_proxy//\'/}" |
|
export no_proxy |
|
export NO_PROXY=$no_proxy |
|
} |
|
|
|
function proxy_off() { |
|
unset no_proxy |
|
unset NO_PROXY |
|
unset http_proxy |
|
unset HTTP_PROXY |
|
unset https_proxy |
|
unset HTTPS_PROXY |
|
unset ftp_proxy |
|
unset FTP_PROXY |
|
} |
|
|
|
# check for unsupported settings |
|
case "$(un_single_quote "$(gsettings get org.gnome.system.proxy mode)")" in |
|
manual) |
|
proxy_on |
|
;; |
|
auto) |
|
echo "ERROR: Auto, autoconfig-url (PAC) is not yet supported" >&2 |
|
exit 1 |
|
;; |
|
none) |
|
proxy_off |
|
;; |
|
*) |
|
echo "ERROR: org.gnome.system.proxy mode unknown" >&2 |
|
exit 1 |
|
;; |
|
esac |
|
if [[ "$(gsettings get org.gnome.system.proxy.http use-authentication)" == 'true' ]]; then |
|
echo "ERROR: HTTP proxy authentication is not yet supported" >&2 |
|
exit 1 |
|
fi |
|
|
|
# if not sourced, then wrap |
|
if ! [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then |
|
if [ -z "$1" ] || [ "$1" == '-h' ] || [ "$1" == '--help' ] ; then |
|
echo 'Usage:' |
|
echo ' - This can be sourced, e.g.' |
|
echo ' `. gnome_proxy_to_env.sh; <command>`' |
|
echo ' - Or can wrap the command, e.g.' |
|
echo ' `./gnome_proxy_to_env.sh <command/application>``' |
|
elif [ "$#" -gt 0 ]; then |
|
exec $@ |
|
fi |
|
fi |
Hi,
thanks for the script!
I made a small improvement (handling empty fields). Perhaps you want to apply that changes, too.