How to Change Firefox Proxy Settings Programmatically?

I’m launching Firefox via command line and I’d like to launch a specific Firefox Profile with a proxy. According to this answer on Stackoverflow, Firefox proxy settings are stored in pref.js in the Firefox Profile folder and it is necessary to edit this file to launch FF with a proxy.

I’ve edited the file as follows:

user_pref("network.proxy.ftp", "");
user_pref("network.proxy.ftp_port", 00000);
user_pref("network.proxy.gopher", "");
user_pref("network.proxy.gopher_port", 00000);
user_pref("network.proxy.http", "");
user_pref("network.proxy.http_port", 22222);
user_pref("network.proxy.no_proxies_on", "localhost,");
user_pref("network.proxy.socks", "");
user_pref("network.proxy.socks_port", 00000);
user_pref("network.proxy.ssl", "");
user_pref("network.proxy.ssl_port", 00000);
user_pref("network.proxy.type", 1);

Note: the IP address and port used above are for demonstration purposes.

However, I’m encountering two problems:

1) Firefox completely ignores these settings and launches FF without any proxy at all

2) When Firefox exits the text modification is reverted/deleted

Note: When I edited the text file above, Firefox was not running. I know there’s a disclaimer at the top of prefs.js:

If you make changes to this file while the application is running, the changes will be overwritten when the application exits.

But there were no live instances of Firefox running at the time I edited the above file.

Manually creating different FF Profiles (as suggested by another user) with different proxies is not an option as everything needs to be done programmatically, without manual intervention.

Does Firefox still support linking proxy via pref.js? If not, what is the current working solution to launch Firefox via command line with a proxy in Java?



A proxy-autoconfig file is what you are looking for.

Docs here. Define a file name.pac, that contains the javascript function

function FindProxyForURL(url, host)

Inside the file you can use any javscript you’d like to decide what proxy to use. Set the path to your .pac file in the firefox settings, under auto-config proxy. Remember to use a file url.

To setup automatic file switching, simply configure firefox to point towards a single file, and overwrite the file programmatically every time you want it to change. You could keep copies of all options, and simply copy an option file into the target file right before running.

An example of a super simple pac file is this:

function FindProxyForURL (url, host) {
  return 'PROXY; DIRECT';

It will always return the identical proxy for all endpoints.

Passwords are not explicitly supported by the pac standard, but there are different ways to approach this. Firefox will prompt you for a login if it thinks it needs one, and you could also embed the password into the url (username:[email protected]). Additionally, a tool like proxy login automator could allow you to use passwords and to dynamically set the proxy without having to fight with firefox.