Skip to content

Commit

Permalink
Change to websockets done. Except https
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Nov 19, 2014
1 parent aa108ae commit e33f1ea
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -148,6 +148,7 @@ public static void main(String[] args) throws Exception
ServerconsoleSocketApplication.register();
ConsoleSocketApplication.register();
AdvancedSettingsSocketApplication.register();
UsersSocketApplication.register();

final NetworkListener networkListener = new NetworkListener("unsecured-listener", Strings.isBlank(SETTINGS.hostname) ? NetworkListener.DEFAULT_NETWORK_HOST : SETTINGS.hostname, SETTINGS.portHTTP);
//networkListener.setSecure(true);
Expand Down
@@ -0,0 +1,145 @@
/*
* 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 com.google.common.base.Strings;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.doubledoordev.backend.permissions.User;
import net.doubledoordev.backend.util.Helper;
import net.doubledoordev.backend.util.Settings;
import net.doubledoordev.backend.util.WebSocketHelper;
import org.glassfish.grizzly.http.server.DefaultSessionManager;
import org.glassfish.grizzly.http.server.Session;
import org.glassfish.grizzly.websockets.DefaultWebSocket;
import org.glassfish.grizzly.websockets.WebSocket;
import org.glassfish.grizzly.websockets.WebSocketApplication;
import org.glassfish.grizzly.websockets.WebSocketEngine;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.TimerTask;

import static net.doubledoordev.backend.util.Constants.*;

/**
* @author Dries007
*/
public class UsersSocketApplication extends WebSocketApplication
{
private static final UsersSocketApplication APPLICATION = new UsersSocketApplication();
private static final String URL_PATTERN = "/users/*";

private UsersSocketApplication()
{
TIMER.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run()
{
for (WebSocket socket : getWebSockets()) socket.sendPing("ping".getBytes());
}
}, SOCKET_PING_TIME, SOCKET_PING_TIME);
}

public static void register()
{
WebSocketEngine.getEngine().register(SOCKET_CONTEXT, URL_PATTERN, APPLICATION);
}

@Override
public void onMessage(WebSocket socket, String text)
{
User user = (User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER);
try
{
JsonObject object = JSONPARSER.parse(text).getAsJsonObject();
String name = object.get("method").getAsString();
ArrayList<String> args = new ArrayList<>();
if (object.has("args")) for (JsonElement arg : object.getAsJsonArray("args")) args.add(arg.getAsString());
if (!Helper.invokeWithRefectionMagic(socket, user, name, args))
{
WebSocketHelper.sendOk(socket);
socket.close();
}
}
catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e)
{
WebSocketHelper.sendError(socket, e);
socket.close();
}
}

@Override
public void onConnect(WebSocket socket)
{
Session session = DefaultSessionManager.instance().getSession(null, ((DefaultWebSocket) socket).getUpgradeRequest().getRequestedSessionId());
if (session == null)
{
WebSocketHelper.sendError(socket, "No valid session.");
socket.close();
return;
}
if (!((User) session.getAttribute(USER)).isAdmin())
{
WebSocketHelper.sendError(socket, "You are no admin.");
socket.close();
return;
}
String[] username = ((DefaultWebSocket) socket).getUpgradeRequest().getPathInfo().substring(1).split("/");
if (Strings.isNullOrEmpty(username[0]) || Strings.isNullOrEmpty(username[0]))
{
WebSocketHelper.sendError(socket, "No valid user.");
socket.close();
return;
}
User user = Settings.getUserByName(username[0]);
if (user == null)
{
WebSocketHelper.sendError(socket, "No valid user.");
socket.close();
return;
}
((DefaultWebSocket) socket).getUpgradeRequest().setAttribute(USER, user);
super.onConnect(socket);
}
}
8 changes: 6 additions & 2 deletions src/main/resources/templates/advancedsettings.ftl
Expand Up @@ -73,18 +73,22 @@
<input id="JvmData_jarName" class="form-control" aria-describedby="helpBlock" type="text" placeholder="minecraft_server.jar">
</div>
<div class="form-group">
<label for="JvmData_ramMin">JVM RAM</label>
<label for="JvmData_ramMin">Server RAM</label>
<div class="input-group">
<div class="input-group-addon">Min: </div>
<input id="JvmData_ramMin" class="form-control" aria-describedby="helpBlock" type="number" min="0" placeholder="0">
<div class="input-group-addon">MB</div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Max: </div>
<input id="JvmData_ramMax" class="form-control" aria-describedby="helpBlock" type="number" min="0" placeholder="0">
<div class="input-group-addon">MB</div>
</div>
</div>
<div class="form-group">
<label for="JvmData_permGen">JVM permgen</label>
<label for="JvmData_permGen">Server permgen</label>
<div class="input-group col-sm-6 col-sm-offset-3">
<input id="JvmData_permGen" class="form-control" aria-describedby="helpBlock" type="number" min="0" placeholder="0">
<div class="input-group-addon">MB</div>
Expand Down

0 comments on commit e33f1ea

Please sign in to comment.