diff options
Diffstat (limited to 'src/main')
6 files changed, 95 insertions, 79 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java index b4a4f161..55785a95 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuide.java @@ -33,6 +33,12 @@ public class DungeonsGuide implements DungeonsGuideInterface { public static boolean DEBUG = false; + @Getter + private Authenticator authenticator; + public DungeonsGuide(Authenticator authenticator) { + this.authenticator = authenticator; + } + public static void sendDebugChat(IChatComponent iChatComponent) { if (DEBUG) Minecraft.getMinecraft().thePlayer.addChatMessage(iChatComponent); diff --git a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuideMain.java b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuideMain.java index e46e49b7..54d10a51 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuideMain.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/DungeonsGuideMain.java @@ -33,25 +33,23 @@ public class DungeonsGuideMain { dungeonsGuideMain = this; - dungeonsGuideInterface.init(event); } - @Getter - private Authenticator authenticator; @EventHandler public void pre(FMLPreInitializationEvent event) { - authenticator = new Authenticator(); + Authenticator authenticator = new Authenticator(); String token = null; try { token = authenticator.authenticate(); if (token != null) { - URL.setURLStreamHandlerFactory(new DGURLStreamHandlerFactory()); + dungeonsGuideMain = this; + URL.setURLStreamHandlerFactory(new DGURLStreamHandlerFactory(authenticator)); LaunchClassLoader launchClassLoader = (LaunchClassLoader) DungeonsGuideMain.class.getClassLoader(); launchClassLoader.addURL(new URL("dungeonsguide:///")); try { - dungeonsGuideInterface = new DungeonsGuide(); + dungeonsGuideInterface = new DungeonsGuide(authenticator); dungeonsGuideInterface.pre(event); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java index fac39796..6a97d96a 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java @@ -9,10 +9,7 @@ import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; @@ -21,8 +18,11 @@ import java.net.URLEncoder; public class DGURLConnection extends URLConnection { private static final String DOMAIN = "http://localhost:8080/"; - protected DGURLConnection(URL url) { + private Authenticator authenticator; + protected DGURLConnection(URL url, Authenticator authenticator) { super(url); + connected = false; + this.authenticator = authenticator; } @Override @@ -32,85 +32,91 @@ public class DGURLConnection extends URLConnection { private boolean checkExist = false; private boolean exists= false; - @SneakyThrows @Override public void connect() throws IOException { - if (!(url.getPath().contains("kr/syeyoung") ||url.getPath().contains("roomdata"))) return; - this.connected = true; - - boolean classLoader = false; - for (StackTraceElement ste:Thread.currentThread().getStackTrace()){ - if (ste.getClassName().equals("net.minecraft.launchwrapper.LaunchClassLoader")) { - classLoader = true; + try { + if (!(url.getPath().contains("kr/syeyoung") || url.getPath().contains("roomdata"))) return; + this.connected = true; + + boolean classLoader = false; + for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { + if (ste.getClassName().equals("net.minecraft.launchwrapper.LaunchClassLoader")) { + classLoader = true; + } } - } - System.out.println("loading "+url.getPath().substring(1) +" called from classloader "+classLoader); + System.out.println("loading " + url.getPath().substring(1) + " called from classloader " + classLoader); - HttpURLConnection huc = (HttpURLConnection) new URL(DOMAIN+"resource/resource?class="+ URLEncoder.encode(url.getPath().substring(1))).openConnection(); - huc.setRequestProperty("User-Agent", "DungeonsGuide/1.0"); - huc.setRequestProperty("Content-Type", "application/json"); - huc.setRequestMethod(checkExist ? "HEAD" : "GET"); - huc.setRequestProperty("Authorization", (url.getUserInfo() == null && classLoader) ? DungeonsGuideMain.getDungeonsGuideMain().getAuthenticator().getToken() : url.getUserInfo()); - huc.setDoInput(true); - huc.setDoOutput(true); + HttpURLConnection huc = (HttpURLConnection) new URL(DOMAIN + "resource/resource?class=" + URLEncoder.encode(url.getPath().substring(1))).openConnection(); + huc.setRequestProperty("User-Agent", "DungeonsGuide/1.0"); + huc.setRequestProperty("Content-Type", "application/json"); + huc.setRequestMethod(checkExist ? "HEAD" : "GET"); + huc.setRequestProperty("Authorization", (url.getUserInfo() == null && classLoader) ? authenticator.getToken() : url.getUserInfo()); + huc.setDoInput(true); + huc.setDoOutput(true); + System.out.println("Resp Code::" + huc.getResponseCode() + "/ " + checkExist); - exists = huc.getResponseCode() != 404; - if (checkExist) { - return; - } + exists = huc.getResponseCode() != 404; + if (checkExist) { + return; + } - InputStream inputStream = huc.getInputStream(); - byte[] bytes = new byte[4]; - inputStream.read(bytes); - int len = ((bytes[0] & 0xFF) << 24) | - ((bytes[1] & 0xFF) << 16) | - ((bytes[2] & 0xFF) << 8 ) | - ((bytes[3] & 0xFF)); - while(inputStream.available() < len); - byte[] pubKey = new byte[len]; - inputStream.read(pubKey); - - Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); - byte[] byteEncrypted = pubKey; - cipher.init(Cipher.DECRYPT_MODE, DungeonsGuideMain.getDungeonsGuideMain().getAuthenticator().getKeyPair().getPrivate()); - byte[] bytePlain = cipher.doFinal(byteEncrypted); - - cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - SecretKeySpec keySpec = new SecretKeySpec(bytePlain, "AES"); - IvParameterSpec ivSpec = new IvParameterSpec(bytePlain); - cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec); - CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher); - - cipherInputStream.read(bytes); - int length = ((bytes[0] & 0xFF) << 24) | - ((bytes[1] & 0xFF) << 16) | - ((bytes[2] & 0xFF) << 8 ) | - ((bytes[3] & 0xFF)); - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - int totalLen = 0; - try { - byte[] buffer = new byte[128]; - int read = 0; - while ( (read = cipherInputStream.read(buffer)) != -1 ) { - totalLen += read; - byteStream.write(buffer, 0, read); - if (totalLen >= length) break; + InputStream inputStream = huc.getInputStream(); + byte[] bytes = new byte[4]; + inputStream.read(bytes); + int len = ((bytes[0] & 0xFF) << 24) | + ((bytes[1] & 0xFF) << 16) | + ((bytes[2] & 0xFF) << 8) | + ((bytes[3] & 0xFF)); + while (inputStream.available() < len) ; + byte[] pubKey = new byte[len]; + inputStream.read(pubKey); + + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + byte[] byteEncrypted = pubKey; + cipher.init(Cipher.DECRYPT_MODE, authenticator.getKeyPair().getPrivate()); + byte[] bytePlain = cipher.doFinal(byteEncrypted); + + cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + SecretKeySpec keySpec = new SecretKeySpec(bytePlain, "AES"); + IvParameterSpec ivSpec = new IvParameterSpec(bytePlain); + cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); + CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher); + + cipherInputStream.read(bytes); + int length = ((bytes[0] & 0xFF) << 24) | + ((bytes[1] & 0xFF) << 16) | + ((bytes[2] & 0xFF) << 8) | + ((bytes[3] & 0xFF)); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + int totalLen = 0; + try { + byte[] buffer = new byte[128]; + int read = 0; + while ((read = cipherInputStream.read(buffer)) != -1) { + totalLen += read; + byteStream.write(buffer, 0, read); + if (totalLen >= length) break; + } + } catch (Exception ignored) { } - } catch (Exception ignored) {} - byte[] byte1 = byteStream.toByteArray(); - byte[] byte2 = new byte[(int) length]; - System.arraycopy(byte1, 0, byte2, 0, byte2.length); - cipherInputStream.close(); - inputStream.close(); - this.inputStream = new ByteArrayInputStream(byte2); + byte[] byte1 = byteStream.toByteArray(); + byte[] byte2 = new byte[(int) length]; + System.arraycopy(byte1, 0, byte2, 0, byte2.length); + cipherInputStream.close(); + inputStream.close(); + this.inputStream = new ByteArrayInputStream(byte2); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } } private InputStream inputStream; @Override public InputStream getInputStream() throws IOException { if (!connected) connect(); + if (checkExist && !exists) throw new FileNotFoundException(); return inputStream; } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java index 727e6e78..b5d0f861 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java @@ -1,15 +1,18 @@ package kr.syeyoung.dungeonsguide.customurl; import kr.syeyoung.dungeonsguide.Authenticator; +import lombok.AllArgsConstructor; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; +@AllArgsConstructor public class DGURLStreamHandler extends URLStreamHandler { + private Authenticator authenticator; @Override protected URLConnection openConnection(URL url) throws IOException { - return new DGURLConnection(url); + return new DGURLConnection(url, authenticator); } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java index a27bda87..418c55f0 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java @@ -1,15 +1,18 @@ package kr.syeyoung.dungeonsguide.customurl; import kr.syeyoung.dungeonsguide.Authenticator; +import lombok.AllArgsConstructor; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; +@AllArgsConstructor public class DGURLStreamHandlerFactory implements URLStreamHandlerFactory { + private Authenticator authenticator; @Override public URLStreamHandler createURLStreamHandler(String s) { if ("dungeonsguide".equals(s)) { - return new DGURLStreamHandler(); + return new DGURLStreamHandler(authenticator); } return null; diff --git a/src/main/java/kr/syeyoung/dungeonsguide/dungeon/roomfinder/DungeonRoomInfoRegistry.java b/src/main/java/kr/syeyoung/dungeonsguide/dungeon/roomfinder/DungeonRoomInfoRegistry.java index f27d1b2f..b08adbbe 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/dungeon/roomfinder/DungeonRoomInfoRegistry.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/dungeon/roomfinder/DungeonRoomInfoRegistry.java @@ -69,12 +69,12 @@ public class DungeonRoomInfoRegistry { registered.clear(); shapeMap.clear(); uuidMap.clear(); - URL url = new URL("dungeonsguide://"+DungeonsGuideMain.getDungeonsGuideMain().getAuthenticator().getToken()+"@/roomdata/datas.txt"); + URL url = new URL("dungeonsguide://"+DungeonsGuide.getDungeonsGuide().getAuthenticator().getToken()+"@/roomdata/datas.txt"); List<String> lines = IOUtils.readLines(url.openConnection().getInputStream()); for (String name : lines) { if (!name.endsWith(".roomdata")) continue; try { - InputStream fis = new URL("dungeonsguide://"+DungeonsGuideMain.getDungeonsGuideMain().getAuthenticator().getToken()+"@/"+name).openStream(); + InputStream fis = new URL("dungeonsguide://"+DungeonsGuide.getDungeonsGuide().getAuthenticator().getToken()+"@/"+name).openStream(); ObjectInputStream ois = new ObjectInputStream(fis); DungeonRoomInfo dri = (DungeonRoomInfo) ois.readObject(); ois.close(); |