diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index d8022245..f01fe5e7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -287,7 +287,7 @@ public static void main(String... args) throws Throwable { public final Path privateKeyFile; @LauncherAPI public final Path updatesDir; - + public static LaunchServer server; @LauncherAPI public final Path profilesDir; // Server config @@ -350,6 +350,7 @@ public LaunchServer(Path dir, boolean portable) throws IOException, InvalidKeySp TextureProvider.registerProviders(); HWIDHandler.registerHandlers(); Response.registerResponses(); + LaunchServer.server = this; // Set command handler CommandHandler localCommandHandler; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java index 2644b359..fb289908 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java @@ -126,6 +126,7 @@ public void run() { //engine.setUseClientMode(false); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); + WebSocketFrameHandler.server = LaunchServer.server; try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java index 58652cb8..96c75328 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java @@ -10,6 +10,7 @@ import io.netty.channel.group.DefaultChannelGroup; import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.util.concurrent.GlobalEventExecutor; +import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.websocket.json.JsonResponse; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.utils.helper.IOHelper; @@ -19,9 +20,11 @@ import java.io.Reader; public class WebSocketFrameHandler extends SimpleChannelInboundHandler { - static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); - static Gson gson; - static GsonBuilder builder = new GsonBuilder(); + public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); + public static Gson gson; + + public static LaunchServer server; + public static GsonBuilder builder = new GsonBuilder(); static { builder.registerTypeAdapter(JsonResponse.class,new JsonResponseAdapter()); gson = builder.create(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java index d8382d92..b905fcdd 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java @@ -2,6 +2,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler; import ru.gravit.utils.helper.LogHelper; public class EchoResponse implements JsonResponse { @@ -19,5 +20,14 @@ public String getType() { @Override public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) { LogHelper.info("Echo: %s",echo); + ctx.channel().write(WebSocketFrameHandler.gson.toJson(new Result(echo))); + } + public class Result + { + String echo; + + public Result(String echo) { + this.echo = echo; + } } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java index 0896c639..0d7ed301 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java @@ -5,5 +5,5 @@ public interface JsonResponse { String getType(); - void execute(ChannelHandlerContext ctx, WebSocketFrame frame); + void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception; } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java new file mode 100644 index 00000000..4e6f46ac --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/AuthResponse.java @@ -0,0 +1,81 @@ +package ru.gravit.launchserver.socket.websocket.json.auth; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import ru.gravit.launcher.profiles.ClientProfile; +import ru.gravit.launcher.serialize.signed.SignedObjectHolder; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.AuthException; +import ru.gravit.launchserver.auth.hwid.HWID; +import ru.gravit.launchserver.auth.hwid.HWIDException; +import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.auth.provider.AuthProviderResult; +import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler; +import ru.gravit.launchserver.socket.websocket.json.JsonResponse; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.Collection; + +public class AuthResponse implements JsonResponse { + public String login; + public String client; + private LaunchServer server = LaunchServer.server; + + public String password; + + public AuthResponse(String login, String password, int authid, HWID hwid) { + this.login = login; + this.password = password; + this.authid = authid; + this.hwid = hwid; + } + + public int authid; + public HWID hwid; + @Override + public String getType() { + return "auth"; + } + + @Override + public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { + try { + String ip = IOHelper.getIP(ctx.channel().remoteAddress()); + if (server.limiter.isLimit(ip)) { + AuthProvider.authError(server.config.authRejectString); + return; + } + AuthProvider provider = server.config.authProvider[authid]; + AuthProviderResult result = provider.auth(login, password, ip); + if (!VerifyHelper.isValidUsername(result.username)) { + AuthProvider.authError(String.format("Illegal result: '%s'", result.username)); + return; + } + Collection> profiles = server.getProfiles(); + for (SignedObjectHolder p : profiles) { + if (p.object.getTitle().equals(client)) { + if (!p.object.isWhitelistContains(login)) { + throw new AuthException(server.config.whitelistRejectString); + } + //clientData.profile = p.object; + } + } + //if(clientData.profile == null) { + // throw new AuthException("You profile not found"); + //} + server.config.hwidHandler.check(hwid, result.username); + } catch (AuthException | HWIDException e) + { + ctx.channel().write(WebSocketFrameHandler.gson.toJson(new ErrorResult(e.getMessage()))); + } + } + public class ErrorResult + { + public ErrorResult(String error) { + this.error = error; + } + + public String error; + } +}