From 6900ae7a8d376fe18acb0a869f23250a59228fdf Mon Sep 17 00:00:00 2001 From: NovaFox161 Date: Wed, 12 Jun 2019 11:36:34 -0500 Subject: [PATCH] Handle some additional settings. --- .../discal/client/DisCalClient.java | 2 +- .../discal/client/conf/ServletConfig.java | 15 +++++++++++++++ .../listeners/discal/PubSubListener.java | 19 ++++++++++--------- .../client/module/command/DevCommand.java | 11 ++++++----- .../client/service/KeepAliveHandler.java | 3 ++- .../discal/core/object/BotSettings.java | 3 ++- .../discal/server/DisCalServer.java | 2 +- .../discal/server/conf/ServletConfig.java | 15 +++++++++++++++ .../server/handler/DashboardHandler.java | 4 ++-- .../server/handler/DiscordAccountHandler.java | 4 ++-- .../server/listeners/PubSubListener.java | 3 ++- .../network/discord/DiscordLoginHandler.java | 2 +- 12 files changed, 59 insertions(+), 24 deletions(-) diff --git a/client/src/main/java/org/dreamexposure/discal/client/DisCalClient.java b/client/src/main/java/org/dreamexposure/discal/client/DisCalClient.java index 0f1c8a8d..4e3c9ee1 100644 --- a/client/src/main/java/org/dreamexposure/discal/client/DisCalClient.java +++ b/client/src/main/java/org/dreamexposure/discal/client/DisCalClient.java @@ -90,7 +90,7 @@ public class DisCalClient { //Start Redis pub/sub listeners PubSubManager.get().init(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()), "N/a", BotSettings.REDIS_PASSWORD.get()); //We must register each channel we want to use. This is super important. - PubSubManager.get().register(clientId(), "DisCal/ToClient/All"); + PubSubManager.get().register(clientId(), BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All"); //Login client.login().block(); diff --git a/client/src/main/java/org/dreamexposure/discal/client/conf/ServletConfig.java b/client/src/main/java/org/dreamexposure/discal/client/conf/ServletConfig.java index afb1fea6..bad0c145 100644 --- a/client/src/main/java/org/dreamexposure/discal/client/conf/ServletConfig.java +++ b/client/src/main/java/org/dreamexposure/discal/client/conf/ServletConfig.java @@ -5,8 +5,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; +import org.springframework.web.servlet.config.annotation.CorsRegistration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableAutoConfiguration @@ -17,4 +21,15 @@ public class ServletConfig implements factory.setPort(Integer.valueOf(BotSettings.PORT.get())); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/")); } + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + CorsRegistration reg = registry.addMapping("/api/**"); + reg.allowedOrigins("*"); + } + }; + } } \ No newline at end of file diff --git a/client/src/main/java/org/dreamexposure/discal/client/listeners/discal/PubSubListener.java b/client/src/main/java/org/dreamexposure/discal/client/listeners/discal/PubSubListener.java index a92caaf1..858757f4 100644 --- a/client/src/main/java/org/dreamexposure/discal/client/listeners/discal/PubSubListener.java +++ b/client/src/main/java/org/dreamexposure/discal/client/listeners/discal/PubSubListener.java @@ -9,6 +9,7 @@ import org.dreamexposure.discal.core.database.DatabaseManager; import org.dreamexposure.discal.core.enums.network.DisCalRealm; import org.dreamexposure.discal.core.enums.network.PubSubReason; import org.dreamexposure.discal.core.logger.Logger; +import org.dreamexposure.discal.core.object.BotSettings; import org.dreamexposure.discal.core.object.GuildSettings; import org.dreamexposure.novautils.events.network.pubsub.PubSubReceiveEvent; @@ -26,17 +27,17 @@ import java.util.Optional; public class PubSubListener { @Subscribe public void handle(PubSubReceiveEvent event) { - Optional g = Optional.empty(); - //Check if this even applies to us! - if (event.getData().has("Guild-Id")) { - g = GuildFinder.findGuild(Snowflake.of(event.getData().getString("Guild-Id"))); - if (!g.isPresent()) return; //Guild not connected to this client, correct client will handle this. - } + if (event.getChannelName().equalsIgnoreCase(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All")) { + Optional g = Optional.empty(); + //Check if this even applies to us! + if (event.getData().has("Guild-Id")) { + g = GuildFinder.findGuild(Snowflake.of(event.getData().getString("Guild-Id"))); + if (!g.isPresent()) return; //Guild not connected to this client, correct client will handle this. + } - PubSubReason reason = PubSubReason.valueOf(event.getData().getString("Reason")); - DisCalRealm realm = DisCalRealm.valueOf(event.getData().getString("Realm")); + PubSubReason reason = PubSubReason.valueOf(event.getData().getString("Reason")); + DisCalRealm realm = DisCalRealm.valueOf(event.getData().getString("Realm")); - if (event.getChannelName().equalsIgnoreCase("DisCal/ToClient/All")) { switch (reason) { case UPDATE: if (realm.equals(DisCalRealm.BOT_SETTINGS)) { diff --git a/client/src/main/java/org/dreamexposure/discal/client/module/command/DevCommand.java b/client/src/main/java/org/dreamexposure/discal/client/module/command/DevCommand.java index 6d27799c..9859f5bd 100644 --- a/client/src/main/java/org/dreamexposure/discal/client/module/command/DevCommand.java +++ b/client/src/main/java/org/dreamexposure/discal/client/module/command/DevCommand.java @@ -14,6 +14,7 @@ import org.dreamexposure.discal.core.crypto.KeyGenerator; import org.dreamexposure.discal.core.database.DatabaseManager; import org.dreamexposure.discal.core.enums.network.DisCalRealm; import org.dreamexposure.discal.core.enums.network.PubSubReason; +import org.dreamexposure.discal.core.object.BotSettings; import org.dreamexposure.discal.core.object.GuildSettings; import org.dreamexposure.discal.core.object.command.CommandInfo; import org.dreamexposure.discal.core.object.web.UserAPIAccount; @@ -159,7 +160,7 @@ public class DevCommand implements ICommand { request.put("Realm", DisCalRealm.GUILD_IS_PATRON); request.put("Guild-Id", args[1]); - PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request); MessageManager.sendMessageAsync("DisCal will update the isPatron status of the guild (if connected). Please allow some time for this to propagate across the network!", event); } else { @@ -236,7 +237,7 @@ public class DevCommand implements ICommand { request.put("Realm", DisCalRealm.GUILD_IS_DEV); request.put("Guild-Id", args[1]); - PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request); MessageManager.sendMessageAsync("DisCal will update the isDevGuild status of the guild (if connected). Please allow some time for this to propagate across the network!", event); } else { @@ -275,7 +276,7 @@ public class DevCommand implements ICommand { request.put("Guild-Id", args[1]); request.put("Max-Calendars", mc); - PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request); MessageManager.sendMessageAsync("DisCal will update the max calendar limit of the specified guild (if connected). Please allow some time for this to propagate across the network!", event); } catch (NumberFormatException e) { @@ -311,7 +312,7 @@ public class DevCommand implements ICommand { request.put("Realm", DisCalRealm.GUILD_LEAVE); request.put("Guild-Id", args[1]); - PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request); MessageManager.sendMessageAsync("DisCal will leave the specified guild (if connected). Please allow some time for this to propagate across the network!", event); } else { @@ -328,7 +329,7 @@ public class DevCommand implements ICommand { request.put("Reason", PubSubReason.HANDLE.name()); request.put("Realm", DisCalRealm.BOT_LANGS); - PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request); MessageManager.sendMessageAsync("Reloading lang files! Please give this time to propagate across the network.", event); } diff --git a/client/src/main/java/org/dreamexposure/discal/client/service/KeepAliveHandler.java b/client/src/main/java/org/dreamexposure/discal/client/service/KeepAliveHandler.java index 76f004f4..07ac938c 100644 --- a/client/src/main/java/org/dreamexposure/discal/client/service/KeepAliveHandler.java +++ b/client/src/main/java/org/dreamexposure/discal/client/service/KeepAliveHandler.java @@ -2,6 +2,7 @@ package org.dreamexposure.discal.client.service; import org.dreamexposure.discal.client.DisCalClient; import org.dreamexposure.discal.core.logger.Logger; +import org.dreamexposure.discal.core.object.BotSettings; import org.dreamexposure.novautils.network.pubsub.PubSubManager; import org.joda.time.Interval; import org.joda.time.Period; @@ -33,7 +34,7 @@ public class KeepAliveHandler { data.put("Uptime", humanReadableUptime()); //TODO: Add announcement count!!! - PubSubManager.get().publish("DisCal/ToServer/KeepAlive", DisCalClient.clientId(), data); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive", DisCalClient.clientId(), data); Logger.getLogger().debug("Sent keep alive to server.", false); } diff --git a/core/src/main/java/org/dreamexposure/discal/core/object/BotSettings.java b/core/src/main/java/org/dreamexposure/discal/core/object/BotSettings.java index 5732eb5e..e4053613 100644 --- a/core/src/main/java/org/dreamexposure/discal/core/object/BotSettings.java +++ b/core/src/main/java/org/dreamexposure/discal/core/object/BotSettings.java @@ -17,8 +17,9 @@ public enum BotSettings { SQL_DB, SQL_PREFIX, REDIS_PASSWORD, REDIS_HOSTNAME, REDIS_PORT, + PUBSUB_PREFIX, - COM_USER, COM_PASS, + COM_USER, COM_PASS, COM_SUB_DOMAIN, TOKEN, SECRET, ID, diff --git a/server/src/main/java/org/dreamexposure/discal/server/DisCalServer.java b/server/src/main/java/org/dreamexposure/discal/server/DisCalServer.java index dfb9e996..9be7c1c7 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/DisCalServer.java +++ b/server/src/main/java/org/dreamexposure/discal/server/DisCalServer.java @@ -59,7 +59,7 @@ public class DisCalServer { //Start Redis PubSub Listeners PubSubManager.get().init(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()), "N/a", BotSettings.REDIS_PASSWORD.get()); //We must register each channel we want to use. This is super important. - PubSubManager.get().register(-1, "DisCal/ToServer/KeepAlive"); + PubSubManager.get().register(-1, BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive"); //Handle the rest of the bullshit UpdateDisBotData.init(); diff --git a/server/src/main/java/org/dreamexposure/discal/server/conf/ServletConfig.java b/server/src/main/java/org/dreamexposure/discal/server/conf/ServletConfig.java index 9fdd0fff..0b5124e1 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/conf/ServletConfig.java +++ b/server/src/main/java/org/dreamexposure/discal/server/conf/ServletConfig.java @@ -5,8 +5,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; +import org.springframework.web.servlet.config.annotation.CorsRegistration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableAutoConfiguration @@ -17,4 +21,15 @@ public class ServletConfig implements factory.setPort(Integer.valueOf(BotSettings.PORT.get())); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/")); } + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + CorsRegistration reg = registry.addMapping("/api/**"); + reg.allowedOrigins("*"); + } + }; + } } \ No newline at end of file diff --git a/server/src/main/java/org/dreamexposure/discal/server/handler/DashboardHandler.java b/server/src/main/java/org/dreamexposure/discal/server/handler/DashboardHandler.java index eeaf2d64..8fe6ecee 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/handler/DashboardHandler.java +++ b/server/src/main/java/org/dreamexposure/discal/server/handler/DashboardHandler.java @@ -70,7 +70,7 @@ public class DashboardHandler { try { Request httpRequest = new Request.Builder() - .url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/guild") + .url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/guild") .post(httpRequestBody) .header("Content-Type", "application/json") .header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get())) @@ -193,7 +193,7 @@ public class DashboardHandler { data.put("Guild-Id", g.getId()); data.put("Bot-Nick", g.getBotNick()); - PubSubManager.get().publish("DisCal/ToClient/All", -1, data); + PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", -1, data); } } else if (queryParams.containsKey("prefix")) { //Update prefix... diff --git a/server/src/main/java/org/dreamexposure/discal/server/handler/DiscordAccountHandler.java b/server/src/main/java/org/dreamexposure/discal/server/handler/DiscordAccountHandler.java index 5507a680..9cb1c065 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/handler/DiscordAccountHandler.java +++ b/server/src/main/java/org/dreamexposure/discal/server/handler/DiscordAccountHandler.java @@ -125,7 +125,7 @@ public class DiscordAccountHandler { try { Request httpRequest = new Request.Builder() - .url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar") + .url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar") .post(httpRequestBody) .header("Content-Type", "application/json") .header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get())) @@ -185,7 +185,7 @@ public class DiscordAccountHandler { try { Request httpRequest = new Request.Builder() - .url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar") + .url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar") .post(httpRequestBody) .header("Content-Type", "application/json") .header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get())) diff --git a/server/src/main/java/org/dreamexposure/discal/server/listeners/PubSubListener.java b/server/src/main/java/org/dreamexposure/discal/server/listeners/PubSubListener.java index 6c6ce000..0e94405a 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/listeners/PubSubListener.java +++ b/server/src/main/java/org/dreamexposure/discal/server/listeners/PubSubListener.java @@ -2,6 +2,7 @@ package org.dreamexposure.discal.server.listeners; import com.google.common.eventbus.Subscribe; import org.dreamexposure.discal.core.logger.Logger; +import org.dreamexposure.discal.core.object.BotSettings; import org.dreamexposure.discal.core.object.network.discal.ConnectedClient; import org.dreamexposure.discal.server.DisCalServer; import org.dreamexposure.novautils.events.network.pubsub.PubSubReceiveEvent; @@ -19,7 +20,7 @@ public class PubSubListener { @Subscribe public static void handle(PubSubReceiveEvent event) { //Handle keep alive... - if (event.getChannelName().equalsIgnoreCase("DisCal/ToServer/KeepAlive")) { + if (event.getChannelName().equalsIgnoreCase(BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive")) { if (DisCalServer.getNetworkInfo().clientExists(event.getClient())) { //In network, update info... ConnectedClient cc = DisCalServer.getNetworkInfo().getClient(event.getClient()); diff --git a/server/src/main/java/org/dreamexposure/discal/server/network/discord/DiscordLoginHandler.java b/server/src/main/java/org/dreamexposure/discal/server/network/discord/DiscordLoginHandler.java index 95aa3525..68e72dd0 100644 --- a/server/src/main/java/org/dreamexposure/discal/server/network/discord/DiscordLoginHandler.java +++ b/server/src/main/java/org/dreamexposure/discal/server/network/discord/DiscordLoginHandler.java @@ -108,7 +108,7 @@ public class DiscordLoginHandler { try { Request requestNew = new Request.Builder() - .url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/defaults") + .url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/defaults") .post(httpRequestBody) .header("Content-Type", "application/json") .header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get()))