如何以编程方式更改Firefox代理设置? [英] How to Change Firefox Proxy Settings Programmatically?

查看:42
本文介绍了如何以编程方式更改Firefox代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过命令行启动Firefox,我想使用代理启动特定的Firefox配置文件.根据关于Stackoverflow的答案,Firefox代理设置存储在Firefox的 pref.js 中配置文件文件夹,需要编辑此文件以使用代理启动FF.

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.

我对文件进行了如下

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);

注意:上面使用的IP地址和端口仅用于演示目的.

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

但是,我遇到两个问题:

However, I'm encountering two problems:

1) Firefox完全忽略了这些设置,并在没有任何代理的情况下启动了FF

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

2):当Firefox退出时,文本修改将被还原/删除

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

注意:当我编辑上面的文本文件时,Firefox没有运行.我知道 prefs.js 的顶部有一个免责声明:

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.

但是,在我编辑上述文件时,没有运行中的Firefox实例.

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

手动创建具有不同代理的不同FF配置文件(由另一个用户建议)是不可行的,因为一切都需要以编程方式完成,而无需人工干预.

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.

Firefox是否仍然支持通过 pref.js 链接代理?如果没有,使用Java中的代理通过命令行启动Firefox的当前可行解决方案是什么?

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.

文档此处.定义文件 name.pac ,其中包含javascript函数

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

function FindProxyForURL(url, host)

在文件中,您可以使用任何要确定要使用的代理的javscript.在自动配置代理下的firefox设置中,将路径设置为.pac文件.记住要使用文件网址.

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.

要设置自动文件切换,只需将firefox配置为指向单个文件,然后在每次更改文件时以编程方式覆盖该文件.您可以保留所有选项的副本,只需在运行之前将选项文件复制到目标文件中即可.

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.

一个超级简单的pac文件的示例是这样的:

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.

pac标准未明确支持密码,但是有多种方法可以解决此问题.如果Firefox认为需要登录,则会提示您进行登录,您也可以将密码嵌入url( username:password@proxy.example.com )中.此外,代理登录自动器之类的工具可以允许您使用密码并动态设置代理,而不必与Firefox战斗.

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.

这篇关于如何以编程方式更改Firefox代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆