diff --git a/api/src/main/java/zone/themcgamer/api/API.java b/api/src/main/java/zone/themcgamer/api/API.java index 2060d3a..b01a4fc 100644 --- a/api/src/main/java/zone/themcgamer/api/API.java +++ b/api/src/main/java/zone/themcgamer/api/API.java @@ -36,7 +36,7 @@ public class API { .create(); private static final List> models = new ArrayList<>(); private static final Map> routes = new HashMap<>(); - private static final boolean requiresAuthentication = false; + private static final boolean requiresAuthentication = true; // Rate Limiting private static final int rateLimit = 120; // The amount of requests a minute @@ -114,15 +114,21 @@ public class API { // if there is a problem checking the key or the key is invalid if (requiresAuthentication) { String apiKey = request.headers("key"); - if (apiKey == null) + if (apiKey == null) { + response.status(401); throw new APIException("Unauthorized"); + } // Check if the provided API key is valid Optional> keys = apiKeyRepository.lookup(apiKey); - if (keys.isEmpty() || (keys.get().isEmpty())) + if (keys.isEmpty() || (keys.get().isEmpty())) { + response.status(401); throw new APIException("Unauthorized"); + } key = keys.get().get(0); - if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel())) + if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel())) { + response.status(401); throw new APIException("Unauthorized"); + } } // Checking if the request has the appropriate headers for the get route if (restPath.headers().length > 0) { @@ -160,9 +166,11 @@ public class API { jsonObject.addProperty("error", message); // If the caught exception is not an API exception, print the stacktrace if (!(ex instanceof APIException)) { + response.status(500); System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:"); ex.printStackTrace(); - } + } else if (response.status() == 200) + response.status(502); } return gson.toJson(jsonObject); }); diff --git a/api/src/main/java/zone/themcgamer/api/route/AccountRoute.java b/api/src/main/java/zone/themcgamer/api/route/AccountRoute.java index 5f63467..98c8393 100644 --- a/api/src/main/java/zone/themcgamer/api/route/AccountRoute.java +++ b/api/src/main/java/zone/themcgamer/api/route/AccountRoute.java @@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit; public class AccountRoute { // Account model cache for players that were looked up via the account route public static final Cache CACHE = CacheBuilder.newBuilder() - .expireAfterWrite(10, TimeUnit.MINUTES) + .expireAfterWrite(5, TimeUnit.MINUTES) .build(); private final AccountRepository accountRepository; @@ -42,6 +42,8 @@ public class AccountRoute { AccountModel account = CACHE.getIfPresent(uuid); if (account == null) { account = accountRepository.getAccount(uuid); + if (account == null) + throw new APIException("Account not found"); account.setTimeCached(System.currentTimeMillis()); CACHE.put(uuid, account); } diff --git a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManReward.java b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManReward.java index 08fef3b..cb75ddc 100644 --- a/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManReward.java +++ b/core/src/main/java/zone/themcgamer/core/deliveryMan/DeliveryManReward.java @@ -2,7 +2,6 @@ package zone.themcgamer.core.deliveryMan; import lombok.AllArgsConstructor; import lombok.Getter; -import zone.themcgamer.core.common.SkullTexture; import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage; import zone.themcgamer.core.deliveryMan.rewardPackage.impl.DailyRewardPackage; import zone.themcgamer.core.deliveryMan.rewardPackage.impl.MonthlyRewardPackage; diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java index b5b9f3e..3df10aa 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java @@ -2,7 +2,6 @@ package zone.themcgamer.discordbot.command.impl; import com.jagrosh.jdautilities.command.CommandEvent; import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.TextChannel; import zone.themcgamer.discordbot.command.BaseCommand; diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java index bb5ff97..2e62294 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java @@ -3,7 +3,6 @@ package zone.themcgamer.discordbot.command.impl; import com.jagrosh.jdautilities.command.CommandEvent; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Activity; -import zone.themcgamer.discordbot.BotConstants; import zone.themcgamer.discordbot.MGZBot; import zone.themcgamer.discordbot.command.BaseCommand; import zone.themcgamer.discordbot.guild.Guild; diff --git a/hub/src/main/java/zone/themcgamer/hub/command/SpawnCommand.java b/hub/src/main/java/zone/themcgamer/hub/command/SpawnCommand.java index 9bbd084..6b5b6c8 100644 --- a/hub/src/main/java/zone/themcgamer/hub/command/SpawnCommand.java +++ b/hub/src/main/java/zone/themcgamer/hub/command/SpawnCommand.java @@ -13,4 +13,4 @@ public class SpawnCommand { public void onCommand(CommandProvider command) { command.getPlayer().teleport(hub.getSpawn()); } -} +} \ No newline at end of file diff --git a/testing/src/main/java/zone/themcgamer/test/Test.java b/testing/src/main/java/zone/themcgamer/test/Test.java index c66ee1e..2cea016 100644 --- a/testing/src/main/java/zone/themcgamer/test/Test.java +++ b/testing/src/main/java/zone/themcgamer/test/Test.java @@ -1,13 +1,12 @@ package zone.themcgamer.test; import lombok.SneakyThrows; -import zone.themcgamer.data.jedis.JedisController; -import zone.themcgamer.data.jedis.data.ServerGroup; -import zone.themcgamer.data.jedis.repository.RedisRepository; -import zone.themcgamer.data.jedis.repository.impl.ServerGroupRepository; -import java.util.List; -import java.util.Optional; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; /** * @author Braydon @@ -16,15 +15,34 @@ import java.util.Optional; public class Test { @SneakyThrows public static void main(String[] args) { - new JedisController().start(); - - Optional repository = RedisRepository.getRepository(ServerGroupRepository.class); - while (repository.isPresent()) { - List cached = repository.get().getCached(); - for (ServerGroup serverGroup : cached) - System.out.println(serverGroup.toString()); - System.out.println("total cached = " + cached.size()); - Thread.sleep(1000L); - } + HttpClient client = HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.NORMAL) + .connectTimeout(Duration.ofSeconds(30L)) + .build(); + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create("https://api.cnetwork.club/v1/status/Rainnny")) + .timeout(Duration.ofMinutes(1L)) + .header("Content-Type", "application/json") + .build(); + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenAccept(response -> { + System.out.println("status = " + response.statusCode()); + System.out.println("body = " + response.body()); + }); +// HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); +// System.out.println("status = " + response.statusCode()); +// System.out.println("body = " + response.body()); +// +// Optional repository = RedisRepository.getRepository(ServerGroupRepository.class); +// while (repository.isPresent()) { +// List cached = repository.get().getCached(); +// for (ServerGroup serverGroup : cached) +// System.out.println(serverGroup.toString()); +// System.out.println("total cached = " + cached.size()); +// Thread.sleep(1000L); +// } + Thread.sleep(2000L); } } \ No newline at end of file