Сохранение данных в сессии

This commit is contained in:
Gravit 2018-10-01 16:42:42 +07:00
parent 6f16819cad
commit bf061bfaf2
6 changed files with 25 additions and 13 deletions

View file

@ -2,36 +2,42 @@
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup; import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.AttributeKey;
import io.netty.util.AttributeMap;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> { public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static LaunchServer server; public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder(); public static GsonBuilder builder = new GsonBuilder();
public static WebSocketService service = new WebSocketService(LaunchServer.server,builder); public static WebSocketService service = new WebSocketService(LaunchServer.server,builder);
private Client client;
static { static {
service.registerResponses(); service.registerResponses();
} }
@Override @Override
public void channelActive(ChannelHandlerContext ctx) { public void channelActive(ChannelHandlerContext ctx) {
LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress())); LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
client = new Client(0);
channels.add(ctx.channel()); channels.add(ctx.channel());
} }
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
// ping and pong frames already handled // ping and pong frames already handled
if (frame instanceof TextWebSocketFrame) { if (frame instanceof TextWebSocketFrame) {
service.process(ctx, (TextWebSocketFrame) frame); service.process(ctx, (TextWebSocketFrame) frame, client);
} else { } else {
String message = "unsupported frame type: " + frame.getClass().getName(); String message = "unsupported frame type: " + frame.getClass().getName();
throw new UnsupportedOperationException(message); throw new UnsupportedOperationException(message);

View file

@ -5,6 +5,7 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.json.EchoResponse; import ru.gravit.launchserver.socket.websocket.json.EchoResponse;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
@ -26,12 +27,12 @@ public WebSocketService(LaunchServer server, GsonBuilder gson) {
private final Gson gson; private final Gson gson;
private final GsonBuilder gsonBuiler; private final GsonBuilder gsonBuiler;
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame) void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client)
{ {
String request = frame.text(); String request = frame.text();
JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class); JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class);
try { try {
response.execute(this,ctx); response.execute(this,ctx,client);
} catch (Exception e) } catch (Exception e)
{ {
LogHelper.error(e); LogHelper.error(e);

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json; package ru.gravit.launchserver.socket.websocket.json;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -17,8 +18,8 @@ public String getType() {
} }
@Override @Override
public void execute(WebSocketService service,ChannelHandlerContext ctx) { public void execute(WebSocketService service,ChannelHandlerContext ctx, Client client) {
LogHelper.info("Echo: %s",echo); LogHelper.info("Echo: %s, isAuth %s",echo,client.isAuth ? "true" : "false");
service.sendObject(ctx,new Result(echo)); service.sendObject(ctx,new Result(echo));
} }
public class Result public class Result

View file

@ -2,9 +2,10 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
public interface JsonResponseInterface { public interface JsonResponseInterface {
String getType(); String getType();
void execute(WebSocketService service,ChannelHandlerContext ctx) throws Exception; void execute(WebSocketService service,ChannelHandlerContext ctx, Client client) throws Exception;
} }

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json; package ru.gravit.launchserver.socket.websocket.json;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
public class SimpleResponse implements JsonResponseInterface { public class SimpleResponse implements JsonResponseInterface {
@ -10,7 +11,7 @@ public String getType() {
} }
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
} }
} }

View file

@ -9,6 +9,7 @@
import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.hwid.HWIDException;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
@ -37,7 +38,7 @@ public String getType() {
} }
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client clientData) throws Exception {
try { try {
String ip = IOHelper.getIP(ctx.channel().remoteAddress()); String ip = IOHelper.getIP(ctx.channel().remoteAddress());
if (LaunchServer.server.limiter.isLimit(ip)) { if (LaunchServer.server.limiter.isLimit(ip)) {
@ -56,13 +57,14 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx) throws
if (!p.object.isWhitelistContains(login)) { if (!p.object.isWhitelistContains(login)) {
throw new AuthException(LaunchServer.server.config.whitelistRejectString); throw new AuthException(LaunchServer.server.config.whitelistRejectString);
} }
//clientData.profile = p.object; clientData.profile = p.object;
} }
} }
//if(clientData.profile == null) { if(clientData.profile == null) {
// throw new AuthException("You profile not found"); throw new AuthException("You profile not found");
//} }
LaunchServer.server.config.hwidHandler.check(hwid, result.username); LaunchServer.server.config.hwidHandler.check(hwid, result.username);
clientData.isAuth = true;
service.sendObject(ctx,new WebSocketService.SuccessResult("auth")); service.sendObject(ctx,new WebSocketService.SuccessResult("auth"));
} catch (AuthException | HWIDException e) } catch (AuthException | HWIDException e)
{ {