From a606cb23e1e4d527bf77c45558acfbb40c0cb700 Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 26 Dec 2018 18:04:45 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B9=20=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=20Per?= =?UTF-8?q?missionsHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 14 ++++--- .../JsonFilePermissionsHandler.java | 17 ++++++--- .../auth/permissions/PermissionsHandler.java | 37 +++++++++++++++++++ .../config/PermissionsHandlerAdapter.java | 29 +++++++++++++++ 4 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index dddf7985..fa576a75 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -15,6 +15,7 @@ import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler; import ru.gravit.launchserver.auth.hwid.HWIDHandler; import ru.gravit.launchserver.auth.permissions.JsonFilePermissionsHandler; +import ru.gravit.launchserver.auth.permissions.PermissionsHandler; import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.EXEL4JLauncherBinary; @@ -24,10 +25,7 @@ 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.config.*; import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.socket.ServerSocketHandler; @@ -64,6 +62,8 @@ public static final class Config { public AuthProvider[] authProvider; + public PermissionsHandler permissionsHandler; + public TextureProvider textureProvider; public HWIDHandler hwidHandler; @@ -280,6 +280,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { AuthProvider.registerProviders(); TextureProvider.registerProviders(); HWIDHandler.registerHandlers(); + PermissionsHandler.registerHandlers(); Response.registerResponses(); LaunchServer.server = this; @@ -351,8 +352,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { e.printStackTrace(); } }); - JsonFilePermissionsHandler.init(); - PermissionsManager.registerPermissionsFunction(JsonFilePermissionsHandler::getPermissions); // init modules modulesManager.initModules(); @@ -387,6 +386,7 @@ public static void initGson() Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter()); Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter()); Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); + Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); Launcher.gson = Launcher.gsonBuilder.create(); @@ -396,6 +396,7 @@ public static void initGson() LaunchServer.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); + LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); LaunchServer.gson = LaunchServer.gsonBuilder.create(); } @@ -460,6 +461,7 @@ private void generateConfigIfNotExists() throws IOException { newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider("Настройте authProvider")}; newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png","http://example.com/cloaks/%username%.png"); + newConfig.permissionsHandler = new JsonFilePermissionsHandler(); newConfig.port = 7420; newConfig.bindAddress = "0.0.0.0"; newConfig.authRejectString = "Превышен лимит авторизаций"; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java index ff149487..6016d73f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java @@ -3,7 +3,9 @@ import com.google.gson.reflect.TypeToken; import ru.gravit.launcher.Launcher; import ru.gravit.launchserver.auth.ClientPermissions; +import ru.gravit.launchserver.manangers.PermissionsManager; import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.LogHelper; import java.io.IOException; import java.io.Reader; @@ -14,32 +16,37 @@ import java.util.HashMap; import java.util.Map; -public class JsonFilePermissionsHandler { - public static String FILE_NAME = "permissions.json"; +public class JsonFilePermissionsHandler extends PermissionsHandler { + public String filename = "permissions.json"; public static Map map; public static class Enity { public String username; public ClientPermissions permissions; } - public static ClientPermissions getPermissions(String username) + @Override + public ClientPermissions getPermissions(String username) { return map.getOrDefault(username,ClientPermissions.DEFAULT); } - public static void init() throws IOException { + public JsonFilePermissionsHandler() { Type type = new TypeToken>(){}.getType(); - Path path = Paths.get(FILE_NAME); + Path path = Paths.get(filename); if(!IOHelper.exists(path)) { map = new HashMap<>(); try(Writer writer = IOHelper.newWriter(path)) { Launcher.gson.toJson(map,writer); + } catch (IOException e) { + LogHelper.error(e); } } try(Reader reader = IOHelper.newReader(path)) { map = Launcher.gson.fromJson(reader,type); + } catch (IOException e) { + LogHelper.error(e); } } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java new file mode 100644 index 00000000..5079eb07 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java @@ -0,0 +1,37 @@ +package ru.gravit.launchserver.auth.permissions; + +import ru.gravit.launchserver.auth.ClientPermissions; +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +public abstract class PermissionsHandler { + private static final Map PERMISSIONS_HANDLERS = new ConcurrentHashMap<>(4); + private static boolean registredHandl = false; + public static void registerHandler(String name, Class adapter) { + VerifyHelper.verifyIDName(name); + VerifyHelper.putIfAbsent(PERMISSIONS_HANDLERS, name, Objects.requireNonNull(adapter, "adapter"), + String.format("Auth handler has been already registered: '%s'", name)); + } + public static Class getHandlerClass(String name) + { + return PERMISSIONS_HANDLERS.get(name); + } + public static String getHandlerName(Class clazz) + { + for(Map.Entry e: PERMISSIONS_HANDLERS.entrySet()) + { + if(e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } + public static void registerHandlers() { + if (!registredHandl) { + registerHandler("json", JsonFilePermissionsHandler.class); + registredHandl = true; + } + } + public abstract ClientPermissions getPermissions(String username); +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java new file mode 100644 index 00000000..a39b8335 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.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.permissions.PermissionsHandler; + +import java.lang.reflect.Type; + +public class PermissionsHandlerAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "type"; + @Override + public PermissionsHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = PermissionsHandler.getHandlerClass(typename); + + + return (PermissionsHandler) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(PermissionsHandler src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = PermissionsHandler.getHandlerName(src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +}