diff options
author | Abhiram555 <63419731+abhithedev200@users.noreply.github.com> | 2023-01-26 21:34:15 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-26 17:04:15 +0100 |
commit | 19f6d6eacf3df61c3079d342482df3ae4a2c3e15 (patch) | |
tree | 30c8c309e46ecfc9d96d505322523178a53c1549 /src/main/java/de/torui/coflsky/proxy | |
parent | 7cfe4c326b668683cccc9c250d58b7ef9c3e9083 (diff) | |
download | COFL-19f6d6eacf3df61c3079d342482df3ae4a2c3e15.tar.gz COFL-19f6d6eacf3df61c3079d342482df3ae4a2c3e15.tar.bz2 COFL-19f6d6eacf3df61c3079d342482df3ae4a2c3e15.zip |
Add Proxy support to the cofl mod (#85)
* Implement basic proxy functionality
* Finish the api key management
* Finished the hypixel api key support
* Fixed a check, for some reason intelij didn't update my code
* Only upload request if the upload is enabled, else just do the request
Co-authored-by: Äkwav <16632490+Ekwav@users.noreply.github.com>
Co-authored-by: Äkwav <16632490+Ekwav@users.noreply.github.com>
Diffstat (limited to 'src/main/java/de/torui/coflsky/proxy')
-rw-r--r-- | src/main/java/de/torui/coflsky/proxy/APIKeyManager.java | 54 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/proxy/ProxyManager.java | 99 |
2 files changed, 153 insertions, 0 deletions
diff --git a/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java b/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java new file mode 100644 index 0000000..e726552 --- /dev/null +++ b/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java @@ -0,0 +1,54 @@ +package de.torui.coflsky.proxy; + +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; +import net.minecraftforge.fml.common.Loader; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Collectors; + +public class APIKeyManager { + private final Gson gson = new Gson(); + private APIInfo apiInfo = new APIInfo(); + + public APIInfo getApiInfo(){ + return this.apiInfo; + } + + + public class APIInfo{ + @SerializedName("api-key") + public String key; + } + + public void loadIfExists() throws Exception { + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json"); + File file = dataPath.toFile(); + if(file.exists()) { + BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file))); + String raw = reader.lines().collect(Collectors.joining("\n")); + this.apiInfo = gson.fromJson(raw,APIInfo.class); + reader.close(); + } + } + + + public void saveKey() throws Exception { + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json"); + File file = dataPath.toFile(); + if(file.exists()) { + file.delete(); + } + file.createNewFile(); + + String data = gson.toJson(apiInfo); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); + bw.append(data); + bw.flush(); + bw.close(); + } + + +} diff --git a/src/main/java/de/torui/coflsky/proxy/ProxyManager.java b/src/main/java/de/torui/coflsky/proxy/ProxyManager.java new file mode 100644 index 0000000..b9abf86 --- /dev/null +++ b/src/main/java/de/torui/coflsky/proxy/ProxyManager.java @@ -0,0 +1,99 @@ +package de.torui.coflsky.proxy; + +import de.torui.coflsky.CoflSky; +import de.torui.coflsky.commands.models.ProxyRequest; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class ProxyManager { + private final String ProxyResponseUrl = "http://sky.coflnet.com/api/data/proxy"; + private final ExecutorService requestExecutor = Executors.newSingleThreadExecutor(); + + + public void handleRequestAsync(ProxyRequest request){ + CompletableFuture<String> req = this.doRequest(request.getUrl()); + if(request.isUploadEnabled()) { + req.thenAcceptAsync(res -> this.uploadData(res,request.getId())); + } + } + + + private String getString(HttpURLConnection con) { + try { + 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); + } + String resString = result.toString("UTF-8"); + return resString; + } catch(IOException e){ + return null; + } + } + + public void uploadData(String data,String id){ + this.requestExecutor.submit(new Runnable() { + @Override + public void run() { + try{ + URL url = new URL(ProxyManager.this.ProxyResponseUrl); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + + con.setRequestProperty("X-Request-Id", id); + + con.setDoOutput(true); + con.setDoInput(true); + + OutputStream os = con.getOutputStream(); + os.write(data.getBytes("UTF-8")); + os.close(); + String response = getString(con); + System.out.println("Response=" + response); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + }); + } + + + private CompletableFuture<String> doRequest(String targetUrl){ + CompletableFuture<String> future = new CompletableFuture<>(); + + this.requestExecutor.submit(new Runnable() { + @Override + public void run() { + try{ + URL url = new URL(targetUrl); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("Accept", "application/json"); + con.setRequestProperty("User-Agent", "CoflMod"); + + String key = CoflSky.getAPIKeyManager().getApiInfo().key; + + if(targetUrl.startsWith("https://api.hypixel.net") && !key.isEmpty()){ + con.setRequestProperty("API-Key", key); + } + + con.setDoInput(true); + future.complete(getString(con)); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + }); + + return future; + } + + +} |