Skip to content

Commit

Permalink
Partial commit 4. Changes include:
Browse files Browse the repository at this point in the history
- Security log
  • Loading branch information
dries007 committed Jul 17, 2017
1 parent 8267e7b commit f711887
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
16 changes: 11 additions & 5 deletions src/main/java/net/doubledoordev/backend/server/FileManager.java
Expand Up @@ -228,38 +228,44 @@ public String getFileContents() throws IOException
}
}

public void rename(String newname)
public void rename(IMethodCaller caller, String newname)
{
server.logAction(caller, "Rename " + stripServer(file) + " to " + newname);
file.renameTo(new File(file.getParentFile(), newname));
FileMonitorSocketApplication.update(getJson(file.getParentFile()), file.getParentFile());
}

public void makeWritable()
public void makeWritable(IMethodCaller caller)
{
server.logAction(caller, "Make " + stripServer(file) + " writable");
file.setWritable(true);
FileMonitorSocketApplication.update(getJson(file.getParentFile()), file);
}

public void delete() throws IOException
public void delete(IMethodCaller caller) throws IOException
{
server.logAction(caller, "Delete " + stripServer(file));
FileUtils.forceDelete(file);
FileMonitorSocketApplication.update(getJson(file.getParentFile()), file.getParentFile());
}

public void newFile(String name) throws IOException
public void newFile(IMethodCaller caller, String name) throws IOException
{
server.logAction(caller, "New File " + stripServer(file));
FileUtils.touch(new File(file, name));
FileMonitorSocketApplication.update(getJson(file), file);
}

public void newFolder(String name) throws IOException
public void newFolder(IMethodCaller caller, String name) throws IOException
{
server.logAction(caller, "New Folder " + stripServer(file));
FileUtils.forceMkdir(new File(file, name));
FileMonitorSocketApplication.update(getJson(file), file);
}

public void set(IMethodCaller caller, String text) throws IOException
{
server.logAction(caller, "Changed file " + stripServer(file));
FileUtils.writeStringToFile(file, text);
FileManagerSocketApplication.sendFile(file.getAbsolutePath(), text);
FileMonitorSocketApplication.update(getJson(file.getParentFile()), file);
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/net/doubledoordev/backend/server/Server.java
Expand Up @@ -32,6 +32,8 @@
import net.doubledoordev.backend.util.exceptions.ServerOfflineException;
import net.doubledoordev.backend.util.exceptions.ServerOnlineException;
import net.doubledoordev.backend.util.methodCaller.IMethodCaller;
import net.doubledoordev.backend.web.socket.ServerMonitorSocketApplication;
import net.doubledoordev.backend.web.socket.ServerPropertiesSocketApplication;
import net.doubledoordev.backend.web.socket.ServerconsoleSocketApplication;
import net.dries007.cmd.Arguments;
import net.dries007.cmd.Worker;
Expand Down Expand Up @@ -408,6 +410,11 @@ public JvmData getJvmData()
return jvmData;
}

public EvictingQueue<String> getActionLog()
{
return actionLog;
}

public Set<String> getPossibleJarnames()
{
LinkedHashSet<String> names = new LinkedHashSet<>();
Expand Down Expand Up @@ -448,6 +455,8 @@ private void saveProperties() throws IOException
properties.store(outputStream, "Modified by the backend");
propertiesFileLastModified = propertiesFile.lastModified();
outputStream.close();

ServerPropertiesSocketApplication.sendUpdateToAll(this);
}

public void printLine(String line)
Expand All @@ -470,8 +479,9 @@ public void error(Throwable e)
public void logAction(IMethodCaller caller, String action)
{
if (caller == CommandHandler.CMDCALLER) return;
printLine("[" + Constants.NAME + "] " + caller.getUser().getUsername() + ": " + action);
actionLog.add(caller.getUser().getUsername() + ": " + action);
String line = String.format("%s %s: %s", Constants.ACTIONLOG_SDF.format(new Date()), caller.getUser().getUsername(), action);
actionLog.add(line);
ServerMonitorSocketApplication.sendActionLog(this, line);
// Logs to disk, otherwise it would be useless, since the printLine already logs to console.
Main.LOGGER.info("Action on {} by {}: {}", ID, caller.getUser().getUsername(), action);
}
Expand Down Expand Up @@ -1210,7 +1220,7 @@ private void normalizeProperties() throws IOException

private void update()
{
WebSocketHelper.sendServerUpdate(this);
ServerMonitorSocketApplication.sendUpdateToAll(this);
Settings.save();
}
}
Expand Up @@ -108,6 +108,7 @@ public class Constants
public static final JsonParser JSONPARSER = new JsonParser();
public static final Joiner JOINER_COMMA_SPACE = Joiner.on(", ");
public static final SimpleDateFormat BACKUP_SDF = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
public static final SimpleDateFormat ACTIONLOG_SDF = new SimpleDateFormat("HH:mm:ss");
public static final Random RANDOM = new Random();

private static String javaPath;
Expand Down
Expand Up @@ -96,12 +96,6 @@ public static void sendData(WebSocket socket, JsonElement s)
socket.send(root.toString());
}

public static void sendServerUpdate(Server instance)
{
ServerMonitorSocketApplication.sendUpdateToAll(instance);
ServerPropertiesSocketApplication.sendUpdateToAll(instance);
}

public static void sendOk(WebSocket socket)
{
JsonObject root = new JsonObject();
Expand Down
Expand Up @@ -69,6 +69,11 @@ public static void sendUpdateToAll(Server server)
ONE_SERVER_APPLICATION.doSendUpdateToAll(server);
}

public static void sendActionLog(Server server, String line)
{
ONE_SERVER_APPLICATION.doSendActionLog(server, line);
}

public static void register()
{
WebSocketEngine.getEngine().register(SOCKET_CONTEXT, ALL_SERVERS_URL_PATTERN, ALL_SERVERS_APPLICATION);
Expand Down Expand Up @@ -129,7 +134,24 @@ public void doSendUpdateToAll(Server server)
for (WebSocket socket : getWebSockets())
{
if (!allServers && ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(SERVER) != server) continue;
if (server.canUserControl((User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER))) WebSocketHelper.sendData(socket, getData(server));
if (server.canUserControl((User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER)))
{
WebSocketHelper.sendData(socket, getData(server));
}
}
}

public void doSendActionLog(Server server, String line)
{
if (allServers) return;

for (WebSocket socket : getWebSockets())
{
if (((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(SERVER) != server) continue;
if (server.canUserControl((User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER)))
{
WebSocketHelper.sendData(socket, line);
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/templates/server.ftl
Expand Up @@ -88,6 +88,9 @@
</#list>
</ul>
<p class="text-muted">Usernames on the backend. Can start, stop & use console.</p>
<hr>
<h4>Security log</h4>
<textarea title="Security log" id="security-log" class="form-control" rows="4" readonly>${server.actionLog?reverse?join('\n')}</textarea>
</div>
</div>
</div>
Expand Down Expand Up @@ -359,7 +362,14 @@
var temp = JSON.parse(evt.data);
if (temp.status === "ok")
{
updateInfo(temp.data);
if (typeof temp.data === "string")
{
document.getElementById("security-log").innerHTML = temp.data + "\n" + document.getElementById("security-log").innerHTML;
}
else
{
updateInfo(temp.data);
}
}
else
{
Expand Down

0 comments on commit f711887

Please sign in to comment.