Skip to content

Instantly share code, notes, and snippets.

@nlpsuge
Forked from JPvRiel/gnome_proxy_to_env.md
Created April 11, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlpsuge/e93b6fd574faf3fe94a0578f193a4422 to your computer and use it in GitHub Desktop.
Save nlpsuge/e93b6fd574faf3fe94a0578f193a4422 to your computer and use it in GitHub Desktop.
a shell wrapper to pull org.gnome.proxy settings into env, e.g. http_proxy, which is useful for .desktop files

Why?

Useful for:

  • updating current terminal's shell env proxy settings, or
  • wrapping exec in .desktop files to inject env proxy settings

GNOME proxy settings should normally get propergated into the shell (bash) environment via gnome-terminal, e.g. http_proxy and no_proxy. However:

  • I noticed that applications exectuted via .desktop entries sometimes don't work. This happens when an app ignores org.gnome.proxy settings but can often use proxy env vars from the shell.
  • If you're proxy settings change, existing shell processes will still have the old proxy enviroment variables, so this can help refresh them whithout having to create a new shell.

How?

Telegram .desktop file example

See #2777 for Telegram Desktop. Bascially:

  • It's defaults works with http_proxy when executed via a shell
  • It's defaults don't work via the telegramdesktop.desktop file

E.g. if avaialbe via $PATH:

...
Exec=gnome_proxy_to_env.sh Telegram -- %u
...

Bash

Sourcing the proxy settings into a current shell (changes current shell's proxy env vars)

$ source gnome_proxy_to_env.sh
$ grep http_proxy
http_proxy=http://localhost:8080

Wrap a command (doesn't change current shell's proxy proxy env vars, only called shell subprocess)

$ ./gnome_proxy_to_env env.sh | grep http_proxy
http_proxy=http://localhost:8080
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment