Added HTTP server on port 80, fixes & impr.

Also ran out of time, so now it'll update on Saturdays
This commit is contained in:
Norbi Peti 2017-11-25 01:37:00 +01:00
parent f1d21df5d8
commit bce6aaa428
3 changed files with 55 additions and 12 deletions

View file

@ -153,6 +153,12 @@ public class AcmeClient {
private Registration findOrRegisterAccount(Session session) throws AcmeException, IOException { private Registration findOrRegisterAccount(Session session) throws AcmeException, IOException {
Registration reg; Registration reg;
URI loc = ButtonWebsiteModule.getRegistration();
if (loc != null) {
LOG.info("Loading account from file");
return Registration.bind(session, loc);
}
try { try {
// Try to create a new Registration. // Try to create a new Registration.
reg = new RegistrationBuilder().create(session); reg = new RegistrationBuilder().create(session);
@ -169,6 +175,7 @@ public class AcmeClient {
// URL of the existing registration's location. Bind it to the session. // URL of the existing registration's location. Bind it to the session.
reg = Registration.bind(session, ex.getLocation()); reg = Registration.bind(session, ex.getLocation());
LOG.info("Account does already exist, URI: " + reg.getLocation(), ex); LOG.info("Account does already exist, URI: " + reg.getLocation(), ex);
ButtonWebsiteModule.storeRegistration(ex.getLocation());
} }
return reg; return reg;
@ -261,7 +268,7 @@ public class AcmeClient {
/* /*
* LOG.info("Press any key to continue..."); if (ButtonWebsiteModule.PORT != 443) try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } * LOG.info("Press any key to continue..."); if (ButtonWebsiteModule.PORT != 443) try { System.in.read(); } catch (IOException e) { e.printStackTrace(); }
*/ */
AcmeChallengePage.setValues(challenge.getToken(), challenge.getAuthorization()); ButtonWebsiteModule.addHttpPage(new AcmeChallengePage(challenge.getToken(), challenge.getAuthorization()));
return challenge; return challenge;
} }

View file

@ -3,6 +3,8 @@ package buttondevteam.website;
import java.io.*; import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.PrivateKey; import java.security.PrivateKey;
@ -23,20 +25,28 @@ import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters; import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer; import com.sun.net.httpserver.HttpsServer;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.website.io.IOHelper;
import buttondevteam.website.page.*; import buttondevteam.website.page.*;
public class ButtonWebsiteModule extends JavaPlugin { public class ButtonWebsiteModule extends JavaPlugin {
public static final int PORT = 443; public static final int PORT = 443;
private static HttpsServer server; private static HttpsServer server;
/**
* For ACME validation and user redirection
*/
private static HttpsServer httpserver;
public ButtonWebsiteModule() { public ButtonWebsiteModule() {
try { try {
server = HttpsServer.create(new InetSocketAddress((InetAddress) null, PORT), 10); server = HttpsServer.create(new InetSocketAddress((InetAddress) null, PORT), 10);
httpserver = HttpsServer.create(new InetSocketAddress((InetAddress) null, 80), 10);
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
// initialise the keystore // initialise the keystore
@ -120,13 +130,16 @@ public class ButtonWebsiteModule extends JavaPlugin {
server.setExecutor( server.setExecutor(
new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100))); new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100)));
final Calendar calendar = Calendar.getInstance(); final Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY && !TBMCCoreAPI.IsTestServer()) { // Only update every week if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && !TBMCCoreAPI.IsTestServer()) // Only update every week
addPage(new AcmeChallengePage()); // Add before the server gets started AcmeClient.main("server.figytuna.com"); // Task is running async so we don't need an extra thread
Thread t = new Thread(() -> AcmeClient.main("server.figytuna.com"));
t.setContextClassLoader(getClass().getClassLoader());
t.start();
}
((Runnable) server::start).run(); // Totally normal way of calling a method ((Runnable) server::start).run(); // Totally normal way of calling a method
httpserver.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
IOHelper.SendResponse(IOHelper.Redirect("https://server.figytuna.com/", exchange));
}
});
httpserver.start();
this.getLogger().info("Webserver started"); this.getLogger().info("Webserver started");
}); });
} }
@ -138,6 +151,29 @@ public class ButtonWebsiteModule extends JavaPlugin {
server.createContext("/" + page.GetName(), page); server.createContext("/" + page.GetName(), page);
} }
/**
* Adds an <b>insecure</b> endpoint to the website. This should be avoided when possible.
*/
public static void addHttpPage(Page page) {
httpserver.createContext("/" + page.GetName(), page);
}
static void storeRegistration(URI location) {
final ButtonWebsiteModule plugin = getPlugin(ButtonWebsiteModule.class);
plugin.getConfig().set("registration", location.toString());
plugin.saveConfig();
}
static URI getRegistration() {
try {
String str = getPlugin(ButtonWebsiteModule.class).getConfig().getString("registration");
return str == null ? null : new URI(str);
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
private static InputStream fullStream(String fname) throws IOException { private static InputStream fullStream(String fname) throws IOException {
FileInputStream fis = new FileInputStream(fname); FileInputStream fis = new FileInputStream(fname);
DataInputStream dis = new DataInputStream(fis); DataInputStream dis = new DataInputStream(fis);

View file

@ -6,6 +6,11 @@ import buttondevteam.website.io.Response;
public class AcmeChallengePage extends Page { public class AcmeChallengePage extends Page {
public AcmeChallengePage(String token, String content) { // The page name needs to be known before server start
AcmeChallengePage.token = token;
AcmeChallengePage.content = content;
}
@Override @Override
public String GetName() { public String GetName() {
return ".well-known/acme-challenge/" + token; return ".well-known/acme-challenge/" + token;
@ -21,9 +26,4 @@ public class AcmeChallengePage extends Page {
private static String token; private static String token;
private static String content; private static String content;
public static void setValues(String token, String content) {
AcmeChallengePage.token = token;
AcmeChallengePage.content = content;
}
} }