From 6d21e25bd6bb030d026dc8f8718c66a00f05be4f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 1 Nov 2018 20:46:00 +0100 Subject: [PATCH] Config & proper disable #12 and #10 --- src/buttondevteam/website/AcmeClient.java | 9 --------- .../website/ButtonWebsiteModule.java | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/buttondevteam/website/AcmeClient.java b/src/buttondevteam/website/AcmeClient.java index bf4b084..ffeef47 100644 --- a/src/buttondevteam/website/AcmeClient.java +++ b/src/buttondevteam/website/AcmeClient.java @@ -261,17 +261,8 @@ public class AcmeClient { if (challenge == null) { throw new AcmeException("Found no " + Http01Challenge.TYPE + " challenge, don't know what to do..."); } - // if (ButtonWebsiteModule.PORT == 443) LOG.info("Storing the challenge data."); - /* - * else LOG.info("Store the challenge data! Can't do automatically."); - */ LOG.info("It should be reachable at: http://" + auth.getDomain() + "/.well-known/acme-challenge/" + challenge.getToken()); - // LOG.info("File name: " + challenge.getToken()); - // LOG.info("Content: " + challenge.getAuthorization()); - /* - * LOG.info("Press any key to continue..."); if (ButtonWebsiteModule.PORT != 443) try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } - */ ButtonWebsiteModule.addHttpPage(new AcmeChallengePage(challenge.getToken(), challenge.getAuthorization())); ButtonWebsiteModule.startHttp(); try { diff --git a/src/buttondevteam/website/ButtonWebsiteModule.java b/src/buttondevteam/website/ButtonWebsiteModule.java index 8e29e30..3c6ffbc 100644 --- a/src/buttondevteam/website/ButtonWebsiteModule.java +++ b/src/buttondevteam/website/ButtonWebsiteModule.java @@ -30,17 +30,19 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ButtonWebsiteModule extends JavaPlugin { - public static final int PORT = 443; private static HttpsServer server; /** * For ACME validation and user redirection */ private static HttpServer httpserver; + private static boolean enabled; public ButtonWebsiteModule() { try { - server = HttpsServer.create(new InetSocketAddress((InetAddress) null, PORT), 10); - httpserver = HttpServer.create(new InetSocketAddress((InetAddress) null, 80), 10); + int ps = getConfig().getInt("https-port", 443); + int p = getConfig().getInt("http-port", 80); + server = HttpsServer.create(new InetSocketAddress((InetAddress) null, ps), 10); + httpserver = HttpServer.create(new InetSocketAddress((InetAddress) null, p), 10); SSLContext sslContext = SSLContext.getInstance("TLS"); // initialise the keystore @@ -106,14 +108,19 @@ public class ButtonWebsiteModule extends JavaPlugin { } } }); + enabled = true; } catch (Exception e) { TBMCCoreAPI.SendException("An error occurred while starting the webserver!", e); - getServer().getPluginManager().disablePlugin(this); + enabled = false; //It's not even enabled yet, so we need a variable } } @Override public void onEnable() { + if (!enabled) { + getServer().getPluginManager().disablePlugin(this); + return; + } addPage(new IndexPage()); addPage(new LoginPage()); addPage(new ProfilePage()); @@ -152,6 +159,8 @@ public class ButtonWebsiteModule extends JavaPlugin { * Adds a new page/endpoint to the website. This method needs to be called before the server finishes loading (onEnable). */ public static void addPage(Page page) { + if (!enabled) + return; server.createContext("/" + page.GetName(), page); } @@ -159,6 +168,8 @@ public class ButtonWebsiteModule extends JavaPlugin { * Adds an insecure endpoint to the website. This should be avoided when possible. */ public static void addHttpPage(Page page) { + if (!enabled) + return; httpserver.createContext("/" + page.GetName(), page); }