Config & proper disable

#12 and #10
This commit is contained in:
Norbi Peti 2018-11-01 20:46:00 +01:00
parent 6911df7f60
commit 6d21e25bd6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 15 additions and 13 deletions

View file

@ -261,17 +261,8 @@ public class AcmeClient {
if (challenge == null) { if (challenge == null) {
throw new AcmeException("Found no " + Http01Challenge.TYPE + " challenge, don't know what to do..."); throw new AcmeException("Found no " + Http01Challenge.TYPE + " challenge, don't know what to do...");
} }
// if (ButtonWebsiteModule.PORT == 443)
LOG.info("Storing the challenge data."); 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("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.addHttpPage(new AcmeChallengePage(challenge.getToken(), challenge.getAuthorization()));
ButtonWebsiteModule.startHttp(); ButtonWebsiteModule.startHttp();
try { try {

View file

@ -30,17 +30,19 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class ButtonWebsiteModule extends JavaPlugin { public class ButtonWebsiteModule extends JavaPlugin {
public static final int PORT = 443;
private static HttpsServer server; private static HttpsServer server;
/** /**
* For ACME validation and user redirection * For ACME validation and user redirection
*/ */
private static HttpServer httpserver; private static HttpServer httpserver;
private static boolean enabled;
public ButtonWebsiteModule() { public ButtonWebsiteModule() {
try { try {
server = HttpsServer.create(new InetSocketAddress((InetAddress) null, PORT), 10); int ps = getConfig().getInt("https-port", 443);
httpserver = HttpServer.create(new InetSocketAddress((InetAddress) null, 80), 10); 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"); SSLContext sslContext = SSLContext.getInstance("TLS");
// initialise the keystore // initialise the keystore
@ -106,14 +108,19 @@ public class ButtonWebsiteModule extends JavaPlugin {
} }
} }
}); });
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);
getServer().getPluginManager().disablePlugin(this); enabled = false; //It's not even enabled yet, so we need a variable
} }
} }
@Override @Override
public void onEnable() { public void onEnable() {
if (!enabled) {
getServer().getPluginManager().disablePlugin(this);
return;
}
addPage(new IndexPage()); addPage(new IndexPage());
addPage(new LoginPage()); addPage(new LoginPage());
addPage(new ProfilePage()); 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). * 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) { public static void addPage(Page page) {
if (!enabled)
return;
server.createContext("/" + page.GetName(), page); server.createContext("/" + page.GetName(), page);
} }
@ -159,6 +168,8 @@ public class ButtonWebsiteModule extends JavaPlugin {
* Adds an <b>insecure</b> endpoint to the website. This should be avoided when possible. * Adds an <b>insecure</b> endpoint to the website. This should be avoided when possible.
*/ */
public static void addHttpPage(Page page) { public static void addHttpPage(Page page) {
if (!enabled)
return;
httpserver.createContext("/" + page.GetName(), page); httpserver.createContext("/" + page.GetName(), page);
} }