java修改firefox代理服务器配置

[b]原创文章,转载请注明出处:http://passerbyy.iteye.com/blog/1285628 作者:passer_by[/b]
修改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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值