Skip to content

Commit

Permalink
added better auth way
Browse files Browse the repository at this point in the history
  • Loading branch information
ezTxmMC committed Dec 30, 2024
1 parent 54a37c5 commit 1fd8196
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,37 @@ public class SmoothCloudLauncher {
private static JarLoader jarLoader;

public static void main(String[] args) throws IOException {
String username = null;
String password = null;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--user" -> {
if (i + 1 < args.length) {
username = args[i + 1];
i++;
} else {
System.out.println("Fehler: Kein Benutzername angegeben.");
return;
}
}
case "--pwd" -> {
if (i + 1 < args.length) {
password = args[i + 1];
i++;
} else {
System.out.println("Fehler: Kein Passwort angegeben.");
return;
}
}
}
}
classLoader = new SmoothCloudClassLoader();
dependencyHandler = new DependencyHandler();
jsonLoader = new JsonLoader("https://github.com/SmoothCloudEU/smoothcloud-manifest/raw/refs/heads/master/dependencyLoader.json");
jsonLoader.processJsonAndDownload();
jsonLoader.processJsonAndDownload(username, password);
System.out.println("Starting smoothcloud-node...");
jarLoader = new JarLoader();
System.out.println("SMOOTHCLOUD-NODE: " + dependencyHandler.getDependencyPaths().get("smoothcloud-node"));
jarLoader.loadJar(dependencyHandler.getDependencyPaths().get("smoothcloud-node"), args);
jarLoader.loadJar(dependencyHandler.getDependencyPaths().get("smoothcloud-node"), new String[]{});
}

public static SmoothCloudClassLoader getClassLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public String buildUrl(String groupId, String artifactId, String version, String
if (matcher.find()) {
repoUrl = matcher.group(1);
}

// URL erstellen
return String.format(repoUrl, groupId.replace('.', '/'), artifactId, version, artifactId, version);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Scanner;

public class Downloader {

public static void downloadJar(String fileUrl, String saveDir, String groupId, String artifactId, String version) throws IOException {
public static void downloadJar(String username, String password, String fileUrl, String saveDir, String groupId, String artifactId, String version) throws IOException {
URL url = new URL(fileUrl);
String checkDir = "dependencies/" + groupId.replace(".", "/") + "/" + artifactId;
File checkDirFile = new File(checkDir);
Expand All @@ -26,14 +25,26 @@ public static void downloadJar(String fileUrl, String saveDir, String groupId, S
skip = true;
break;
}
return;
}
}
if (skip) {
System.out.println("Skipped " + artifactId + ".");
return;
}
System.out.println("Downloading " + artifactId + "...");
boolean update = false;
for (File file : checkDirFile.listFiles()) {
if (file.isDirectory()) {
if (file.delete()) {
update = true;
System.out.println("Updating... " + artifactId + ".");
}
}
}
if (update) {
System.out.println("Updating " + artifactId + "...");
} else {
System.out.println("Downloading " + artifactId + "...");
}
}
}
}
Expand All @@ -43,13 +54,6 @@ public static void downloadJar(String fileUrl, String saveDir, String groupId, S
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
System.out.println("Authentifizierung erforderlich.");
Scanner scanner = new Scanner(System.in);
System.out.print("Benutzername: ");
String username = scanner.nextLine();
System.out.print("Passwort: ");
String password = scanner.nextLine();
scanner.close();
String auth = username + ":" + password;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
connection.disconnect();
Expand All @@ -61,7 +65,8 @@ public static void downloadJar(String fileUrl, String saveDir, String groupId, S
responseCode = connection.getResponseCode();
}
if (responseCode != HttpURLConnection.HTTP_OK) {
System.out.println("Kein erfolgreicher HTTP-Code: " + responseCode);
System.out.println("Argument-Authentifizierung --user --pwd erforderlich.");
System.exit(1);
return;
}
File saveDirectory = new File(saveDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JarLoader {
public void loadJar(Path jarPath, String[] args) {
try (SmoothCloudClassLoader classLoader = SmoothCloudLauncher.getClassLoader()) {
classLoader.addURL(jarPath.toUri().toURL());
System.setProperty("startup", String.valueOf(System.currentTimeMillis()));
System.setProperty("smoothcloud-startup", String.valueOf(System.currentTimeMillis()));
Thread.currentThread().setContextClassLoader(classLoader);
String mainClass = mainClass(jarPath);
Class<?> clazz = Class.forName(mainClass, true, classLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public JsonLoader(String urlString) throws IOException {
this.json = response.toString();
}

public void processJsonAndDownload() {
public void processJsonAndDownload(String username, String password) {
DependencyHandler dependencyHandler = SmoothCloudLauncher.getDependencyHandler();
Pattern pattern = Pattern.compile("\"groupId\": \"([^\"]+)\",\\s*\"artifactId\": \"([^\"]+)\",\\s*\"version\": \"([^\"]+)\",\\s*\"repository\": \"([^\"]+)\"");
Matcher matcher = pattern.matcher(this.json);
Expand All @@ -44,7 +44,7 @@ public void processJsonAndDownload() {
String saveDir = "dependencies/" + groupId.replace(".", "/") + "/" + artifactId + "/" + version + "/";
Path path = Path.of(saveDir + artifactId + "-" + version + ".jar");
try {
Downloader.downloadJar(url, saveDir, groupId, artifactId, version);
Downloader.downloadJar(username, password, url, saveDir, groupId, artifactId, version);
dependencyHandler.getDependencyPaths().put(artifactId, path);
SmoothCloudLauncher.getClassLoader().addURL(path.toUri().toURL());
} catch (IOException e) {
Expand Down

0 comments on commit 1fd8196

Please sign in to comment.