Add polkit authorization and additional terminals
Gitea/Java-Installer/pipeline/head There was a failure building this commit
Details
Gitea/Java-Installer/pipeline/head There was a failure building this commit
Details
parent
3e289e756a
commit
0090eec1c9
9
.project
9
.project
|
@ -21,15 +21,6 @@
|
|||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>0</id>
|
||||
<name></name>
|
||||
<type>26</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.ui.ide.multiFilter</id>
|
||||
<arguments>1.0-name-matches-false-false-release</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
<filter>
|
||||
<id>0</id>
|
||||
<name></name>
|
||||
|
|
|
@ -84,6 +84,7 @@ public class Installer {
|
|||
}
|
||||
}
|
||||
|
||||
in.close();
|
||||
error = 2; return;
|
||||
|
||||
} catch (Exception ex) { logger.log("e", ex, "installProgramm");
|
||||
|
|
|
@ -48,9 +48,9 @@ public class RunInConsole {
|
|||
|
||||
String executableName = getExecutableName();
|
||||
|
||||
// probably executed inside IDE
|
||||
// Probably executed inside an IDE
|
||||
if (executableName == null) return;
|
||||
// is already executed in console
|
||||
// Application is already executed within a console
|
||||
if (System.console() != null && !forceRestart) return;
|
||||
|
||||
startExecutableInConsole(executableName, keepOpen, asAdmin, args);
|
||||
|
@ -60,11 +60,12 @@ public class RunInConsole {
|
|||
|
||||
|
||||
/**
|
||||
* Opens the console windows and starts the jar file
|
||||
* Opens a console window and starts the provided jar file.
|
||||
* The executable name is NOT escaped because it shouldn't be critical. Make sure to provide a valid name.
|
||||
*
|
||||
* @param executableName the name of the jar file (without the path -> relativ)
|
||||
* @param executableName the name of the jar file (without the path -> relative)
|
||||
* @param stayOpenAfterEnd keep the console windows opened after the run of the jar file
|
||||
* @param asAdmin start the console with administrator privileges (on Windows Powershell is required)
|
||||
* @param asAdmin start the console with administrator privileges (for Windows a Poowershell is required)
|
||||
*/
|
||||
private static void startExecutableInConsole(String executableName, final boolean keepOpen, final boolean asAdmin, String[] args) {
|
||||
|
||||
|
@ -83,7 +84,8 @@ public class RunInConsole {
|
|||
if (keepOpen) command = "cmd /c start cmd /k java -jar \"" + executableName + "\" " + strArgs;
|
||||
else command = "cmd /c start java -jar \"" + executableName +"\" " + strArgs;
|
||||
} else {
|
||||
// da die Administratorkonsole im C:/Windows/System32 pfad geöffnet wird, muss der absolute Pfad der Jar-Datei ermittelt werden
|
||||
// Because the administrative terminal is opened in 'C:/Windows/System32',
|
||||
// we need to query the absolute path of the JAR file before starting it
|
||||
executableName = new File(executableName).getAbsolutePath();
|
||||
|
||||
if (keepOpen) command = "powershell \"Start-Process cmd -Verb RunAs -ArgumentList '/C', 'start cmd /k java -jar \" " + executableName + "\" " + strArgs + "'";
|
||||
|
@ -96,31 +98,51 @@ public class RunInConsole {
|
|||
String terminal = null;
|
||||
String terminalCommand = null;
|
||||
|
||||
// es muss zunächst ein installiertes Terminal "gefunden" werden, das geöffnet werden kann
|
||||
// Find a installed terminal that we can use to opened up a new terminal
|
||||
try {
|
||||
String[][] terminals = { { "gnome-terminal", "--"}, {"xterm", "-e"}, {"xfce4-terminal", "-e"}, {"tilix", "-e"}, {"konsole", "-e"}, {"terminal", "-e"}};
|
||||
String[][] terminals = {
|
||||
{ "gnome-terminal", "--"}, {"xterm", "-e"}, {"xfce4-terminal", "-e"}, {"tilix", "-e"}, {"konsole", "-e"}, {"terminal", "-e"},
|
||||
{ "wezterm", "start -e" }, { "alacritty", "-e" }
|
||||
};
|
||||
|
||||
// Find the first available terminal
|
||||
for (String currentTerminal[]: terminals) {
|
||||
Process p = new ProcessBuilder("bash", "-c", "which " + currentTerminal[0]).start();
|
||||
p.waitFor(5000, TimeUnit.SECONDS);
|
||||
String output = "";
|
||||
BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
output = buf.readLine();
|
||||
|
||||
if (output != null && !output.equals("")) { terminal = currentTerminal[0]; terminalCommand = currentTerminal[1]; break; }
|
||||
if (isCommandAvailable(currentTerminal[0])) {
|
||||
terminal = currentTerminal[0]; terminalCommand = currentTerminal[1]; break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (terminal == null) break;
|
||||
|
||||
if (!asAdmin) {
|
||||
if (keepOpen) new ProcessBuilder("bash", "-c", terminal + " " + terminalCommand + " /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "; exec bash'").start();
|
||||
else new ProcessBuilder("bash", "-c", terminal + " " + terminalCommand + " /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "'").start();
|
||||
if (keepOpen) new ProcessBuilder("sh", "-c", terminal + " " + terminalCommand + " /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "; exec sh'").start();
|
||||
else new ProcessBuilder("sh", "-c", terminal + " " + terminalCommand + " /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "'").start();
|
||||
} else {
|
||||
if (keepOpen) new ProcessBuilder("bash", "-c", terminal + " " + terminalCommand + " sudo /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "; exec bash'").start();
|
||||
else new ProcessBuilder("bash", "-c", terminal + " " + terminalCommand + " sudo /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "'").start();
|
||||
|
||||
if (isCommandAvailable("pkexec")) {
|
||||
// A polkit daemon is available on the system. We use it to authenticate the installer as root
|
||||
Process proc = new ProcessBuilder(
|
||||
"pkexec", "--user", "root",
|
||||
// By default, the command started by pkexec will run in a minimal and safe environment. This does NOT include the $DISPLAY variable by default.
|
||||
// Because the most terminal needs this (and the XAUTH), we use the env command
|
||||
"env", "DISPLAY=" + System.getenv("DISPLAY"), "XAUTH=" + System.getenv("XAUTH"), "HOME=" + System.getenv("HOME"),
|
||||
"/bin/sh", "-c",
|
||||
terminal + " " + terminalCommand + " /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs
|
||||
+ (keepOpen ? "; exec sh'" : ";")
|
||||
).inheritIO().start();
|
||||
|
||||
// The polkit process runs in foreground. So we need to wait until the installation process finished
|
||||
synchronized(proc) {
|
||||
proc.wait();
|
||||
}
|
||||
} else {
|
||||
if (keepOpen) new ProcessBuilder("sh", "-c", terminal + " " + terminalCommand + " sudo /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "; exec sh'").start();
|
||||
else new ProcessBuilder("sh", "-c", terminal + " " + terminalCommand + " sudo /bin/sh -c 'java -jar \"" + executableName + "\" " + strArgs + "'").start();
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
} catch (Exception ex) { }
|
||||
} catch (Exception ex) { ex.printStackTrace(); }
|
||||
break;
|
||||
case MACOS: break;
|
||||
}
|
||||
|
@ -131,6 +153,26 @@ public class RunInConsole {
|
|||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries if the provided command or application is available on this system.
|
||||
*
|
||||
* @param command Command to check
|
||||
*
|
||||
* @return Weather the command is available or not
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private static boolean isCommandAvailable(String command) throws IOException, InterruptedException {
|
||||
Process p = new ProcessBuilder("sh", "-c", "which " + command).start();
|
||||
p.waitFor(2000, TimeUnit.SECONDS);
|
||||
String output = "";
|
||||
BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
output = buf.readLine();
|
||||
|
||||
return output != null && !output.isBlank();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue