Thursday, March 6, 2014

[Debian/Ubuntu/Gnome] Set the proxy settings system-wide (apt, bash, wget, ssh, git, svn)

If you often change network locations, work with different network settings, you may find it annoying to update your proxy settings every time in all configuration files (/etc/apt/apt.conf, ~/.ssh/config, maybe proxychains, environment variables and gnome settings).

I use the following script to update the settings in every location. I may add other files to the script when required.

Currently the script with update your proxy settings for:

  • Apt (if running as root)
  • Proxychains (if running as root)
  • Firefox
  • Chrome / Gnome / System proxy
  • SSH (for entries already containing "ProxyCommand")
  • Bash / Wget / any program using the $http_proxy environment variable
  • Git
  • SVN

Note: Some proxy tunnels are configured to use socat, so you probably need to install it before.

#!/bin/bash

if [ -z $2 ]; then
 echo "usage: $0 <proxy ip> <proxy port> [ <proxy user> <proxy pass> ]"
 exit
fi

if [ $EUID -eq 0 ]; then
 # Apt.conf
 echo "updating apt.conf..."
 if [ $# -eq 4 ]; then
  echo "Acquire::http::Proxy \"http://$3:$4@$1:$2/\";" > /etc/apt/apt.conf
 else
  echo "Acquire::http::Proxy \"http://$1:$2/\";" > /etc/apt/apt.conf
 fi
 chmod 600 /etc/apt/apt.conf

 # Proxychains
 if [ -e /etc/proxychains.conf ]; then
  echo "updating proxychains.conf..."
  echo strict_chain > /etc/proxychains.conf
  echo tcp_read_time_out 15000 >> /etc/proxychains.conf
  echo tcp_connect_time_out 8000 >> /etc/proxychains.conf
  echo \[ProxyList\] >> /etc/proxychains.conf
  if [ $# -eq 4 ]; then
   echo http $1 $2 $3 $4 >> /etc/proxychains.conf
  else
   echo http $1 $2 >> /etc/proxychains.conf
  fi
  chmod 600 /etc/proxychains.conf
 fi
else
 echo "Warning: Must run as root to update all config files (skipped: apt, proxychains)"
fi

# Firefox
PREFS_FILE="${HOME}/.mozilla/firefox/$(cat ${HOME}/.mozilla/firefox/profiles.ini | grep Path | sed 's/Path=//')/prefs.js"
if [ -e $PREFS_FILE ]; then
 echo "updating firefox config..."
 sed -i '/^user_pref("network.proxy./d' $PREFS_FILE
 echo "user_pref(\"network.proxy.ftp\", \"$1\");" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.ftp_port\", $2);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.http\", \"$1\");" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.http_port\", $2);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.share_proxy_settings\", true);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.socks\", \"$1\");" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.socks_port\", $2);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.socks_remote_dns\", true);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.ssl\", \"$1\");" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.ssl_port\", $2);" >> $PREFS_FILE
 echo "user_pref(\"network.proxy.type\", 1);" >> $PREFS_FILE
fi

# ssh
if [ -e ~/.ssh/config ]; then
 echo "updating ssh config..."
 if [ $# -eq 4 ]; then
  CFG="ProxyCommand socat - PROXY:$1:%h:%p,proxyport=$2,proxyauth=$3:$4"
 else
  CFG="ProxyCommand socat - PROXY:$1:%h:%p,proxyport=$2"
 fi
 sed -i -r "s/ProxyCommand socat (.*)/$CFG/g" ~/.ssh/config
 chmod 600 ~/.ssh/config
fi

# Bash / Wget
if [ -e ~/.bashrc ]; then
 echo "updating ~/.bashrc..."

 sed -i '/export http[s]*_proxy=/d' ~/.bashrc

 if [ $# -eq 4 ]; then
  echo "export http_proxy=http://$3:$4@$1:$2/" >> ~/.bashrc
 else
  echo "export http_proxy=http://$1:$2/" >> ~/.bashrc
 fi

 echo "export https_proxy=\$http_proxy" >> ~/.bashrc

 source ~/.bashrc
fi

# Gnome / system
echo "updating system proxy..."
if [ $# -eq 4 ]; then
 gsettings set org.gnome.system.proxy.http authentication-user "$3"
 gsettings set org.gnome.system.proxy.http authentication-password "$4"
else
 gsettings set org.gnome.system.proxy.http authentication-user ""
 gsettings set org.gnome.system.proxy.http authentication-password ""
fi
gsettings set org.gnome.system.proxy mode "manual"
gsettings set org.gnome.system.proxy.http host "$1"
gsettings set org.gnome.system.proxy.http port $2
gsettings set org.gnome.system.proxy.ftp host "$1"
gsettings set org.gnome.system.proxy.ftp port $2
gsettings set org.gnome.system.proxy.https host "$1"
gsettings set org.gnome.system.proxy.https port $2
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16', '*.localdomain.com', '*.mycompany.com' ]"

# Git
if [ -e ~/.gitconfig ]; then
 echo "updating git..."
 if [ $# -eq 4 ]; then
  echo "exec socat STDIO PROXY:$1:\$1:\$2,proxyport=$2,proxyauth=$3:$4" > ~/.gitproxy
 else
  echo "exec socat STDIO PROXY:$1:\$1:\$2,proxyport=$2" > ~/.gitproxy
 fi
 git config --global core.gitproxy ~/.gitproxy
fi

# SVN
if [ -e ~/.subversion/servers ]; then
 echo "updating subversion..."
 if [ $# -eq 4 ]; then
  sed -i "s/^[# ]*\(http-proxy-username\).*/\1 = $3/" ~/.subversion/servers
  sed -i "s/^[# ]*\(http-proxy-password\).*/\1 = $4/" ~/.subversion/servers
 else
  sed -i 's/^\(http-proxy-username.*\)/# \1/' ~/.subversion/servers
  sed -i 's/^\(http-proxy-password.*\)/# \1/' ~/.subversion/servers
 fi
 sed -i "s/^[# ]*\(http-proxy-host\).*/\1 = $1/" ~/.subversion/servers
 sed -i "s/^[# ]*\(http-proxy-port\).*/\1 = $2/" ~/.subversion/servers
fi

No comments:

Post a Comment