3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-04 23:30:17 +01:00

Use more generic method for posting logs

Dieser Commit ist enthalten in:
rtm516 2021-08-24 21:34:50 +01:00
Ursprung d26aed0a87
Commit 9fb509010a
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 331715B8B007C67A
2 geänderte Dateien mit 39 neuen und 35 gelöschten Zeilen

Datei anzeigen

@ -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<String, String> 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) { }
}
}

Datei anzeigen

@ -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<String, String> 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<String, String> 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);
}
}