Skip to content

Commit

Permalink
1.09_15-pre2
Browse files Browse the repository at this point in the history
  • Loading branch information
Moresteck committed Sep 27, 2021
1 parent e584910 commit 302cc80
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/betacraft/launcher/Lang.java
Expand Up @@ -326,7 +326,7 @@ public static void refresh(boolean download, boolean force) {
public static String LOGIN_EMAIL_NICKNAME = "E-mail:";
public static String LOGIN_PASSWORD = "Password:";
public static String LOGIN_MOJANG_HEADER = "... or login with a Mojang account:";
public static String LOGIN_MICROSOFT_HEADER = "Login with Microsoft:";
public static String LOGIN_MICROSOFT_HEADER = "Login with Microsoft";
public static String LOGIN_MICROSOFT_BROWSER = "via browser";
public static String LOGIN_MICROSOFT_PROMPT = "via prompt";
public static String LOGIN_MICROSOFT_TITLE = "Login with your Microsoft account";
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/betacraft/launcher/Launcher.java
Expand Up @@ -53,7 +53,7 @@

/** Main class */
public class Launcher {
public static String VERSION = "1.09_15-pre1"; // TODO Always update this
public static String VERSION = "1.09_15-pre2"; // TODO Always update this

public static Instance currentInstance;
public static boolean forceUpdate = false;
Expand Down Expand Up @@ -329,7 +329,6 @@ public static void main(String[] args) {
ex.printStackTrace();
Logger.printException(ex);
}
Util.registerProtocol();

Logger.a("Loaded in: " + (System.nanoTime() - nano) + " ns");
}
Expand Down
44 changes: 39 additions & 5 deletions src/main/java/org/betacraft/launcher/ListenThread.java
@@ -1,6 +1,10 @@
package org.betacraft.launcher;

import java.io.DataInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

Expand All @@ -25,19 +29,33 @@ public void run() {
while (running) {
Socket sock = this.socket.accept();
try {
DataInputStream bais = new DataInputStream(sock.getInputStream());
String action = bais.readUTF();
byte[] bytes = readInputStreamBytes(sock.getInputStream());
String http = new String(bytes, "UTF-8");
System.out.println(http);
// msa
if (action.startsWith(MicrosoftAuth.REDIRECT_URI)) {
if (http.startsWith("GET " + MicrosoftAuth.AUTH_URI)) {
System.out.println("Received Microsoft login response");
MicrosoftAuth msa = new MicrosoftAuth(null);
msa.code = action.substring(action.indexOf("=")+1);
msa.code = http.substring(http.indexOf("=")+1, http.indexOf(" HTTP/"));
try {
LoginPanel.continueMSA(msa);
} catch (Throwable t) {
t.printStackTrace();
}
} else {
sock.close();
continue;
}
BufferedWriter bos = new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(sock.getOutputStream()), "UTF-8"));
bos.write("HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"\r\n\r\n");
bos.write("<html> <head> <title>All done!</title> <script> window.onload = function() { window.close(); } </script> </head> <body> <center><h1>You can now close this tab.</h1></center> </html>");
bos.flush();
bos.close();
sock.close();
} catch (Throwable t) {}
}
// close on exit
Expand All @@ -56,4 +74,20 @@ public static boolean isAvailable() {
return false;
}
}

public static byte[] readInputStreamBytes(InputStream in) {
try {
byte[] buffer = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (in.available() > 0) {
in.read(buffer);
baos.write(buffer);
}
byte[] data = baos.toByteArray();
return data;
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
}
22 changes: 4 additions & 18 deletions src/main/java/org/betacraft/launcher/LoginPanel.java
Expand Up @@ -50,19 +50,17 @@ public LoginPanel() {
constr.gridwidth = 1;
constr.weightx = 1.0;
constr.insets = new Insets(10, 10, 0, 10);
JLabel microsoftlabel = new JLabel(Lang.LOGIN_MICROSOFT_HEADER);
microsoftlabel.setForeground(Color.LIGHT_GRAY);
panel.add(microsoftlabel, constr);
constr.gridx = 1;
JButton microsoftbrowser = new JButton(Lang.LOGIN_MICROSOFT_BROWSER);

constr.gridwidth = 3;
JButton microsoftbrowser = new JButton(Lang.LOGIN_MICROSOFT_HEADER);
microsoftbrowser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Util.openURL(new URL("https://login.live.com/oauth20_authorize.srf" +
"?client_id=" + MicrosoftAuth.CLIENT_ID +
"&response_type=code" +
"&scope=XboxLive.signin%20offline_access" +
"&prompt=login" +
"&prompt=select_account" +
"&redirect_uri=" + MicrosoftAuth.REDIRECT_URI).toURI());

//Window.quit(true);
Expand All @@ -75,18 +73,6 @@ public void actionPerformed(ActionEvent e) {
}
});
panel.add(microsoftbrowser, constr);
constr.gridx = 2;
JButton microsoftjfx = new JButton(Lang.LOGIN_MICROSOFT_PROMPT);
microsoftjfx.setEnabled(Util.hasJFX());
microsoftjfx.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MicrosoftAuth auth = new MicrosoftAuth(null);
setVisible(false);
Window.loginPanel = null;
auth.showPrompt();
}
});
panel.add(microsoftjfx, constr);
// JButton microsoftauth = new JButton(Lang.LOGIN_MICROSOFT_BUTTON);
// microsoftauth.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
Expand Down
135 changes: 36 additions & 99 deletions src/main/java/org/betacraft/launcher/Util.java
Expand Up @@ -400,120 +400,57 @@ public static boolean hasJFX() {
}
}

public static void registerProtocol() {
public static String getFullJavaVersion() {
String line = null;
try {
if (OS.isWindows()) {
if (!hasRegistryKey("HKCU\\Software\\Classes\\betacraft")) {
addRegistryKey("HKCU\\Software\\Classes\\betacraft", "/t", "REG_SZ", "/d", "URL:betacraft");
addRegistryKey("HKCU\\Software\\Classes\\betacraft", "/v", "URL Protocol", "/t", "REG_SZ");

addRegistryKey("HKCU\\Software\\Classes\\betacraft\\shell\\open\\command", "/ve", "/t", "REG_SZ", "/d", "\"\\\"" + BC.currentPath.toPath().toString() + "\\\" \\\"%1\\\"\"");
} else {
// always make sure the reference is up to date
if (!getRegistryKeyValue("HKCU\\Software\\Classes\\betacraft\\shell\\open\\command", null).contains(BC.currentPath.toPath().toString())) {
addRegistryKey("HKCU\\Software\\Classes\\betacraft\\shell\\open\\command", "/ve", "/f", "/t", "REG_SZ", "/d", "\"\\\"" + BC.currentPath.toPath().toString() + "\\\" \\\"%1\\\"\"");
}
}
} else if (OS.isLinux()) {
File local = new File(System.getProperty("user.home"), ".local/share/");
File icon = new File(local, "icons/betacraft.png");
String content = "[Desktop Entry]\n" +
"Name=BetaCraft Launcher\n" +
"Comment=BetaCraft - a Minecraft launcher\n" +
"Exec=\"" + Launcher.javaRuntime.toPath().toString() + "\" -jar " + BC.currentPath.toPath().toString() + " %U\n" +
"Icon=" + icon.toPath().toString() + "\n" +
"Type=Application\n" +
"Categories=Game;\n" +
"MimeType=x-scheme-handler/betacraft;";
File desktopfile = new File(local, "applications/betacraft-launcher.desktop");
Files.write(desktopfile.toPath(), content.getBytes("UTF-8"));
byte[] png = RequestUtil.readInputStream(Launcher.class.getClassLoader().getResourceAsStream("icons/icon.png"));
Files.write(icon.toPath(), png);

Runtime.getRuntime().exec("xdg-mime default betacraft-launcher.desktop x-scheme-handler/betacraft");
}
// macos is handled automatically (?) TODO buy a mac and add proper support for it
} catch (Throwable t) {
t.printStackTrace();
}
}

public static boolean hasRegistryKey(String... key) {
try {
ArrayList<String> start = new ArrayList<>();
start.add("reg");
start.add("query");
start.addAll(Arrays.asList(key));
System.out.println("Regcommand: " + start);
Process p = Runtime.getRuntime().exec(start.toArray(new String[0]));
ArrayList<String> arl = new ArrayList<>();
arl.add(Launcher.javaRuntime.toPath().toString());
arl.add("-version");
ProcessBuilder pb = new ProcessBuilder(arl);
Process p = pb.start();
InputStreamReader isr_log = new InputStreamReader(p.getErrorStream());
BufferedReader br_log = new BufferedReader(isr_log);
StringBuilder strb = new StringBuilder();
String line = null;
while ((line = br_log.readLine()) != null) {
strb.append(line + "\n");
}
System.out.println(strb.toString());
if (strb.toString().contains("ERROR") || p.exitValue() != 0) {
return false;
if (line.contains("version")) {
p.destroyForcibly();
break;
}
}
return true;
} catch (Throwable t) {
t.printStackTrace();
return false;
}
// java version "1.8.0_281" ---> 1.8.0_281"
String verstart = line.substring(line.indexOf("\"")+1);
// 1.8.0_281" ---> 1.8.0_281
String fullver = verstart.substring(0, verstart.indexOf("\""));

return fullver;
}

public static String getRegistryKeyValue(String location, String key) {
try {
ArrayList<String> start = new ArrayList<>();
start.add("reg");
start.add("query");
start.add(location);
if (key != null) {
start.add("/v");
start.add(key);
}
System.out.println("Regcommand: " + start);
Process p = Runtime.getRuntime().exec(start.toArray(new String[0]));
InputStreamReader isr_log = new InputStreamReader(p.getInputStream());
BufferedReader br_log = new BufferedReader(isr_log);
StringBuilder strb = new StringBuilder();
String line = null;
while ((line = br_log.readLine()) != null) {
strb.append(line + "\n");
}
String all = strb.toString();
System.out.println(all);
if (all == null || !all.contains("\t") || p.exitValue() != 0) {
return "";
} else {
return all.split("\t")[2];
public static String getJavaVersion() {
String fullver = getFullJavaVersion();

// This basically filters out the actual core version, skipping all the fancy stuff
String whitelist = "0123456789.";
for (int i = 0; i < fullver.length(); i++) {
if (!whitelist.contains(Character.toString(fullver.charAt(i)))) {
fullver = fullver.substring(0, i);
}
} catch (Throwable t) {
t.printStackTrace();
return "";
}
return fullver;
}

public static void addRegistryKey(String... key) {
try {
ArrayList<String> start = new ArrayList<>();
start.add("reg");
start.add("add");
start.addAll(Arrays.asList(key));
System.out.println("Regcommand: " + start);
Process p = Runtime.getRuntime().exec(start.toArray(new String[0]));
InputStreamReader isr_log = new InputStreamReader(p.getErrorStream());
BufferedReader br_log = new BufferedReader(isr_log);
StringBuilder strb = new StringBuilder();
String line = null;
while ((line = br_log.readLine()) != null) {
strb.append(line + "\n");
public static int getMajorJavaVersion() {
String ver = getJavaVersion();
if (ver.startsWith("1.")) {
return Integer.parseInt(ver.split("\\.")[1]);
} else {
int cut = ver.indexOf('.');
if (cut == -1) {
cut = ver.length();
}
System.out.println(strb.toString());
} catch (Throwable t) {
t.printStackTrace();

return Integer.parseInt(ver.substring(0, cut));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pl/betacraft/auth/MicrosoftAuth.java
Expand Up @@ -28,7 +28,8 @@

public class MicrosoftAuth implements Authenticator {

public static final String REDIRECT_URI = "betacraft://msaresponse";
public static final String AUTH_URI = "/msaresponse";
public static final String REDIRECT_URI = "http://localhost:11799/msaresponse";
public static final String CLIENT_ID = "8075fa74-4091-4356-a0b8-a7c118ef121c";

public String code;
Expand Down

0 comments on commit 302cc80

Please sign in to comment.