2022-08-21 21:17:28 -04:00
|
|
|
package net.i2p.i2pfirefox;
|
|
|
|
|
2022-08-27 13:27:28 -04:00
|
|
|
import java.util.ArrayList;
|
2022-08-21 21:17:28 -04:00
|
|
|
|
2022-08-27 13:27:28 -04:00
|
|
|
/**
|
2022-08-21 23:49:08 -04:00
|
|
|
* I2PBrowser.java
|
|
|
|
* Copyright (C) 2022 idk <hankhill19580@gmail.com>
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT License. See LICENSE.md for details.
|
2022-08-30 15:49:44 -04:00
|
|
|
*
|
2022-08-21 23:49:08 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
2022-08-30 15:49:44 -04:00
|
|
|
* @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.
|
|
|
|
*
|
2022-08-21 23:49:08 -04:00
|
|
|
* @author idk
|
|
|
|
* @since 0.0.16
|
|
|
|
*/
|
2022-08-21 21:17:28 -04:00
|
|
|
public class I2PBrowser {
|
2022-08-30 15:49:44 -04:00
|
|
|
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() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2022-08-27 23:19:16 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
if (chrome.isEmpty()) {
|
|
|
|
return false;
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-21 21:17:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
2022-08-27 23:48:07 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
if (fox.isEmpty()) {
|
|
|
|
return false;
|
2022-08-27 23:48:59 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-27 23:48:07 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches 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;
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
if (chromiumFirst) {
|
|
|
|
if (chromium) {
|
|
|
|
this.launchChromium(privateWindow, url);
|
|
|
|
} else if (firefox) {
|
|
|
|
this.launchFirefox(privateWindow, url);
|
|
|
|
} else {
|
|
|
|
this.launchGeneric(privateWindow, url);
|
|
|
|
}
|
|
|
|
return;
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
if (firefox) {
|
|
|
|
this.launchFirefox(privateWindow, url);
|
|
|
|
} else if (chromium) {
|
|
|
|
this.launchChromium(privateWindow, url);
|
|
|
|
} else {
|
|
|
|
this.launchGeneric(privateWindow, url);
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2022-08-21 21:17:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches 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); }
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
/**
|
|
|
|
* Populates a profile directory with a proxy configuration.
|
|
|
|
* Waits for an HTTP proxy on the port 4444 to be ready.
|
|
|
|
* Launches either Firefox or Chromium with the profile directory.
|
|
|
|
*
|
|
|
|
* @since 0.0.16
|
|
|
|
*/
|
|
|
|
public void launch() { launch(false); }
|
2022-08-27 13:27:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
private static String ValidURL(String inUrl) {
|
|
|
|
String[] schemes = {"http", "https"};
|
|
|
|
for (String scheme : schemes) {
|
|
|
|
if (inUrl.startsWith(scheme)) {
|
|
|
|
return inUrl;
|
|
|
|
}
|
2022-08-27 13:27:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
return "";
|
|
|
|
}
|
2022-08-21 21:17:28 -04:00
|
|
|
|
2022-08-30 15:49:44 -04:00
|
|
|
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;
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
if (arg.equals("-firefox")) {
|
|
|
|
i2pBrowser.firefox = true;
|
|
|
|
}
|
|
|
|
if (!arg.startsWith("-")) {
|
|
|
|
visitURL.add(ValidURL(arg));
|
|
|
|
}
|
|
|
|
}
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|
2022-08-30 15:49:44 -04:00
|
|
|
i2pBrowser.launch(privateBrowsing,
|
|
|
|
visitURL.toArray(new String[visitURL.size()]));
|
|
|
|
}
|
2022-08-21 21:17:28 -04:00
|
|
|
}
|