mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 02:36:41 +00:00
Implemented guest BLOCK and ALLOW options #25
This commit is contained in:
parent
950f6b6b4b
commit
1435d31d36
@ -160,8 +160,6 @@ public class GameServer implements Runnable {
|
|||||||
uTime = System.currentTimeMillis() - startTime;
|
uTime = System.currentTimeMillis() - startTime;
|
||||||
waitTime = config.getInt("tick_length") - uTime;
|
waitTime = config.getInt("tick_length") - uTime;
|
||||||
|
|
||||||
// LogManager.LOGGER.info("Wait time : " + waitTime + "ms | Update time: " + uTime + "ms | " + (int) (((double) uTime / waitTime) * 100) + "% load");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (waitTime >= 0) {
|
if (waitTime >= 0) {
|
||||||
Thread.sleep(waitTime);
|
Thread.sleep(waitTime);
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package net.simon987.server.web;
|
||||||
|
|
||||||
|
public enum GuestPolicy {
|
||||||
|
/**
|
||||||
|
* Allow guests, must login to have Cubot
|
||||||
|
*/
|
||||||
|
ALLOW,
|
||||||
|
/**
|
||||||
|
* Block guests completely
|
||||||
|
*/
|
||||||
|
BLOCK,
|
||||||
|
/**
|
||||||
|
* Allow guests, can have Cubot, but it is not saved in database
|
||||||
|
*/
|
||||||
|
TEMPORARY
|
||||||
|
}
|
@ -12,6 +12,7 @@ import java.util.Properties;
|
|||||||
public class WebServer {
|
public class WebServer {
|
||||||
|
|
||||||
private SocketServer socketServer;
|
private SocketServer socketServer;
|
||||||
|
private GuestPolicy guestPolicy;
|
||||||
|
|
||||||
public WebServer(ServerConfiguration config) {
|
public WebServer(ServerConfiguration config) {
|
||||||
|
|
||||||
@ -30,15 +31,16 @@ public class WebServer {
|
|||||||
*
|
*
|
||||||
* Certificates generated from Let's Encrypt are usually in /etc/letsencrypt/live/www.site.com
|
* Certificates generated from Let's Encrypt are usually in /etc/letsencrypt/live/www.site.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Spark.secure(
|
Spark.secure(
|
||||||
config.getString("keyStore_path"),
|
config.getString("keyStore_path"),
|
||||||
config.getString("keyStore_password"), null, null);
|
config.getString("keyStore_password"), null, null);
|
||||||
LogManager.LOGGER.info("(Web) Enabled ssl");
|
LogManager.LOGGER.info("(Web) Enabled ssl");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initGuestPolicy(config);
|
||||||
|
|
||||||
|
socketServer = new SocketServer(guestPolicy);
|
||||||
|
|
||||||
socketServer = new SocketServer();
|
|
||||||
Spark.webSocket("/socket", socketServer);
|
Spark.webSocket("/socket", socketServer);
|
||||||
|
|
||||||
Spark.get("/", new HomePage(), templateEngine);
|
Spark.get("/", new HomePage(), templateEngine);
|
||||||
@ -57,7 +59,22 @@ public class WebServer {
|
|||||||
Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
|
Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load guest policy from config.
|
||||||
|
* If no valid policy is specified in the config, the default <code>GuestPolicy.ALLOW</code> is used
|
||||||
|
*/
|
||||||
|
private void initGuestPolicy(ServerConfiguration config) {
|
||||||
|
String guestPolicyStr = config.getString("guest_policy");
|
||||||
|
try {
|
||||||
|
this.guestPolicy = GuestPolicy.valueOf(guestPolicyStr);
|
||||||
|
} catch (IllegalArgumentException | NullPointerException e) {
|
||||||
|
System.err.println("Invalid argument for guest policy: " + guestPolicyStr);
|
||||||
|
this.guestPolicy = GuestPolicy.ALLOW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public SocketServer getSocketServer() {
|
public SocketServer getSocketServer() {
|
||||||
return socketServer;
|
return socketServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import net.simon987.server.GameServer;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.user.User;
|
||||||
|
import net.simon987.server.web.GuestPolicy;
|
||||||
import org.eclipse.jetty.websocket.api.Session;
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
||||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
||||||
@ -24,8 +25,14 @@ public class SocketServer {
|
|||||||
private MessageDispatcher messageDispatcher = new MessageDispatcher();
|
private MessageDispatcher messageDispatcher = new MessageDispatcher();
|
||||||
|
|
||||||
private static final String AUTH_OK_MESSAGE = "{\"t\":\"auth\", \"m\":\"ok\"}";
|
private static final String AUTH_OK_MESSAGE = "{\"t\":\"auth\", \"m\":\"ok\"}";
|
||||||
|
private static final String FORBIDDEN_MESSAGE = "{\"t\":\"auth\", \"m\":\"forbidden\"}";
|
||||||
|
private static final int AUTH_TOKEN_LEN = 128;
|
||||||
|
|
||||||
public SocketServer() {
|
private GuestPolicy guestPolicy;
|
||||||
|
|
||||||
|
public SocketServer(GuestPolicy guestPolicy) {
|
||||||
|
|
||||||
|
this.guestPolicy = guestPolicy;
|
||||||
|
|
||||||
messageDispatcher.addHandler(new UserInfoRequestHandler());
|
messageDispatcher.addHandler(new UserInfoRequestHandler());
|
||||||
messageDispatcher.addHandler(new TerrainRequestHandler());
|
messageDispatcher.addHandler(new TerrainRequestHandler());
|
||||||
@ -34,6 +41,7 @@ public class SocketServer {
|
|||||||
messageDispatcher.addHandler(new CodeRequestHandler());
|
messageDispatcher.addHandler(new CodeRequestHandler());
|
||||||
messageDispatcher.addHandler(new KeypressHandler());
|
messageDispatcher.addHandler(new KeypressHandler());
|
||||||
messageDispatcher.addHandler(new DebugCommandHandler());
|
messageDispatcher.addHandler(new DebugCommandHandler());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnWebSocketConnect
|
@OnWebSocketConnect
|
||||||
@ -52,55 +60,62 @@ public class SocketServer {
|
|||||||
public void onMessage(Session session, String message) {
|
public void onMessage(Session session, String message) {
|
||||||
OnlineUser onlineUser = onlineUserManager.getUser(session);
|
OnlineUser onlineUser = onlineUserManager.getUser(session);
|
||||||
|
|
||||||
if (onlineUser != null) {
|
//Shouldn't happen
|
||||||
|
if (onlineUser == null) {
|
||||||
if (onlineUser.isAuthenticated()) {
|
|
||||||
|
|
||||||
messageDispatcher.dispatch(onlineUser, message);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
LogManager.LOGGER.info("(WS) Received message from unauthenticated user " + session.getRemoteAddress().getAddress());
|
|
||||||
if (message.length() == 128) {
|
|
||||||
|
|
||||||
User user = GameServer.INSTANCE.getUserManager().validateAuthToken(message);
|
|
||||||
|
|
||||||
if (user != null) {
|
|
||||||
|
|
||||||
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
|
|
||||||
|
|
||||||
onlineUser.setUser(user);
|
|
||||||
onlineUser.setAuthenticated(true);
|
|
||||||
|
|
||||||
try {
|
|
||||||
session.getRemote().sendString(AUTH_OK_MESSAGE);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
User guestUser = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(GameServer.INSTANCE.getGameUniverse().getGuestUsername(), false);
|
|
||||||
onlineUser.setUser(guestUser);
|
|
||||||
onlineUser.setAuthenticated(true);
|
|
||||||
onlineUser.getUser().setGuest(true);
|
|
||||||
|
|
||||||
LogManager.LOGGER.info("(WS) Created guest user " +
|
|
||||||
onlineUser.getUser().getUsername() + session.getRemoteAddress().getAddress());
|
|
||||||
|
|
||||||
try {
|
|
||||||
session.getRemote().sendString(AUTH_OK_MESSAGE);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
LogManager.LOGGER.severe("(WS) FIXME: SocketServer:onMessage");
|
LogManager.LOGGER.severe("(WS) FIXME: SocketServer:onMessage");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Authenticated user
|
||||||
|
if (onlineUser.isAuthenticated()) {
|
||||||
|
messageDispatcher.dispatch(onlineUser, message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Handle auth request
|
||||||
|
if (message.length() == AUTH_TOKEN_LEN) {
|
||||||
|
LogManager.LOGGER.info("(WS) Received message from unauthenticated user " + session.getRemoteAddress().getAddress());
|
||||||
|
|
||||||
|
User user = GameServer.INSTANCE.getUserManager().validateAuthToken(message);
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
doPostAuthUser(session, onlineUser, user);
|
||||||
|
} else if (this.guestPolicy != GuestPolicy.BLOCK) {
|
||||||
|
doPostAuthGuest(session, onlineUser);
|
||||||
|
} else {
|
||||||
|
LogManager.LOGGER.info("(WS) Blocked guest user " + session.getRemoteAddress().getAddress());
|
||||||
|
kickOnlineUser(session, onlineUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Ignore other cases
|
||||||
|
}
|
||||||
|
|
||||||
|
private void kickOnlineUser(Session session, OnlineUser onlineUser) {
|
||||||
|
sendString(session, FORBIDDEN_MESSAGE);
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doPostAuthGuest(Session session, OnlineUser onlineUser) {
|
||||||
|
User guestUser = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(GameServer.INSTANCE.getGameUniverse().getGuestUsername(), false);
|
||||||
|
onlineUser.setUser(guestUser);
|
||||||
|
onlineUser.setAuthenticated(true);
|
||||||
|
onlineUser.getUser().setGuest(true);
|
||||||
|
|
||||||
|
LogManager.LOGGER.info("(WS) Created guest user " +
|
||||||
|
onlineUser.getUser().getUsername() + session.getRemoteAddress().getAddress());
|
||||||
|
|
||||||
|
sendString(session, AUTH_OK_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void doPostAuthUser(Session session, OnlineUser onlineUser, User user) {
|
||||||
|
LogManager.LOGGER.info("(WS) User was successfully authenticated: " + user.getUsername());
|
||||||
|
|
||||||
|
onlineUser.setUser(user);
|
||||||
|
onlineUser.setAuthenticated(true);
|
||||||
|
|
||||||
|
sendString(session, AUTH_OK_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,6 +149,14 @@ public class SocketServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendString(Session session, String message) {
|
||||||
|
try {
|
||||||
|
session.getRemote().sendString(message);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendJSONObject(OnlineUser user, JSONObject json) {
|
private void sendJSONObject(OnlineUser user, JSONObject json) {
|
||||||
try {
|
try {
|
||||||
user.getWebSocket().getRemote().sendString((json.toJSONString()));
|
user.getWebSocket().getRemote().sendString((json.toJSONString()));
|
||||||
@ -158,7 +181,6 @@ public class SocketServer {
|
|||||||
private JSONArray intListToJSON(List<Integer> ints) {
|
private JSONArray intListToJSON(List<Integer> ints) {
|
||||||
|
|
||||||
JSONArray jsonInts = new JSONArray();
|
JSONArray jsonInts = new JSONArray();
|
||||||
|
|
||||||
jsonInts.addAll(ints);
|
jsonInts.addAll(ints);
|
||||||
|
|
||||||
return jsonInts;
|
return jsonInts;
|
||||||
|
@ -9,6 +9,8 @@ keyStore_password=
|
|||||||
#Server
|
#Server
|
||||||
mar_address=ws://localhost:4567/socket
|
mar_address=ws://localhost:4567/socket
|
||||||
server_name=MAR dev
|
server_name=MAR dev
|
||||||
|
# ALLOW | TEMPORARY | BLOCK
|
||||||
|
guest_policy=ALLOW
|
||||||
|
|
||||||
#Database
|
#Database
|
||||||
mongo_dbname=mar_beta
|
mongo_dbname=mar_beta
|
||||||
|
9
Server/src/main/resources/static/js/mar.js
vendored
9
Server/src/main/resources/static/js/mar.js
vendored
@ -555,8 +555,9 @@ var AuthListener = (function () {
|
|||||||
console.log("[MAR] Auth successful");
|
console.log("[MAR] Auth successful");
|
||||||
}
|
}
|
||||||
mar.client.requestUserInfo();
|
mar.client.requestUserInfo();
|
||||||
}
|
} else if (message.m == "forbidden") {
|
||||||
else {
|
alert("Authentication failed. Guest accounts are blocked on this server");
|
||||||
|
} else {
|
||||||
alert("Authentication failed. Please make sure you are logged in and reload the page.");
|
alert("Authentication failed. Please make sure you are logged in and reload the page.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -712,7 +713,9 @@ var GameClient = (function () {
|
|||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
console.log("[MAR] Received server info " + xhr.responseText);
|
console.log("[MAR] Received server info " + xhr.responseText);
|
||||||
}
|
}
|
||||||
setTimeout(self.connectToGameServer(JSON.parse(xhr.responseText)), 100);
|
setTimeout(function () {
|
||||||
|
return self.connectToGameServer(JSON.parse(xhr.responseText));
|
||||||
|
}, 100);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
@ -134,6 +134,8 @@ class AuthListener implements MessageListener {
|
|||||||
}
|
}
|
||||||
mar.client.requestUserInfo();
|
mar.client.requestUserInfo();
|
||||||
|
|
||||||
|
} else if (message.m == "forbidden") {
|
||||||
|
alert("Authentication failed. Guest accounts are blocked on this server")
|
||||||
} else {
|
} else {
|
||||||
alert("Authentication failed. Please make sure you are logged in and reload the page.");
|
alert("Authentication failed. Please make sure you are logged in and reload the page.");
|
||||||
}
|
}
|
||||||
@ -343,7 +345,6 @@ class GameClient {
|
|||||||
this.socket.send(JSON.stringify(json));
|
this.socket.send(JSON.stringify(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get server info from game website
|
* Get server info from game website
|
||||||
*/
|
*/
|
||||||
@ -364,7 +365,7 @@ class GameClient {
|
|||||||
console.log("[MAR] Received server info " + xhr.responseText);
|
console.log("[MAR] Received server info " + xhr.responseText);
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(self.connectToGameServer(JSON.parse(xhr.responseText)), 100);
|
setTimeout(() => self.connectToGameServer(JSON.parse(xhr.responseText)), 100);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user