|
| 1 | +package io.github.techstreet.dfscript.features; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | +import com.mojang.authlib.exceptions.AuthenticationException; |
| 8 | +import io.github.techstreet.dfscript.DFScript; |
| 9 | +import io.github.techstreet.dfscript.loader.Loadable; |
| 10 | +import net.minecraft.client.util.Session; |
| 11 | +import org.apache.commons.codec.digest.DigestUtils; |
| 12 | + |
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.io.OutputStream; |
| 17 | +import java.net.HttpURLConnection; |
| 18 | +import java.net.URL; |
| 19 | +import java.nio.charset.Charset; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import static io.github.techstreet.dfscript.screen.script.ScriptAddScreen.readAll; |
| 24 | + |
| 25 | +public class AuthHandler implements Loadable { |
| 26 | + private static String authCode = null; |
| 27 | + private static boolean staff = false; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void load() { |
| 31 | + regen(); |
| 32 | + } |
| 33 | + |
| 34 | + public static void regen() { |
| 35 | + URL url; |
| 36 | + HttpURLConnection con; |
| 37 | + String commonSecret; |
| 38 | + JsonObject obj; |
| 39 | + |
| 40 | + try { |
| 41 | + // Authorization step one - Create a random clientcode |
| 42 | + url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/secret/"); |
| 43 | + con = (HttpURLConnection) url.openConnection(); |
| 44 | + con.setRequestMethod("POST"); |
| 45 | + con.setRequestProperty("Content-Type", "application/json"); |
| 46 | + con.setRequestProperty("Accept", "application/json"); |
| 47 | + con.setDoOutput(true); |
| 48 | + con.setReadTimeout(5000); |
| 49 | + con.setConnectTimeout(5000); |
| 50 | + |
| 51 | + String clientCode = UUID.randomUUID().toString(); |
| 52 | + |
| 53 | + obj = new JsonObject(); |
| 54 | + obj.addProperty("uuid", DFScript.PLAYER_UUID); |
| 55 | + obj.addProperty("clientcode", clientCode); |
| 56 | + |
| 57 | + try (OutputStream os = con.getOutputStream()) { |
| 58 | + byte[] input = obj.toString().getBytes("utf-8"); |
| 59 | + os.write(input, 0, input.length); |
| 60 | + } |
| 61 | + |
| 62 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) { |
| 63 | + StringBuilder response = new StringBuilder(); |
| 64 | + String responseLine; |
| 65 | + |
| 66 | + while ((responseLine = br.readLine()) != null) { |
| 67 | + response.append(responseLine.trim()); |
| 68 | + } |
| 69 | + |
| 70 | +// ServercodeResponse servercodeResponse = DFScript.GSON.fromJson(response.toString(), ServercodeResponse.class); |
| 71 | +// commonSecret = DigestUtils.sha256Hex(servercodeResponse.getServercode() + clientCode); |
| 72 | +// commonSecret = commonSecret.substring(0, 30); |
| 73 | + } |
| 74 | + |
| 75 | + // Authorization step two - Fake server connect |
| 76 | +// try { |
| 77 | +// Session session = DFScript.MC.getSession(); |
| 78 | +// DFScript.MC.getSessionService().joinServer(session.getProfile(), session.getAccessToken(), commonSecret); |
| 79 | +// } catch (AuthenticationException e) { |
| 80 | +// DFScript.LOGGER.error(e.getMessage()); |
| 81 | +// e.printStackTrace(); |
| 82 | +// } |
| 83 | + |
| 84 | + // Authorization step two - Generate the authcode |
| 85 | + url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/auth/"); |
| 86 | + con = (HttpURLConnection) url.openConnection(); |
| 87 | + con.setRequestMethod("POST"); |
| 88 | + con.setRequestProperty("Content-Type", "application/json"); |
| 89 | + con.setRequestProperty("Accept", "application/json"); |
| 90 | + con.setDoOutput(true); |
| 91 | + con.setReadTimeout(5000); |
| 92 | + con.setConnectTimeout(5000); |
| 93 | + |
| 94 | + obj = new JsonObject(); |
| 95 | +// obj.addProperty("secret", commonSecret); |
| 96 | + obj.addProperty("uuid", DFScript.PLAYER_UUID); |
| 97 | + |
| 98 | + try (OutputStream os = con.getOutputStream()) { |
| 99 | + byte[] input = obj.toString().getBytes("utf-8"); |
| 100 | + os.write(input, 0, input.length); |
| 101 | + } |
| 102 | + |
| 103 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) { |
| 104 | + StringBuilder response = new StringBuilder(); |
| 105 | + String responseLine; |
| 106 | + |
| 107 | + while ((responseLine = br.readLine()) != null) { |
| 108 | + response.append(responseLine.trim()); |
| 109 | + } |
| 110 | + |
| 111 | +// AuthcodeResponse authcodeResponse = DFScript.GSON.fromJson(response.toString(), AuthcodeResponse.class); |
| 112 | +// authCode = authcodeResponse.getAuthcode(); |
| 113 | + DFScript.LOGGER.info("Server authorization code successfully generated!"); |
| 114 | + } |
| 115 | + } catch (Exception e) { |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + InputStream is = new URL("https://dfscript-server.techstreetdev.repl.co/staff/").openStream(); |
| 121 | + BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); |
| 122 | + obj = JsonParser.parseString(readAll(rd)).getAsJsonObject(); |
| 123 | + |
| 124 | + JsonArray array = obj.get("staff").getAsJsonArray(); |
| 125 | + boolean localStaff = false; |
| 126 | + |
| 127 | + for (JsonElement staffMember : array) { |
| 128 | + if (Objects.equals(staffMember.getAsString(), DFScript.PLAYER_UUID)) { |
| 129 | + staff = true; |
| 130 | + localStaff = true; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (!localStaff) { |
| 135 | + staff = false; |
| 136 | + } |
| 137 | + } catch (Exception e) { |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static String getAuthCode() { |
| 143 | + return authCode; |
| 144 | + } |
| 145 | + |
| 146 | + public static boolean getStaffMember() { |
| 147 | + return staff; |
| 148 | + } |
| 149 | +} |
0 commit comments