clangfmt everything
Former-commit-id: 84bc6480d9
Former-commit-id: fa1c9c8f207bc52eb2e8fa9c1d643bea0b17af4f
This commit is contained in:
2
TODO.md
2
TODO.md
@ -9,4 +9,4 @@ To-Do List:
|
||||
- Launch $chromium_variant in I2P Mode
|
||||
- Launch system default browser in I2P Mode(Potentially Unsafe!)
|
||||
4. Registry-based discovery of Firefox variants
|
||||
5. Registry-based discovery of Chrome variants
|
||||
5. Registry-based discovery of Chrome variants
|
||||
|
19
build.xml
19
build.xml
@ -319,7 +319,24 @@ Linux(because the top command will be run and the script will exit).\n\nBoth det
|
||||
</exec>
|
||||
</target>
|
||||
|
||||
<target name="distclean" depends="clean" />
|
||||
<target name="distclean" depends="clean,clangFmt" />
|
||||
|
||||
<target name="clangFmt">
|
||||
<exec executable="clang-format">
|
||||
<arg value="-i"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PBrowser.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PCommonBrowser.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PChromium.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PChromiumProfileBuilder.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PChromiumProfileChecker.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PChromiumProfileUnpacker.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PFirefox.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PFirefoxProfileBuilder.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PFirefoxProfileChecker.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PFirefoxProfileUnpacker.java"/>
|
||||
<arg value="src/java/net/i2p/i2pfirefox/I2PGenericUnsafeBrowser.java"/>
|
||||
</exec>
|
||||
</target>
|
||||
|
||||
<target name="clean" >
|
||||
<ant dir="src" target="clean" />
|
||||
|
@ -7,192 +7,192 @@ import java.util.ArrayList;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @description I2PBrowser is a class that is used to open a browser window to the I2P network.
|
||||
* It automatically detects the operating system and available browsers, and selects
|
||||
* the best one to use, with Tor Browser at the top for Firefox and Brave at the top
|
||||
* for Chrome.
|
||||
*
|
||||
* @description I2PBrowser is a class that is used to open a browser window to
|
||||
* the I2P network. It automatically detects the operating system and available
|
||||
* browsers, and selects the best one to use, with Tor Browser at the top for
|
||||
* Firefox and Brave at the top for Chrome.
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public class I2PBrowser {
|
||||
private final I2PFirefox i2pFirefox = new I2PFirefox();
|
||||
private final I2PChromium i2pChromium = new I2PChromium();
|
||||
private final I2PGenericUnsafeBrowser i2pGeneral = new I2PGenericUnsafeBrowser();
|
||||
public boolean firefox = false;
|
||||
public boolean chromium = false;
|
||||
public boolean generic = false;
|
||||
public boolean chromiumFirst = false;
|
||||
|
||||
private void launchFirefox(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PFirefox");
|
||||
i2pFirefox.launch(privateWindow, url);
|
||||
}
|
||||
private void launchChromium(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PChromium");
|
||||
i2pChromium.launch(privateWindow, url);
|
||||
}
|
||||
private void launchGeneric(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PChromium");
|
||||
i2pGeneral.launch(privateWindow, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an I2PBrowser class which automatically determines which browser to use.
|
||||
*
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public I2PBrowser() {
|
||||
}
|
||||
private final I2PFirefox i2pFirefox = new I2PFirefox();
|
||||
private final I2PChromium i2pChromium = new I2PChromium();
|
||||
private final I2PGenericUnsafeBrowser i2pGeneral =
|
||||
new I2PGenericUnsafeBrowser();
|
||||
public boolean firefox = false;
|
||||
public boolean chromium = false;
|
||||
public boolean generic = false;
|
||||
public boolean chromiumFirst = false;
|
||||
|
||||
/**
|
||||
* Construct an I2PBrowser class which automatically determines which browser to use.
|
||||
*
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public I2PBrowser(String browserPath) {
|
||||
I2PGenericUnsafeBrowser.BROWSER = browserPath;
|
||||
}
|
||||
private void launchFirefox(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PFirefox");
|
||||
i2pFirefox.launch(privateWindow, url);
|
||||
}
|
||||
private void launchChromium(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PChromium");
|
||||
i2pChromium.launch(privateWindow, url);
|
||||
}
|
||||
private void launchGeneric(boolean privateWindow, String[] url) {
|
||||
System.out.println("I2PChromium");
|
||||
i2pGeneral.launch(privateWindow, url);
|
||||
}
|
||||
|
||||
public void setBrowser(String browserPath) {
|
||||
I2PGenericUnsafeBrowser.BROWSER = browserPath;
|
||||
}
|
||||
/**
|
||||
* Construct an I2PBrowser class which automatically determines which browser
|
||||
* to use.
|
||||
*
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public I2PBrowser() {}
|
||||
|
||||
/**
|
||||
* Return true if there is a Chromium available
|
||||
*
|
||||
* @return true if Chromium is available, false otherwise
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public boolean hasChromium() {
|
||||
String chrome = i2pChromium.topChromium();
|
||||
if (chrome == null) {
|
||||
return false;
|
||||
/**
|
||||
* Construct an I2PBrowser class which automatically determines which browser
|
||||
* to use.
|
||||
*
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public I2PBrowser(String browserPath) {
|
||||
I2PGenericUnsafeBrowser.BROWSER = browserPath;
|
||||
}
|
||||
|
||||
public void setBrowser(String browserPath) {
|
||||
I2PGenericUnsafeBrowser.BROWSER = browserPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is a Chromium available
|
||||
*
|
||||
* @return true if Chromium is available, false otherwise
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public boolean hasChromium() {
|
||||
String chrome = i2pChromium.topChromium();
|
||||
if (chrome == null) {
|
||||
return false;
|
||||
}
|
||||
if (chrome.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is a Firefox variant available
|
||||
*
|
||||
* @return true if Firefox variant is available, false otherwise
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public boolean hasFirefox() {
|
||||
String fox = i2pFirefox.topFirefox();
|
||||
if (fox == null) {
|
||||
return false;
|
||||
}
|
||||
if (fox.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @param bool if true, the profile will be ephemeral(i.e. a --private-window
|
||||
* profile).
|
||||
* @since 0.0.17
|
||||
*/
|
||||
public void launch(boolean privateWindow, String[] url) {
|
||||
if (generic)
|
||||
this.launchGeneric(privateWindow, url);
|
||||
if ((chromium && firefox) || (!chromium && !firefox)) {
|
||||
if (this.hasFirefox()) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
} else if (this.hasChromium()) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
} else {
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (chromiumFirst) {
|
||||
if (chromium) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
} else if (firefox) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
} else {
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (firefox) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
} else if (chromium) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
} else {
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @param bool if true, the profile will be ephemeral(i.e. a --private-window
|
||||
* profile).
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public void launch(boolean privateWindow) { launch(privateWindow, null); }
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public void launch() { launch(false); }
|
||||
|
||||
private static String ValidURL(String inUrl) {
|
||||
String[] schemes = {"http", "https"};
|
||||
for (String scheme : schemes) {
|
||||
if (inUrl.startsWith(scheme)) {
|
||||
return inUrl;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
boolean privateBrowsing = false;
|
||||
System.out.println("I2PBrowser");
|
||||
I2PBrowser i2pBrowser = new I2PBrowser();
|
||||
ArrayList<String> visitURL = new ArrayList<String>();
|
||||
if (args != null && args.length > 0) {
|
||||
for (String arg : args) {
|
||||
if (arg.equals("-private")) {
|
||||
privateBrowsing = true;
|
||||
}
|
||||
if (chrome.isEmpty()) {
|
||||
return false;
|
||||
if (arg.equals("-chromium")) {
|
||||
i2pBrowser.chromium = true;
|
||||
}
|
||||
return true;
|
||||
if (arg.equals("-firefox")) {
|
||||
i2pBrowser.firefox = true;
|
||||
}
|
||||
if (!arg.startsWith("-")) {
|
||||
visitURL.add(ValidURL(arg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is a Firefox variant available
|
||||
*
|
||||
* @return true if Firefox variant is available, false otherwise
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public boolean hasFirefox() {
|
||||
String fox = i2pFirefox.topFirefox();
|
||||
if (fox == null) {
|
||||
return false;
|
||||
}
|
||||
if (fox.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @param bool if true, the profile will be ephemeral(i.e. a --private-window profile).
|
||||
* @since 0.0.17
|
||||
*/
|
||||
public void launch(boolean privateWindow, String[] url){
|
||||
if (generic)
|
||||
this.launchGeneric(privateWindow, url);
|
||||
if ((chromium && firefox) || (!chromium && !firefox)) {
|
||||
if (this.hasFirefox()) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
} else if (this.hasChromium()) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
}else{
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (chromiumFirst){
|
||||
if (chromium) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
}else if (firefox) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
}else{
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (firefox) {
|
||||
this.launchFirefox(privateWindow, url);
|
||||
}else if (chromium) {
|
||||
this.launchChromium(privateWindow, url);
|
||||
}else{
|
||||
this.launchGeneric(privateWindow, url);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @param bool if true, the profile will be ephemeral(i.e. a --private-window profile).
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public void launch(boolean privateWindow){
|
||||
launch(privateWindow, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates a profile directory with a proxy configuration.
|
||||
* Waits for an HTTP proxy on the port 4444 to be ready.
|
||||
* Launches either Firefox or Chromium with the profile directory.
|
||||
*
|
||||
* @since 0.0.16
|
||||
*/
|
||||
public void launch(){
|
||||
launch(false);
|
||||
}
|
||||
|
||||
private static String ValidURL(String inUrl){
|
||||
String[] schemes = {"http", "https"};
|
||||
for (String scheme: schemes) {
|
||||
if (inUrl.startsWith(scheme)) {
|
||||
return inUrl;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
boolean privateBrowsing = false;
|
||||
System.out.println("I2PBrowser");
|
||||
I2PBrowser i2pBrowser = new I2PBrowser();
|
||||
ArrayList<String> visitURL = new ArrayList<String>();
|
||||
if (args != null && args.length > 0) {
|
||||
for (String arg : args) {
|
||||
if (arg.equals("-private")) {
|
||||
privateBrowsing = true;
|
||||
}
|
||||
if (arg.equals("-chromium")) {
|
||||
i2pBrowser.chromium = true;
|
||||
}
|
||||
if (arg.equals("-firefox")) {
|
||||
i2pBrowser.firefox = true;
|
||||
}
|
||||
if (!arg.startsWith("-")){
|
||||
visitURL.add(ValidURL(arg));
|
||||
}
|
||||
}
|
||||
}
|
||||
i2pBrowser.launch(privateBrowsing, visitURL.toArray(new String[visitURL.size()]));
|
||||
}
|
||||
|
||||
i2pBrowser.launch(privateBrowsing,
|
||||
visitURL.toArray(new String[visitURL.size()]));
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -7,7 +7,7 @@ import java.io.File;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
@ -16,134 +16,135 @@ import java.io.File;
|
||||
* contains the I2P browser profile for the Chromium browser family. It manages
|
||||
* the base profile directory and copies it's contents to the active profile
|
||||
* directory, which is actually used by Chromium.
|
||||
*
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PChromiumProfileBuilder extends I2PCommonBrowser {
|
||||
//private static boolean strict;
|
||||
// private static boolean strict;
|
||||
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String profileDirectory() {
|
||||
return profileDirectory("I2P_CHROMIUM_PROFILE", "chromium");
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String profileDirectory() {
|
||||
return profileDirectory("I2P_CHROMIUM_PROFILE", "chromium");
|
||||
}
|
||||
|
||||
private static String baseProfileDir(String file) {
|
||||
File profileDir = new File(file, "i2p.chromium.base.profile");
|
||||
// make sure the directory exists
|
||||
if (profileDir.exists()) {
|
||||
return profileDir.getAbsolutePath();
|
||||
} else {
|
||||
// create the directory
|
||||
I2PChromiumProfileUnpacker unpacker = new I2PChromiumProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(profileDir.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
return profileDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
private static String baseProfileDir(String file) {
|
||||
File profileDir = new File(file, "i2p.chromium.base.profile");
|
||||
// make sure the directory exists
|
||||
if (profileDir.exists()) {
|
||||
return profileDir.getAbsolutePath();
|
||||
} else {
|
||||
// create the directory
|
||||
I2PChromiumProfileUnpacker unpacker = new I2PChromiumProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(profileDir.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
return profileDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the base profile directory, creating it if necessary
|
||||
*
|
||||
* @return the base profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String baseProfileDirectory() {
|
||||
String pd = System.getenv("I2P_CHROMIUM_BASE_PROFILE");
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
} else {
|
||||
I2PChromiumProfileUnpacker unpacker = new I2PChromiumProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(pdf.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory();
|
||||
return baseProfileDir(rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the base profile directory, creating it if necessary
|
||||
*
|
||||
* @return the base profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String baseProfileDirectory() {
|
||||
String pd = System.getenv("I2P_CHROMIUM_BASE_PROFILE");
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
}else{
|
||||
I2PChromiumProfileUnpacker unpacker = new I2PChromiumProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(pdf.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory();
|
||||
return baseProfileDir(rtd);
|
||||
}
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_CHROMIUM_DIR environment variable
|
||||
String rtd = System.getenv("I2P_CHROMIUM_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return runtimeDirectory(rtd);
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
}
|
||||
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_CHROMIUM_DIR environment variable
|
||||
String rtd = System.getenv("I2P_CHROMIUM_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return runtimeDirectory(rtd);
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
/**
|
||||
* Copy the inert base profile directory to the runtime profile directory
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyBaseProfiletoProfile() {
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
System.out.println("Copying base profile to profile directory: " +
|
||||
baseProfile + " -> " + profile);
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
|
||||
/**
|
||||
* Copy the inert base profile directory to the runtime profile directory
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyBaseProfiletoProfile() {
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
System.out.println("Copying base profile to profile directory: " + baseProfile + " -> " + profile);
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
|
||||
try {
|
||||
System.out.println("Copying base profile to profile directory");
|
||||
copyDirectory(baseProfileDir, profileDir);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile"+e);
|
||||
return false;
|
||||
}
|
||||
System.out.println("Copied base profile to profile directory");
|
||||
return true;
|
||||
try {
|
||||
System.out.println("Copying base profile to profile directory");
|
||||
copyDirectory(baseProfileDir, profileDir);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile" + e);
|
||||
return false;
|
||||
}
|
||||
System.out.println("Copied base profile to profile directory");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PChromiumProfileBuilder() {
|
||||
//I2PChromiumProfileBuilder.strict = false;
|
||||
}
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PChromiumProfileBuilder() {
|
||||
// I2PChromiumProfileBuilder.strict = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
* @param strict if true, the strict overrides will be copied to the profile
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PChromiumProfileBuilder(boolean strict) {
|
||||
//I2PChromiumProfileBuilder.strict = strict;
|
||||
}
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
* @param strict if true, the strict overrides will be copied to the profile
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PChromiumProfileBuilder(boolean strict) {
|
||||
// I2PChromiumProfileBuilder.strict = strict;
|
||||
}
|
||||
}
|
||||
|
@ -7,122 +7,122 @@ import java.io.File;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* I2PChromiumProfileChecker is a class that checks if the Chromium profile directory
|
||||
* exists and is valid.
|
||||
*
|
||||
* I2PChromiumProfileChecker is a class that checks if the Chromium profile
|
||||
* directory exists and is valid.
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PChromiumProfileChecker {
|
||||
/**
|
||||
* Output feedback if the profile directory is valid or invalid
|
||||
*
|
||||
* @description Output feedback if the profile directory is valid or invalid
|
||||
* @args unused
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PChromiumProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
System.out.println("Profile directory: " + profileDirectory);
|
||||
boolean ok = validateProfileDirectory(profileDirectory);
|
||||
if (ok) {
|
||||
System.out.println("Profile directory is valid");
|
||||
} else {
|
||||
System.out.println("Profile directory is invalid");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Return true if the profile directory is valid.
|
||||
*
|
||||
* @param profileDirectory the profile directory to check
|
||||
* @return true if the profile directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateProfileDirectory(String profileDirectory) {
|
||||
File profileDir = new File(profileDirectory);
|
||||
if (!profileDir.exists()) {
|
||||
System.out.println("Profile directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.isDirectory()) {
|
||||
System.out.println("Profile directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canRead()) {
|
||||
System.out.println("Profile directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canWrite()) {
|
||||
System.out.println("Profile directory is not writable");
|
||||
return false;
|
||||
}
|
||||
if (!validateExtensionDirectory(profileDir+"/extensions")){
|
||||
System.out.println("extensions directory is invalid");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
/**
|
||||
* Output feedback if the profile directory is valid or invalid
|
||||
*
|
||||
* @description Output feedback if the profile directory is valid or invalid
|
||||
* @args unused
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PChromiumProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Return true if the file is valid.
|
||||
*
|
||||
* @param file the file to check
|
||||
* @return true if the file is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateFile(String file) {
|
||||
File f = new File(file);
|
||||
if (!f.exists()) {
|
||||
System.out.println("User JavaScript file does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!f.isFile()) {
|
||||
System.out.println("User JavaScript file is not a file");
|
||||
return false;
|
||||
}
|
||||
if (!f.canRead()) {
|
||||
System.out.println("User JavaScript file is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!f.canWrite()) {
|
||||
System.out.println("User JavaScript file is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
System.out.println("Profile directory: " + profileDirectory);
|
||||
boolean ok = validateProfileDirectory(profileDirectory);
|
||||
if (ok) {
|
||||
System.out.println("Profile directory is valid");
|
||||
} else {
|
||||
System.out.println("Profile directory is invalid");
|
||||
}
|
||||
/**
|
||||
* Return true if the extension directory is valid.
|
||||
*
|
||||
* @param extensionDirectory the extension directory to check
|
||||
* @return true if the extension directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateExtensionDirectory(String extensionDirectory) {
|
||||
File extensionDir = new File(extensionDirectory);
|
||||
if (!extensionDir.exists()) {
|
||||
System.out.println("Extension directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.isDirectory()) {
|
||||
System.out.println("Extension directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canRead()) {
|
||||
System.out.println("Extension directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canWrite()) {
|
||||
System.out.println("Extension directory is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the profile directory is valid.
|
||||
*
|
||||
* @param profileDirectory the profile directory to check
|
||||
* @return true if the profile directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateProfileDirectory(String profileDirectory) {
|
||||
File profileDir = new File(profileDirectory);
|
||||
if (!profileDir.exists()) {
|
||||
System.out.println("Profile directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.isDirectory()) {
|
||||
System.out.println("Profile directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canRead()) {
|
||||
System.out.println("Profile directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canWrite()) {
|
||||
System.out.println("Profile directory is not writable");
|
||||
return false;
|
||||
}
|
||||
if (!validateExtensionDirectory(profileDir + "/extensions")) {
|
||||
System.out.println("extensions directory is invalid");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the file is valid.
|
||||
*
|
||||
* @param file the file to check
|
||||
* @return true if the file is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateFile(String file) {
|
||||
File f = new File(file);
|
||||
if (!f.exists()) {
|
||||
System.out.println("User JavaScript file does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!f.isFile()) {
|
||||
System.out.println("User JavaScript file is not a file");
|
||||
return false;
|
||||
}
|
||||
if (!f.canRead()) {
|
||||
System.out.println("User JavaScript file is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!f.canWrite()) {
|
||||
System.out.println("User JavaScript file is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the extension directory is valid.
|
||||
*
|
||||
* @param extensionDirectory the extension directory to check
|
||||
* @return true if the extension directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateExtensionDirectory(String extensionDirectory) {
|
||||
File extensionDir = new File(extensionDirectory);
|
||||
if (!extensionDir.exists()) {
|
||||
System.out.println("Extension directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.isDirectory()) {
|
||||
System.out.println("Extension directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canRead()) {
|
||||
System.out.println("Extension directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canWrite()) {
|
||||
System.out.println("Extension directory is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -5,38 +5,37 @@ package net.i2p.i2pfirefox;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* I2PChromiumProfileUnpacker is a class that unpacks the Chromium profile zip file
|
||||
* into the Chromium base profile directory. This is not used by the Chromium browser
|
||||
* instance, it's unpacked to the disk to be copied to the active profile directory.
|
||||
*
|
||||
* I2PChromiumProfileUnpacker is a class that unpacks the Chromium profile zip
|
||||
* file into the Chromium base profile directory. This is not used by the
|
||||
* Chromium browser instance, it's unpacked to the disk to be copied to the
|
||||
* active profile directory.
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PChromiumProfileUnpacker extends I2PCommonBrowser {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PChromiumProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PChromiumProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* unpack the profile directory
|
||||
*
|
||||
* @return true if the profile directory was successfully unpacked
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public boolean unpackProfile(String profileDirectory) {
|
||||
System.out.println("Unpacking base profile to " + profileDirectory);
|
||||
return unpackProfile(profileDirectory, "chromium", "base");
|
||||
}
|
||||
|
||||
/**
|
||||
* unpack the profile directory
|
||||
*
|
||||
* @return true if the profile directory was successfully unpacked
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public boolean unpackProfile(String profileDirectory) {
|
||||
System.out.println("Unpacking base profile to " + profileDirectory);
|
||||
return unpackProfile(profileDirectory, "chromium", "base");
|
||||
}
|
||||
}
|
||||
|
@ -12,162 +12,173 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class I2PCommonBrowser {
|
||||
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static File runtimeDirectory(boolean create, String override) {
|
||||
String rtd = runtimeDirectory(override);
|
||||
File rtdFile = new File(rtd);
|
||||
if (create) {
|
||||
if (!rtdFile.exists()) {
|
||||
rtdFile.mkdir();
|
||||
}
|
||||
}
|
||||
return new File(rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static String runtimeDirectory(String override) {
|
||||
// get the I2P_FIREFOX_DIR environment variable
|
||||
String rtd = System.getenv(override);
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return rtd;
|
||||
}
|
||||
}
|
||||
// obtain the PLUGIN environment variable
|
||||
String plugin = System.getenv("PLUGIN");
|
||||
if (plugin != null && !plugin.isEmpty()) {
|
||||
File pluginDir = new File(plugin);
|
||||
if (pluginDir.exists()) {
|
||||
return pluginDir.toString();
|
||||
}
|
||||
}
|
||||
String userDir = System.getProperty("user.dir");
|
||||
if (userDir != null && !userDir.isEmpty()) {
|
||||
File userDir1 = new File(userDir);
|
||||
if (userDir1.exists()) {
|
||||
return userDir1.toString();
|
||||
}
|
||||
}
|
||||
String homeDir = System.getProperty("user.home");
|
||||
if (homeDir != null && !homeDir.isEmpty()) {
|
||||
File homeDir1 = new File(homeDir+"/.i2p");
|
||||
if (homeDir1.exists()) {
|
||||
return homeDir.toString();
|
||||
}
|
||||
File homeDir2 = new File(homeDir+"/i2p");
|
||||
if (homeDir2.exists()) {
|
||||
return homeDir2.toString();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static File runtimeDirectory(boolean create, String override) {
|
||||
String rtd = runtimeDirectory(override);
|
||||
File rtdFile = new File(rtd);
|
||||
if (create) {
|
||||
if (!rtdFile.exists()) {
|
||||
rtdFile.mkdir();
|
||||
}
|
||||
}
|
||||
return new File(rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static String profileDirectory(String envVar, String browser) {
|
||||
String pd = System.getenv(envVar);
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory("");
|
||||
return profileDir(rtd, browser);
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static String runtimeDirectory(String override) {
|
||||
// get the I2P_FIREFOX_DIR environment variable
|
||||
String rtd = System.getenv(override);
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return rtd;
|
||||
}
|
||||
}
|
||||
// obtain the PLUGIN environment variable
|
||||
String plugin = System.getenv("PLUGIN");
|
||||
if (plugin != null && !plugin.isEmpty()) {
|
||||
File pluginDir = new File(plugin);
|
||||
if (pluginDir.exists()) {
|
||||
return pluginDir.toString();
|
||||
}
|
||||
}
|
||||
String userDir = System.getProperty("user.dir");
|
||||
if (userDir != null && !userDir.isEmpty()) {
|
||||
File userDir1 = new File(userDir);
|
||||
if (userDir1.exists()) {
|
||||
return userDir1.toString();
|
||||
}
|
||||
}
|
||||
String homeDir = System.getProperty("user.home");
|
||||
if (homeDir != null && !homeDir.isEmpty()) {
|
||||
File homeDir1 = new File(homeDir + "/.i2p");
|
||||
if (homeDir1.exists()) {
|
||||
return homeDir.toString();
|
||||
}
|
||||
File homeDir2 = new File(homeDir + "/i2p");
|
||||
if (homeDir2.exists()) {
|
||||
return homeDir2.toString();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
protected static String profileDir(String file, String browser) {
|
||||
File profileDir = new File(file, "i2p."+browser+".profile");
|
||||
return profileDir.getAbsolutePath();
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
* @since 0.0.19
|
||||
*/
|
||||
protected static String profileDirectory(String envVar, String browser) {
|
||||
String pd = System.getenv(envVar);
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory("");
|
||||
return profileDir(rtd, browser);
|
||||
}
|
||||
|
||||
protected boolean unpackProfile(String profileDirectory, String browser, String base) {
|
||||
System.out.println("Unpacking base profile to " + profileDirectory);
|
||||
try {
|
||||
final InputStream resources = this.getClass().getClassLoader().getResourceAsStream("i2p."+browser+"."+base+".profile.zip");
|
||||
if (resources == null) {
|
||||
System.out.println("Could not find resources");
|
||||
return false;
|
||||
}
|
||||
System.out.println(resources.toString());
|
||||
// InputStream corresponds to a zip file. Unzip it.
|
||||
//Files.copy(r, new File(profileDirectory).toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
ZipInputStream zis = new ZipInputStream(resources);
|
||||
ZipEntry entry;
|
||||
// while there are entries I process them
|
||||
while ((entry = zis.getNextEntry()) != null)
|
||||
{
|
||||
System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
|
||||
// consume all the data from this entry
|
||||
if (entry.isDirectory()) {
|
||||
System.out.println("Creating directory: " + entry.getName());
|
||||
File dir = new File(profileDirectory + "/" + entry.getName());
|
||||
dir.mkdirs();
|
||||
} else {
|
||||
System.out.println("Creating file: " + entry.getName());
|
||||
File file = new File(profileDirectory + "/" + entry.getName());
|
||||
file.createNewFile();
|
||||
Files.copy(zis, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
while (zis.available() > 0)
|
||||
zis.read();
|
||||
// I could close the entry, but getNextEntry does it automatically
|
||||
// zis.closeEntry()
|
||||
}
|
||||
// loop through the Enumeration
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying profile files: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
|
||||
destinationDirectory = new File(destinationDirectory.toString().replace("i2p.chromium.base.profile", ""));
|
||||
if (!destinationDirectory.exists()) {
|
||||
destinationDirectory.mkdir();
|
||||
}
|
||||
for (String f : sourceDirectory.list()) {
|
||||
copyDirectoryCompatibityMode(new File(sourceDirectory, f), new File(destinationDirectory, f));
|
||||
}
|
||||
}
|
||||
protected static String profileDir(String file, String browser) {
|
||||
File profileDir = new File(file, "i2p." + browser + ".profile");
|
||||
return profileDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static void copyDirectoryCompatibityMode(File source, File destination) throws IOException {
|
||||
if (source.isDirectory()) {
|
||||
copyDirectory(source, destination);
|
||||
protected boolean unpackProfile(String profileDirectory, String browser,
|
||||
String base) {
|
||||
System.out.println("Unpacking base profile to " + profileDirectory);
|
||||
try {
|
||||
final InputStream resources =
|
||||
this.getClass().getClassLoader().getResourceAsStream(
|
||||
"i2p." + browser + "." + base + ".profile.zip");
|
||||
if (resources == null) {
|
||||
System.out.println("Could not find resources");
|
||||
return false;
|
||||
}
|
||||
System.out.println(resources.toString());
|
||||
// InputStream corresponds to a zip file. Unzip it.
|
||||
// Files.copy(r, new File(profileDirectory).toPath(),
|
||||
// StandardCopyOption.REPLACE_EXISTING);
|
||||
ZipInputStream zis = new ZipInputStream(resources);
|
||||
ZipEntry entry;
|
||||
// while there are entries I process them
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
System.out.println("entry: " + entry.getName() + ", " +
|
||||
entry.getSize());
|
||||
// consume all the data from this entry
|
||||
if (entry.isDirectory()) {
|
||||
System.out.println("Creating directory: " + entry.getName());
|
||||
File dir = new File(profileDirectory + "/" + entry.getName());
|
||||
dir.mkdirs();
|
||||
} else {
|
||||
copyFile(source, destination);
|
||||
System.out.println("Creating file: " + entry.getName());
|
||||
File file = new File(profileDirectory + "/" + entry.getName());
|
||||
file.createNewFile();
|
||||
Files.copy(zis, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
while (zis.available() > 0)
|
||||
zis.read();
|
||||
// I could close the entry, but getNextEntry does it automatically
|
||||
// zis.closeEntry()
|
||||
}
|
||||
// loop through the Enumeration
|
||||
|
||||
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
|
||||
try (InputStream in = new FileInputStream(sourceFile);
|
||||
OutputStream out = new FileOutputStream(destinationFile)) {
|
||||
byte[] buf = new byte[1024];
|
||||
int length;
|
||||
while ((length = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying profile files: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static void copyDirectory(File sourceDirectory,
|
||||
File destinationDirectory)
|
||||
throws IOException {
|
||||
destinationDirectory = new File(destinationDirectory.toString().replace(
|
||||
"i2p.chromium.base.profile", ""));
|
||||
if (!destinationDirectory.exists()) {
|
||||
destinationDirectory.mkdir();
|
||||
}
|
||||
for (String f : sourceDirectory.list()) {
|
||||
copyDirectoryCompatibityMode(new File(sourceDirectory, f),
|
||||
new File(destinationDirectory, f));
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyDirectoryCompatibityMode(File source, File destination)
|
||||
throws IOException {
|
||||
if (source.isDirectory()) {
|
||||
copyDirectory(source, destination);
|
||||
} else {
|
||||
copyFile(source, destination);
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyFile(File sourceFile, File destinationFile)
|
||||
throws IOException {
|
||||
try (InputStream in = new FileInputStream(sourceFile);
|
||||
OutputStream out = new FileOutputStream(destinationFile)) {
|
||||
byte[] buf = new byte[1024];
|
||||
int length;
|
||||
while ((length = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,7 @@ import java.nio.file.StandardCopyOption;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
@ -18,183 +18,181 @@ import java.nio.file.StandardCopyOption;
|
||||
* contains the I2P browser profile for the Firefox browser family. It manages
|
||||
* the base profile directory and copies it's contents to the active profile
|
||||
* directory, which is actually used by Firefox.
|
||||
*
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PFirefoxProfileBuilder extends I2PCommonBrowser {
|
||||
private static boolean strict;
|
||||
private static boolean strict;
|
||||
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String profileDirectory() {
|
||||
return profileDirectory("I2P_FIREFOX_PROFILE", "firefox");
|
||||
}
|
||||
|
||||
private static String baseProfileDir(String file) {
|
||||
File profileDir = new File(file, "i2p.firefox.base.profile");
|
||||
// make sure the directory exists
|
||||
if (profileDir.exists()) {
|
||||
return profileDir.getAbsolutePath();
|
||||
} else {
|
||||
// create the directory
|
||||
I2PFirefoxProfileUnpacker unpacker = new I2PFirefoxProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(profileDir.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
return profileDir.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* get the profile directory, creating it if necessary
|
||||
*
|
||||
* @return the profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String profileDirectory() {
|
||||
return profileDirectory("I2P_FIREFOX_PROFILE", "firefox");
|
||||
}
|
||||
|
||||
/**
|
||||
* get the base profile directory, creating it if necessary
|
||||
*
|
||||
* @return the base profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String baseProfileDirectory() {
|
||||
String pd = System.getenv("I2P_FIREFOX_BASE_PROFILE");
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
}else{
|
||||
I2PFirefoxProfileUnpacker unpacker = new I2PFirefoxProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(pdf.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory();
|
||||
return baseProfileDir(rtd);
|
||||
private static String baseProfileDir(String file) {
|
||||
File profileDir = new File(file, "i2p.firefox.base.profile");
|
||||
// make sure the directory exists
|
||||
if (profileDir.exists()) {
|
||||
return profileDir.getAbsolutePath();
|
||||
} else {
|
||||
// create the directory
|
||||
I2PFirefoxProfileUnpacker unpacker = new I2PFirefoxProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(profileDir.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
return profileDir.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
/**
|
||||
* get the base profile directory, creating it if necessary
|
||||
*
|
||||
* @return the base profile directory, or null if it could not be created
|
||||
*/
|
||||
public static String baseProfileDirectory() {
|
||||
String pd = System.getenv("I2P_FIREFOX_BASE_PROFILE");
|
||||
if (pd != null && !pd.isEmpty()) {
|
||||
File pdf = new File(pd);
|
||||
if (pdf.exists() && pdf.isDirectory()) {
|
||||
return pd;
|
||||
} else {
|
||||
I2PFirefoxProfileUnpacker unpacker = new I2PFirefoxProfileUnpacker();
|
||||
if (!unpacker.unpackProfile(pdf.getAbsolutePath())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
String rtd = runtimeDirectory();
|
||||
return baseProfileDir(rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_FIREFOX_DIR environment variable
|
||||
String rtd = System.getenv("I2P_FIREFOX_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return runtimeDirectory(rtd);
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_FIREFOX_DIR environment variable
|
||||
String rtd = System.getenv("I2P_FIREFOX_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return runtimeDirectory(rtd);
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the inert base profile directory to the runtime profile directory
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyBaseProfiletoProfile() {
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
System.out.println("Copying base profile to profile directory: " + baseProfile + " -> " + profile);
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
|
||||
try {
|
||||
System.out.println("Copying base profile to profile directory");
|
||||
copyDirectory(baseProfileDir, profileDir);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile"+e);
|
||||
return false;
|
||||
}
|
||||
System.out.println("Copied base profile to profile directory");
|
||||
// if user.js does not exist yet, make an empty one.
|
||||
//if (!touch(profileDir.toString(), "user.js")) {
|
||||
//return false;
|
||||
//}
|
||||
// if extensions does not exist yet, make an empty one.
|
||||
//if (!mkExtensionsDir(profileDir.toString())){
|
||||
//return false;
|
||||
//}
|
||||
|
||||
|
||||
return copyStrictOptions();
|
||||
/**
|
||||
* Copy the inert base profile directory to the runtime profile directory
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyBaseProfiletoProfile() {
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
System.out.println("Copying base profile to profile directory: " +
|
||||
baseProfile + " -> " + profile);
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
|
||||
/**
|
||||
* Copy the strict options from the base profile to the profile
|
||||
*
|
||||
* @return true if successful, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyStrictOptions() {
|
||||
if (!strict){
|
||||
return true;
|
||||
}
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
if (!baseProfileDir.exists() || !profileDir.exists()) {
|
||||
return false;
|
||||
}
|
||||
File baseOverrides = new File(baseProfile, "strict-overrides.js");
|
||||
File userOverrides = new File(baseProfile, "user-overrides.js");
|
||||
if (!baseOverrides.exists()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Files.copy(baseOverrides.toPath(), userOverrides.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile"+e);
|
||||
return false;
|
||||
}
|
||||
// if user-overrides.js does not exist yet, make an empty one.
|
||||
//if (!touch(profileDir.toString(), "user-overrides.js")) {
|
||||
//return false;
|
||||
//}
|
||||
return true;
|
||||
try {
|
||||
System.out.println("Copying base profile to profile directory");
|
||||
copyDirectory(baseProfileDir, profileDir);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile" + e);
|
||||
return false;
|
||||
}
|
||||
System.out.println("Copied base profile to profile directory");
|
||||
// if user.js does not exist yet, make an empty one.
|
||||
// if (!touch(profileDir.toString(), "user.js")) {
|
||||
// return false;
|
||||
//}
|
||||
// if extensions does not exist yet, make an empty one.
|
||||
// if (!mkExtensionsDir(profileDir.toString())){
|
||||
// return false;
|
||||
//}
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PFirefoxProfileBuilder() {
|
||||
I2PFirefoxProfileBuilder.strict = false;
|
||||
}
|
||||
return copyStrictOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
* @param strict if true, the strict overrides will be copied to the profile
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PFirefoxProfileBuilder(boolean strict) {
|
||||
I2PFirefoxProfileBuilder.strict = strict;
|
||||
/**
|
||||
* Copy the strict options from the base profile to the profile
|
||||
*
|
||||
* @return true if successful, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean copyStrictOptions() {
|
||||
if (!strict) {
|
||||
return true;
|
||||
}
|
||||
String baseProfile = baseProfileDirectory();
|
||||
String profile = profileDirectory();
|
||||
if (baseProfile.isEmpty() || profile.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
File baseProfileDir = new File(baseProfile);
|
||||
File profileDir = new File(profile);
|
||||
if (!baseProfileDir.exists() || !profileDir.exists()) {
|
||||
return false;
|
||||
}
|
||||
File baseOverrides = new File(baseProfile, "strict-overrides.js");
|
||||
File userOverrides = new File(baseProfile, "user-overrides.js");
|
||||
if (!baseOverrides.exists()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Files.copy(baseOverrides.toPath(), userOverrides.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error copying base profile to profile" + e);
|
||||
return false;
|
||||
}
|
||||
// if user-overrides.js does not exist yet, make an empty one.
|
||||
// if (!touch(profileDir.toString(), "user-overrides.js")) {
|
||||
// return false;
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PFirefoxProfileBuilder() { I2PFirefoxProfileBuilder.strict = false; }
|
||||
|
||||
/**
|
||||
* Construct a new Profile Builder
|
||||
* @param strict if true, the strict overrides will be copied to the profile
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public I2PFirefoxProfileBuilder(boolean strict) {
|
||||
I2PFirefoxProfileBuilder.strict = strict;
|
||||
}
|
||||
}
|
||||
|
@ -7,127 +7,127 @@ import java.io.File;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* I2PFirefoxProfileChecker is a class that checks if the Firefox profile directory
|
||||
* exists and is valid.
|
||||
*
|
||||
* I2PFirefoxProfileChecker is a class that checks if the Firefox profile
|
||||
* directory exists and is valid.
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PFirefoxProfileChecker {
|
||||
|
||||
/**
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PFirefoxProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
System.out.println("Profile directory: " + profileDirectory);
|
||||
boolean ok = validateProfileDirectory(profileDirectory);
|
||||
if (ok) {
|
||||
System.out.println("Profile directory is valid");
|
||||
} else {
|
||||
System.out.println("Profile directory is invalid");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args unused
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PFirefoxProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Return true if the profile directory is valid.
|
||||
*
|
||||
* @param profileDirectory the profile directory to check
|
||||
* @return true if the profile directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateProfileDirectory(String profileDirectory) {
|
||||
File profileDir = new File(profileDirectory);
|
||||
if (!profileDir.exists()) {
|
||||
System.out.println("Profile directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.isDirectory()) {
|
||||
System.out.println("Profile directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canRead()) {
|
||||
System.out.println("Profile directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canWrite()) {
|
||||
System.out.println("Profile directory is not writable");
|
||||
return false;
|
||||
}
|
||||
if (!validateFile(profileDir+"/prefs.js")){
|
||||
System.out.println("prefs.js is not valid");
|
||||
return false;
|
||||
}
|
||||
if (!validateFile(profileDir+"/user.js")){
|
||||
System.out.println("user.js is not valid");
|
||||
return false;
|
||||
}
|
||||
if (!validateExtensionDirectory(profileDir+"/extensions")){
|
||||
System.out.println("extensions directory is invalid");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
System.out.println("Profile directory: " + profileDirectory);
|
||||
boolean ok = validateProfileDirectory(profileDirectory);
|
||||
if (ok) {
|
||||
System.out.println("Profile directory is valid");
|
||||
} else {
|
||||
System.out.println("Profile directory is invalid");
|
||||
}
|
||||
/**
|
||||
* Return true if the file is valid.
|
||||
*
|
||||
* @param file the file to check
|
||||
* @return true if the file is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateFile(String file) {
|
||||
File f = new File(file);
|
||||
if (!f.exists()) {
|
||||
System.out.println("User JavaScript file does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!f.isFile()) {
|
||||
System.out.println("User JavaScript file is not a file");
|
||||
return false;
|
||||
}
|
||||
if (!f.canRead()) {
|
||||
System.out.println("User JavaScript file is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!f.canWrite()) {
|
||||
System.out.println("User JavaScript file is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the profile directory is valid.
|
||||
*
|
||||
* @param profileDirectory the profile directory to check
|
||||
* @return true if the profile directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateProfileDirectory(String profileDirectory) {
|
||||
File profileDir = new File(profileDirectory);
|
||||
if (!profileDir.exists()) {
|
||||
System.out.println("Profile directory does not exist");
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Return true if the extension directory is valid.
|
||||
*
|
||||
* @param extensionDirectory the extension directory to check
|
||||
* @return true if the extension directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateExtensionDirectory(String extensionDirectory) {
|
||||
File extensionDir = new File(extensionDirectory);
|
||||
if (!extensionDir.exists()) {
|
||||
System.out.println("Extension directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.isDirectory()) {
|
||||
System.out.println("Extension directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canRead()) {
|
||||
System.out.println("Extension directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canWrite()) {
|
||||
System.out.println("Extension directory is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
if (!profileDir.isDirectory()) {
|
||||
System.out.println("Profile directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canRead()) {
|
||||
System.out.println("Profile directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!profileDir.canWrite()) {
|
||||
System.out.println("Profile directory is not writable");
|
||||
return false;
|
||||
}
|
||||
if (!validateFile(profileDir + "/prefs.js")) {
|
||||
System.out.println("prefs.js is not valid");
|
||||
return false;
|
||||
}
|
||||
if (!validateFile(profileDir + "/user.js")) {
|
||||
System.out.println("user.js is not valid");
|
||||
return false;
|
||||
}
|
||||
if (!validateExtensionDirectory(profileDir + "/extensions")) {
|
||||
System.out.println("extensions directory is invalid");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the file is valid.
|
||||
*
|
||||
* @param file the file to check
|
||||
* @return true if the file is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateFile(String file) {
|
||||
File f = new File(file);
|
||||
if (!f.exists()) {
|
||||
System.out.println("User JavaScript file does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!f.isFile()) {
|
||||
System.out.println("User JavaScript file is not a file");
|
||||
return false;
|
||||
}
|
||||
if (!f.canRead()) {
|
||||
System.out.println("User JavaScript file is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!f.canWrite()) {
|
||||
System.out.println("User JavaScript file is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Return true if the extension directory is valid.
|
||||
*
|
||||
* @param extensionDirectory the extension directory to check
|
||||
* @return true if the extension directory is valid, false otherwise
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public static boolean validateExtensionDirectory(String extensionDirectory) {
|
||||
File extensionDir = new File(extensionDirectory);
|
||||
if (!extensionDir.exists()) {
|
||||
System.out.println("Extension directory does not exist");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.isDirectory()) {
|
||||
System.out.println("Extension directory is not a directory");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canRead()) {
|
||||
System.out.println("Extension directory is not readable");
|
||||
return false;
|
||||
}
|
||||
if (!extensionDir.canWrite()) {
|
||||
System.out.println("Extension directory is not writable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ package net.i2p.i2pfirefox;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
@ -14,30 +14,27 @@ package net.i2p.i2pfirefox;
|
||||
* from a zip file embedded in the `jar` file. The zip is unpacked to a base
|
||||
* directory where it is left untouched, and the base profile is copied to the
|
||||
* active profile directory.
|
||||
*
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class I2PFirefoxProfileUnpacker extends I2PCommonBrowser {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PFirefoxProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String profileDirectory = I2PFirefoxProfileBuilder.profileDirectory();
|
||||
if (profileDirectory == null) {
|
||||
System.out.println("No profile directory found");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* unpack the profile directory
|
||||
*
|
||||
* @return true if the profile directory was successfully unpacked
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public boolean unpackProfile(String profileDirectory){
|
||||
return unpackProfile(profileDirectory, "firefox", "base");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* unpack the profile directory
|
||||
*
|
||||
* @return true if the profile directory was successfully unpacked
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public boolean unpackProfile(String profileDirectory) {
|
||||
return unpackProfile(profileDirectory, "firefox", "base");
|
||||
}
|
||||
}
|
||||
|
@ -11,399 +11,405 @@ import java.util.Scanner;
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* I2PGenericUnsafeBrowser is a wrapper which sets common environment
|
||||
* variables for the process controlled by a processbuilder.
|
||||
*
|
||||
*
|
||||
* ALWAYS ALWAYS ALWAYS try the Firefox and Chromium specific launchers
|
||||
* first.
|
||||
*
|
||||
*
|
||||
* @author idk
|
||||
* @since 0.0.18
|
||||
*/
|
||||
|
||||
public class I2PGenericUnsafeBrowser extends I2PCommonBrowser {
|
||||
private final int DEFAULT_TIMEOUT = 200;
|
||||
public static String BROWSER = "";
|
||||
private Process p = null;
|
||||
// Ideally, EVERY browser in this list should honor http_proxy, https_proxy, ftp_proxy and no_proxy.
|
||||
// in practice, this is going to be hard to guarantee. For now, we're just assuming. So don't use this until
|
||||
// I understand the situation better, unless you think you know better.
|
||||
private static final String[] browsers = {
|
||||
// This debian script tries everything in $BROWSER, then gnome-www-browser and x-www-browser
|
||||
// if X is running and www-browser otherwise. Those point to the user's preferred
|
||||
// browser using the update-alternatives system.
|
||||
"sensible-browser",
|
||||
// another one that opens a preferred browser
|
||||
"xdg-open",
|
||||
// Try x-www-browser directly
|
||||
"x-www-browser",
|
||||
"gnome-www-browser",
|
||||
// general graphical browsers that aren't Firefox or Chromium based
|
||||
"defaultbrowser", // puppy linux
|
||||
"dillo",
|
||||
"seamonkey",
|
||||
"konqueror",
|
||||
"galeon",
|
||||
"surf",
|
||||
// Text Mode Browsers only below here
|
||||
"www-browser",
|
||||
"links",
|
||||
"lynx"
|
||||
};
|
||||
private final int DEFAULT_TIMEOUT = 200;
|
||||
public static String BROWSER = "";
|
||||
private Process p = null;
|
||||
// Ideally, EVERY browser in this list should honor http_proxy, https_proxy,
|
||||
// ftp_proxy and no_proxy. in practice, this is going to be hard to guarantee.
|
||||
// For now, we're just assuming. So don't use this until I understand the
|
||||
// situation better, unless you think you know better.
|
||||
private static final String[] browsers = {
|
||||
// This debian script tries everything in $BROWSER, then gnome-www-browser
|
||||
// and x-www-browser
|
||||
// if X is running and www-browser otherwise. Those point to the user's
|
||||
// preferred
|
||||
// browser using the update-alternatives system.
|
||||
"sensible-browser",
|
||||
// another one that opens a preferred browser
|
||||
"xdg-open",
|
||||
// Try x-www-browser directly
|
||||
"x-www-browser", "gnome-www-browser",
|
||||
// general graphical browsers that aren't Firefox or Chromium based
|
||||
"defaultbrowser", // puppy linux
|
||||
"dillo", "seamonkey", "konqueror", "galeon", "surf",
|
||||
// Text Mode Browsers only below here
|
||||
"www-browser", "links", "lynx"};
|
||||
|
||||
private static String getOperatingSystem() {
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
return "Windows";
|
||||
} else if (os.contains("Linux")) {
|
||||
return "Linux";
|
||||
} else if (os.contains("BSD")) {
|
||||
return "BSD";
|
||||
} else if (os.contains("Mac")) {
|
||||
return "Mac";
|
||||
private static String getOperatingSystem() {
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
return "Windows";
|
||||
} else if (os.contains("Linux")) {
|
||||
return "Linux";
|
||||
} else if (os.contains("BSD")) {
|
||||
return "BSD";
|
||||
} else if (os.contains("Mac")) {
|
||||
return "Mac";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the default browser for the Windows platform, which by now should
|
||||
* be Edgium in the worst-case scenario but in case it isn't, we can use this
|
||||
* function to figure it out. It can find:
|
||||
*
|
||||
* 1. The current user's HTTPS default browser if they configured it to be
|
||||
* non-default
|
||||
* 2. The current user's HTTP default browser if they configured it to be
|
||||
* non-default
|
||||
* 3. Edgium if it's available
|
||||
* 4. iexplore if it's not
|
||||
*
|
||||
* and it will return the first one we find in exactly that order.
|
||||
*
|
||||
* Adapted from:
|
||||
* https://stackoverflow.com/questions/15852885/method-returning-default-browser-as-a-string
|
||||
* and from:
|
||||
* https://github.com/i2p/i2p.i2p/blob/master/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java
|
||||
*
|
||||
* @return path to the default browser ready for execution. Empty string on
|
||||
* Linux and OSX.
|
||||
*/
|
||||
public static String getDefaultWindowsBrowser() {
|
||||
if (getOperatingSystem() == "Windows") {
|
||||
String defaultBrowser = getDefaultOutOfRegistry(
|
||||
"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\https\\UserChoice");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry(
|
||||
"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\http\\UserChoice");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry(
|
||||
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\microsoft-edge\\shell\\open\\command");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry(
|
||||
"HKEY_CLASSES_ROOT\\http\\shell\\open\\command");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* obtains information out of the Windows registry.
|
||||
*
|
||||
* @param hkeyquery registry entry to ask for.
|
||||
* @return
|
||||
*/
|
||||
public static String getDefaultOutOfRegistry(String hkeyquery) {
|
||||
if (getOperatingSystem() == "Windows") {
|
||||
try {
|
||||
// Get registry where we find the default browser
|
||||
Process process = Runtime.getRuntime().exec("REG QUERY " + hkeyquery);
|
||||
Scanner kb = new Scanner(process.getInputStream());
|
||||
while (kb.hasNextLine()) {
|
||||
String line = kb.nextLine();
|
||||
if (line.contains("(Default")) {
|
||||
String[] splitLine = line.split(" ");
|
||||
kb.close();
|
||||
return splitLine[splitLine.length - 1]
|
||||
.replace("%1", "")
|
||||
.replaceAll("\\s+$", "")
|
||||
.replaceAll("\"", "");
|
||||
}
|
||||
}
|
||||
return "Unknown";
|
||||
// Match wasn't found, still need to close Scanner
|
||||
kb.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String scanAPath(String dir) {
|
||||
for (String browser : browsers) {
|
||||
File test = new File(dir, browser);
|
||||
if (test.exists()) {
|
||||
return test.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any browser in our list within a UNIX path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getAnyUnixBrowser() {
|
||||
// read the PATH environment variable and split it by ":"
|
||||
String[] path = System.getenv("PATH").split(":");
|
||||
if (path != null && path.length > 0) {
|
||||
for (String p : path) {
|
||||
String f = scanAPath(p);
|
||||
if (f != "") {
|
||||
return f;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any usable browser and output the whole path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String findUnsafeBrowserAnywhere() {
|
||||
if (BROWSER != "") {
|
||||
File f = new File(BROWSER);
|
||||
if (f.exists())
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the default browser for the Windows platform, which by now should be Edgium in
|
||||
* the worst-case scenario but in case it isn't, we can use this function to figure it out.
|
||||
* It can find:
|
||||
*
|
||||
* 1. The current user's HTTPS default browser if they configured it to be non-default
|
||||
* 2. The current user's HTTP default browser if they configured it to be non-default
|
||||
* 3. Edgium if it's available
|
||||
* 4. iexplore if it's not
|
||||
*
|
||||
* and it will return the first one we find in exactly that order.
|
||||
*
|
||||
* Adapted from:
|
||||
* https://stackoverflow.com/questions/15852885/method-returning-default-browser-as-a-string
|
||||
* and from:
|
||||
* https://github.com/i2p/i2p.i2p/blob/master/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java
|
||||
*
|
||||
* @return path to the default browser ready for execution. Empty string on Linux and OSX.
|
||||
*/
|
||||
public static String getDefaultWindowsBrowser() {
|
||||
if (getOperatingSystem() == "Windows"){
|
||||
String defaultBrowser = getDefaultOutOfRegistry("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\https\\UserChoice");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\URLAssociations\\http\\UserChoice");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\microsoft-edge\\shell\\open\\command");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
defaultBrowser = getDefaultOutOfRegistry("HKEY_CLASSES_ROOT\\http\\shell\\open\\command");
|
||||
if (defaultBrowser != "")
|
||||
return defaultBrowser;
|
||||
}
|
||||
return "";
|
||||
if (getOperatingSystem() == "Windows") {
|
||||
return getDefaultWindowsBrowser();
|
||||
}
|
||||
return getAnyUnixBrowser();
|
||||
}
|
||||
|
||||
/**
|
||||
* obtains information out of the Windows registry.
|
||||
*
|
||||
* @param hkeyquery registry entry to ask for.
|
||||
* @return
|
||||
*/
|
||||
public static String getDefaultOutOfRegistry(String hkeyquery){
|
||||
if (getOperatingSystem() == "Windows"){
|
||||
try {
|
||||
// Get registry where we find the default browser
|
||||
Process process = Runtime.getRuntime().exec("REG QUERY " + hkeyquery);
|
||||
Scanner kb = new Scanner(process.getInputStream());
|
||||
while (kb.hasNextLine()) {
|
||||
String line = kb.nextLine();
|
||||
if (line.contains("(Default")){
|
||||
String[] splitLine = line.split(" ");
|
||||
kb.close();
|
||||
return splitLine[splitLine.length-1].replace("%1", "").replaceAll("\\s+$", "").replaceAll("\"", "");
|
||||
}
|
||||
}
|
||||
// Match wasn't found, still need to close Scanner
|
||||
kb.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//
|
||||
public ProcessBuilder baseProcessBuilder(String[] args) {
|
||||
String browser = findUnsafeBrowserAnywhere();
|
||||
if (!browser.isEmpty()) {
|
||||
String[] newArgs = new String[args.length + 1];
|
||||
newArgs[0] = browser;
|
||||
if (args != null && args.length > 0) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
newArgs[i + 1] = args[i];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
ProcessBuilder pb =
|
||||
new ProcessBuilder(newArgs).directory(runtimeDirectory(true));
|
||||
pb.environment().put("http_proxy", "http://127.0.0.1:4444");
|
||||
pb.environment().put("https_proxy", "http://127.0.0.1:4444");
|
||||
pb.environment().put("ftp_proxy", "http://127.0.0.1:0");
|
||||
pb.environment().put("all_proxy", "http://127.0.0.1:4444");
|
||||
pb.environment().put("no_proxy", "http://127.0.0.1:7657");
|
||||
pb.environment().put("HTTP_PROXY", "http://127.0.0.1:4444");
|
||||
pb.environment().put("HTTPS_PROXY", "http://127.0.0.1:4444");
|
||||
pb.environment().put("FTP_PROXY", "http://127.0.0.1:0");
|
||||
pb.environment().put("ALL_PROXY", "http://127.0.0.1:4444");
|
||||
pb.environment().put("NO_PROXY", "http://127.0.0.1:7657");
|
||||
return pb;
|
||||
} else {
|
||||
System.out.println("No Browser found.");
|
||||
return new ProcessBuilder(args);
|
||||
}
|
||||
}
|
||||
|
||||
private static String scanAPath(String dir) {
|
||||
for (String browser : browsers) {
|
||||
File test = new File(dir, browser);
|
||||
if (test.exists()){
|
||||
return test.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
/**
|
||||
* delete the runtime directory
|
||||
*
|
||||
* @return true if successful, false if not
|
||||
*/
|
||||
public static boolean deleteRuntimeDirectory() {
|
||||
File rtd = runtimeDirectory(true);
|
||||
if (rtd.exists()) {
|
||||
rtd.delete();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any browser in our list within a UNIX path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getAnyUnixBrowser() {
|
||||
// read the PATH environment variable and split it by ":"
|
||||
String[] path = System.getenv("PATH").split(":");
|
||||
if (path != null && path.length > 0){
|
||||
for (String p : path){
|
||||
String f = scanAPath(p);
|
||||
if (f != "") {
|
||||
return f;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any usable browser and output the whole path
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String findUnsafeBrowserAnywhere() {
|
||||
if (BROWSER != ""){
|
||||
File f = new File(BROWSER);
|
||||
if (f.exists())
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
|
||||
if (getOperatingSystem() == "Windows"){
|
||||
return getDefaultWindowsBrowser();
|
||||
}
|
||||
return getAnyUnixBrowser();
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_BROWSER_DIR environment variable
|
||||
String rtd = System.getenv("I2P_BROWSER_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return rtd;
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
}
|
||||
|
||||
//
|
||||
public ProcessBuilder baseProcessBuilder(String[] args) {
|
||||
String browser = findUnsafeBrowserAnywhere();
|
||||
if (!browser.isEmpty()) {
|
||||
String[] newArgs = new String[args.length+1];
|
||||
newArgs[0] = browser;
|
||||
if (args != null && args.length > 0) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
newArgs[i+1] = args[i];
|
||||
}
|
||||
}
|
||||
ProcessBuilder pb = new ProcessBuilder(newArgs).directory(runtimeDirectory(true));
|
||||
pb.environment().put("http_proxy","http://127.0.0.1:4444");
|
||||
pb.environment().put("https_proxy","http://127.0.0.1:4444");
|
||||
pb.environment().put("ftp_proxy","http://127.0.0.1:0");
|
||||
pb.environment().put("all_proxy","http://127.0.0.1:4444");
|
||||
pb.environment().put("no_proxy","http://127.0.0.1:7657");
|
||||
pb.environment().put("HTTP_PROXY","http://127.0.0.1:4444");
|
||||
pb.environment().put("HTTPS_PROXY","http://127.0.0.1:4444");
|
||||
pb.environment().put("FTP_PROXY","http://127.0.0.1:0");
|
||||
pb.environment().put("ALL_PROXY","http://127.0.0.1:4444");
|
||||
pb.environment().put("NO_PROXY","http://127.0.0.1:7657");
|
||||
return pb;
|
||||
} else {
|
||||
System.out.println("No Browser found.");
|
||||
return new ProcessBuilder(args);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Waits for an HTTP proxy on port 4444 to be ready.
|
||||
* Returns false on timeout of 200 seconds.
|
||||
*
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy() { return waitForProxy(DEFAULT_TIMEOUT); }
|
||||
|
||||
/**
|
||||
* delete the runtime directory
|
||||
*
|
||||
* @return true if successful, false if not
|
||||
*/
|
||||
public static boolean deleteRuntimeDirectory(){
|
||||
File rtd = runtimeDirectory(true);
|
||||
if (rtd.exists()) {
|
||||
rtd.delete();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Waits for an HTTP proxy on port 4444 to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout) {
|
||||
return waitForProxy(timeout, 4444);
|
||||
}
|
||||
/**
|
||||
* Waits for an HTTP proxy on the specified port to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @param port the port to wait for the proxy to be ready on.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout, int port) {
|
||||
return waitForProxy(timeout, port, "localhost");
|
||||
}
|
||||
|
||||
/**
|
||||
* get the runtime directory, creating it if create=true
|
||||
*
|
||||
* @param create if true, create the runtime directory if it does not exist
|
||||
* @return the runtime directory, or null if it could not be created
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public static File runtimeDirectory(boolean create) {
|
||||
String rtd = runtimeDirectory();
|
||||
return runtimeDirectory(create, rtd);
|
||||
/**
|
||||
* Waits for an HTTP proxy on the specified port to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @param port the port to wait for the proxy to be ready on.
|
||||
* @param host the host to wait for the proxy to be ready on.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout, int port, String host) {
|
||||
for (int i = 0; i < timeout; i++) {
|
||||
if (checkifPortIsOccupied(port, host)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the correct runtime directory
|
||||
*
|
||||
* @return the runtime directory, or null if it could not be created or found
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public static String runtimeDirectory() {
|
||||
// get the I2P_BROWSER_DIR environment variable
|
||||
String rtd = System.getenv("I2P_BROWSER_DIR");
|
||||
// if it is not null and not empty
|
||||
if (rtd != null && !rtd.isEmpty()) {
|
||||
// check if the file exists
|
||||
File rtdFile = new File(rtd);
|
||||
if (rtdFile.exists()) {
|
||||
// if it does, return it
|
||||
return rtd;
|
||||
}
|
||||
}
|
||||
return runtimeDirectory("");
|
||||
public Process launchAndDetatch(boolean privateWindow, String[] url) {
|
||||
if (waitForProxy()) {
|
||||
ProcessBuilder pb;
|
||||
if (privateWindow) {
|
||||
pb = baseProcessBuilder(url);
|
||||
} else {
|
||||
pb = baseProcessBuilder(url);
|
||||
}
|
||||
try {
|
||||
System.out.println(pb.command());
|
||||
p = pb.start();
|
||||
System.out.println("I2PBrowser");
|
||||
sleep(2000);
|
||||
return p;
|
||||
} catch (Throwable e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an HTTP proxy on port 4444 to be ready.
|
||||
* Returns false on timeout of 200 seconds.
|
||||
*
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy() {
|
||||
return waitForProxy(DEFAULT_TIMEOUT);
|
||||
public void launch(boolean privateWindow, String[] url) {
|
||||
if (waitForProxy()) {
|
||||
p = launchAndDetatch(privateWindow, url);
|
||||
try {
|
||||
System.out.println("Waiting for I2PBrowser to close...");
|
||||
int exit = p.waitFor();
|
||||
if (privateWindow) {
|
||||
if (deleteRuntimeDirectory())
|
||||
System.out.println(
|
||||
"Private browsing enforced, deleting runtime directory");
|
||||
}
|
||||
System.out.println("I2PBrowser exited with value: " + exit);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an HTTP proxy on port 4444 to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout) {
|
||||
return waitForProxy(timeout, 4444);
|
||||
private static void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException bad) {
|
||||
bad.printStackTrace();
|
||||
throw new RuntimeException(bad);
|
||||
}
|
||||
/**
|
||||
* Waits for an HTTP proxy on the specified port to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @param port the port to wait for the proxy to be ready on.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout, int port) {
|
||||
return waitForProxy(timeout, port, "localhost");
|
||||
}
|
||||
private static String ValidURL(String inUrl) {
|
||||
String[] schemes = {"http", "https"};
|
||||
for (String scheme : schemes) {
|
||||
if (inUrl.startsWith(scheme)) {
|
||||
System.out.println("Valid URL: " + inUrl);
|
||||
return inUrl;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
private boolean checkifPortIsOccupied(int port, String host) {
|
||||
try {
|
||||
Socket socket = new Socket(host, port);
|
||||
socket.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an HTTP proxy on the specified port to be ready.
|
||||
* Returns false on timeout of the specified number of seconds.
|
||||
*
|
||||
* @param timeout the number of seconds to wait for the proxy to be ready.
|
||||
* @param port the port to wait for the proxy to be ready on.
|
||||
* @param host the host to wait for the proxy to be ready on.
|
||||
* @return true if the proxy is ready, false if it is not.
|
||||
* @since 0.0.18
|
||||
*/
|
||||
public boolean waitForProxy(int timeout, int port, String host) {
|
||||
for (int i = 0; i < timeout; i++) {
|
||||
if (checkifPortIsOccupied(port, host)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//
|
||||
public static void main(String[] args) {
|
||||
boolean privateBrowsing = false;
|
||||
System.out.println("checking for private browsing");
|
||||
ArrayList<String> visitURL = new ArrayList<String>();
|
||||
if (args != null && args.length > 0) {
|
||||
for (String arg : args) {
|
||||
if (arg.equals("-private")) {
|
||||
privateBrowsing = true;
|
||||
System.out.println(
|
||||
"private browsing is true, profile will be discarded at end of session");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public Process launchAndDetatch(boolean privateWindow, String[] url){
|
||||
if (waitForProxy()){
|
||||
ProcessBuilder pb;
|
||||
if (privateWindow) {
|
||||
pb = baseProcessBuilder(url);
|
||||
} else {
|
||||
pb = baseProcessBuilder(url);
|
||||
}
|
||||
try{
|
||||
System.out.println(pb.command());
|
||||
p = pb.start();
|
||||
System.out.println("I2PBrowser");
|
||||
sleep(2000);
|
||||
return p;
|
||||
}catch(Throwable e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
if (!arg.startsWith("-")) {
|
||||
// check if it's a URL
|
||||
visitURL.add(ValidURL(arg));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void launch(boolean privateWindow, String[] url){
|
||||
if (waitForProxy()){
|
||||
p = launchAndDetatch(privateWindow, url);
|
||||
try{
|
||||
System.out.println("Waiting for I2PBrowser to close...");
|
||||
int exit = p.waitFor();
|
||||
if (privateWindow){
|
||||
if (deleteRuntimeDirectory())
|
||||
System.out.println("Private browsing enforced, deleting runtime directory");
|
||||
}
|
||||
System.out.println("I2PBrowser exited with value: "+exit);
|
||||
}catch(Exception e){
|
||||
System.out.println("Error: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException bad) {
|
||||
bad.printStackTrace();
|
||||
throw new RuntimeException(bad);
|
||||
}
|
||||
}
|
||||
private static String ValidURL(String inUrl){
|
||||
String[] schemes = {"http", "https"};
|
||||
for (String scheme: schemes) {
|
||||
if (inUrl.startsWith(scheme)) {
|
||||
System.out.println("Valid URL: " + inUrl);
|
||||
return inUrl;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
private boolean checkifPortIsOccupied(int port, String host) {
|
||||
try {
|
||||
Socket socket = new Socket(host, port);
|
||||
socket.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
public static void main(String[] args) {
|
||||
boolean privateBrowsing = false;
|
||||
System.out.println("checking for private browsing");
|
||||
ArrayList<String> visitURL = new ArrayList<String>();
|
||||
if (args != null && args.length > 0) {
|
||||
for (String arg : args) {
|
||||
if (arg.equals("-private")) {
|
||||
privateBrowsing = true;
|
||||
System.out.println("private browsing is true, profile will be discarded at end of session");
|
||||
}
|
||||
if (!arg.startsWith("-")){
|
||||
// check if it's a URL
|
||||
visitURL.add(ValidURL(arg));
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("I2PGenericUnsafeBrowser");
|
||||
I2PGenericUnsafeBrowser i2pBrowser = new I2PGenericUnsafeBrowser();
|
||||
i2pBrowser.launch(privateBrowsing, visitURL.toArray(new String[visitURL.size()]));
|
||||
}
|
||||
}
|
||||
System.out.println("I2PGenericUnsafeBrowser");
|
||||
I2PGenericUnsafeBrowser i2pBrowser = new I2PGenericUnsafeBrowser();
|
||||
i2pBrowser.launch(privateBrowsing,
|
||||
visitURL.toArray(new String[visitURL.size()]));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user