mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-11-28 08:44:07 +00:00
log upload commands fixed - upload to mclo.gs website
This commit is contained in:
1 parent
97ee43e434
commit
04309fba14
7 files changed
+134
-108
No files matched your search
+6
-14
@@ -13,9 +13,6 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.player.commands.RTPCommand;
|
||||
@@ -147,27 +144,22 @@ public class CmdInfo implements RTPCommand, RTPCommandHelpable {
|
||||
if (!upload) {
|
||||
sendi.sendMessage(list.toArray(new String[0]));
|
||||
if (sendi instanceof Player) {
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload command log to &flogs.ronanplugins.com"));
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload command log to &fmclo.gs"));
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, cmd + " _UPLOAD_"));
|
||||
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Message.color("&6Suggested command&f: &7" + "/betterrtp " + String.join(" ", args) + " _UPLOAD_")).create()));
|
||||
((Player) sendi).spigot().sendMessage(component);
|
||||
} else {
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload command log to https://logs.ronanplugins.com");
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload command log to https://mclo.gs");
|
||||
}
|
||||
} else {
|
||||
list.add(0, "Command: " + cmd);
|
||||
list.forEach(str -> list.set(list.indexOf(str), ChatColor.stripColor(str)));
|
||||
CompletableFuture.runAsync(() -> {
|
||||
String key = LogUploader.post(list);
|
||||
if (key == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cAn error occured attempting to upload log!")), null);
|
||||
String url = LogUploader.post(list);
|
||||
if (url == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cCould not upload the log. Check your connection and ensure the log is under 5 MiB, then try again.")), null);
|
||||
} else {
|
||||
try {
|
||||
JSONObject json = (JSONObject) new JSONParser().parse(key);
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6https://logs.ronanplugins.com/" + json.get("key")), null);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6" + url), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+6
-14
@@ -13,9 +13,6 @@ import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -36,25 +33,20 @@ public class CmdLogger implements RTPCommand {
|
||||
String cmd = "/" + label + " " + String.join(" ", args);
|
||||
if (!upload) {
|
||||
if (sendi instanceof Player) {
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload log to &flogs.ronanplugins.com"));
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload log to &fmclo.gs"));
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, cmd + " _UPLOAD_"));
|
||||
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Message.color("&6Suggested command&f: &7" + "/betterrtp " + String.join(" ", args) + " _UPLOAD_")).create()));
|
||||
((Player) sendi).spigot().sendMessage(component);
|
||||
} else {
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload log to https://logs.ronanplugins.com");
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload log to https://mclo.gs");
|
||||
}
|
||||
} else {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
String key = LogUploader.post(BetterRTP.getInstance().getRtpLogger().getFile());
|
||||
if (key == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cAn error occured attempting to upload log!")), null);
|
||||
String url = LogUploader.post(BetterRTP.getInstance().getRtpLogger().getFile());
|
||||
if (url == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cCould not upload the log. Check your connection and ensure the log is under 5 MiB, then try again.")), null);
|
||||
} else {
|
||||
try {
|
||||
JSONObject json = (JSONObject) new JSONParser().parse(key);
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6https://logs.ronanplugins.com/" + json.get("key")), null);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6" + url), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+6
-14
@@ -22,9 +22,6 @@ import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -59,27 +56,22 @@ public class CmdQueue implements RTPCommand {
|
||||
if (!upload) {
|
||||
sendi.sendMessage(list.toArray(new String[0]));
|
||||
if (sendi instanceof Player) {
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload command log to &flogs.ronanplugins.com"));
|
||||
TextComponent component = new TextComponent(Message.color("&7- &7Click to upload command log to &fmclo.gs"));
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, cmd + " _UPLOAD_"));
|
||||
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Message.color("&6Suggested command&f: &7" + "/betterrtp " + String.join(" ", args) + " _UPLOAD_")).create()));
|
||||
((Player) sendi).spigot().sendMessage(component);
|
||||
} else {
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload command log to https://logs.ronanplugins.com");
|
||||
sendi.sendMessage("Execute `" + cmd + " _UPLOAD_`" + " to upload command log to https://mclo.gs");
|
||||
}
|
||||
} else {
|
||||
list.add(0, "Command: " + cmd);
|
||||
list.forEach(str -> list.set(list.indexOf(str), ChatColor.stripColor(str)));
|
||||
CompletableFuture.runAsync(() -> {
|
||||
String key = LogUploader.post(list);
|
||||
if (key == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cAn error occured attempting to upload log!")), null);
|
||||
String url = LogUploader.post(list);
|
||||
if (url == null) {
|
||||
Message.sms(sendi, new ArrayList<>(Collections.singletonList("&cCould not upload the log. Check your connection and ensure the log is under 5 MiB, then try again.")), null);
|
||||
} else {
|
||||
try {
|
||||
JSONObject json = (JSONObject) new JSONParser().parse(key);
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6https://logs.ronanplugins.com/" + json.get("key")), null);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Message.sms(sendi, Arrays.asList(" ", Message.getPrefix(Message_RTP.msg) + "&aLog uploaded! &fView&7: &6" + url), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+86
-65
@@ -1,88 +1,109 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.web;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Map;
|
||||
|
||||
public class LogUploader {
|
||||
public static final String KEY_URL = "https://mclo.gs/";
|
||||
private static final String UPLOAD_URL = "https://api.mclo.gs/1/log";
|
||||
private static final int MAX_LOG_BYTES = 5 * 1024 * 1024;
|
||||
|
||||
private static final String UPLOAD_URL = "https://logs.ronanplugins.com/documents";
|
||||
public static final String KEY_URL = "https://logs.ronanplugins.com/";
|
||||
|
||||
/** Returns the validated viewing URL, or null when the upload fails. */
|
||||
@Nullable
|
||||
public static String post(List<String> requestBody) {
|
||||
try {
|
||||
URL url = new URL(UPLOAD_URL);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "text/plain");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
//String requestBody = "This is the raw body of text.";
|
||||
|
||||
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||
for (String str : requestBody) {
|
||||
byte[] input = (str + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
|
||||
outputStream.write(input, 0, input.length);
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder response = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
return response.toString();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
public static String post(List<String> lines) {
|
||||
ByteArrayOutputStream content = new ByteArrayOutputStream();
|
||||
for (String line : lines) {
|
||||
byte[] bytes = (line + "\n").getBytes(StandardCharsets.UTF_8);
|
||||
if (content.size() + bytes.length > MAX_LOG_BYTES) return null;
|
||||
content.write(bytes, 0, bytes.length);
|
||||
}
|
||||
return upload(new String(content.toByteArray(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String post(File file) {
|
||||
// Create the connection to the server
|
||||
try {
|
||||
URL url = new URL(UPLOAD_URL);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "text/yaml");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
try (OutputStream outputStream = connection.getOutputStream()) {
|
||||
Scanner scan = new Scanner(file);
|
||||
while(scan.hasNextLine()){
|
||||
String str = scan.nextLine();
|
||||
byte[] input = (str + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
|
||||
outputStream.write(input, 0, input.length);
|
||||
}
|
||||
scan.close();
|
||||
}
|
||||
|
||||
StringBuilder response = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
return response.toString();
|
||||
if (file == null || !file.isFile() || file.length() > MAX_LOG_BYTES) return null;
|
||||
try (InputStream input = new FileInputStream(file)) {
|
||||
return upload(readBounded(input, MAX_LOG_BYTES));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
//getLogger().log(Level.INFO, "Response: " + response.toString());
|
||||
}
|
||||
|
||||
private static String readBounded(InputStream input, int limit) throws IOException {
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[8192];
|
||||
int count;
|
||||
while ((count = input.read(buffer)) != -1) {
|
||||
if (result.size() + count > limit) throw new IOException("Upload size limit exceeded");
|
||||
result.write(buffer, 0, count);
|
||||
}
|
||||
return new String(result.toByteArray(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String upload(String content) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = (HttpURLConnection) new URL(UPLOAD_URL).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setConnectTimeout(10000);
|
||||
connection.setReadTimeout(15000);
|
||||
connection.setInstanceFollowRedirects(false);
|
||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
byte[] body = requestBody(content);
|
||||
connection.setFixedLengthStreamingMode(body.length);
|
||||
try (OutputStream output = connection.getOutputStream()) {
|
||||
output.write(body);
|
||||
}
|
||||
int status = connection.getResponseCode();
|
||||
if (status < 200 || status >= 300) return null;
|
||||
try (InputStream input = connection.getInputStream()) {
|
||||
return viewingUrl(readBounded(input, 64 * 1024));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
if (connection != null) connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
static byte[] requestBody(String content) {
|
||||
Map<String, String> request = new LinkedHashMap<>();
|
||||
request.put("content", content);
|
||||
request.put("source", "BetterRTP");
|
||||
return JSONObject.toJSONString(request).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static String viewingUrl(String response) {
|
||||
try {
|
||||
Object parsed = new JSONParser().parse(response);
|
||||
if (!(parsed instanceof JSONObject)) return null;
|
||||
JSONObject json = (JSONObject) parsed;
|
||||
if (!Boolean.TRUE.equals(json.get("success")) || !(json.get("url") instanceof String)) return null;
|
||||
String url = (String) json.get("url");
|
||||
// Only show links to the mclo.gs log viewer; never render arbitrary API response text in chat.
|
||||
return url.matches("https://mclo\\.gs/[A-Za-z0-9_-]+") ? url : null;
|
||||
} catch (ParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,12 @@ Sync validates config.yml, saves a backup in `editor-backups`, then replaces the
|
||||
|
||||
Shared behavior, deployment, and verification are documented in the [webapp repository](https://github.com/RonanPlugins/BetterRTP-Webapp/blob/main/docs/web-editor.md).
|
||||
|
||||
## Support log uploads
|
||||
|
||||
The logger, info, and queue upload actions share logs through `https://mclo.gs`. Uploads are manual and use the mclogs JSON API at `https://api.mclo.gs/1/log`. Files larger than 5 MiB are rejected locally. Uploads have connection/read timeouts, and failed or malformed responses show an upload error instead of a broken link.
|
||||
|
||||
No self-hosted log service, DNS setup, account pairing, or API key is required. Logs are uploaded to the third-party mclo.gs service only when the administrator chooses the upload action. Server owners need an updated BetterRTP JAR to use this endpoint.
|
||||
|
||||
## Where's the Lang files?/Want to Contribute translating?
|
||||
All language files are located [here](BetterRTP-Core/src/main/resources/lang)
|
||||
feel free to fork one of the language files and help translate!
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<properties><bettersync.version>2.0.0</bettersync.version><maven.compiler.release>8</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
|
||||
<repositories><repository><id>spigot</id><url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url></repository><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository></repositories>
|
||||
<dependencies>
|
||||
<dependency><groupId>org.jetbrains</groupId><artifactId>annotations</artifactId><version>24.0.1</version><scope>provided</scope></dependency>
|
||||
<dependency><groupId>com.github.RonanPlugins</groupId><artifactId>BetterSync</artifactId><version>${bettersync.version}</version></dependency>
|
||||
<dependency><groupId>org.spigotmc</groupId><artifactId>spigot-api</artifactId><version>1.8.8-R0.1-SNAPSHOT</version><exclusions><exclusion><groupId>net.md-5</groupId><artifactId>bungeecord-chat</artifactId></exclusion></exclusions></dependency>
|
||||
<dependency><groupId>net.md-5</groupId><artifactId>bungeecord-chat</artifactId><version>1.16-R0.4</version></dependency>
|
||||
@@ -12,7 +13,7 @@
|
||||
<build>
|
||||
<sourceDirectory>../../BetterRTP-Core/src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version><configuration><includes><include>**/web/editor/*.java</include></includes></configuration></plugin>
|
||||
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version><configuration><includes><include>**/web/editor/*.java</include><include>**/web/LogUploader.java</include></includes></configuration></plugin>
|
||||
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.3</version></plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package me.SuperRonanCraft.BetterRTP.references.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class LogUploaderTest {
|
||||
@Test public void encodesLogContentWithoutLosingFormatting() throws Exception {
|
||||
String content = "Quotes: \"hello\"\nUnicode: \u00e9 \u2603\nPath: C:\\server\\logs";
|
||||
JSONObject body = (JSONObject) new JSONParser().parse(new String(LogUploader.requestBody(content), StandardCharsets.UTF_8));
|
||||
assertEquals(content, body.get("content"));
|
||||
assertEquals("BetterRTP", body.get("source"));
|
||||
}
|
||||
@Test public void acceptsSuccessfulViewerLinksOnly() {
|
||||
assertEquals("https://mclo.gs/abc123", LogUploader.viewingUrl("{\"success\":true,\"url\":\"https://mclo.gs/abc123\"}"));
|
||||
for (String response : new String[] {"null", "[]", "not json", "{\"key\":\"legacy\"}", "{\"success\":false,\"url\":\"https://mclo.gs/abc\"}", "{\"success\":true,\"url\":\"https://evil.example/abc\"}", "{\"success\":true,\"url\":42}"}) {
|
||||
assertNull(response, LogUploader.viewingUrl(response));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user