PDA

View Full Version : Change Firefox Proxy Settings From Python



aboutblank
April 20th, 2008, 03:47 AM
I'm writing a script to be run when my laptop connects to a wireless network. It will detect the SSID that it is connected to, and call a method for that SSID. When I connect to a network that is not my own, I want to open a tunnel and change Firefox proxy settings to use the http proxy on the other end of my tunnel. When I connect to my home network, I want to disregard the proxy.

So how do we modify Firefox proxy settings from python?

As a user, I can go through the GUI or go to about:config and set the "network.proxy.type" variable to "1" in order to enable using the current proxy settings.

From the programmers standpoint, those changes are reflected in prefs.js in your Firefox profile folder. However, if we modify this using a text editor while Firefox is running, the settings do not take effect. If we modify it while Firefox is not running, the settings do take effect when Firefox is started.

Anyone know of a way to change these variables (I'd have to change more than just that one to specify the proxy) from Python while Firefox is running?

heikaman
April 20th, 2008, 04:02 AM
This is from the prefs.js file:



# Mozilla User Preferences

/* Do not edit this file.
*
* If you make changes to this file while the application is running,
* the changes will be overwritten when the application exits.
*
...


so maybe you can make the changes and somehow restart firefox.

ghostdog74
April 20th, 2008, 04:47 AM
#!/bin/bash
pgrep firefox && pkill firefox
read -p "What value (1 or 0)" choice
find ~/ -type f -name "prefs.js" | while read -r files
do
awk -v val="$choice" 'BEGIN{FS=","}
/network\.proxy\.type/{
$0 = $1","val");"
}1' "$files" > temp
mv temp "$files"
done
firefox &

or you can follow the suggestion here (http://www.mozilla.org/unix/customizing.html#prefs) using user.js

mssever
April 21st, 2008, 03:28 AM
Another option is to change the system proxy settings through GConf. Here's a script that I wrote some time ago that does exactly that; I use it for SSH tunnelling.


#!/bin/bash

returnVal=
proxyLocalAddr="192.168.1.50"
proxyEnabled=
prog="$(basename "$0")"

function usage {
echo "This program sets up port forwarding to a proxy server. Once connected,"
echo "use 127.0.0.1:3128 as the proxy address."
echo
echo "Usage:"
echo " $prog [switch ip-or-hostname]"
echo
echo " switch is either --help or one of the following, which are synonyms:"
echo " --address"
echo " --ip"
echo " -a"
echo " -i"
echo " All of these switches must be followed by a hostname or IP address to"
echo " connect to."
echo
echo " If no switch is given, $prog will connect to scottseverance.us"
echo " to discover the last known IP address."
}

function reportError {
local exitStatus="$1"
local message="$2"
echo -e "$message" 1>&2
[ "$exitStatus" = "false" ] || exit $exitStatus
}

function getGconfProxy {
#local proxy="$(gconftool --get /system/http_proxy/use_http_proxy)"
local mode="$(gconftool --get /system/proxy/mode)"
if [ "$mode" = "none" ]; then returnVal='false'
else
returnVal="true"
fi
proxyMode="$mode"
}

function setGconfProxy {
local val="$1"
local mode="$2"
[ -n "$mode" ] || mode="$proxyMode"

gconftool --type bool --set /system/http_proxy/use_http_proxy "$val"
gconftool --type string --set /system/proxy/mode "$mode"
local exitStatus=$?
[ $exitStatus -ne 0 ] && reportError 'false' "Couldn't set the proxy setting to $mode.\ngconftool exited with status $exitStatus."
}

function getRemoteAddr {
# This line was necessary before OpenDNS enabled dynamic DNS updating. My router wouldn't do it automatically.
local ip="$(curl --fail 'http://some.domain.com/script/that/returns/last/known/ip' 2>/dev/null)"
[ -n "$ip" ] || reportError 5 "Failed to download remote IP address"
returnVal="$ip"
}

function beginForwarding {
local host="$1"
setGconfProxy "true" 'manual'
echo "Connecting to server $host..."
ssh -tL "3128:$proxyLocalAddr:3128" "$host" proxy-connect-message "\"$host\""
local status=$?
echo -n "Cleaning up..."
#ssh "$host" killall proxy-connect-message
setGconfProxy "$proxyEnabled"
echo " Done"
[ $status -eq 130 ] && reportError 0 "Shutting down proxy tunnel"
[ $status -ne 0 ] && reportError $status "ssh exited with status $status."
}

function main {
local switch="$1"
local addr="$2"

if [ "$switch" = "--help" ]; then
usage
exit
elif [ "$switch" = "--address" -o "$switch" = '--ip' -o "$switch" = "-a" -o "$switch" = '-i' ]; then
if [ ! -n "$addr" ]; then
usage
reportError 6 "\n---\nYou must specify a hostname or IP address if you use the \"$switch\" switch."
fi
local host="$addr"
else
getRemoteAddr
local host="$returnVal"
fi

getGconfProxy
local status=$?
[ $status -ne 0 ] && reportError $status "Couldn't determine the current proxy status. Aborting."
proxyEnabled="$returnVal"

beginForwarding "$host"
}

main "$@"