Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/browser/firefox/FirefoxSettingParser.java

raw symbols SCIP
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 * Parser for the Firefox settings file. Will extract all relevant proxy
19 * settings form the configuration file.
20 *
21 * @author Markus Bernhardt, Copyright 2016
22 * @author Bernd Rosstauscher, Copyright 2009
23 ****************************************************************************/
24
25 class FirefoxSettingParser {
26
27 /*************************************************************************
28 * Constructor
29 ************************************************************************/
30
31 public FirefoxSettingParser() {
32 super();
33 }
34
35 /*************************************************************************
36 * Parse the settings file and extract all network.proxy.* settings from it.
37 *
38 * @param source
39 * of the Firefox profiles.
40 * @return the parsed properties.
41 * @throws IOException
42 * on read error.
43 ************************************************************************/
44
45 public Properties parseSettings(FirefoxProfileSource source) throws IOException {
46 // Search settings folder
47 File profileFolder = null;
48
49 // Read profiles.ini
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 // Read settings from file
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 }