[FEATURE] Реализована ServerWrapper часть вебсокетов

This commit is contained in:
Gravit 2019-04-04 15:16:23 +07:00
parent 27ab69cd78
commit 30645741f4
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
10 changed files with 65 additions and 6 deletions

View file

@ -16,6 +16,7 @@
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;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
@ -121,6 +122,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
result.session = clientData.session; result.session = clientData.session;
} }
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, uuid, aresult.username, client, clientData.auth.textureProvider); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, uuid, aresult.username, client, clientData.auth.textureProvider);
LogHelper.debug("Auth: %s accessToken %s uuid: %s", login, result.accessToken, uuid.toString());
service.sendObject(ctx, result); service.sendObject(ctx, result);
} catch (AuthException | HWIDException e) { } catch (AuthException | HWIDException e) {
service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));

View file

@ -28,6 +28,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
result.uuid = pClient.auth.handler.checkServer(username, serverID); result.uuid = pClient.auth.handler.checkServer(username, serverID);
if (result.uuid != null) if (result.uuid != null)
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, result.uuid, username, client, pClient.auth.textureProvider); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, result.uuid, username, client, pClient.auth.textureProvider);
LogHelper.debug("checkServer: %s uuid: %s serverID: %s", result.playerProfile.username, result.uuid.toString(), serverID);
} catch (AuthException e) { } catch (AuthException e) {
service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
return; return;
@ -36,7 +37,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error")); service.sendObject(ctx, new ErrorRequestEvent("Internal authHandler error"));
return; return;
} }
service.sendObject(ctx, new CheckServerRequestEvent()); service.sendObject(ctx, result);
} }
} }

View file

@ -30,6 +30,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
success = LaunchServer.server.config.getAuthProviderPair().handler.joinServer(username, accessToken, serverID); success = LaunchServer.server.config.getAuthProviderPair().handler.joinServer(username, accessToken, serverID);
} }
else success = client.auth.handler.joinServer(username, accessToken, serverID); else success = client.auth.handler.joinServer(username, accessToken, serverID);
LogHelper.debug("joinServer: %s accessToken: %s serverID: %s", username, accessToken, serverID);
} catch (AuthException e) { } catch (AuthException e) {
service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
return; return;

View file

@ -19,7 +19,7 @@ public String getType() {
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
if (!client.checkSign) { if (!client.checkSign && !client.isAuth) {
service.sendObject(ctx, new ErrorRequestEvent("Access denied")); service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return; return;
} }

View file

@ -16,14 +16,24 @@
import java.io.IOException; import java.io.IOException;
public final class AuthRequest extends Request<AuthRequestEvent> implements RequestInterface { public final class AuthRequest extends Request<AuthRequestEvent> implements RequestInterface {
@LauncherNetworkAPI
private final String login; private final String login;
@LauncherNetworkAPI
private final byte[] encryptedPassword; private final byte[] encryptedPassword;
@LauncherNetworkAPI
private final String auth_id; private final String auth_id;
@LauncherNetworkAPI
private final HWID hwid; private final HWID hwid;
@LauncherNetworkAPI
private final String customText; private final String customText;
@LauncherNetworkAPI
private final boolean getSession; private final boolean getSession;
@LauncherNetworkAPI
private final ConnectTypes authType;
public enum ConnectTypes {
SERVER, CLIENT, BOT
}
@LauncherAPI @LauncherAPI
public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid) { public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hwid) {
@ -34,6 +44,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
customText = ""; customText = "";
auth_id = ""; auth_id = "";
getSession = true; getSession = true;
authType = ConnectTypes.CLIENT;
} }
@LauncherAPI @LauncherAPI
@ -45,6 +56,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
this.auth_id = auth_id; this.auth_id = auth_id;
customText = ""; customText = "";
getSession = true; getSession = true;
authType = ConnectTypes.CLIENT;
} }
@LauncherAPI @LauncherAPI
@ -56,6 +68,7 @@ public AuthRequest(LauncherConfig config, String login, byte[] password, HWID hw
this.auth_id = auth_id; this.auth_id = auth_id;
this.customText = customText; this.customText = customText;
getSession = true; getSession = true;
authType = ConnectTypes.CLIENT;
} }
@LauncherAPI @LauncherAPI
@ -63,6 +76,16 @@ public AuthRequest(String login, byte[] password, HWID hwid) {
this(null, login, password, hwid); this(null, login, password, hwid);
} }
public AuthRequest(String login, byte[] encryptedPassword, String auth_id, ConnectTypes authType) {
this.login = login;
this.encryptedPassword = encryptedPassword;
this.auth_id = auth_id;
this.authType = authType;
this.hwid = null;
this.customText = "";
this.getSession = false;
}
@Override @Override
public AuthRequestEvent requestWebSockets() throws Exception { public AuthRequestEvent requestWebSockets() throws Exception {
return (AuthRequestEvent) LegacyRequestBridge.sendRequest(this); return (AuthRequestEvent) LegacyRequestBridge.sendRequest(this);

View file

@ -1,6 +1,7 @@
package ru.gravit.launcher.request.websockets; package ru.gravit.launcher.request.websockets;
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -16,6 +17,10 @@ public JsonRequestAdapter(ClientWebSocketService service) {
public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends RequestInterface> cls = service.getRequestClass(typename); Class<? extends RequestInterface> cls = service.getRequestClass(typename);
if(cls == null)
{
LogHelper.error("Request type %s not found", typename);
}
return (RequestInterface) context.deserialize(json, cls); return (RequestInterface) context.deserialize(json, cls);

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launcher.request.ResultInterface; import ru.gravit.launcher.request.ResultInterface;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -17,6 +18,10 @@ public JsonResultAdapter(ClientWebSocketService service) {
public ResultInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public ResultInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends ResultInterface> cls = service.getResultClass(typename); Class<? extends ResultInterface> cls = service.getResultClass(typename);
if(cls == null)
{
LogHelper.error("Result type %s not found", typename);
}
return (ResultInterface) context.deserialize(json, cls); return (ResultInterface) context.deserialize(json, cls);

View file

@ -7,6 +7,7 @@
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.events.request.ProfilesRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.request.auth.AuthRequest;
import ru.gravit.launcher.request.auth.AuthServerRequest; import ru.gravit.launcher.request.auth.AuthServerRequest;
import ru.gravit.launcher.request.update.ProfilesRequest; import ru.gravit.launcher.request.update.ProfilesRequest;
import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.launcher.server.setup.ServerWrapperSetup;
@ -51,7 +52,13 @@ public ServerWrapper(Type type, Path configPath) {
public boolean auth() { public boolean auth() {
try { try {
LauncherConfig cfg = Launcher.getConfig(); LauncherConfig cfg = Launcher.getConfig();
if(!config.websocket.enabled)
permissions = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, config.title).request(); permissions = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, config.title).request();
else
{
AuthRequest request = new AuthRequest(config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, AuthRequest.ConnectTypes.SERVER);
permissions = request.request().permissions;
}
ProfilesRequestEvent result = new ProfilesRequest(cfg).request(); ProfilesRequestEvent result = new ProfilesRequest(cfg).request();
for (ClientProfile p : result.profiles) { for (ClientProfile p : result.profiles) {
LogHelper.debug("Get profile: %s", p.getTitle()); LogHelper.debug("Get profile: %s", p.getTitle());
@ -184,6 +191,12 @@ public void updateLauncherConfig() {
LauncherConfig cfg = null; LauncherConfig cfg = null;
try { try {
cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname); cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname);
if(config.websocket != null && config.websocket.enabled)
{
cfg.isNettyEnabled = true;
cfg.nettyAddress = config.websocket.address;
cfg.nettyPort = 1111;
}
} catch (InvalidKeySpecException | IOException e) { } catch (InvalidKeySpecException | IOException e) {
LogHelper.error(e); LogHelper.error(e);
} }
@ -216,6 +229,9 @@ public Config getDefaultConfig() {
newConfig.stopOnError = true; newConfig.stopOnError = true;
newConfig.reconnectCount = 10; newConfig.reconnectCount = 10;
newConfig.reconnectSleep = 1000; newConfig.reconnectSleep = 1000;
newConfig.websocket = new WebSocketConf();
newConfig.websocket.address = "ws://localhost:9274/api";
newConfig.websocket.enabled = false;
newConfig.env = LauncherConfig.LauncherEnvironment.STD; newConfig.env = LauncherConfig.LauncherEnvironment.STD;
return newConfig; return newConfig;
} }
@ -229,6 +245,7 @@ public static final class Config {
public String title; public String title;
public String projectname; public String projectname;
public String address; public String address;
public WebSocketConf websocket;
public int port; public int port;
public int reconnectCount; public int reconnectCount;
public int reconnectSleep; public int reconnectSleep;
@ -246,6 +263,11 @@ public static final class Config {
public String auth_id = ""; public String auth_id = "";
public LauncherConfig.LauncherEnvironment env; public LauncherConfig.LauncherEnvironment env;
} }
public static final class WebSocketConf
{
public boolean enabled;
public String address;
}
public ClientProfile profile; public ClientProfile profile;
} }

View file

@ -38,7 +38,7 @@ public static AutogenConfig getAutogenConfig() {
public final boolean isUsingWrapper; public final boolean isUsingWrapper;
public final boolean isDownloadJava; public final boolean isDownloadJava;
public final boolean isWarningMissArchJava; public final boolean isWarningMissArchJava;
public final boolean isNettyEnabled; public boolean isNettyEnabled;
public final String guardLicenseName; public final String guardLicenseName;
public final String guardLicenseKey; public final String guardLicenseKey;

View file

@ -28,6 +28,6 @@ public UUID getUUID() {
@Override @Override
public String getType() { public String getType() {
return "checkServe"; return "checkServer";
} }
} }