Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots of linting #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified gradlew
100644 → 100755
Empty file.
30 changes: 17 additions & 13 deletions src/main/java/com/xpdustry/claj/server/Blacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@

public class Blacklist {

public static final String actionsURL = "https://api.github.com/meta";
public static final Seq<String> ips = new Seq<>();

public static void refresh() {
Http.get(actionsURL, result -> {
var json = Jval.read(result.getResultAsString());
json.get("actions").asArray().each(element -> {
String ip = element.asString();
private static final String ACTIONS_URL = "https://api.github.com/meta";
private static final Seq<String> ips = new Seq<>();

private Blacklist() {
// no-op
}

static void refresh() {
Http.get(ACTIONS_URL, (final Http.HttpResponse result) -> {
final var json = Jval.read(result.getResultAsString());
json.get("actions").asArray().each((final Jval element) -> {
final String ip = element.asString();
if (ip.charAt(4) != ':') ips.add(ip); // skip IPv6
});

Log.info("Added @ GitHub Actions IPs to blacklist.", ips.size);
}, error -> Log.err("Failed to fetch GitHub Actions IPs", error));
}, (final Throwable error) -> Log.err("Failed to fetch GitHub Actions IPs", error));
}

public static void add(String ip) {
public static void add(final String ip) {
ips.add(ip);
}

public static boolean contains(String ip) {
static boolean contains(final String ip) {
return ips.contains(ip);
}

public static void remove(String ip) {
static void remove(final String ip) {
ips.remove(ip);
}

public static void clear() {
static void clear() {
ips.clear();
}
}
42 changes: 22 additions & 20 deletions src/main/java/com/xpdustry/claj/server/Control.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.xpdustry.claj.server;

import arc.struct.IntMap;
import arc.util.CommandHandler;
import arc.util.CommandHandler.Command;
import arc.util.CommandHandler.CommandResponse;
import arc.util.CommandHandler.ResponseType;
import arc.util.Log;
Expand All @@ -9,52 +11,52 @@

import java.util.Scanner;

public class Control {
class Control {

public final CommandHandler handler = new CommandHandler("");
public final Distributor distributor;
private final CommandHandler handler = new CommandHandler("");
private final Distributor distributor;

public Control(Distributor distributor) {
Control(final Distributor distributor) {
this.distributor = distributor;
this.registerCommands();
registerCommands();

Threads.daemon("Application Control", () -> {
try (Scanner scanner = new Scanner(System.in)) {
try (final Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNext()) handleCommand(scanner.nextLine());
}
});
}

private void handleCommand(String command) {
CommandResponse response = handler.handleMessage(command);
private void handleCommand(final String command) {
final CommandResponse response = handler.handleMessage(command);

if (response.type == ResponseType.unknownCommand) {
String closest = handler.getCommandList().map(cmd -> cmd.text).min(cmd -> Strings.levenshtein(cmd, command));
final String closest = handler.getCommandList().map((Command cmd) -> cmd.text).min((String cmd) -> Strings.levenshtein(cmd, command));
Log.err("Command not found. Did you mean @?", closest);
} else if (response.type != ResponseType.noCommand && response.type != ResponseType.valid)
Log.err("Too @ command arguments.", response.type == ResponseType.fewArguments ? "few" : "many");
}

private void registerCommands() {
handler.register("help", "Display the command list.", args -> {
handler.register("help", "Display the command list.", (final String[] args) -> {
Log.info("Commands:");
handler.getCommandList().each(command -> Log.info(" &b&lb@@&fr - @",
handler.getCommandList().each((Command command) -> Log.info(" &b&lb@@&fr - @",
command.text, command.paramText.isEmpty() ? "" : " &lc&fi" + command.paramText, command.description));
});

handler.register("list", "Displays all current rooms.", args -> {
handler.register("list", "Displays all current rooms.", (final String[] args) -> {
Log.info("Rooms:");
distributor.rooms.forEach(entry -> {
distributor.rooms.forEach((final IntMap.Entry<Room> entry) -> {
Log.info(" &b&lbRoom @&fr", entry.value.link);
entry.value.redirectors.each(r -> {
entry.value.redirectors.each((final Redirector r) -> {
Log.info(" [H] &b&lbConnection @&fr - @", r.host.getID(), Main.getIP(r.host));
if (r.client == null) return;
Log.info(" [C] &b&lbConnection @&fr - @", r.client.getID(), Main.getIP(r.client));
});
});
});

handler.register("limit", "[amount]", "Sets spam packet limit.", args -> {
handler.register("limit", "[amount]", "Sets spam packet limit.", (final String[] args) -> {
if (args.length == 0)
Log.info("Current limit - @ packets per 3 seconds.", distributor.spamLimit);
else {
Expand All @@ -63,23 +65,23 @@ private void registerCommands() {
}
});

handler.register("ban", "<IP>", "Adds the IP to blacklist.", args -> {
handler.register("ban", "<IP>", "Adds the IP to blacklist.", (final String[] args) -> {
Blacklist.add(args[0]);
Log.info("IP @ has been blacklisted.", args[0]);
});

handler.register("unban", "<IP>", "Removes the IP from blacklist.", args -> {
handler.register("unban", "<IP>", "Removes the IP from blacklist.", (final String[] args) -> {
Blacklist.remove(args[0]);
Log.info("IP @ has been removed from blacklist.", args[0]);
});

handler.register("refresh", "Unbans all IPs and refresh GitHub Actions IPs.", args -> {
handler.register("refresh", "Unbans all IPs and refresh GitHub Actions IPs.", (final String[] args) -> {
Blacklist.clear();
Blacklist.refresh();
});

handler.register("exit", "Stop hosting distributor and exit the application.", args -> {
distributor.rooms.forEach(entry -> entry.value.sendMessage("[scarlet]\u26A0[] The server is shutting down.\nTry to reconnect in a minute."));
handler.register("exit", "Stop hosting distributor and exit the application.", (final String[] args) -> {
distributor.rooms.forEach((IntMap.Entry<Room> entry) -> entry.value.sendMessage("[scarlet][] The server is shutting down.\nTry to reconnect in a minute."));

Log.info("Shutting down the application.");
distributor.stop();
Expand Down
Loading