From 137b83f428d8081c13e40f54bf05c156328fa0f3 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 24 Dec 2018 00:19:43 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=BE=D0=B2=20=D0=BD=D0=B0?= =?UTF-8?q?=20Json=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=206=20=D0=90=D0=B4?= =?UTF-8?q?=D0=B0=D0=BF=D1=82=D0=B5=D1=80=D1=8B=20=D0=9A=D0=BE=D0=BC=D0=BF?= =?UTF-8?q?=D0=BB=D0=B8=D1=80=D1=83=D0=B5=D1=82=D1=81=D1=8F,=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BA=D0=B0=20=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 16 ++++++++++ .../auth/handler/AuthHandler.java | 12 ++++++++ .../launchserver/auth/hwid/HWIDHandler.java | 13 ++++++++ .../auth/provider/AuthProvider.java | 13 ++++++++ .../config/AuthHandlerAdapter.java | 29 ++++++++++++++++++ .../config/AuthProviderAdapter.java | 30 +++++++++++++++++++ .../config/HWIDHandlerAdapter.java | 29 ++++++++++++++++++ .../config/TextureProviderAdapter.java | 29 ++++++++++++++++++ .../launchserver/texture/TextureProvider.java | 13 ++++++++ 9 files changed, 184 insertions(+) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index f0bef164..bf2d8e88 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -10,8 +10,11 @@ import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launchserver.auth.AuthLimiter; import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.auth.handler.MemoryAuthHandler; +import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler; import ru.gravit.launchserver.auth.hwid.HWIDHandler; import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.EXEL4JLauncherBinary; import ru.gravit.launchserver.binary.EXELauncherBinary; import ru.gravit.launchserver.binary.JARLauncherBinary; @@ -19,6 +22,10 @@ import ru.gravit.launchserver.command.handler.CommandHandler; import ru.gravit.launchserver.command.handler.JLineCommandHandler; import ru.gravit.launchserver.command.handler.StdCommandHandler; +import ru.gravit.launchserver.config.AuthHandlerAdapter; +import ru.gravit.launchserver.config.AuthProviderAdapter; +import ru.gravit.launchserver.config.HWIDHandlerAdapter; +import ru.gravit.launchserver.config.TextureProviderAdapter; import ru.gravit.launchserver.manangers.BuildHookManager; import ru.gravit.launchserver.manangers.MirrorManager; import ru.gravit.launchserver.manangers.ModulesManager; @@ -361,6 +368,10 @@ public static void initGson() if(Launcher.gson != null) return; Launcher.gsonBuilder = new GsonBuilder(); Launcher.gsonBuilder.setPrettyPrinting(); + Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter()); + Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter()); + Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); + Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); Launcher.gson = Launcher.gsonBuilder.create(); } @@ -412,6 +423,11 @@ private void generateConfigIfNotExists() throws IOException { newConfig.launch4j = new ExeConf(); newConfig.buildPostTransform = new PostBuildTransformConf(); newConfig.env = LauncherConfig.LauncherEnvironment.STD; + newConfig.authHandler = new AuthHandler[]{new MemoryAuthHandler()}; + newConfig.hwidHandler = new AcceptHWIDHandler(); + newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider()}; + newConfig.port = 7420; + newConfig.bindAddress = "0.0.0.0"; //try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launchserver/defaults/config.cfg"))) { // newConfig = Launcher.gson.fromJson(reader,Config.class); //} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java index a07b3f62..77b568b0 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java @@ -25,6 +25,18 @@ public static void registerHandler(String name, Class adapter) { VerifyHelper.putIfAbsent(AUTH_HANDLERS, name, Objects.requireNonNull(adapter, "adapter"), String.format("Auth handler has been already registered: '%s'", name)); } + public static Class getHandlerClass(String name) + { + return AUTH_HANDLERS.get(name); + } + public static String getHandlerName(Class clazz) + { + for(Map.Entry e: AUTH_HANDLERS.entrySet()) + { + if(e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } public static void registerHandlers() { if (!registredHandl) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java index cfd2a43f..0dc364fb 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java @@ -43,4 +43,17 @@ public void check(HWID hwid, String username) throws HWIDException { public abstract List getHwid(String username) throws HWIDException; public abstract void unban(List hwid) throws HWIDException; + + public static Class getHandlerClass(String name) + { + return HW_HANDLERS.get(name); + } + public static String getHandlerName(Class clazz) + { + for(Map.Entry e: HW_HANDLERS.entrySet()) + { + if(e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java index df3842f4..937c5bdb 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java @@ -50,4 +50,17 @@ public AuthHandler getAccociateHandler(int this_position) { @Override public abstract void close() throws IOException; + + public static Class getProviderClass(String name) + { + return AUTH_PROVIDERS.get(name); + } + public static String getProviderName(Class clazz) + { + for(Map.Entry e: AUTH_PROVIDERS.entrySet()) + { + if(e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java new file mode 100644 index 00000000..e194f32d --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java @@ -0,0 +1,29 @@ +package ru.gravit.launchserver.config; + +import com.google.gson.*; +import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; + +import java.lang.reflect.Type; + +public class AuthHandlerAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "type"; + @Override + public AuthHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = AuthHandler.getHandlerClass(typename); + + + return (AuthHandler) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(AuthHandler src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = AuthHandler.getHandlerName(src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java new file mode 100644 index 00000000..26e8687e --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java @@ -0,0 +1,30 @@ +package ru.gravit.launchserver.config; + +import com.google.gson.*; +import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; + +import java.lang.reflect.Type; + +public class AuthProviderAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "type"; + @Override + public AuthProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = AuthProvider.getProviderClass(typename); + + + return (AuthProvider) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(AuthProvider src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = AuthProvider.getProviderName(src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java new file mode 100644 index 00000000..353a87e6 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java @@ -0,0 +1,29 @@ +package ru.gravit.launchserver.config; + +import com.google.gson.*; +import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.auth.hwid.HWIDHandler; + +import java.lang.reflect.Type; + +public class HWIDHandlerAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "type"; + @Override + public HWIDHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = HWIDHandler.getHandlerClass(typename); + + + return (HWIDHandler) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(HWIDHandler src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = HWIDHandler.getHandlerName(src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java new file mode 100644 index 00000000..f4b64b8c --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java @@ -0,0 +1,29 @@ +package ru.gravit.launchserver.config; + +import com.google.gson.*; +import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.texture.TextureProvider; + +import java.lang.reflect.Type; + +public class TextureProviderAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "type"; + @Override + public TextureProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = TextureProvider.getProviderClass(typename); + + + return (TextureProvider) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(TextureProvider src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = TextureProvider.getProviderName(src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/texture/TextureProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/texture/TextureProvider.java index d443db56..7f22515d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/texture/TextureProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/texture/TextureProvider.java @@ -38,4 +38,17 @@ public static void registerProviders() { public abstract Texture getSkinTexture(UUID uuid, String username, String client) throws IOException; + + public static Class getProviderClass(String name) + { + return TEXTURE_PROVIDERS.get(name); + } + public static String getProviderName(Class clazz) + { + for(Map.Entry e: TEXTURE_PROVIDERS.entrySet()) + { + if(e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } }