From 9fb509010abb6d2b7ce395492965dc0bb895c630 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Tue, 24 Aug 2021 21:34:50 +0100 Subject: [PATCH] Use more generic method for posting logs --- .../org/geysermc/connector/dump/DumpInfo.java | 17 ++++-- .../geysermc/connector/utils/WebUtils.java | 57 +++++++++---------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java b/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java index 123dcc3fa..c006c28b2 100644 --- a/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java +++ b/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java @@ -26,6 +26,7 @@ package org.geysermc.connector.dump; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.databind.JsonNode; import com.github.steveice10.mc.protocol.MinecraftConstants; import com.google.common.hash.Hashing; import com.google.common.io.ByteSource; @@ -47,10 +48,9 @@ import org.geysermc.floodgate.util.FloodgateInfoHolder; import java.io.File; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.UnknownHostException; +import java.net.*; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; @Getter @@ -200,8 +200,13 @@ public class DumpInfo { public LogsInfo() { try { - this.link = WebUtils.postLogs(GeyserConnector.getInstance().getBootstrap().getLogsPath()); - } catch (IOException ignored) {} + Map fields = new HashMap<>(); + fields.put("content", String.join("\n", java.nio.file.Files.readAllLines(GeyserConnector.getInstance().getBootstrap().getLogsPath()))); + + JsonNode logData = GeyserConnector.JSON_MAPPER.readTree(WebUtils.postForm("https://api.mclo.gs/1/log", fields)); + + this.link = logData.get("url").textValue(); + } catch (IOException ignored) { } } } diff --git a/connector/src/main/java/org/geysermc/connector/utils/WebUtils.java b/connector/src/main/java/org/geysermc/connector/utils/WebUtils.java index e46f9126f..b736a282c 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/WebUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/WebUtils.java @@ -31,14 +31,12 @@ import org.geysermc.connector.GeyserConnector; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.stream.Collectors; +import java.util.Map; public class WebUtils { @@ -90,6 +88,14 @@ public class WebUtils { } } + /** + * Post a string to the given URL + * + * @param reqURL URL to post to + * @param postContent String data to post + * @return String returned by the server + * @throws IOException + */ public static String post(String reqURL, String postContent) throws IOException { URL url = new URL(reqURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); @@ -138,35 +144,28 @@ public class WebUtils { } /** - * Get the server log file and uploads it to mclo.gs + * Post fields to a URL as a form * - * @param log File to fetch + * @param reqURL URL to post to + * @param fields Form data to post + * @return String returned by the server + * @throws IOException */ - public static String postLogs(Path log) throws IOException { - // Connect to api - URL url = new URL("https://api.mclo.gs/1/log"); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection) con; - http.setRequestMethod("POST"); - http.setDoOutput(true); - // Convert log to application/x-www-form-urlencoded - String content = "content=" + URLEncoder.encode(new BufferedReader(new InputStreamReader(Files.newInputStream(log.toRealPath()))).lines().collect(Collectors.joining("\n")), StandardCharsets.UTF_8.toString()); - byte[] out = content.getBytes(StandardCharsets.UTF_8); - int length = out.length; + public static String postForm(String reqURL, Map fields) throws IOException { + URL url = new URL(reqURL); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); + con.setDoOutput(true); - // Send log to api - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - http.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); - http.connect(); - try (OutputStream os = http.getOutputStream()) { - os.write(out); + try (OutputStream out = con.getOutputStream()) { + // Write the form data to the output + for (Map.Entry field : fields.entrySet()) { + out.write((field.getKey() + "=" + URLEncoder.encode(field.getValue(), StandardCharsets.UTF_8.toString()) + "&").getBytes(StandardCharsets.UTF_8)); + } } - String is = new BufferedReader(new InputStreamReader(http.getInputStream())) - .lines() - .collect(Collectors.joining()); - JsonNode jn = GeyserConnector.JSON_MAPPER.readTree(is); - // Handle response - return jn.get("url").textValue(); + + return connectionToString(con); } }