lazily fix the yfd chomens bot crash exploit

This commit is contained in:
Chayapak 2023-06-06 16:23:54 +07:00
parent c75de26c38
commit 7cb3164a3c
3 changed files with 83 additions and 70 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot/commands

View file

@ -53,39 +53,45 @@ public class TranslateCommand implements Command {
final Gson gson = new Gson();
try {
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
new Thread(() -> {
try {
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
final String jsonOutput = HttpUtilities.postRequest(
url,
"application/x-www-form-urlencoded;charset=utf-8",
String.format(
"sl=%s&tl=%s&q=%s",
from,
to,
URLEncoder.encode(
message,
StandardCharsets.UTF_8
final String jsonOutput = HttpUtilities.postRequest(
url,
"application/x-www-form-urlencoded;charset=utf-8",
String.format(
"sl=%s&tl=%s&q=%s",
from,
to,
URLEncoder.encode(
message,
StandardCharsets.UTF_8
)
)
);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonArray sentences = jsonObject.getAsJsonArray("sentences");
final JsonObject translation = sentences.get(0).getAsJsonObject();
final String output = translation.get("trans").getAsString();
context.sendOutput(
Component
.translatable(
"Result: %s",
Component.text(output).color(NamedTextColor.GREEN)
)
)
);
.color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()))
);
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
}).start();
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonArray sentences = jsonObject.getAsJsonArray("sentences");
final JsonObject translation = sentences.get(0).getAsJsonObject();
final String output = translation.get("trans").getAsString();
return Component
.translatable(
"Result: %s",
Component.text(output).color(NamedTextColor.GREEN)
)
.color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()));
} catch (Exception e) {
return Component.text(e.toString()).color(NamedTextColor.RED);
}
return null;
}
}

View file

@ -46,38 +46,40 @@ public class UrbanCommand implements Command {
final Gson gson = new Gson();
try {
final URL url = new URL(
"https://api.urbandictionary.com/v0/define?term=" +
URLEncoder.encode(term, StandardCharsets.UTF_8)
);
new Thread(() -> {
try {
final URL url = new URL(
"https://api.urbandictionary.com/v0/define?term=" +
URLEncoder.encode(term, StandardCharsets.UTF_8)
);
final String jsonOutput = HttpUtilities.getRequest(url);
final String jsonOutput = HttpUtilities.getRequest(url);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonArray list = jsonObject.getAsJsonArray("list");
final JsonArray list = jsonObject.getAsJsonArray("list");
if (list.size() == 0) return Component.text("No results found").color(NamedTextColor.RED);
if (list.size() == 0) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED));
for (JsonElement element : list) {
final JsonObject definitionObject = element.getAsJsonObject();
for (JsonElement element : list) {
final JsonObject definitionObject = element.getAsJsonObject();
final String word = definitionObject.get("word").getAsString();
final String definition = definitionObject.get("definition").getAsString();
final String word = definitionObject.get("word").getAsString();
final String definition = definitionObject.get("definition").getAsString();
final Component component = Component.translatable(
"[%s] %s - %s",
Component.text("Urban").color(NamedTextColor.RED),
Component.text(word).color(NamedTextColor.GRAY),
Component.text(definition).color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
final Component component = Component.translatable(
"[%s] %s - %s",
Component.text("Urban").color(NamedTextColor.RED),
Component.text(word).color(NamedTextColor.GRAY),
Component.text(definition).color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
context.sendOutput(component);
context.sendOutput(component);
}
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
} catch (Exception e) {
return Component.text(e.toString()).color(NamedTextColor.RED);
}
}).start();
return null;
}

View file

@ -45,24 +45,29 @@ public class WikipediaCommand implements Command {
final Gson gson = new Gson();
try {
final URL url = new URL(
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
URLEncoder.encode(
page.replace(" ", "_"), // mabe.
StandardCharsets.UTF_8
)
);
// TODO: mabe use the executor service thingy instead of threads because threads are b a d
new Thread(() -> {
try {
final URL url = new URL(
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
URLEncoder.encode(
page.replace(" ", "_"), // mabe.
StandardCharsets.UTF_8
)
);
final String jsonOutput = HttpUtilities.getRequest(url);
final String jsonOutput = HttpUtilities.getRequest(url);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
return Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN);
} catch (FileNotFoundException ignored) {
return Component.text("Cannot find page: " + page).color(NamedTextColor.RED);
} catch (Exception e) {
return Component.text(e.toString()).color(NamedTextColor.RED);
}
context.sendOutput(Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN));
} catch (FileNotFoundException ignored) {
context.sendOutput(Component.text("Cannot find page: " + page).color(NamedTextColor.RED));
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}
}).start();
return null;
}
}