Files
i2p.firefox/java/net/i2p/router/WinLauncher.java

234 lines
7.8 KiB
Java
Raw Normal View History

2021-04-03 16:39:26 -04:00
package net.i2p.router;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
2021-04-03 16:39:26 -04:00
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
2021-07-01 19:52:21 -04:00
import net.i2p.app.ClientAppManager;
import net.i2p.crypto.*;
//import net.i2p.i2pfirefox.*;
import net.i2p.router.Router;
import net.i2p.router.RouterLaunch;
import net.i2p.update.*;
2021-07-01 19:52:21 -04:00
import net.i2p.update.UpdateManager;
import net.i2p.update.UpdatePostProcessor;
import net.i2p.update.UpdateType.*;
2021-07-01 19:52:21 -04:00
2021-04-03 16:39:26 -04:00
/**
* Launches a router from %WORKINGDIR%/I2P using configuration data in
* %WORKINGDIR%/I2P.. Uses Java 9 APIs.
*
2021-04-03 16:39:26 -04:00
* Sets the following properties:
* i2p.dir.base - this points to the (read-only) resources inside the bundle
* i2p.dir.config this points to the (read-write) config directory in local
* appdata
2021-04-03 16:39:26 -04:00
* router.pid - the pid of the java process.
*/
public class WinLauncher extends CopyConfigDir {
WindowsUpdatePostProcessor wupp = null;
private Router i2pRouter;
2023-02-12 16:48:49 +00:00
public static void main(String[] args) {
var launcher = new WinLauncher();
launcher.setupLauncher();
2023-02-12 16:50:59 +00:00
launcher.initLogger();
int proxyTimeoutTime = 200;
2022-09-07 02:28:23 -04:00
ArrayList<String> newArgsList = new ArrayList<String>();
if (args != null) {
if (args.length > 0) {
2022-09-07 02:28:23 -04:00
for (String arg : args) {
if (arg.equals("-private")) {
launcher.logger.info(
2022-09-07 02:28:23 -04:00
"Private browsing is true, profile will be discarded at end of session.");
} else if (arg.equals("-chromium")) {
launcher.logger.info("Chromium will be selected before Firefox.");
2022-09-07 02:28:23 -04:00
} else if (arg.equals("-usability")) {
launcher.logger.info(
2022-09-07 02:28:23 -04:00
"Usability mode is true, using alternate extensions loadout.");
} else if (arg.equals("-noproxycheck")) {
proxyTimeoutTime = 0;
launcher.logger.info("Proxy timeout time set to zero");
2022-09-07 02:28:23 -04:00
} else {
// make an effort to not let people launch into sites if the proxy
// isn't quite ready yet, but also disable the proxy timeout if
// they're reaching a router console
if (arg.startsWith("http://localhost:76")) {
newArgsList.add(arg);
proxyTimeoutTime = 0;
} else if (arg.startsWith("http://127.0.0.1:76")) {
newArgsList.add(arg);
proxyTimeoutTime = 0;
} else if (arg.startsWith("https://localhost:76")) {
newArgsList.add(arg);
proxyTimeoutTime = 0;
} else if (arg.startsWith("https://127.0.0.1:76")) {
newArgsList.add(arg);
proxyTimeoutTime = 0;
} else if (proxyTimeoutTime > 0) {
newArgsList.add(arg);
2023-02-12 16:48:49 +00:00
} else if (!launcher.isAvailable(4444)) {
newArgsList.add(arg);
}
2022-09-07 02:28:23 -04:00
}
}
}
}
2022-09-11 00:03:30 -04:00
2023-02-12 16:48:49 +00:00
File programs = launcher.programFile();
File home = launcher.homeDir();
2022-09-10 23:51:13 -04:00
System.setProperty(
"i2p.dir.base",
new File(programs.getAbsolutePath(), "config").getAbsolutePath());
System.setProperty("i2p.dir.config", home.getAbsolutePath());
System.setProperty("router.pid",
String.valueOf(ProcessHandle.current().pid()));
/**
* IMPORTANT: You must set user.dir to the same directory where the
* jpackage is intstalled, or when the launcher tries to re-run itself
* to start the browser, it will start in the wrong directory and fail
* to find the JVM and Runtime bundle. This broke Windows 11 installs.
*/
System.setProperty("user.dir", programs.getAbsolutePath());
launcher.logger.info("\t" + System.getProperty("user.dir"));
launcher.logger.info("\t" + System.getProperty("i2p.dir.base"));
launcher.logger.info("\t" + System.getProperty("i2p.dir.config"));
launcher.logger.info("\t" + System.getProperty("router.pid"));
2023-02-12 19:18:25 +00:00
boolean continuerunning = launcher.promptServiceStartIfAvailable("i2p");
if (!continuerunning) {
launcher.logger.severe(
"Service startup failure, please start I2P service with services.msc");
2022-09-19 19:31:21 -04:00
System.exit(2);
}
2023-02-12 19:18:25 +00:00
continuerunning = launcher.promptUserInstallStartIfAvailable();
2022-09-19 20:03:57 -04:00
if (!continuerunning) {
launcher.logger.severe("User-install startup required.");
2022-09-19 20:03:57 -04:00
System.exit(2);
}
2022-09-19 19:31:21 -04:00
2022-09-12 20:29:09 -04:00
// This actually does most of what we use NSIS for if NSIS hasn't
// already done it, which essentially makes this whole thing portable.
2023-02-12 19:18:25 +00:00
if (!launcher.copyConfigDir()) {
launcher.logger.severe("Cannot copy the configuration directory");
System.exit(1);
}
2022-09-11 02:18:32 -04:00
2023-02-12 19:18:25 +00:00
launcher.i2pRouter =
new Router(launcher.routerConfig(), System.getProperties());
if (!launcher.isInstalled("i2p")) {
2023-02-12 16:48:49 +00:00
if (launcher.i2pRouter.saveConfig("routerconsole.browser", null)) {
launcher.logger.info("removed routerconsole.browser config");
}
2023-02-12 16:48:49 +00:00
if (launcher.i2pRouter.saveConfig("routerconsole.browser",
2023-02-12 19:18:25 +00:00
launcher.appImageExe() +
" -noproxycheck")) {
launcher.logger.info("updated routerconsole.browser config " +
2023-02-12 19:27:24 +00:00
launcher.appImageExe());
}
}
launcher.logger.info("Router is configured");
2022-09-10 23:51:13 -04:00
2023-02-12 16:48:49 +00:00
Thread registrationThread = new Thread(launcher.REGISTER_UPP);
2022-09-10 23:51:13 -04:00
registrationThread.setName("UPP Registration");
registrationThread.setDaemon(true);
registrationThread.start();
2023-02-12 16:48:49 +00:00
launcher.setNotStarting();
2023-02-12 16:48:49 +00:00
launcher.i2pRouter.runRouter();
2022-09-10 23:51:13 -04:00
}
private void setupLauncher() {
2022-09-10 23:51:13 -04:00
File jrehome = javaHome();
logger.info("jre home is: " + jrehome.getAbsolutePath());
File appimagehome = appImageHome();
logger.info("appimage home is: " + appimagehome.getAbsolutePath());
}
2021-04-03 16:39:26 -04:00
private File programFile() {
File programs = selectProgramFile();
if (!programs.exists())
programs.mkdirs();
else if (!programs.isDirectory()) {
logger.warning(
programs +
" exists but is not a directory. Please get it out of the way");
System.exit(1);
}
2022-09-10 23:51:13 -04:00
return programs;
}
private File homeDir() {
File home = selectHome();
if (!home.exists())
home.mkdirs();
else if (!home.isDirectory()) {
logger.warning(
home +
" exists but is not a directory. Please get it out of the way");
System.exit(1);
}
2022-09-10 23:51:13 -04:00
return home;
}
// see
// https://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
private boolean isAvailable(int portNr) {
boolean portFree;
try (var ignored = new ServerSocket(portNr)) {
portFree = true;
} catch (IOException e) {
portFree = false;
}
return portFree;
}
private void setNotStarting() {
2022-09-10 23:51:13 -04:00
logger.info("removing startup file, the application has started");
2022-09-05 01:47:16 -04:00
File home = selectHome();
2022-09-10 23:51:13 -04:00
File starting = new File(home, "starting");
if (starting.exists()) {
starting.delete();
2022-09-05 01:47:16 -04:00
}
}
2022-09-11 00:03:30 -04:00
private final Runnable REGISTER_UPP = () -> {
RouterContext ctx;
while ((ctx = i2pRouter.getContext()) == null) {
sleep(1000);
}
// then wait for the update manager
ClientAppManager cam;
while ((cam = ctx.clientAppManager()) == null) {
sleep(1000);
}
UpdateManager um;
while ((um = (UpdateManager)cam.getRegisteredApp(UpdateManager.APP_NAME)) ==
null) {
sleep(1000);
}
WindowsUpdatePostProcessor wupp = new WindowsUpdatePostProcessor(ctx);
um.register(wupp, UpdateType.ROUTER_SIGNED_SU3, SU3File.TYPE_EXE);
um.register(wupp, UpdateType.ROUTER_DEV_SU3, SU3File.TYPE_EXE);
};
/**
* sleep for 1 second
*
* @param millis
*/
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException bad) {
bad.printStackTrace();
throw new RuntimeException(bad);
}
}
2021-04-03 16:39:26 -04:00
}