Skip to content

Commit

Permalink
The rebirth, Part 5. Separate server.properties editor
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Nov 11, 2014
1 parent 9695520 commit e9a0dad
Show file tree
Hide file tree
Showing 19 changed files with 942 additions and 556 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -50,6 +50,7 @@
import net.doubledoordev.backend.web.http.ServerFileHandler;
import net.doubledoordev.backend.web.socket.ServerControlSocketApplication;
import net.doubledoordev.backend.web.socket.ServerMonitorSocketApplication;
import net.doubledoordev.backend.web.socket.ServerPropertiesSocketApplication;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.grizzly.http.server.CLStaticHttpHandler;
Expand Down Expand Up @@ -143,6 +144,7 @@ public static void main(String[] args) throws Exception
// Socket stuff
ServerMonitorSocketApplication.register();
ServerControlSocketApplication.register();
ServerPropertiesSocketApplication.register();

final NetworkListener networkListener = new NetworkListener("secured-listener");
//networkListener.setSecure(true);
Expand All @@ -168,6 +170,7 @@ public static void main(String[] args) throws Exception
CommandHandler.init();
for (Server server : SETTINGS.servers.values())
{
server.init();
if (server.getAutoStart())
{
try
Expand Down
43 changes: 15 additions & 28 deletions src/main/java/net/doubledoordev/backend/server/FileManager.java
Expand Up @@ -48,6 +48,7 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.logging.log4j.util.Strings;
import org.glassfish.grizzly.http.util.MimeType;
import org.spout.nbt.Tag;

import java.io.File;
Expand Down Expand Up @@ -140,6 +141,9 @@ public boolean canEdit(File file)
case "jar":
case "zip":
case "disabled":
case "exe":
case "mca":
case "mcr":
return false;

default:
Expand All @@ -153,15 +157,19 @@ public String getEditor()
if (file.getName().equals("whitelist.json")) return "whitelist.ftl";
if (file.getName().equals("banned-players.json")) return "banned-players.ftl";
if (file.getName().equals("banned-ips.json")) return "banned-ips.ftl";
if (file.getName().equals("server.properties")) return "serverProperties.ftl";
switch (getExtension())
{
case "jar":
case "zip":
case "disabled":
case "mca":
case "mcr":
return null;

case "json":
case "dat":
case "dat_old":
return "json.ftl";

case "jpg":
Expand All @@ -182,6 +190,8 @@ public String getIcon(File file)
case "html":
case "json":
case "dat":
case "dat_old":
case "properties":
return "file-code-o";

case "txt":
Expand All @@ -208,6 +218,7 @@ public String getFileContentsAsJson() throws IOException
case "json":
return FileUtils.readFileToString(file);
case "dat":
case "dat_old":
Tag tag = Helper.readRawNBT(file, true);
if (tag == null) tag = Helper.readRawNBT(file, false);
if (tag != null) return JsonNBTHelper.parseNBT(tag).toString();
Expand All @@ -221,34 +232,10 @@ public String getFileContentsAsString() throws IOException
return StringEscapeUtils.escapeHtml4(FileUtils.readFileToString(file));
}

// public String getFileContentsAsBase64() throws IOException
// {
// return String.format("data:%s;base64,%s", SimpleWebServer.MIME_TYPES.get(getExtension()), Base64.encodeBase64String(FileUtils.readFileToByteArray(file)));
// }

// public NanoHTTPD.Response set(String contents)
// {
// if (!file.canWrite()) return new NanoHTTPD.Response(FORBIDDEN, MIME_PLAINTEXT, "File is write protected.");
// try
// {
// switch (getExtension())
// {
// case "dat":
// Helper.writeRawNBT(file, Helper.readRawNBT(file, true) != null, JsonNBTHelper.parseJSON(Constants.JSONPARSER.parse(contents)));
// break;
// default:
// FileUtils.writeStringToFile(file, contents);
// break;
// }
// }
// catch (IOException e)
// {
// e.printStackTrace();
// return new NanoHTTPD.Response(INTERNAL_ERROR, MIME_PLAINTEXT, e.toString());
// }
//
// return new NanoHTTPD.Response(OK, MIME_PLAINTEXT, "OK");
// }
public String getFileContentsAsBase64() throws IOException
{
return String.format("data:%s;base64,%s", MimeType.get(getExtension()), Base64.encodeBase64String(FileUtils.readFileToByteArray(file)));
}

public void rename(String newname)
{
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/net/doubledoordev/backend/server/Server.java
Expand Up @@ -77,14 +77,14 @@
@SuppressWarnings("UnusedDeclaration")
public class Server
{
private static final String SERVER_PROPERTIES = "server.properties";
private static final String SERVER_PORT = "server-port";
private static final String QUERY_PORT = "query.port";
private static final String QUERY_ENABLE = "enable-query";
private static final String RCON_ENABLE = "enable-rcon";
private static final String RCON_PASSWORD = "rcon.password";
private static final String RCON_PORT = "rcon.port";
private static final String SERVER_IP = "server-ip";
public static final String SERVER_PROPERTIES = "server.properties";
public static final String SERVER_PORT = "server-port";
public static final String QUERY_PORT = "query.port";
public static final String QUERY_ENABLE = "enable-query";
public static final String RCON_ENABLE = "enable-rcon";
public static final String RCON_PASSWORD = "rcon.password";
public static final String RCON_PORT = "rcon.port";
public static final String SERVER_IP = "server-ip";

/*
* START exposed Json data
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/net/doubledoordev/backend/util/Helper.java
Expand Up @@ -40,14 +40,20 @@

package net.doubledoordev.backend.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import net.doubledoordev.backend.Main;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.util.methodCaller.IMethodCaller;
import net.doubledoordev.backend.util.methodCaller.WebSocketCaller;
import org.apache.logging.log4j.util.Strings;
import org.glassfish.grizzly.websockets.WebSocket;
import org.spout.nbt.Tag;
import org.spout.nbt.stream.NBTInputStream;
import org.spout.nbt.stream.NBTOutputStream;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URL;
Expand All @@ -60,6 +66,7 @@

import static net.doubledoordev.backend.util.Constants.JSONPARSER;
import static net.doubledoordev.backend.util.Constants.RANDOM;
import static net.doubledoordev.backend.util.Constants.SERVERS;
import static net.doubledoordev.backend.util.Settings.SETTINGS;

/**
Expand Down Expand Up @@ -294,4 +301,49 @@ public static String getOnlineTime(String dayString, String hoursString, String
sb.append(String.format(secondsString, time / (1000)));
return sb.toString();
}

public static String getReadOnlyProperties()
{
JsonArray array = new JsonArray();

if (SETTINGS.fixedPorts)
{
array.add(new JsonPrimitive(Server.SERVER_PORT));
array.add(new JsonPrimitive(Server.QUERY_PORT));
array.add(new JsonPrimitive(Server.RCON_PORT));
}
if (SETTINGS.fixedIP) array.add(new JsonPrimitive(Server.SERVER_IP));

array.add(new JsonPrimitive(Server.RCON_ENABLE));
array.add(new JsonPrimitive(Server.RCON_PASSWORD));
array.add(new JsonPrimitive(Server.QUERY_ENABLE));

return array.toString();
}

public static boolean invokeWithRefectionMagic(WebSocket caller, Object instance, String[] split, int start) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
start++;
for (java.lang.reflect.Method method : instance.getClass().getDeclaredMethods())
{
if (!method.getName().equalsIgnoreCase(split[start - 1])) continue; // Name match
boolean userMethodCaller = method.getParameterTypes().length != 0 && method.getParameterTypes()[0].isAssignableFrom(IMethodCaller.class); // See if first type is IMethodCaller
if (method.getParameterTypes().length == split.length - start + (userMethodCaller ? 1 : 0)) // parameter length match
{
try
{
Object parms[] = new Object[split.length - start + (userMethodCaller ? 1 : 0)];
if (userMethodCaller) parms[0] = new WebSocketCaller(caller);
for (int i = userMethodCaller ? 1 : 0; i < method.getParameterTypes().length; i++) parms[i] = TypeHellhole.convert(method.getParameterTypes()[i], split[i + start - (userMethodCaller ? 1 : 0)]);
method.invoke(instance, parms);
return userMethodCaller;
}
catch (ClassCastException ignored)
{
ignored.printStackTrace();
}
}
}
throw new NoSuchMethodException(split[start - 1]);
}
}
15 changes: 15 additions & 0 deletions src/main/java/net/doubledoordev/backend/util/WebSocketHelper.java
Expand Up @@ -46,6 +46,7 @@
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.web.socket.ServerControlSocketApplication;
import net.doubledoordev.backend.web.socket.ServerMonitorSocketApplication;
import net.doubledoordev.backend.web.socket.ServerPropertiesSocketApplication;
import org.glassfish.grizzly.websockets.WebSocket;

import static net.doubledoordev.backend.util.Constants.*;
Expand Down Expand Up @@ -122,6 +123,7 @@ public static void sendData(WebSocket socket, JsonElement s)
public static void sendServerUpdate(Server instance)
{
ServerMonitorSocketApplication.sendUpdateToAll(instance);
ServerPropertiesSocketApplication.sendUpdateToAll(instance);
}

public static void sendOk(WebSocket socket)
Expand All @@ -132,4 +134,17 @@ public static void sendOk(WebSocket socket)

socket.send(root.toString());
}

public static void sendError(WebSocket socket, Throwable e)
{
StringBuilder error = new StringBuilder();
do
{
error.append('\n').append(e.getClass().getSimpleName());
if (e.getMessage() != null) error.append(": ").append(e.getMessage());
}
while ((e = e.getCause()) != null);
WebSocketHelper.sendError(socket, error.substring(1));
socket.close();
}
}
@@ -0,0 +1,52 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package net.doubledoordev.backend.web.socket;

import org.glassfish.grizzly.websockets.WebSocketApplication;

/**
* @author Dries007
*/
public class FileManagerSocketApplication extends WebSocketApplication
{
//TODO:
}
Expand Up @@ -45,6 +45,7 @@
import net.doubledoordev.backend.Main;
import net.doubledoordev.backend.permissions.User;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.util.Helper;
import net.doubledoordev.backend.util.Settings;
import net.doubledoordev.backend.util.TypeHellhole;
import net.doubledoordev.backend.util.WebSocketHelper;
Expand Down Expand Up @@ -73,32 +74,6 @@ private ServerControlSocketApplication()
{
}

private static boolean invokeWithRefectionMagic(WebSocket caller, Object instance, String[] split, int start) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
start++;
for (java.lang.reflect.Method method : instance.getClass().getDeclaredMethods())
{
if (!method.getName().equalsIgnoreCase(split[start - 1])) continue; // Name match
boolean userMethodCaller = method.getParameterTypes().length != 0 && method.getParameterTypes()[0].isAssignableFrom(IMethodCaller.class); // See if first type is IMethodCaller
if (method.getParameterTypes().length == split.length - start + (userMethodCaller ? 1 : 0)) // parameter length match
{
try
{
Object parms[] = new Object[split.length - start + (userMethodCaller ? 1 : 0)];
if (userMethodCaller) parms[0] = new WebSocketCaller(caller);
for (int i = userMethodCaller ? 1 : 0; i < method.getParameterTypes().length; i++) parms[i] = TypeHellhole.convert(method.getParameterTypes()[i], split[i + start - (userMethodCaller ? 1 : 0)]);
method.invoke(instance, parms);
return userMethodCaller;
}
catch (ClassCastException ignored)
{
ignored.printStackTrace();
}
}
}
throw new NoSuchMethodException(split[start - 1]);
}

@Override
public void onConnect(WebSocket socket)
{
Expand Down Expand Up @@ -147,16 +122,15 @@ public void onMessage(WebSocket socket, String text)
}
try
{
if (!invokeWithRefectionMagic(socket, server, args, 0))
if (!Helper.invokeWithRefectionMagic(socket, server, args, 0))
{
WebSocketHelper.sendOk(socket);
socket.close();
}
}
catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e)
{
WebSocketHelper.sendError(socket, e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : ""));
socket.close();
WebSocketHelper.sendError(socket, e);
}
}

Expand Down

0 comments on commit e9a0dad

Please sign in to comment.