原创文章,转载请注明出处:http://passerbyy.iteye.com/blog/1285628 作者:passer_by
修改firefox代理服务器的文件为prefs.js或profile.ini两个
(1)修改profile.ini中的StartWithLastProfile=0
(2)修改prefs.js中user_pref("network.proxy.开头的文件。
下面代码
(1)包含备份老的配置文件
(2)修改代理(不包含修改为不设置代理情况,个人可以类推写一下)
(3)恢复到老的配置
(4)未考虑到中文情况,请个人修改
(5)未完成修改profile.ini中的StartWithLastProfile=0,请个人补充,也非常简单。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.lang.StringUtils;
public class Firefox {
//firefox配置文件路径,默认的为C:\Documents and Settings\${user}\Application Data\Mozilla\Firefox
private String configPath;
// prefs.js的全路径
private String prefsJsFullPath;
// profiles.ini的全路径
private String iniFullPath;
private static String iniName = "profiles.ini";
private static String prefsJsName = "prefs.js";
private static String profilesName = "Profiles";
// 初始化方法,如果没有配置configPath,可以获取默认值
public void init() {
if (StringUtils.isBlank(configPath))
configPath = System.getProperty("user.home") + File.separator
+ "Application Data" + File.separator + "Mozilla"
+ File.separator + "Firefox" + File.separator;
else if (configPath.endsWith(File.separator)) // 保证以File.separator结尾
configPath += File.separator;
System.out.println("firefox配置文件地址为:" + configPath);
}
// 恢复到之前配置
public boolean backToOriginValue() {
if (StringUtils.isBlank(configPath) || StringUtils.isBlank(iniFullPath)
|| StringUtils.isBlank(prefsJsFullPath)) {
System.out.println("无法恢复后之前的firefox配置,configPath、iniFullPath、prefsJsFullPath其中之一为空");
return false;
}
// 初始化profiles.ini文件和备份profiles.ini文件
File iniFile = new File(iniFullPath);
String bakIniFilePath = getBakFileFullPath(iniFile);
File backIniFile = new File(bakIniFilePath);
if (!backIniFile.exists() || backIniFile.isDirectory()) {
System.out.println("备份文件" + bakIniFilePath + "不存在或者不是文件");
return false;
}
// 恢复到备份文件
iniFile.delete();
backIniFile.renameTo(iniFile);
// 初始化prefs.js文件和备份prefs.js文件
File prefsJsFile = new File(prefsJsFullPath);
String bakPrefsFilePath = getBakFileFullPath(iniFile);
File backPrefsFile = new File(bakPrefsFilePath);
if (!backPrefsFile.exists() || backPrefsFile.isDirectory()) {
System.out.println("备份文件" + bakPrefsFilePath + "不存在或者不是文件");
return false;
}
// 恢复到备份文件
prefsJsFile.delete();
backPrefsFile.renameTo(prefsJsFile);
return true;
}
// 找到prefs.js的全路径
private String findPrefsJsFileFullPath(File iniFile) {
DataInputStream out = null;
try {
out = new DataInputStream(new BufferedInputStream(
new FileInputStream(iniFile), 4096));
int saveVariablCount = 0;
boolean isRelative = true;
String path = null;
while (true) {
String line = out.readLine();
if (line == null)
break;
if (line.startsWith("IsRelative=")) {
String relativeString = line.split("IsRelative=")[1];
if (relativeString.equals("0"))
isRelative = false;
saveVariablCount += 1;
}
if (line.startsWith("Path=")) {
path = line.split("Path=")[1];
saveVariablCount += 1;
}
if (saveVariablCount == 2)
break;
}
if (path == null)
return null;
if (isRelative)
path = configPath + path;
path = path + File.separator + prefsJsName;
return path;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
// 修改代理文件配置
// 调用该方法前,要把目前打开的firefox关掉,否则本地修改可能不会生效
public boolean changeProxy(String proxyIp, int proxyPort) {
// 修改profiles.ini中的StartWithLastProfile为0
boolean success = changeProfileIni();
if(!success)
return false;
return changePrefsJsFile(proxyIp, proxyPort);
}
// 修改prefs.js文件
private boolean changePrefsJsFile(String proxyIp, int proxyPort) {
if (prefsJsFullPath == null) {
System.out.println("修改代理时,没有找到prefs.js的路径");
return false;
}
// FIXME 有没有简便的删除若干行的方法?
boolean success = false;
StringBuffer sb = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(prefsJsFullPath));
while (true) {
String line = reader.readLine();
if (line == null)
break; //
// 要删除所有的user_pref("network.proxy.开头的配置,只能先把不是以其开头的行读取出来
if (!line.startsWith("user_pref(\"network.proxy.")) {
sb.append(line).append("\r\n");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 重写文件
BufferedWriter bw = null;
try {
// 把去除若干行的老的配置重新写入文件
bw = new BufferedWriter(new FileWriter(prefsJsFullPath));
bw.write(sb.toString());
/**
* 把代理相关的配置写入到文件中,类似于:
*
* user_pref("network.proxy.ftp", "10.0.0.0");
* user_pref("network.proxy.ftp_port", 1000);
* user_pref("network.proxy.gopher", "10.0.0.0");
* user_pref("network.proxy.gopher_port", 1000);
* user_pref("network.proxy.http", "10.0.0.0");
* user_pref("network.proxy.http_port", 1000);
* user_pref("network.proxy.no_proxies_on", "");
* user_pref("network.proxy.share_proxy_settings", true);
* user_pref("network.proxy.socks", "10.0.0.0");
* user_pref("network.proxy.socks_port", 1000);
* user_pref("network.proxy.ssl", "10.0.0.0");
* user_pref("network.proxy.ssl_port", 1000);
* user_pref("network.proxy.type", 1);
*/
bw.write("user_pref(\"network.proxy.ftp\", \"" + proxyIp+ "\");\r\n");
bw.write("user_pref(\"network.proxy.ftp_port\", " + proxyPort+ ");\r\n");
bw.write("user_pref(\"network.proxy.gopher\", \"" + proxyIp+ "\");\r\n");
bw.write("user_pref(\"network.proxy.gopher_port\", " + proxyPort+ ");\r\n");
bw.write("user_pref(\"network.proxy.http\", \"" + proxyIp+ "\");\r\n");
bw.write("user_pref(\"network.proxy.http_port\", " + proxyPort
+ ");\r\n");
bw.write("user_pref(\"network.proxy.socks\", \"" + proxyIp
+ "\");\r\n");
bw.write("user_pref(\"network.proxy.socks_port\", " + proxyPort
+ ");\r\n");
bw.write("user_pref(\"network.proxy.ssl\", \"" + proxyIp
+ "\");\r\n");
bw.write("user_pref(\"network.proxy.ssl_port\", " + proxyPort
+ ");\r\n");
bw.write("user_pref(\"network.proxy.no_proxies_on\", \"\");\r\n");
bw
.write("user_pref(\"network.proxy.share_proxy_settings\", true);\r\n");
bw.write("user_pref(\"network.proxy.type\", 1);");
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return success;
}
// 修改profiles.ini中的StartWithLastProfile为0
private boolean changeProfileIni() {
// TODO Auto-generated method stub 这里我没写。。。。。。。。。。。。。。。请个人补充
return false;
}
private boolean backupFile(File file) {
if (file == null || !file.exists() || file.isDirectory()) {
System.out.println("firefox配置文件目录或文件存在错误");
return false;
}
File backFile = new File(getBakFileFullPath(file));
if (backFile.exists()) {
boolean success = backFile.delete();
if (!success) {
System.out.println("无法删除已经存在的备份文件 " + backFile.getPath());
}
}
try {
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(backFile);
byte[] buffer = new byte[1024];
while (true) {
int readSize = is.read(buffer);
if (readSize == -1)
break;
out.write(buffer, 0, readSize);
}
is.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private String getBakFileFullPath(File file) {
return file.getParent() + File.separator + file.getName() + ".bak";
}
public boolean saveOriginValue() {
if (StringUtils.isBlank(configPath))
return false;
// 备份profiles.ini文件
File iniFile = new File(configPath + iniName);
boolean iniBackUpSuccess = backupFile(iniFile);
if (!iniBackUpSuccess)
return false;
iniFullPath = configPath + iniName;
// 查找prefs.js文件的全路径
prefsJsFullPath = findPrefsJsFileFullPath(iniFile);
if (prefsJsFullPath == null)
return false;
// 备份prefs.js文件
File prefsJsFile = new File(prefsJsFullPath);
if (!prefsJsFile.exists() || prefsJsFile.isDirectory()) {
prefsJsFullPath = null;
System.out.println(prefsJsFullPath + "文件不存在");
return false;
}
boolean prefsBackUpSuccess = backupFile(prefsJsFile);
if (!prefsBackUpSuccess)
return false;
return true;
}
public void setConfigPath(String configPath) {
this.configPath = configPath;
}
public static void main(String[] args){
Firefox ff = new Firefox();
//ff.setConfigPath("");
ff.init();
ff.saveOriginValue();
ff.changeProxy("localhost", 8080);
//这里可以省去个人的一些操作.............
// 用完后,可以执行这个..
ff.backToOriginValue();
}
}
分享到:
相关推荐
3. **浏览器设置**:有时可能需要修改Firefox的配置,比如禁用隐私模式或自定义用户代理,以适应自动化需求。 4. **版本更新**:保持GeckoDriver和Firefox的版本同步更新,以利用最新的功能和修复已知问题。 通过...
可以将/dev/random替换为/dev/urandom以提高启动速度,或者通过修改Tomcat的`conf/server.xml`配置文件中的`<Connector>`元素,设置`useRandom="false"`。 【Tomcat应用案例】 1. **部署虚拟主机**: 在Tomcat中...
Firefox中设置代理也比较简单,只需使用`FirefoxProfile`来配置代理服务器。 ```python from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile profile = ...
6. **代理支持**:对于需要使用代理的网络环境,用户可以通过修改`config.properties`文件配置代理服务器IP和端口。 7. **指定车次购票**:用户可以编辑`config.properties`文件,指定想要购买的车次,程序会优先...
7. **反爬虫策略**:百度贴吧可能会有反爬虫机制,如验证码、IP限制、User-Agent检测等,爬虫开发者需要研究并采取相应的应对措施,如设置代理IP、修改User-Agent、使用Cookies等。 8. **数据存储**:爬取的数据...
ByteBuddy是一个运行时代码生成库,它在Selenium中扮演着动态代理的角色,允许在运行时创建和修改类。这对于创建自定义的WebDriver实现或者动态地增强已有类的功能至关重要。 接着是`guava-23.6-jre.jar`,Google的...
- **Firefox/Chrome/Edge等浏览器** - 打开浏览器设置。 - 寻找网络代理设置。 - 设置代理服务器地址为本机IP(通常是127.0.0.1),端口号为8080(Burp Suite默认端口)。 ##### 2. 使用Burp Suite - **启动...
Burp Suite 的核心组件之一是其代理功能,用户可以通过配置浏览器或其他客户端使用Burp作为HTTP代理服务器,从而捕获和拦截HTTP(S)流量,以便分析和修改。 #### 二、Burp Suite 使用环境配置 在使用Burp Suite ...
默认情况下,VuGen会修改代理服务器设置,以便所有通过浏览器的网络请求都经过VuGen代理服务器,从而能够捕获和录制用户的操作。 **注意点:** 在测试完成后,记得恢复代理服务器的原始设置,避免影响后续的网络...
2. **浏览器配置:** 在常用的浏览器中(如 Firefox),进入网络设置,手动配置 HTTP 代理,地址为本地 IP 地址(通常是 127.0.0.1),端口与 Burp Suite 设置相同。 #### 五、开始使用 完成上述安装和配置步骤后...
- EFGP系统是基于J2EE平台开发的,使用Java技术构建,支持多种浏览器包括Internet Explorer, Firefox, Chrome和Safari等。 - 它是一个能够处理浏览器请求、Java Web Start、Web服务调用以及支持多种Web容器的系统...
- 浏览器:支持IE8及以上版本、Chrome、Firefox等现代浏览器。 - Java环境:推荐安装Java Runtime Environment (JRE) 1.6及以上版本。 **1.5 网络环境要求** - 稳定的局域网或广域网连接,带宽满足实际业务需求。...
在开始录制基于浏览器的Web Vuser脚本时,VuGen会启动指定的浏览器,并修改该浏览器上的代理服务器设置。具体来说,VuGen会指示浏览器访问VuGen代理服务器,因此会将代理服务器设置更改为指向Localhost:7777。录制...
在Web开发中,开发者工具(如Chrome的DevTools或Firefox的Developer Edition)提供了网络面板,可以实时查看和跟踪HTTP请求。这些工具能帮助开发者检查请求头、响应头、加载时间等信息,对优化网页性能和解决网络...
修改后的源文件需重新打包成.zip格式,并在Firefox中重新安装。 #### 四、Selenium Remote Control (RC) RC作为代理服务器,通过Jetty作为webserver,解决了JavaScript同源策略限制的问题,使得不同来源的...
- **操作步骤**:检查IE的代理设置是否正确,确保没有不必要的代理服务器配置。 **3. 使用第三方工具** - 对于Windows 98系统用户,可以下载专门的修复工具,如 `mcrepair.exe` 来修复浏览器问题。 #### 四、...
脚本时,LoadRunner 的 Virtual User Generator (VuGen) 会自动配置录制浏览器的代理设置,以便通过代理服务器捕获网络通信。这是为了确保VuGen能够记录下所有的网络请求和响应,包括HTTP和HTTPS流量。 关于...
- 修改 Firefox 设置以适应 IPv6 环境。 - 调整后退按钮的行为。 #### 应用程序与工具 - **邮件客户端**: 如何配置邮件客户端以接收和发送电子邮件。 - **音频播放器**: - Rhythmbox: GNOME 默认音乐播放器。 ...
虽然大多数情况下,跨域问题都由后端或反向代理服务器解决,但在某些开发环境中,我们可能需要直接在浏览器中配置跨域。对于 Chrome 和 Firefox 浏览器而言,也有相应的设置方式: - **Chrome**:可以在启动参数中...