12

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", "1.0.0.1");
user_pref("network.proxy.ftp_port", 00000);
user_pref("network.proxy.gopher", "1.0.0.1");
user_pref("network.proxy.gopher_port", 00000);
user_pref("network.proxy.http", "1.0.0.1");
user_pref("network.proxy.http_port", 22222);
user_pref("network.proxy.no_proxies_on", "localhost, 1.0.0.1");
user_pref("network.proxy.socks", "1.0.0.1");
user_pref("network.proxy.socks_port", 00000);
user_pref("network.proxy.ssl", "1.0.0.1");
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?

Thanks

alpha1
  • 426
  • 3
  • 14

1 Answers1

9

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 proxy.example.com:8080; 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:password@proxy.example.com). 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.

Carson
  • 2,700
  • 11
  • 24
  • thanks for your reply. How would I tell FF to use a specific pac file on startup? I need to do this automatically in Java without manual intervention. If I need to set the path to .pac manually it defeats the purpose. Also, if you could share sample configuration of .pac file where **all** requests are forwarded to proxy server (as in my OP) that would be super helpful. Finally, I'm assuming it is also possible to add a password (for the proxy) to the .pac file? Kindly update answer to address these questions and I will accept and award bounty later tonight. Thanks! – alpha1 Mar 25 '21 at 17:19
  • Thanks for the update. "To setup automatic file switching, simply configure firefox to point towards a single file" How do I configure FF to point to the single pac file **programmatically**? If I need to configure FF manually to point to said file it defeats the purpose. Is there a way to achieve this programmatically in Java? BTW, upvoted answer. – alpha1 Mar 26 '21 at 16:37