2022-08-07 12:40:11 -04:00
|
|
|
package net.i2p.i2pfirefox;
|
2022-08-07 02:00:32 -04:00
|
|
|
|
|
|
|
import java.io.File;
|
2022-10-22 20:12:51 -04:00
|
|
|
import java.io.FileOutputStream;
|
2022-10-01 17:26:19 -04:00
|
|
|
import java.io.FileWriter;
|
2022-08-07 02:26:04 -04:00
|
|
|
import java.io.IOException;
|
2022-10-22 20:12:51 -04:00
|
|
|
import java.io.OutputStream;
|
2022-10-01 17:26:19 -04:00
|
|
|
import java.io.PrintWriter;
|
2022-08-07 02:00:32 -04:00
|
|
|
import java.util.ArrayList;
|
2022-09-29 23:16:29 -04:00
|
|
|
import java.util.Arrays;
|
2022-10-22 20:12:51 -04:00
|
|
|
import java.util.List;
|
2022-09-06 23:50:19 -04:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-10-22 20:12:51 -04:00
|
|
|
import java.util.stream.Collectors;
|
2022-10-01 17:26:19 -04:00
|
|
|
import java.util.stream.Stream;
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-27 13:27:28 -04:00
|
|
|
/**
|
2022-08-21 23:49:08 -04:00
|
|
|
* I2PFirefox.java
|
|
|
|
* Copyright (C) 2022 idk <hankhill19580@gmail.com>
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT License. See LICENSE.md for details.
|
2022-08-30 15:49:44 -04:00
|
|
|
*
|
2022-08-21 23:49:08 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
2022-08-30 15:49:44 -04:00
|
|
|
*
|
2022-08-21 23:49:08 -04:00
|
|
|
* @author idk
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
2023-01-01 01:45:50 +00:00
|
|
|
public class I2PFirefox extends I2PFirefoxProfileUnpacker {
|
2022-08-30 15:49:44 -04:00
|
|
|
private final String[] FIREFOX_SEARCH_PATHS = FIREFOX_FINDER();
|
2022-12-21 03:01:51 +00:00
|
|
|
private Process process = null;
|
2024-03-07 11:00:13 -05:00
|
|
|
private String firefoxPath;
|
2023-01-01 02:37:26 +00:00
|
|
|
public boolean usability = false;
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
private String baseMode() {
|
2022-10-23 01:20:46 -04:00
|
|
|
if (usability)
|
|
|
|
return "usability";
|
|
|
|
return "base";
|
|
|
|
}
|
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Construct an I2PFirefox class which manages an instance of Firefox and
|
|
|
|
* an accompanying Firefox profile. This version includes Firefox variants
|
|
|
|
* and forks.
|
|
|
|
*
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public I2PFirefox() {
|
|
|
|
for (String path : FIREFOX_SEARCH_PATHS) {
|
|
|
|
File f = new File(path);
|
|
|
|
if (f.exists()) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Found Firefox at " + path);
|
2022-08-30 15:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
public void storeFirefoxDefaults() {
|
2022-10-22 20:12:51 -04:00
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
list = Arrays.asList(firefoxPathsWindows());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.paths.windows",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
list = Arrays.asList(firefoxPathsUnix());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.paths.linux",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
list = Arrays.asList(firefoxPathsOSX());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.paths.osx",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
|
|
|
|
list = Arrays.asList(firefoxBinsWindows());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.bins.windows",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
list = Arrays.asList(firefoxBinsUnix());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.bins.linux",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
list = Arrays.asList(firefoxBinsUnix());
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().setProperty("firefox.bins.osx",
|
2024-03-05 16:31:28 -05:00
|
|
|
list.stream().collect(Collectors.joining(",")));
|
2022-10-22 20:12:51 -04:00
|
|
|
try (OutputStream fos = new FileOutputStream(
|
|
|
|
new File(runtimeDirectory(""), "browser.config"))) {
|
2023-07-11 13:46:22 -04:00
|
|
|
getProperties().store(fos, "Firefox Configuration Section");
|
2022-10-22 20:12:51 -04:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
logger.warning(ioe.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
public String[] firefoxPathsUnix() {
|
2023-07-11 13:46:22 -04:00
|
|
|
String firefoxPathsProp = getProperties().getProperty("firefox.paths.unix");
|
2022-10-22 18:39:01 -04:00
|
|
|
if (firefoxPathsProp != null)
|
2022-10-22 18:46:09 -04:00
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
2022-10-22 18:39:01 -04:00
|
|
|
return new String[] {"/usr/bin", "/usr/local/bin", "/opt/firefox/bin",
|
|
|
|
"/snap/bin"};
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
public String[] firefoxBinsUnix() {
|
2022-10-22 20:12:51 -04:00
|
|
|
String firefoxPathsProp;
|
2022-10-22 18:39:01 -04:00
|
|
|
if (isOSX()) {
|
2023-07-11 13:46:22 -04:00
|
|
|
firefoxPathsProp = getProperties().getProperty("firefox.bins.osx");
|
2022-10-22 18:39:01 -04:00
|
|
|
if (firefoxPathsProp != null)
|
2022-10-22 18:46:09 -04:00
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
2022-10-22 18:39:01 -04:00
|
|
|
}
|
2023-07-11 13:46:22 -04:00
|
|
|
firefoxPathsProp = getProperties().getProperty("firefox.bins.unix");
|
2022-10-22 20:12:51 -04:00
|
|
|
if (firefoxPathsProp != null)
|
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
|
|
|
|
2022-10-22 18:39:01 -04:00
|
|
|
return new String[] {"firefox", "firefox-bin", "firefox-esr",
|
|
|
|
"waterfox", "waterfox-bin", "librewolf"};
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIND_FIREFOX_SEARCH_PATHS_UNIX() {
|
2022-10-22 18:39:01 -04:00
|
|
|
String[] path = firefoxPathsUnix();
|
|
|
|
String[] exes = firefoxBinsUnix();
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] exePath = new String[path.length * exes.length];
|
|
|
|
int i = 0;
|
|
|
|
for (String s : path) {
|
|
|
|
for (String exe : exes) {
|
|
|
|
exePath[i] = s + "/" + exe;
|
|
|
|
i++;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return exePath;
|
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
public String[] firefoxPathsOSX() {
|
2023-07-11 13:46:22 -04:00
|
|
|
String firefoxPathsProp = getProperties().getProperty("firefox.paths.osx");
|
2022-10-22 18:39:01 -04:00
|
|
|
if (firefoxPathsProp != null)
|
2022-10-22 18:46:09 -04:00
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
2022-10-22 18:39:01 -04:00
|
|
|
return new String[] {"/Applications/Tor Browser.app/Contents/MacOS",
|
|
|
|
"/Applications/Firefox.app/Contents/MacOS",
|
|
|
|
"/Applications/Waterfox.app/Contents/MacOS",
|
|
|
|
"/Applications/Librewolf.app/Contents/MacOS"};
|
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIND_FIREFOX_SEARCH_PATHS_OSX() {
|
2022-10-22 18:39:01 -04:00
|
|
|
String[] path = firefoxPathsOSX();
|
|
|
|
String[] exes = firefoxBinsUnix();
|
2022-10-01 17:26:19 -04:00
|
|
|
String[] exePath = new String[path.length * exes.length];
|
2022-08-30 15:49:44 -04:00
|
|
|
int i = 0;
|
|
|
|
for (String s : path) {
|
2022-10-01 17:26:19 -04:00
|
|
|
for (String exe : exes) {
|
|
|
|
exePath[i] = s + "/" + exe;
|
|
|
|
i++;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return exePath;
|
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
public String[] firefoxPathsWindows() {
|
2024-03-05 16:31:28 -05:00
|
|
|
String firefoxPathsProp =
|
|
|
|
getProperties().getProperty("firefox.paths.windows");
|
2022-10-22 18:39:01 -04:00
|
|
|
if (firefoxPathsProp != null)
|
2022-10-22 18:46:09 -04:00
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
2022-08-30 15:49:44 -04:00
|
|
|
String userHome = System.getProperty("user.home");
|
|
|
|
String programFiles = System.getenv("ProgramFiles");
|
|
|
|
// String localAppData = System.getenv("LOCALAPPDATA");
|
|
|
|
// Is there some way Mozilla does adminless installs to LocalAppData? Don't
|
|
|
|
// know for sure.
|
|
|
|
String programFiles86 = System.getenv("ProgramFiles(x86)");
|
2022-08-08 01:37:52 -04:00
|
|
|
|
2022-10-23 11:27:59 -04:00
|
|
|
if (programFiles == null)
|
2022-10-23 11:40:39 -04:00
|
|
|
programFiles = "C:/Program Files/";
|
2022-10-23 11:27:59 -04:00
|
|
|
if (programFiles86 == null)
|
2022-10-23 11:52:56 -04:00
|
|
|
programFiles86 = "C:/Program Files (x86)/";
|
2022-10-23 11:27:59 -04:00
|
|
|
if (!isWindows())
|
2022-10-23 11:40:39 -04:00
|
|
|
userHome = "C:/Users/user/";
|
2022-10-23 11:26:45 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] tbPath = new String[] {
|
|
|
|
new File(userHome, "/OneDrive/Desktop/Tor Browser/Browser/").toString(),
|
|
|
|
new File(userHome, "/Desktop/Tor Browser/Browser/").toString()};
|
|
|
|
|
2022-10-22 18:39:01 -04:00
|
|
|
return new String[] {
|
2022-08-30 15:49:44 -04:00
|
|
|
tbPath[0],
|
|
|
|
tbPath[1],
|
|
|
|
new File(programFiles, "Mozilla Firefox/").toString(),
|
|
|
|
new File(programFiles86, "Mozilla Firefox/").toString(),
|
|
|
|
new File(programFiles, "Waterfox/").toString(),
|
|
|
|
new File(programFiles86, "Waterfox/").toString(),
|
|
|
|
new File(programFiles, "Librewolf/").toString(),
|
|
|
|
};
|
2022-10-22 18:39:01 -04:00
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] firefoxBinsWindows() {
|
2024-03-05 16:31:28 -05:00
|
|
|
String firefoxPathsProp =
|
|
|
|
getProperties().getProperty("firefox.bins.windows");
|
2022-10-22 18:39:01 -04:00
|
|
|
if (firefoxPathsProp != null)
|
2022-10-22 18:46:09 -04:00
|
|
|
if (!firefoxPathsProp.equals(""))
|
|
|
|
return firefoxPathsProp.split(",");
|
2022-10-22 18:39:01 -04:00
|
|
|
return new String[] {
|
2022-08-30 15:49:44 -04:00
|
|
|
"firefox.exe", "firefox-bin.exe", "firefox-esr.exe",
|
|
|
|
"waterfox.exe", "waterfox-bin.exe", "librewolf.exe",
|
|
|
|
};
|
2022-10-22 18:39:01 -04:00
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIND_FIREFOX_SEARCH_PATHS_WINDOWS() {
|
2022-10-22 18:39:01 -04:00
|
|
|
String[] path = firefoxPathsWindows();
|
|
|
|
String[] exes = firefoxBinsWindows();
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] exePath = new String[path.length * exes.length];
|
|
|
|
int i = 0;
|
|
|
|
for (String s : path) {
|
|
|
|
for (String exe : exes) {
|
|
|
|
exePath[i] = s + "\\" + exe;
|
|
|
|
i++;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return exePath;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIND_ALL_FIREFOX_SEARCH_PATHS() {
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] Unix = FIND_FIREFOX_SEARCH_PATHS_UNIX();
|
|
|
|
String[] Windows = FIND_FIREFOX_SEARCH_PATHS_WINDOWS();
|
|
|
|
String[] Mac = FIND_FIREFOX_SEARCH_PATHS_OSX();
|
|
|
|
String[] exePath = new String[Unix.length + Windows.length + Mac.length];
|
|
|
|
int i = 0;
|
|
|
|
for (String s : Unix) {
|
|
|
|
exePath[i] = s;
|
|
|
|
i++;
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
for (String s : Windows) {
|
|
|
|
exePath[i] = s;
|
|
|
|
i++;
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
for (String s : Mac) {
|
|
|
|
exePath[i] = s;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return exePath;
|
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIND_FIREFOX_SEARCH_PATHS() {
|
2022-08-30 15:49:44 -04:00
|
|
|
switch (getOperatingSystem()) {
|
|
|
|
case "Windows":
|
|
|
|
return FIND_FIREFOX_SEARCH_PATHS_WINDOWS();
|
|
|
|
case "Linux":
|
|
|
|
return FIND_FIREFOX_SEARCH_PATHS_UNIX();
|
|
|
|
case "Mac":
|
|
|
|
return FIND_FIREFOX_SEARCH_PATHS_OSX();
|
|
|
|
case "BSD":
|
|
|
|
return FIND_FIREFOX_SEARCH_PATHS_UNIX();
|
|
|
|
default:
|
|
|
|
return FIND_ALL_FIREFOX_SEARCH_PATHS();
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 23:55:45 -04:00
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] NEARBY_FIREFOX_SEARCH_PATHS() {
|
2022-08-30 15:49:44 -04:00
|
|
|
// obtain the PLUGIN environment variable
|
2022-12-23 06:20:09 +00:00
|
|
|
// crashreporterFolder := utl.CreateFolder(app.DataPath, "crashreporter")
|
|
|
|
// pluginsFolder := utl.CreateFolder(app.DataPath, "plugins")
|
2022-08-30 15:49:44 -04:00
|
|
|
String plugin = System.getenv("PLUGIN");
|
|
|
|
if (plugin != null && !plugin.isEmpty()) {
|
2022-10-01 20:58:14 -04:00
|
|
|
File userDir = new File(plugin);
|
|
|
|
if (userDir.exists()) {
|
2022-10-23 22:59:23 -04:00
|
|
|
if (isWindows()) {
|
|
|
|
File searchResult = searchFile(userDir, "firefox-esr.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox-bin.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox-bin.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "librewolf.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
} else {
|
|
|
|
File searchResult = searchFile(userDir, "firefox-esr");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox-bin");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox-bin");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "librewolf");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-10-23 22:59:23 -04:00
|
|
|
}
|
|
|
|
// now, do the same thing, but with user.dir instead of plugin
|
|
|
|
// list the directories in the user.dir directory
|
2024-07-08 13:03:57 -04:00
|
|
|
File userDir = userHomeDir();
|
2022-10-23 22:59:23 -04:00
|
|
|
if (userDir.exists()) {
|
|
|
|
if (isWindows()) {
|
|
|
|
File searchResult = searchFile(userDir, "firefox-esr.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox-bin.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox-bin.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "librewolf.exe");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
} else {
|
|
|
|
File searchResult = searchFile(userDir, "firefox-esr");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "firefox-bin");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "waterfox-bin");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
|
|
|
searchResult = searchFile(userDir, "librewolf");
|
|
|
|
if (searchResult != null)
|
|
|
|
return new String[] {searchResult.getAbsolutePath()};
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-10-23 22:59:23 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
return new String[] {};
|
|
|
|
}
|
2023-01-01 01:45:50 +00:00
|
|
|
private String[] FIREFOX_FINDER() {
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] nearby = NEARBY_FIREFOX_SEARCH_PATHS();
|
|
|
|
String[] all = FIND_FIREFOX_SEARCH_PATHS();
|
2022-08-07 12:06:05 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
if (nearby != null && nearby.length > 0) {
|
|
|
|
return nearby;
|
|
|
|
} else if (all != null && all.length > 0) {
|
|
|
|
return all;
|
|
|
|
} else {
|
|
|
|
return new String[] {};
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Check our list of firefox paths for a valid firefox binary.
|
|
|
|
* Just an existence check for now, but should check versions
|
|
|
|
* in the future.
|
|
|
|
*
|
|
|
|
* @return a list of usable Firefoxes, or an empty list if none are found.
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public String[] onlyValidFirefoxes() {
|
|
|
|
String[] firefoxes = FIREFOX_FINDER();
|
|
|
|
ArrayList<String> validFirefoxes = new ArrayList<String>();
|
|
|
|
for (String firefox : firefoxes) {
|
|
|
|
File firefoxFile = new File(firefox);
|
|
|
|
if (firefoxFile.exists()) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Found valid firefox at " + firefox);
|
2022-08-30 15:49:44 -04:00
|
|
|
validFirefoxes.add(firefox);
|
|
|
|
}
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("firefox at " + firefox + "does not exist");
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return validFirefoxes.toArray(new String[validFirefoxes.size()]);
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Return the best available Firefox from the list of Firefoxes we have.
|
|
|
|
*
|
2022-10-22 21:59:44 -04:00
|
|
|
* @return the path to the best available Firefox, or null if none are
|
|
|
|
* found.
|
2022-08-30 15:49:44 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public String topFirefox() {
|
2024-03-07 11:00:13 -05:00
|
|
|
if (firefoxPath != null) {
|
|
|
|
return firefoxPath;
|
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
// get the FIREFOX environment variable
|
|
|
|
String firefox = System.getenv("FIREFOX");
|
|
|
|
// if it is not null and not empty
|
|
|
|
if (firefox != null && !firefox.isEmpty()) {
|
|
|
|
// check if the file exists
|
|
|
|
File firefoxFile = new File(firefox);
|
|
|
|
if (firefoxFile.exists()) {
|
|
|
|
// if it does, return it
|
2024-03-07 11:00:13 -05:00
|
|
|
firefoxPath = firefox;
|
2022-08-30 15:49:44 -04:00
|
|
|
return firefox;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] firefoxes = onlyValidFirefoxes();
|
|
|
|
if (firefoxes.length > 0) {
|
2024-03-07 11:00:13 -05:00
|
|
|
firefoxPath = firefoxes[0];
|
2022-08-30 15:49:44 -04:00
|
|
|
return firefoxes[0];
|
|
|
|
} else {
|
|
|
|
return "";
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Return the best available Firefox from the list of Firefoxes we have.
|
|
|
|
* if override is passed it will be validated and if it validates, it will
|
|
|
|
* be used.
|
|
|
|
*
|
|
|
|
* @param override the path to a valid Firefox binary to use.
|
2022-10-22 21:59:44 -04:00
|
|
|
* @return the path to the best available Firefox, or null if none are
|
|
|
|
* found.
|
2022-08-30 15:49:44 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public String topFirefox(String overrideFirefox) {
|
|
|
|
if (overrideFirefox != null && !overrideFirefox.isEmpty()) {
|
|
|
|
File firefoxFile = new File(overrideFirefox);
|
|
|
|
if (firefoxFile.exists()) {
|
|
|
|
return overrideFirefox;
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return topFirefox();
|
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public ProcessBuilder defaultProcessBuilder() {
|
2022-09-15 19:21:42 -04:00
|
|
|
return processBuilder(new String[] {}, false);
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* * Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*
|
|
|
|
* @param args the args to pass to the Firefox binary
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*/
|
|
|
|
public ProcessBuilder defaultProcessBuilder(String[] args) {
|
2022-09-15 19:21:42 -04:00
|
|
|
return processBuilder(args, false);
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-19 18:30:14 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile. Pass the --private-window flag to
|
|
|
|
* open a private window.
|
|
|
|
*
|
|
|
|
* @param args the arguments to pass to the Firefox binary.
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public ProcessBuilder privateProcessBuilder() {
|
2022-09-15 19:21:42 -04:00
|
|
|
return processBuilder(new String[] {"--private-window"}, false);
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
2022-09-06 20:05:29 -04:00
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
2022-08-30 15:49:44 -04:00
|
|
|
* the default profile. Pass the --private-window flag to
|
|
|
|
* open a private window.
|
|
|
|
*
|
|
|
|
* @param args the arguments to pass to the Firefox binary
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*/
|
|
|
|
public ProcessBuilder privateProcessBuilder(String[] args) {
|
|
|
|
ArrayList<String> argList = new ArrayList<String>();
|
|
|
|
argList.add("--private-window");
|
2022-09-03 21:38:20 -04:00
|
|
|
if (args != null) {
|
|
|
|
if (args.length > 0) {
|
|
|
|
for (String arg : args) {
|
|
|
|
argList.add(arg);
|
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
}
|
2022-09-15 19:21:42 -04:00
|
|
|
return processBuilder(argList.toArray(new String[argList.size()]), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile. Pass the --private-window flag to
|
|
|
|
* open a private window.
|
|
|
|
*
|
|
|
|
* @param args the arguments to pass to the Firefox binary.
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public ProcessBuilder appProcessBuilder() {
|
|
|
|
return appProcessBuilder(new String[] {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile. Pass the --private-window flag to
|
|
|
|
* open a private window.
|
|
|
|
*
|
|
|
|
* @param args the arguments to pass to the Firefox binary
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*/
|
|
|
|
public ProcessBuilder appProcessBuilder(String[] args) {
|
|
|
|
ArrayList<String> argList = new ArrayList<String>();
|
|
|
|
if (args != null) {
|
|
|
|
if (args.length > 0) {
|
|
|
|
for (String arg : args) {
|
|
|
|
argList.add(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return processBuilder(argList.toArray(new String[argList.size()]), true);
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-09-06 20:05:29 -04:00
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile. Pass the --headless flag to open
|
|
|
|
* without a window.
|
|
|
|
*
|
|
|
|
* @param args the arguments to pass to the Firefox binary
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile.
|
|
|
|
*/
|
|
|
|
public ProcessBuilder headlessProcessBuilder(String[] args) {
|
|
|
|
ArrayList<String> argList = new ArrayList<String>();
|
|
|
|
argList.add("--headless");
|
|
|
|
if (args != null) {
|
|
|
|
if (args.length > 0) {
|
|
|
|
for (String arg : args) {
|
|
|
|
argList.add(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 19:21:42 -04:00
|
|
|
return processBuilder(argList.toArray(new String[argList.size()]), false);
|
2022-09-06 20:05:29 -04:00
|
|
|
}
|
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Build a ProcessBuilder for the top Firefox binary and
|
|
|
|
* the default profile, with a specific set of extended
|
|
|
|
* arguments.
|
|
|
|
*
|
|
|
|
* @param args the extended arguments to pass to the Firefox binary.
|
|
|
|
* @return a ProcessBuilder for the top Firefox binary and
|
|
|
|
* default profile, with a specific set of extended arguments.
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
2022-09-15 19:21:42 -04:00
|
|
|
/*
|
|
|
|
public ProcessBuilder processBuilder(String[] args ) {
|
|
|
|
return processBuilder(args, false);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
public ProcessBuilder processBuilder(String[] args, boolean app) {
|
2022-08-30 15:49:44 -04:00
|
|
|
String firefox = topFirefox();
|
|
|
|
if (!firefox.isEmpty()) {
|
2022-09-03 21:38:20 -04:00
|
|
|
int arglength = 0;
|
|
|
|
if (args != null)
|
|
|
|
arglength = args.length;
|
2022-09-07 03:16:13 -04:00
|
|
|
String[] newArgs = new String[arglength + 4];
|
2022-08-30 15:49:44 -04:00
|
|
|
newArgs[0] = firefox;
|
2022-09-07 03:16:13 -04:00
|
|
|
newArgs[1] = "--new-instance";
|
|
|
|
newArgs[2] = "--profile";
|
2023-01-01 01:45:50 +00:00
|
|
|
newArgs[3] = this.profileDirectory(app, baseMode());
|
2022-09-03 21:38:20 -04:00
|
|
|
if (args != null) {
|
|
|
|
if (arglength > 0) {
|
|
|
|
for (int i = 0; i < arglength; i++) {
|
2022-09-07 03:16:13 -04:00
|
|
|
newArgs[i + 4] = args[i];
|
2022-09-03 21:38:20 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-09-30 00:52:20 -04:00
|
|
|
if (isOSX()) {
|
2022-10-01 17:26:19 -04:00
|
|
|
String[] fg = {""};
|
|
|
|
String[] lastArgs =
|
|
|
|
Stream.concat(Arrays.stream(newArgs), Arrays.stream(fg))
|
|
|
|
.toArray(String[] ::new);
|
|
|
|
// String[] finalArgs = Stream.concat(Arrays.stream(initArgs),
|
|
|
|
// Arrays.stream(lastArgs)).toArray(String[]::new);
|
2022-11-09 23:21:36 -05:00
|
|
|
String applicationSupportDirectory =
|
|
|
|
System.getProperty("user.home") +
|
|
|
|
"/Library/Application Support/i2pbrowser";
|
|
|
|
File applicationSupportDirectoryFile =
|
|
|
|
new File(applicationSupportDirectory);
|
|
|
|
if (!applicationSupportDirectoryFile.exists())
|
|
|
|
applicationSupportDirectoryFile.mkdirs();
|
|
|
|
File bashScript =
|
2022-11-09 23:54:24 -05:00
|
|
|
new File(applicationSupportDirectoryFile, "i2pfirefox.sh");
|
2022-10-01 17:26:19 -04:00
|
|
|
if (bashScript.exists()) {
|
|
|
|
bashScript.delete();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
FileWriter bWriter = new FileWriter(bashScript);
|
|
|
|
PrintWriter bpWriter = new PrintWriter(bWriter);
|
|
|
|
bpWriter.println("#! /usr/bin/env sh");
|
|
|
|
bpWriter.println(join(lastArgs));
|
|
|
|
bpWriter.close();
|
|
|
|
bWriter.close();
|
|
|
|
if (!bashScript.canExecute()) {
|
|
|
|
bashScript.setExecutable(true);
|
|
|
|
}
|
2022-12-23 06:20:09 +00:00
|
|
|
ProcessBuilder pb = new ProcessBuilder(bashScript.getAbsolutePath());
|
2023-01-01 01:45:50 +00:00
|
|
|
File rtd = this.runtimeDirectory(true);
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.directory(rtd);
|
|
|
|
String crashreporterFolder =
|
|
|
|
new File(rtd.getAbsolutePath(), "crashreporter").toString();
|
|
|
|
String pluginsFolder =
|
2022-12-23 06:20:42 +00:00
|
|
|
new File(rtd.getAbsolutePath(), "plugins").toString();
|
2022-12-23 18:36:04 +00:00
|
|
|
pb.environment().put("HOME", rtd.getAbsolutePath());
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.environment().put("MOZ_CRASHREPORTER", "0");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DATA_DIRECTORY",
|
|
|
|
crashreporterFolder);
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DISABLE", "1");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_NO_REPORT", "1");
|
|
|
|
pb.environment().put("MOZ_DATA_REPORTING", "0");
|
|
|
|
pb.environment().put("MOZ_MAINTENANCE_SERVICE", "0");
|
|
|
|
pb.environment().put("MOZ_PLUGIN_PATH", pluginsFolder);
|
|
|
|
pb.environment().put("MOZ_UPDATER", "0");
|
2022-12-31 22:12:12 +00:00
|
|
|
pb.environment().put("TB_CUSTOM_HOMEPAGE", newArgs[4]);
|
|
|
|
pb.environment().put("TOR_FORCE_NET_CONFIG", "0");
|
|
|
|
pb.environment().put("TOR_SKIP_LAUNCH", "1");
|
|
|
|
pb.environment().put("TOR_SKIP_CONTROLPORTTEST", "1");
|
|
|
|
pb.environment().put("TOR_NONTOR_PROXY", "1");
|
2022-12-23 06:20:09 +00:00
|
|
|
return pb;
|
2022-10-01 17:26:19 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
logger.warning(e.toString());
|
|
|
|
}
|
|
|
|
return null;
|
2022-09-30 00:52:20 -04:00
|
|
|
} else {
|
2022-12-23 06:20:09 +00:00
|
|
|
ProcessBuilder pb = new ProcessBuilder(newArgs);
|
2023-01-01 01:45:50 +00:00
|
|
|
File rtd = this.runtimeDirectory(true);
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.directory(rtd);
|
|
|
|
String crashreporterFolder =
|
|
|
|
new File(rtd.getAbsolutePath(), "crashreporter").toString();
|
|
|
|
String pluginsFolder =
|
|
|
|
new File(rtd.getAbsolutePath(), "crashreporter").toString();
|
2022-12-23 18:36:04 +00:00
|
|
|
pb.environment().put("HOME", rtd.getAbsolutePath());
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.environment().put("MOZ_CRASHREPORTER", "0");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DATA_DIRECTORY",
|
|
|
|
crashreporterFolder);
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DISABLE", "1");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_NO_REPORT", "1");
|
|
|
|
pb.environment().put("MOZ_DATA_REPORTING", "0");
|
|
|
|
pb.environment().put("MOZ_MAINTENANCE_SERVICE", "0");
|
|
|
|
pb.environment().put("MOZ_PLUGIN_PATH", pluginsFolder);
|
|
|
|
pb.environment().put("MOZ_UPDATER", "0");
|
2022-12-31 22:12:12 +00:00
|
|
|
if (args.length > 4)
|
|
|
|
pb.environment().put("TB_CUSTOM_HOMEPAGE", args[4]);
|
|
|
|
pb.environment().put("TOR_FORCE_NET_CONFIG", "0");
|
|
|
|
pb.environment().put("TOR_SKIP_LAUNCH", "1");
|
|
|
|
pb.environment().put("TOR_SKIP_CONTROLPORTTEST", "1");
|
|
|
|
pb.environment().put("TOR_NONTOR_PROXY", "1");
|
2022-12-23 06:20:09 +00:00
|
|
|
return pb;
|
2022-09-29 23:16:29 -04:00
|
|
|
}
|
2022-09-30 00:52:20 -04:00
|
|
|
|
2022-10-01 17:26:19 -04:00
|
|
|
} // else {
|
|
|
|
logger.info("No Firefox found.");
|
2022-12-23 06:20:09 +00:00
|
|
|
ProcessBuilder pb = new ProcessBuilder(args);
|
2023-01-01 01:45:50 +00:00
|
|
|
File rtd = this.runtimeDirectory(true);
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.directory(rtd);
|
|
|
|
String crashreporterFolder =
|
|
|
|
new File(rtd.getAbsolutePath(), "crashreporter").toString();
|
|
|
|
String pluginsFolder =
|
|
|
|
new File(rtd.getAbsolutePath(), "crashreporter").toString();
|
2022-12-23 18:36:04 +00:00
|
|
|
pb.environment().put("HOME", rtd.getAbsolutePath());
|
2022-12-23 06:20:09 +00:00
|
|
|
pb.environment().put("MOZ_CRASHREPORTER", "0");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DATA_DIRECTORY",
|
|
|
|
crashreporterFolder);
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_DISABLE", "1");
|
|
|
|
pb.environment().put("MOZ_CRASHREPORTER_NO_REPORT", "1");
|
|
|
|
pb.environment().put("MOZ_DATA_REPORTING", "0");
|
|
|
|
pb.environment().put("MOZ_MAINTENANCE_SERVICE", "0");
|
|
|
|
pb.environment().put("MOZ_PLUGIN_PATH", pluginsFolder);
|
|
|
|
pb.environment().put("MOZ_UPDATER", "0");
|
2022-12-31 22:12:12 +00:00
|
|
|
if (args.length > 4)
|
|
|
|
pb.environment().put("TB_CUSTOM_HOMEPAGE", args[4]);
|
|
|
|
pb.environment().put("TOR_FORCE_NET_CONFIG", "0");
|
|
|
|
pb.environment().put("TOR_SKIP_LAUNCH", "1");
|
|
|
|
pb.environment().put("TOR_SKIP_CONTROLPORTTEST", "1");
|
|
|
|
pb.environment().put("TOR_NONTOR_PROXY", "1");
|
2022-12-23 06:20:09 +00:00
|
|
|
return pb;
|
2022-10-01 17:26:19 -04:00
|
|
|
//}
|
|
|
|
// return null;
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:00:32 -04:00
|
|
|
|
2022-08-30 17:23:56 -04:00
|
|
|
private String usabilityMode() {
|
|
|
|
if (usability) {
|
|
|
|
return "usability";
|
|
|
|
}
|
|
|
|
return "base";
|
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
public Process launchAndDetatch(boolean privateWindow, String[] url) {
|
2022-09-15 19:21:42 -04:00
|
|
|
int privateWindowInt = 0;
|
|
|
|
if (privateWindow)
|
|
|
|
privateWindowInt = 1;
|
|
|
|
return launchAndDetatch(privateWindowInt, url);
|
|
|
|
}
|
|
|
|
public Process launchAndDetatch(int privateWindow, String[] url) {
|
2023-07-11 13:46:22 -04:00
|
|
|
validateUserDirectory();
|
2022-09-15 19:21:42 -04:00
|
|
|
boolean app = false;
|
|
|
|
if (privateWindow == 2)
|
|
|
|
app = true;
|
2022-08-30 15:49:44 -04:00
|
|
|
if (waitForProxy()) {
|
2023-01-01 01:45:50 +00:00
|
|
|
String profileDirectory = this.profileDirectory(app, baseMode());
|
|
|
|
if (this.validateProfileDirectory(profileDirectory)) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Valid profile directory: " + profileDirectory);
|
2022-08-30 15:49:44 -04:00
|
|
|
} else {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Invalid profile directory: " + profileDirectory +
|
|
|
|
" rebuilding...");
|
2023-01-01 01:45:50 +00:00
|
|
|
if (!this.copyBaseProfiletoProfile(usabilityMode(), app)) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Failed to rebuild profile directory: " +
|
|
|
|
profileDirectory);
|
2022-08-30 15:49:44 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Rebuilt profile directory: " + profileDirectory);
|
2022-08-28 00:25:21 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-09-06 20:05:29 -04:00
|
|
|
if (validateProfileFirstRun(profileDirectory)) {
|
2022-10-01 17:26:19 -04:00
|
|
|
if (isWindows()) {
|
2022-09-06 20:12:31 -04:00
|
|
|
ProcessBuilder hpb = headlessProcessBuilder(url);
|
2022-09-06 20:05:29 -04:00
|
|
|
try {
|
2022-09-06 20:12:31 -04:00
|
|
|
Process hp = hpb.start();
|
|
|
|
try {
|
2022-09-07 03:22:11 -04:00
|
|
|
boolean hev = hp.waitFor(5, TimeUnit.SECONDS);
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Headless browser run completed, exit: " + hev);
|
2022-09-06 23:50:19 -04:00
|
|
|
if (!hev)
|
|
|
|
hp.destroy();
|
2022-09-07 11:49:21 -04:00
|
|
|
hev = hp.waitFor(5, TimeUnit.SECONDS);
|
2022-09-07 03:22:11 -04:00
|
|
|
if (hp.isAlive()) {
|
|
|
|
int forcedExitCode = hp.destroyForcibly().waitFor();
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Headless browser run forcibly terminated, exit: " +
|
|
|
|
forcedExitCode);
|
2022-09-07 03:22:11 -04:00
|
|
|
}
|
|
|
|
int exitCode = hp.exitValue();
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Headless browser run completed, exit: " + exitCode);
|
2022-09-06 20:12:31 -04:00
|
|
|
} catch (InterruptedException e) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Headless browser error " + e.toString());
|
2022-09-06 20:12:31 -04:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Headless browser error " + e.toString());
|
2022-09-06 20:05:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
ProcessBuilder pb;
|
2022-09-15 19:21:42 -04:00
|
|
|
switch (privateWindow) {
|
|
|
|
case 0:
|
|
|
|
pb = this.defaultProcessBuilder(url);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
pb = this.privateProcessBuilder(url);
|
|
|
|
break;
|
|
|
|
case 2:
|
2022-09-15 23:34:42 -04:00
|
|
|
logger.info("Setting up app mode " + url.toString());
|
|
|
|
if (url == null || url.length == 0) {
|
|
|
|
String[] newurl = {"http://127.0.0.1:7657"};
|
|
|
|
logger.info("Setting up default urls" + newurl.toString());
|
|
|
|
pb = this.appProcessBuilder(newurl);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pb = this.appProcessBuilder(url);
|
|
|
|
break;
|
|
|
|
}
|
2022-09-15 19:21:42 -04:00
|
|
|
default:
|
|
|
|
pb = this.defaultProcessBuilder(url);
|
|
|
|
break;
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
|
|
|
try {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info(pb.command().toString());
|
2022-12-21 03:01:51 +00:00
|
|
|
process = pb.start();
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("I2PFirefox");
|
2022-08-30 15:49:44 -04:00
|
|
|
sleep(2000);
|
2022-12-21 03:01:51 +00:00
|
|
|
return process;
|
2022-08-30 15:49:44 -04:00
|
|
|
} catch (Throwable e) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info(e.toString());
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-28 00:25:21 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-28 00:25:21 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches Firefox with the profile directory.
|
|
|
|
*
|
2022-10-22 21:59:44 -04:00
|
|
|
* @param bool if true, the profile will be ephemeral(i.e. a
|
|
|
|
* --private-window profile).
|
2022-08-30 15:49:44 -04:00
|
|
|
* @param String[] a list of URL's to pass to the browser window
|
|
|
|
* @since 0.0.17
|
|
|
|
*/
|
|
|
|
public void launch(boolean privateWindow, String[] url) {
|
2022-09-15 22:29:04 -04:00
|
|
|
int priv = 0;
|
|
|
|
if (privateWindow)
|
|
|
|
priv = 1;
|
|
|
|
launch(priv, url);
|
|
|
|
}
|
|
|
|
public void launch(int privateWindow, String[] url) {
|
2022-08-30 15:49:44 -04:00
|
|
|
if (waitForProxy()) {
|
2022-12-21 03:01:51 +00:00
|
|
|
process = launchAndDetatch(privateWindow, url);
|
|
|
|
if (process == null)
|
2022-09-04 10:59:35 -04:00
|
|
|
return;
|
2022-08-30 15:49:44 -04:00
|
|
|
try {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Waiting for I2PFirefox to close...");
|
2022-12-21 03:01:51 +00:00
|
|
|
int exit = process.waitFor();
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("I2PFirefox exited with value: " + exit);
|
2022-11-09 23:54:24 -05:00
|
|
|
if (isOSX())
|
|
|
|
System.exit(exit);
|
2022-08-30 15:49:44 -04:00
|
|
|
} catch (Exception e) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Error: " + e.getMessage());
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:26:04 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
2022-08-07 02:26:04 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches Firefox with the profile directory.
|
|
|
|
*
|
2022-10-22 21:59:44 -04:00
|
|
|
* @param bool if true, the profile will be ephemeral(i.e. a
|
|
|
|
* --private-window profile).
|
2022-08-30 15:49:44 -04:00
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public void launch(boolean privateWindow) { launch(privateWindow, null); }
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches Firefox with the profile directory. Uses a semi-permanent
|
|
|
|
* profile.
|
|
|
|
*
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
public void launch() { launch(false); }
|
2022-08-19 18:30:14 -04:00
|
|
|
|
2024-03-01 16:52:37 -05:00
|
|
|
/**
|
|
|
|
* Stop all running processes managed by the browser manager.
|
2024-03-05 16:31:28 -05:00
|
|
|
*
|
2024-03-01 16:52:37 -05:00
|
|
|
* @return true if successful, false if not
|
|
|
|
*/
|
|
|
|
public boolean stop() {
|
|
|
|
if (process != null) {
|
|
|
|
process.destroy();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:26:15 -05:00
|
|
|
public boolean running() {
|
|
|
|
if (process != null)
|
|
|
|
return process.isAlive();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
private String ValidURL(String inUrl) {
|
2022-08-30 15:49:44 -04:00
|
|
|
String[] schemes = {"http", "https"};
|
|
|
|
for (String scheme : schemes) {
|
|
|
|
if (inUrl.startsWith(scheme)) {
|
2022-09-11 01:42:07 -04:00
|
|
|
logger.info("Valid URL: " + inUrl);
|
2022-08-30 15:49:44 -04:00
|
|
|
return inUrl;
|
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return "";
|
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
public static void main(String[] args) {
|
2022-09-15 22:29:04 -04:00
|
|
|
int privateBrowsing = 0;
|
2022-09-07 11:55:10 -04:00
|
|
|
I2PFirefox i2pFirefox = new I2PFirefox();
|
2023-07-11 13:46:22 -04:00
|
|
|
i2pFirefox.validateUserDirectory();
|
2023-01-01 01:45:50 +00:00
|
|
|
i2pFirefox.logger.info("checking for private browsing");
|
|
|
|
i2pFirefox.logger.info("I2PFirefox");
|
2022-08-30 15:49:44 -04:00
|
|
|
ArrayList<String> visitURL = new ArrayList<String>();
|
2022-09-03 21:38:20 -04:00
|
|
|
if (args != null) {
|
|
|
|
if (args.length > 0) {
|
|
|
|
for (String arg : args) {
|
|
|
|
if (arg.equals("-private")) {
|
2022-09-15 22:29:04 -04:00
|
|
|
privateBrowsing = 1;
|
2023-01-01 01:45:50 +00:00
|
|
|
i2pFirefox.logger.info(
|
2022-09-03 21:38:20 -04:00
|
|
|
"private browsing is true, profile will be discarded at end of session");
|
|
|
|
}
|
|
|
|
if (arg.equals("-usability")) {
|
2023-01-01 02:37:26 +00:00
|
|
|
i2pFirefox.usability = true;
|
2022-09-03 21:38:20 -04:00
|
|
|
}
|
2022-09-15 22:29:04 -04:00
|
|
|
if (arg.equals("-app")) {
|
2023-01-01 02:37:26 +00:00
|
|
|
i2pFirefox.usability = true;
|
2022-09-15 22:29:04 -04:00
|
|
|
privateBrowsing = 2;
|
|
|
|
}
|
2022-09-07 11:55:10 -04:00
|
|
|
if (arg.equals("-noproxycheck")) {
|
2023-01-01 01:45:50 +00:00
|
|
|
i2pFirefox.logger.info("zeroing out proxy check");
|
2022-09-07 11:55:10 -04:00
|
|
|
i2pFirefox.setProxyTimeoutTime(0);
|
|
|
|
}
|
2022-09-03 21:38:20 -04:00
|
|
|
if (!arg.startsWith("-")) {
|
|
|
|
// check if it's a URL
|
2023-01-01 01:45:50 +00:00
|
|
|
visitURL.add(i2pFirefox.ValidURL(arg));
|
2022-09-03 21:38:20 -04:00
|
|
|
}
|
2022-08-21 20:01:34 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i2pFirefox.launch(privateBrowsing,
|
|
|
|
visitURL.toArray(new String[visitURL.size()]));
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:45:50 +00:00
|
|
|
/*private void sleep(int millis) {
|
2022-08-30 15:49:44 -04:00
|
|
|
try {
|
|
|
|
Thread.sleep(millis);
|
|
|
|
} catch (InterruptedException bad) {
|
|
|
|
bad.printStackTrace();
|
|
|
|
throw new RuntimeException(bad);
|
2022-08-21 20:01:34 -04:00
|
|
|
}
|
2022-10-23 19:50:27 -04:00
|
|
|
}*/
|
2022-08-07 02:00:32 -04:00
|
|
|
}
|