Update to Chroma-Core 1.0

This commit is contained in:
Norbi Peti 2021-08-24 01:21:02 +02:00
parent 9d07c7bf2b
commit 71d1a652b4
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
8 changed files with 37 additions and 40 deletions

View file

@ -119,8 +119,8 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.TBMCPlugins.ButtonCore</groupId> <groupId>com.github.TBMCPlugins.ChromaCore</groupId>
<artifactId>ButtonCore</artifactId> <artifactId>Chroma-Core</artifactId>
<version>${branch}-SNAPSHOT</version> <version>${branch}-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View file

@ -301,7 +301,7 @@ public class AcmeClient {
*/ */
public static void main(String... args) { public static void main(String... args) {
if (args.length == 0) { if (args.length == 0) {
TBMCCoreAPI.SendException("Error while doing ACME!", new Exception("No domains given")); LOG.error("Error while doing ACME!", new Exception("No domains given"));
return; return;
} }

View file

@ -1,7 +1,7 @@
package buttondevteam.website; package buttondevteam.website;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.architecture.ButtonPlugin;
import buttondevteam.website.io.IOHelper; import buttondevteam.website.io.IOHelper;
import buttondevteam.website.page.*; import buttondevteam.website.page.*;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
@ -13,7 +13,6 @@ import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser; import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import javax.net.ssl.*; import javax.net.ssl.*;
import java.io.*; import java.io.*;
@ -29,7 +28,7 @@ import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class ButtonWebsiteModule extends JavaPlugin { public class ButtonWebsiteModule extends ButtonPlugin {
private static HttpsServer server; private static HttpsServer server;
/** /**
* For ACME validation and user redirection * For ACME validation and user redirection
@ -110,13 +109,13 @@ public class ButtonWebsiteModule extends JavaPlugin {
}); });
enabled = true; enabled = true;
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occurred while starting the webserver!", e); TBMCCoreAPI.SendException("An error occurred while starting the webserver!", e, this);
enabled = false; //It's not even enabled yet, so we need a variable enabled = false; //It's not even enabled yet, so we need a variable
} }
} }
@Override @Override
public void onEnable() { public void pluginEnable() {
if (!enabled) { if (!enabled) {
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
@ -126,8 +125,8 @@ public class ButtonWebsiteModule extends JavaPlugin {
addPage(new ProfilePage()); addPage(new ProfilePage());
addPage(new BuildNotificationsPage()); addPage(new BuildNotificationsPage());
addPage(new BridgePage()); addPage(new BridgePage());
TBMCCoreAPI.RegisterUserClass(WebUser.class); TBMCCoreAPI.RegisterUserClass(WebUser.class, WebUser::new);
TBMCChatAPI.AddCommand(this, LoginCommand.class); getCommand2MC().registerCommand(new LoginCommand());
Bukkit.getScheduler().runTaskAsynchronously(this, () -> { Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
this.getLogger().info("Starting webserver..."); this.getLogger().info("Starting webserver...");
server.setExecutor( server.setExecutor(
@ -145,6 +144,10 @@ public class ButtonWebsiteModule extends JavaPlugin {
}); });
} }
@Override
protected void pluginDisable() {
}
private static boolean httpstarted = false; private static boolean httpstarted = false;
/** /**

View file

@ -1,27 +1,23 @@
package buttondevteam.website; package buttondevteam.website;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.PlayerCommandBase; import buttondevteam.lib.chat.ICommand2;
import buttondevteam.lib.chat.ICommand2MC;
import buttondevteam.website.page.LoginPage; import buttondevteam.website.page.LoginPage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@CommandClass @CommandClass(helpText = {//
public class LoginCommand extends PlayerCommandBase {
@Override //TODO: Ask about linking already existing accounts, to prevent linking someone else's
public boolean OnCommand(Player player, String s, String[] strings) {
String state = LoginPage.generateState("minecraft", player.getUniqueId().toString()).toString();
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"\",{\"text\":\"Please \",\"color\":\"aqua\"},{\"text\":\"Click Here\",\"color\":\"aqua\",\"bold\":true,\"underlined\":true,\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://server.figytuna.com/login?type=minecraft&state=" + state + "\"}},{\"text\":\" to log in to our site using your Minecraft account.\",\"color\":\"aqua\",\"bold\":false,\"underlined\":false}]");
return true;
}
@Override
public String[] GetHelpText(String s) {
return new String[]{//
"§6---- Login ----", // "§6---- Login ----", //
"This command allows you to log in to our website using your Minecraft account.", // "This command allows you to log in to our website using your Minecraft account.", //
"If you are already logged in to the site, you can connect your MC account to it.", // "If you are already logged in to the site, you can connect your MC account to it.", //
"This is good for getting Minecraft rewards if you're a patron for example." // "This is good for getting Minecraft rewards if you're a patron for example." //
}; })
public class LoginCommand extends ICommand2MC {
//TODO: Ask about linking already existing accounts, to prevent linking someone else's
public boolean def(Player player) {
String state = LoginPage.generateState("minecraft", player.getUniqueId().toString()).toString();
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"\",{\"text\":\"Please \",\"color\":\"aqua\"},{\"text\":\"Click Here\",\"color\":\"aqua\",\"bold\":true,\"underlined\":true,\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://server.figytuna.com/login?type=minecraft&state=" + state + "\"}},{\"text\":\" to log in to our site using your Minecraft account.\",\"color\":\"aqua\",\"bold\":false,\"underlined\":false}]");
return true;
} }
} }

View file

@ -2,8 +2,8 @@ package buttondevteam.website;
import java.util.UUID; import java.util.UUID;
import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.PlayerData;
import buttondevteam.lib.player.UserClass; import buttondevteam.lib.player.UserClass;
@UserClass(foldername = "web") @UserClass(foldername = "web")
@ -16,7 +16,6 @@ public class WebUser extends ChromaGamerBase {
return uuid; return uuid;
} }
public PlayerData<UUID> sessionID() { //It's used with toString() directly, so can't be null
return data(new UUID(0, 0)); //It's used with toString() directly, so can't be null public ConfigData<UUID> sessionID = getConfig().getData("sessionID", new UUID(0, 0));
}
} }

View file

@ -84,16 +84,14 @@ public class IOHelper {
*/ */
public static void LoginUser(HttpExchange exchange, WebUser user) { public static void LoginUser(HttpExchange exchange, WebUser user) {
Bukkit.getLogger().fine("Logging in user: " + user); Bukkit.getLogger().fine("Logging in user: " + user);
user.sessionID().set(UUID.randomUUID()); user.sessionID.set(UUID.randomUUID());
user.save();
new Cookies(2).add(new Cookie("user_id", user.getUUID() + "")) new Cookies(2).add(new Cookie("user_id", user.getUUID() + ""))
.add(new Cookie("session_id", user.sessionID().get().toString())).AddHeaders(exchange); .add(new Cookie("session_id", user.sessionID.get().toString())).AddHeaders(exchange);
Bukkit.getLogger().fine("Logged in user."); Bukkit.getLogger().fine("Logged in user.");
} }
public static void LogoutUser(HttpExchange exchange, WebUser user) { public static void LogoutUser(HttpExchange exchange, WebUser user) {
user.sessionID().set(new UUID(0, 0)); user.sessionID.set(new UUID(0, 0));
user.save();
SendLogoutHeaders(exchange); SendLogoutHeaders(exchange);
} }
@ -147,7 +145,7 @@ public class IOHelper {
return null; return null;
WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class); WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class);
if (user != null && cookies.get("session_id") != null if (user != null && cookies.get("session_id") != null
&& cookies.get("session_id").getValue().equals(user.sessionID().get().toString())) { && cookies.get("session_id").getValue().equals(user.sessionID.get().toString())) {
if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT")))) if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT"))))
LoginUser(exchange, user); LoginUser(exchange, user);
return user; return user;

View file

@ -1,6 +1,5 @@
package buttondevteam.website.page; package buttondevteam.website.page;
import buttondevteam.core.component.updater.PluginUpdater;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.website.io.IOHelper; import buttondevteam.website.io.IOHelper;
import buttondevteam.website.io.Response; import buttondevteam.website.io.Response;
@ -52,8 +51,8 @@ public class BuildNotificationsPage extends Page {
if (signatures != null && signatures.size() > 0 && post.containsKey("payload") if (signatures != null && signatures.size() > 0 && post.containsKey("payload")
&& verifySignature(payload.getBytes(StandardCharsets.UTF_8), && verifySignature(payload.getBytes(StandardCharsets.UTF_8),
Base64.getDecoder().decode(signatures.get(0)))) { Base64.getDecoder().decode(signatures.get(0)))) {
Bukkit.getPluginManager() /*Bukkit.getPluginManager()
.callEvent(new PluginUpdater.UpdatedEvent(gson.fromJson(payload, JsonObject.class))); .callEvent(new PluginUpdater.UpdatedEvent(gson.fromJson(payload, JsonObject.class)));*/
return new Response(200, "All right", exchange); return new Response(200, "All right", exchange);
} }
} catch (Exception e) { } catch (Exception e) {

View file

@ -1,6 +1,7 @@
package buttondevteam.website.page; package buttondevteam.website.page;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.website.ButtonWebsiteModule;
import buttondevteam.website.io.IOHelper; import buttondevteam.website.io.IOHelper;
import buttondevteam.website.io.Response; import buttondevteam.website.io.Response;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
@ -25,7 +26,8 @@ public abstract class Page implements HttpHandler {
IOHelper.SendResponse(404, "404 Not found: " + exchange.getRequestURI().getPath(), exchange); IOHelper.SendResponse(404, "404 Not found: " + exchange.getRequestURI().getPath(), exchange);
} }
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e); ButtonWebsiteModule module = ButtonWebsiteModule.getPlugin(ButtonWebsiteModule.class);
TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e, module);
try { try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream str = new PrintStream(baos); PrintStream str = new PrintStream(baos);
@ -34,7 +36,7 @@ public abstract class Page implements HttpHandler {
str.print("</pre>"); str.print("</pre>");
IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange);
} catch (Exception e1) { } catch (Exception e1) {
TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1); TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1, module);
} }
} }
} }