| 1 |
package com.github.markusbernhardt.proxy.search.browser.firefox; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.InputStreamReader; |
| 8 |
import java.util.Map.Entry; |
| 9 |
import java.util.Properties; |
| 10 |
|
| 11 |
import org.ini4j.Ini; |
| 12 |
import org.ini4j.Profile.Section; |
| 13 |
|
| 14 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 15 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class FirefoxSettingParser { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public FirefoxSettingParser() { |
| 32 |
super(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public Properties parseSettings(FirefoxProfileSource source) throws IOException { |
| 46 |
|
| 47 |
File profileFolder = null; |
| 48 |
|
| 49 |
|
| 50 |
File profilesIniFile = source.getProfilesIni(); |
| 51 |
if (profilesIniFile.exists()) { |
| 52 |
Ini profilesIni = new Ini(profilesIniFile); |
| 53 |
for (Entry<String, Section> entry : profilesIni.entrySet()) { |
| 54 |
if ("default".equals(entry.getValue().get("Name"))) { |
| 55 |
if ("1".equals(entry.getValue().get("IsRelative"))) { |
| 56 |
profileFolder = new File(profilesIniFile.getParentFile().getAbsolutePath(), |
| 57 |
entry.getValue().get("Path")); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
if (profileFolder != null) { |
| 63 |
Logger.log(getClass(), LogLevel.DEBUG, "Firefox settings folder is {0}", profileFolder); |
| 64 |
} else { |
| 65 |
Logger.log(getClass(), LogLevel.DEBUG, "Firefox settings folder not found!"); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
File settingsFile = new File(profileFolder, "prefs.js"); |
| 70 |
|
| 71 |
BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(settingsFile))); |
| 72 |
|
| 73 |
Properties result = new Properties(); |
| 74 |
try { |
| 75 |
String line = fin.readLine(); |
| 76 |
while (line != null) { |
| 77 |
line = line.trim(); |
| 78 |
if (line.startsWith("user_pref(\"network.proxy")) { |
| 79 |
line = line.substring(10, line.length() - 2); |
| 80 |
int index = line.indexOf(","); |
| 81 |
String key = line.substring(0, index).trim(); |
| 82 |
if (key.startsWith("\"")) { |
| 83 |
key = key.substring(1); |
| 84 |
} |
| 85 |
if (key.endsWith("\"")) { |
| 86 |
key = key.substring(0, key.length() - 1); |
| 87 |
} |
| 88 |
String value = line.substring(index + 1).trim(); |
| 89 |
if (value.startsWith("\"")) { |
| 90 |
value = value.substring(1); |
| 91 |
} |
| 92 |
if (value.endsWith("\"")) { |
| 93 |
value = value.substring(0, value.length() - 1); |
| 94 |
} |
| 95 |
result.put(key, value); |
| 96 |
} |
| 97 |
line = fin.readLine(); |
| 98 |
} |
| 99 |
} finally { |
| 100 |
fin.close(); |
| 101 |
} |
| 102 |
|
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
} |