diff options
author | Florian Rinke <develop@torui.de> | 2021-09-30 20:41:46 +0200 |
---|---|---|
committer | Florian Rinke <develop@torui.de> | 2021-09-30 20:41:46 +0200 |
commit | 593611f745cab688ee251c047855b1beae41f9ef (patch) | |
tree | 85b6c067552b74e7f36fe511d638f5860eb8cbfd /src/main/java/de/torui/coflsky/minecraft_integration | |
parent | 832403ba363d29d0252702c626405cbc9ba4d7bd (diff) | |
download | COFL-593611f745cab688ee251c047855b1beae41f9ef.tar.gz COFL-593611f745cab688ee251c047855b1beae41f9ef.tar.bz2 COFL-593611f745cab688ee251c047855b1beae41f9ef.zip |
implements #2, #3 #5, #1 Session IDs
Diffstat (limited to 'src/main/java/de/torui/coflsky/minecraft_integration')
-rw-r--r-- | src/main/java/de/torui/coflsky/minecraft_integration/PlayerDataProvider.java | 70 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/minecraft_integration/TemporarySession.java | 137 |
2 files changed, 207 insertions, 0 deletions
diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/PlayerDataProvider.java b/src/main/java/de/torui/coflsky/minecraft_integration/PlayerDataProvider.java new file mode 100644 index 0000000..4c3c39b --- /dev/null +++ b/src/main/java/de/torui/coflsky/minecraft_integration/PlayerDataProvider.java @@ -0,0 +1,70 @@ +package de.torui.coflsky.minecraft_integration; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; + + +import de.torui.coflsky.websocket.WSClient; +import net.minecraft.client.Minecraft; + +public class PlayerDataProvider { + + private static class UUIDHelper { + public String id; + public String name; + } + + public static String getActivePlayerUUID() { + try { + URL url = new URL("https://api.mojang.com/profiles/minecraft"); + HttpURLConnection con; + con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + + con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + con.setRequestProperty("Accept", "application/json"); + con.setDoInput(true); + con.setDoOutput(true); + + // ... + + OutputStream os = con.getOutputStream(); + byte[] bytes = ("[\"" + getUsername() + "\"]").getBytes("UTF-8"); + os.write(bytes); + os.close(); + + InputStream in = new BufferedInputStream(con.getInputStream()); + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + for (int length; (length = in.read(buffer)) != -1; ) { + result.write(buffer, 0, length); + } + // StandardCharsets.UTF_8.name() > JDK 7 + String resString = result.toString("UTF-8"); + + System.out.println("Result= " + resString); + UUIDHelper[] helpers = WSClient.gson.fromJson(resString, UUIDHelper[].class); + if(helpers.length == 1) { + return helpers[0].id; + } + + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return UUID.randomUUID().toString(); + } + + public static String getUsername() { + String username = Minecraft.getSessionInfo().get("X-Minecraft-Username"); + return username; + } + +} diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/TemporarySession.java b/src/main/java/de/torui/coflsky/minecraft_integration/TemporarySession.java new file mode 100644 index 0000000..542bd53 --- /dev/null +++ b/src/main/java/de/torui/coflsky/minecraft_integration/TemporarySession.java @@ -0,0 +1,137 @@ +package de.torui.coflsky.minecraft_integration; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import net.minecraftforge.fml.common.Loader; + +public class TemporarySession { + private static Gson gson = new GsonBuilder() .registerTypeAdapter(ZonedDateTime.class, new TypeAdapter<ZonedDateTime>() { + @Override + public void write(JsonWriter out, ZonedDateTime value) throws IOException { + out.value(value.toString()); + } + + @Override + public ZonedDateTime read(JsonReader in) throws IOException { + return ZonedDateTime.parse(in.nextString()); + } + }) + .enableComplexMapKeySerialization().create(); + public static class TempSession { + + public String SessionUUID; + public ZonedDateTime timestampCreated; + public TempSession() {} + public TempSession(String sessionUUID, ZonedDateTime timestampCreated) { + super(); + SessionUUID = sessionUUID; + this.timestampCreated = timestampCreated; + } + + } + + public static void UpdateSessions() throws IOException { + Map<String, TempSession> sessions = GetSessions(); + + for (String username : sessions.keySet()) { + if(!isValidSession(sessions.get(username))) { + DeleteTempSession(username); + } + } + } + + public static Path GetTempFileFolder() { + + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "sessions"); + dataPath.toFile().mkdirs(); + + return dataPath; + } + + public static Map<String, TempSession> GetSessions() throws IOException{ + + File[] sessions = GetTempFileFolder().toFile().listFiles(); + + Map<String, TempSession> map = new HashMap<>(); + + for (int i= 0; i<sessions.length;i++) { + map.put(sessions[i].getName(), GetSession(sessions[i].getName())); + } + + return map; + } + + public static boolean isValidSession(TempSession session) { + if(session.timestampCreated.plus(Duration.ofDays(7)).isAfter(ZonedDateTime.now())) { + return true; + } + return false; + } + + private static Path GetUserPath(String username) { + return Paths.get(GetTempFileFolder().toString() + "/" + username); + } + public static void DeleteTempSession(String username) { + Path path =GetUserPath(username); + path.toFile().delete(); + } + + public static TempSession GetSession(String username) throws IOException { + Path path = GetUserPath(username); + File file = path.toFile(); + + if(!file.exists()) { + TempSession session = new TempSession(UUID.randomUUID().toString(), ZonedDateTime.now()); + OverwriteTempSession(username, session); + return session; + } + + BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file))); + String raw = reader.lines().collect(Collectors.joining("\n")); + + reader.close(); + TempSession session = gson.fromJson(raw, TempSession.class); + return session; + } + + public static boolean OverwriteTempSession(String username, TempSession session) throws IOException { + + + Path path = GetUserPath(username); + File file = path.toFile(); + file.createNewFile(); + + String data = gson.toJson(session); + + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); + bw.append(data); + bw.flush(); + bw.close(); + + return true; + } +} |