Use more generic method for posting logs

This commit is contained in:
rtm516 2021-08-24 21:34:50 +01:00
parent d26aed0a87
commit 9fb509010a
No known key found for this signature in database
GPG key ID: 331715B8B007C67A
2 changed files with 39 additions and 35 deletions

View file

@ -26,6 +26,7 @@
package org.geysermc.connector.dump; package org.geysermc.connector.dump;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.steveice10.mc.protocol.MinecraftConstants; import com.github.steveice10.mc.protocol.MinecraftConstants;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import com.google.common.io.ByteSource; import com.google.common.io.ByteSource;
@ -47,10 +48,9 @@ import org.geysermc.floodgate.util.FloodgateInfoHolder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.*;
import java.net.InetSocketAddress; import java.util.HashMap;
import java.net.Socket; import java.util.Map;
import java.net.UnknownHostException;
import java.util.Properties; import java.util.Properties;
@Getter @Getter
@ -200,8 +200,13 @@ public class DumpInfo {
public LogsInfo() { public LogsInfo() {
try { try {
this.link = WebUtils.postLogs(GeyserConnector.getInstance().getBootstrap().getLogsPath()); Map<String, String> fields = new HashMap<>();
} catch (IOException ignored) {} 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) { }
} }
} }

View file

@ -31,14 +31,12 @@ import org.geysermc.connector.GeyserConnector;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.stream.Collectors; import java.util.Map;
public class WebUtils { 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 { public static String post(String reqURL, String postContent) throws IOException {
URL url = new URL(reqURL); URL url = new URL(reqURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection(); 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 { public static String postForm(String reqURL, Map<String, String> fields) throws IOException {
// Connect to api URL url = new URL(reqURL);
URL url = new URL("https://api.mclo.gs/1/log"); HttpURLConnection con = (HttpURLConnection) url.openConnection();
URLConnection con = url.openConnection(); con.setRequestMethod("POST");
HttpURLConnection http = (HttpURLConnection) con; con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION);
http.setDoOutput(true); con.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;
// Send log to api try (OutputStream out = con.getOutputStream()) {
http.setFixedLengthStreamingMode(length); // Write the form data to the output
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); for (Map.Entry<String, String> field : fields.entrySet()) {
http.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); out.write((field.getKey() + "=" + URLEncoder.encode(field.getValue(), StandardCharsets.UTF_8.toString()) + "&").getBytes(StandardCharsets.UTF_8));
http.connect(); }
try (OutputStream os = http.getOutputStream()) {
os.write(out);
} }
String is = new BufferedReader(new InputStreamReader(http.getInputStream()))
.lines() return connectionToString(con);
.collect(Collectors.joining());
JsonNode jn = GeyserConnector.JSON_MAPPER.readTree(is);
// Handle response
return jn.get("url").textValue();
} }
} }