Тестовый AuthResponse

This commit is contained in:
Gravit 2018-10-01 14:48:24 +07:00
parent 5fa271314c
commit fdb7da1460
6 changed files with 101 additions and 5 deletions

View file

@ -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;

View file

@ -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)

View file

@ -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<WebSocketFrame> {
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();

View file

@ -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;
}
}
}

View file

@ -5,5 +5,5 @@
public interface JsonResponse {
String getType();
void execute(ChannelHandlerContext ctx, WebSocketFrame frame);
void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception;
}

View file

@ -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<SignedObjectHolder<ClientProfile>> profiles = server.getProfiles();
for (SignedObjectHolder<ClientProfile> 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;
}
}