2022-04-02 03:59:47 +00:00
|
|
|
package dev.plex;
|
|
|
|
|
2022-04-17 23:21:44 +00:00
|
|
|
import dev.plex.cache.FileCache;
|
2022-04-02 03:59:47 +00:00
|
|
|
import dev.plex.config.ModuleConfig;
|
|
|
|
import dev.plex.module.PlexModule;
|
2022-04-18 03:05:20 +00:00
|
|
|
import dev.plex.request.AbstractServlet;
|
|
|
|
import dev.plex.request.SchematicUploadServlet;
|
2022-06-06 03:13:20 +00:00
|
|
|
import dev.plex.request.impl.IndefBansEndpoint;
|
|
|
|
import dev.plex.request.impl.IndexEndpoint;
|
|
|
|
import dev.plex.request.impl.ListEndpoint;
|
|
|
|
import dev.plex.request.impl.PunishmentsEndpoint;
|
|
|
|
import dev.plex.request.impl.SchematicDownloadEndpoint;
|
|
|
|
import dev.plex.request.impl.SchematicUploadEndpoint;
|
2022-04-02 03:59:47 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2022-06-06 03:13:20 +00:00
|
|
|
import jakarta.servlet.MultipartConfigElement;
|
2022-04-18 03:05:20 +00:00
|
|
|
import java.io.File;
|
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-18 03:05:20 +00:00
|
|
|
import org.eclipse.jetty.servlet.ServletHolder;
|
2022-04-02 03:59:47 +00:00
|
|
|
|
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
|
|
|
|
2022-04-17 23:21:44 +00:00
|
|
|
public static final FileCache fileCache = new FileCache();
|
|
|
|
|
2022-04-18 03:05:20 +00:00
|
|
|
public static final String template = AbstractServlet.readFileReal(HTTPDModule.class.getResourceAsStream("/httpd/template.html"));
|
|
|
|
|
2022-04-02 04:54:09 +00:00
|
|
|
@Override
|
|
|
|
public void load()
|
|
|
|
{
|
2022-04-14 04:49:12 +00:00
|
|
|
// Move it from /httpd/config.yml to /plugins/Plex/modules/Plex-HTTPD/config.yml
|
|
|
|
moduleConfig = new ModuleConfig(this, "httpd/config.yml", "config.yml");
|
2022-04-02 04:54:09 +00:00
|
|
|
}
|
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"));
|
2023-08-29 00:02:46 +00:00
|
|
|
if ((!Bukkit.getPluginManager().isPluginEnabled("Vault") || !setupPermissions()))
|
2022-04-02 04:14:12 +00:00
|
|
|
{
|
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 IndefBansEndpoint();
|
2022-04-17 21:39:08 +00:00
|
|
|
new IndexEndpoint();
|
2022-04-02 21:34:29 +00:00
|
|
|
new ListEndpoint();
|
2022-04-02 23:34:19 +00:00
|
|
|
new PunishmentsEndpoint();
|
2022-04-18 00:10:12 +00:00
|
|
|
new SchematicDownloadEndpoint();
|
|
|
|
new SchematicUploadEndpoint();
|
2022-04-02 03:59:47 +00:00
|
|
|
|
2022-04-18 03:05:20 +00:00
|
|
|
ServletHolder uploadHolder = HTTPDModule.context.addServlet(SchematicUploadServlet.class, "/api/schematics/uploading");
|
|
|
|
|
|
|
|
File uploadLoc = new File(System.getProperty("java.io.tmpdir"), "schematic-temp-dir");
|
2022-06-06 03:13:20 +00:00
|
|
|
if (!uploadLoc.exists())
|
|
|
|
{
|
|
|
|
uploadLoc.mkdirs();
|
|
|
|
}
|
2022-04-18 03:05:20 +00:00
|
|
|
uploadHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(uploadLoc.getAbsolutePath(), 1024 * 1024 * 5, 1024 * 1024 * 25, 1024 * 1024));
|
|
|
|
|
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;
|
|
|
|
}
|
2022-04-18 03:05:20 +00:00
|
|
|
|
|
|
|
public static File getWorldeditFolder()
|
|
|
|
{
|
|
|
|
if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit"))
|
|
|
|
{
|
|
|
|
return new File(Bukkit.getPluginManager().getPlugin("WorldEdit").getDataFolder() + "/schematics/");
|
|
|
|
}
|
|
|
|
else if (Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit"))
|
|
|
|
{
|
|
|
|
return new File(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit").getDataFolder() + "/schematics/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 03:13:20 +00:00
|
|
|
private static boolean isFileSystemCaseSensitive = !new File("a").equals(new File("A"));
|
2022-04-18 03:05:20 +00:00
|
|
|
|
|
|
|
public static boolean fileNameEquals(String filename1, String filename2)
|
|
|
|
{
|
|
|
|
if (isFileSystemCaseSensitive)
|
|
|
|
{
|
|
|
|
return filename1.equals(filename2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return filename1.equalsIgnoreCase(filename2);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|