change firefox proxy from terminal

How can I change proxy for Firefox from Terminal app? Not the whole computer proxy settings (the ones in Network tab), but just Firefox's.


Solution 1:

Here is a shell script I drafted for Mac OS X to use SOCKS, which works okay for me. You can modify it toward your need. For you, you will need to modify values of network.proxy.http and network.proxy.http_port in prefs.js

#!/bin/bash
FIREFOX="/Applications/Firefox.app/Contents/MacOS/firefox"
PREDIR="$(ls $HOME/Library/Application\ Support/Firefox/Profiles/ | grep release)"
PREFILE="prefs.js"
FULLPATH="$HOME/Library/Application Support/Firefox/Profiles/$PREDIR/$PREFILE"

#IPADDR="localhost"
#PORT="7777"
#PROXYTYPE="1"

IPADDR="$1"
PORT="$2"
PROXYTYPE="$3"

TMPSOCKS="/tmp/network.proxy.socks.tmp"
TMPSOCKSPORT="/tmp/network.proxy.socks_port.tmp"


# Insert an ip into firefox for the proxy if there isn't one
if ! grep 'network.proxy.socks\"' "$FULLPATH" 
  then 
    echo "no network.proxy.socks defined"
    echo "no need to save value"
    echo 'user_pref("network.proxy.socks", "'"$IPADDR"'");' >> "$FULLPATH"
    echo 'user_pref("network.proxy.socks", "'"$IPADDR"'");' placed in $FULLPATH
  else 
    echo "network.proxy.socks exists, preserving value ..."
    grep 'network.proxy.socks\"' "$FULLPATH" > "$TMPSOCKS"
    sed -i '' 's/^.*network.proxy.socks".*$/user_pref("network.proxy.socks", "'"$IPADDR"'");/' "$FULLPATH"
  echo replaced network.proxy.socks to $IPADDR
fi

# Set the port
if ! grep network.proxy.socks_port "$FULLPATH" 
  then 
    echo "no network.proxy.socks_port value defined"
    echo "no need to save value"
    echo 'user_pref("network.proxy.socks_port", '''$PORT''');' >> "$FULLPATH" 
    echo 'user_pref("network.proxy.socks_port", '''$PORT''');' placed in "$FULLPATH"
  else 
    echo "network.proxy.socks_port found, preserving value ..."
    grep network.proxy.socks_port "$FULLPATH" > $TMPSOCKSPORT
    sed -i '' 's/^.*network.proxy.socks_port.*$/user_pref("network.proxy.socks_port", '''$PORT''');/' "$FULLPATH" 
    echo inserted port $PORT to network.proxy.socks_port
fi

# Turn on the proxy
if ! grep network.proxy.type  "$FULLPATH"
  then echo 'user_pref("network.proxy.type", '''$PROXYTYPE''');' >> "$FULLPATH" 
  else sed -i '' 's/^.*network.proxy.type.*$/user_pref("network.proxy.type", '''$PROXYTYPE''');/' "$FULLPATH" 
fi

# Open Firefox ...
/Applications/Firefox.app/Contents/MacOS/firefox

# After Firefox is closed, need to replace the original value
if [ -f "$TMPSOCKS" ];
    then 
    ORGSOCKS=`cat $TMPSOCKS`
    sed -i '' 's/^.*network.proxy.socks".*$/'''$ORGSOCKS'''/' "$FULLPATH"
fi
if [ -f "$TMPSOCKSPORT" ];
    then 
    ORGSOCKSPORT=`cat $TMPSOCKSPORT`
    sed -i '' 's/^.*network.proxy.socks_port.*$/'''$ORGSOCKSPORT'''/' "$FULLPATH"
fi

# Turn off the proxy
if ! grep network.proxy.type  "$FULLPATH"
  then echo 'user_pref("network.proxy.type", 0);' >> "$FULLPATH"
  else sed -i '' 's/^.*network.proxy.type.*$/user_pref("network.proxy.type", 0);/' "$FULLPATH"
fi