aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/customurl
diff options
context:
space:
mode:
authorsyeyoung <42869671+cyoung06@users.noreply.github.com>2020-12-09 00:54:16 +0900
committersyeyoung <42869671+cyoung06@users.noreply.github.com>2020-12-09 00:54:16 +0900
commit51284d9aca25fb1f892985e41cf5d9742df4a476 (patch)
treea69ef8a9475c0afa3c5f04aa222b8884ff8beab7 /src/main/java/kr/syeyoung/dungeonsguide/customurl
parent1f4774812f3f26dad9ac8d0fbeef7ff9a911c67d (diff)
downloadSkyblock-Dungeons-Guide-51284d9aca25fb1f892985e41cf5d9742df4a476.tar.gz
Skyblock-Dungeons-Guide-51284d9aca25fb1f892985e41cf5d9742df4a476.tar.bz2
Skyblock-Dungeons-Guide-51284d9aca25fb1f892985e41cf5d9742df4a476.zip
custom classloader to url
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide/customurl')
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java97
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java19
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java21
3 files changed, 137 insertions, 0 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java
new file mode 100644
index 00000000..5335f7b3
--- /dev/null
+++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLConnection.java
@@ -0,0 +1,97 @@
+package kr.syeyoung.dungeonsguide.customurl;
+
+import kr.syeyoung.dungeonsguide.Authenticator;
+import lombok.SneakyThrows;
+
+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.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+
+public class DGURLConnection extends URLConnection {
+ private Authenticator authenticator;
+ protected DGURLConnection(URL url, Authenticator authenticator) {
+ super(url);
+ this.authenticator = authenticator;
+ try {
+ connect();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static final String DOMAIN = "http://localhost:8080/";
+
+ @SneakyThrows
+ @Override
+ public void connect() throws IOException {
+ this.connected = true;
+
+ System.out.println("loading "+url.getPath());
+
+ HttpURLConnection huc = (HttpURLConnection) new URL(DOMAIN+"resource/resource?class="+ URLEncoder.encode(url.getPath())).openConnection();
+ huc.setRequestProperty("User-Agent", "DungeonsGuide/1.0");
+ huc.setRequestProperty("Content-Type", "application/json");
+ huc.setRequestProperty("Authorization", authenticator.getToken());
+ huc.setDoInput(true);
+ huc.setDoOutput(true);
+
+ 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);
+
+ 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) {}
+ 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);
+ }
+ private InputStream inputStream;
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ return inputStream;
+ }
+}
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java
new file mode 100644
index 00000000..0a8f53a9
--- /dev/null
+++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandler.java
@@ -0,0 +1,19 @@
+package kr.syeyoung.dungeonsguide.customurl;
+
+import kr.syeyoung.dungeonsguide.Authenticator;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+public class DGURLStreamHandler extends URLStreamHandler {
+ private Authenticator authenticator;
+ public DGURLStreamHandler(Authenticator authenticator) {
+ this.authenticator = authenticator;
+ }
+ @Override
+ protected URLConnection openConnection(URL url) throws IOException {
+ 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
new file mode 100644
index 00000000..15defa5e
--- /dev/null
+++ b/src/main/java/kr/syeyoung/dungeonsguide/customurl/DGURLStreamHandlerFactory.java
@@ -0,0 +1,21 @@
+package kr.syeyoung.dungeonsguide.customurl;
+
+import kr.syeyoung.dungeonsguide.Authenticator;
+
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
+
+public class DGURLStreamHandlerFactory implements URLStreamHandlerFactory {
+ private Authenticator authenticator;
+ public DGURLStreamHandlerFactory(Authenticator authenticator) {
+ this.authenticator = authenticator;
+ }
+ @Override
+ public URLStreamHandler createURLStreamHandler(String s) {
+ if ("dungeonsguide".equals(s)) {
+ return new DGURLStreamHandler(authenticator);
+ }
+
+ return null;
+ }
+}