2022-04-02 03:59:47 +00:00
|
|
|
package dev.plex;
|
|
|
|
|
|
|
|
import dev.plex.config.ModuleConfig;
|
|
|
|
import dev.plex.module.PlexModule;
|
2022-04-02 20:51:14 +00:00
|
|
|
import dev.plex.request.impl.AdminsEndpoint;
|
|
|
|
import dev.plex.request.impl.IndefBansEndpoint;
|
2022-04-02 21:34:29 +00:00
|
|
|
import dev.plex.request.impl.ListEndpoint;
|
2022-04-02 23:34:19 +00:00
|
|
|
import dev.plex.request.impl.PunishmentsEndpoint;
|
2022-04-02 03:59:47 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2022-04-02 04:14:12 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2022-04-02 03:59:47 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import net.milkbowl.vault.permission.Permission;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
2022-04-02 04:14:12 +00:00
|
|
|
import org.eclipse.jetty.server.Connector;
|
|
|
|
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
|
|
|
|
import org.eclipse.jetty.server.HttpConfiguration;
|
|
|
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
|
|
|
import org.eclipse.jetty.server.Server;
|
|
|
|
import org.eclipse.jetty.server.ServerConnector;
|
2022-04-02 03:59:47 +00:00
|
|
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
|
|
|
import org.eclipse.jetty.servlet.ServletHandler;
|
|
|
|
|
2022-04-02 04:14:12 +00:00
|
|
|
public class HTTPDModule extends PlexModule
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
public static ServletContextHandler context;
|
|
|
|
private Thread serverThread;
|
|
|
|
private AtomicReference<Server> atomicServer = new AtomicReference<>();
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private static Permission permissions = null;
|
|
|
|
|
2022-04-02 20:51:14 +00:00
|
|
|
public static ModuleConfig moduleConfig;
|
2022-04-02 04:54:09 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void load()
|
|
|
|
{
|
|
|
|
moduleConfig = new ModuleConfig(this, "settings.yml");
|
|
|
|
}
|
2022-04-02 03:59:47 +00:00
|
|
|
|
|
|
|
@Override
|
2022-04-02 04:14:12 +00:00
|
|
|
public void enable()
|
|
|
|
{
|
2022-04-02 04:54:09 +00:00
|
|
|
moduleConfig.load();
|
|
|
|
PlexLog.debug("HTTPD Module Port: {0}", moduleConfig.getInt("server.port"));
|
2022-04-02 04:14:12 +00:00
|
|
|
if (!setupPermissions() && getPlex().getSystem().equalsIgnoreCase("permissions") && !Bukkit.getPluginManager().isPluginEnabled("Vault"))
|
|
|
|
{
|
2022-04-02 04:54:09 +00:00
|
|
|
throw new RuntimeException("Plex-HTTPD requires the 'Vault' plugin as well as a Permissions plugin that hooks into 'Vault'. We recommend LuckPerms!");
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
2022-04-02 04:14:12 +00:00
|
|
|
serverThread = new Thread(() ->
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
Server server = new Server();
|
|
|
|
ServletHandler servletHandler = new ServletHandler();
|
|
|
|
|
|
|
|
context = new ServletContextHandler(servletHandler, "/", ServletContextHandler.SESSIONS);
|
|
|
|
HttpConfiguration configuration = new HttpConfiguration();
|
|
|
|
configuration.addCustomizer(new ForwardedRequestCustomizer());
|
|
|
|
HttpConnectionFactory factory = new HttpConnectionFactory(configuration);
|
|
|
|
ServerConnector connector = new ServerConnector(server, factory);
|
2022-04-02 04:54:09 +00:00
|
|
|
connector.setHost(moduleConfig.getString("server.bind-address"));
|
2022-04-02 05:35:39 +00:00
|
|
|
connector.setPort(moduleConfig.getInt("server.port"));
|
2022-04-02 03:59:47 +00:00
|
|
|
|
2022-04-02 20:51:14 +00:00
|
|
|
new AdminsEndpoint();
|
|
|
|
new IndefBansEndpoint();
|
2022-04-02 21:34:29 +00:00
|
|
|
new ListEndpoint();
|
2022-04-02 23:34:19 +00:00
|
|
|
new PunishmentsEndpoint();
|
2022-04-02 03:59:47 +00:00
|
|
|
|
|
|
|
server.setConnectors(new Connector[]{connector});
|
|
|
|
server.setHandler(context);
|
|
|
|
|
|
|
|
atomicServer.set(server);
|
2022-04-02 04:14:12 +00:00
|
|
|
try
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
server.start();
|
|
|
|
server.join();
|
2022-04-02 04:14:12 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-04-02 05:35:39 +00:00
|
|
|
}, "Jetty-Server");
|
2022-04-02 03:59:47 +00:00
|
|
|
serverThread.start();
|
2022-04-02 20:51:14 +00:00
|
|
|
PlexLog.log("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-02 04:14:12 +00:00
|
|
|
public void disable()
|
|
|
|
{
|
2022-04-02 04:54:09 +00:00
|
|
|
PlexLog.debug("Stopping Jetty server");
|
2022-04-02 04:14:12 +00:00
|
|
|
try
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
atomicServer.get().stop();
|
|
|
|
atomicServer.get().destroy();
|
2022-04-02 04:14:12 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:14:12 +00:00
|
|
|
private boolean setupPermissions()
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
|
|
|
|
permissions = rsp.getProvider();
|
|
|
|
return permissions != null;
|
|
|
|
}
|
|
|
|
}
|