From 24aefd47f113f93641d9c90bc23915ee98e74185 Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 28 Mar 2019 17:13:27 +0700 Subject: [PATCH] [FEATURE] MemoryHWIDHandler/JsonFileHWIDHandler --- .../ru/gravit/launchserver/LaunchServer.java | 4 + .../auth/hwid/AcceptHWIDHandler.java | 5 + .../launchserver/auth/hwid/HWIDHandler.java | 3 + .../auth/hwid/JsonFileHWIDHandler.java | 118 ++++++++++++++++++ .../auth/hwid/JsonHWIDHandler.java | 5 + .../auth/hwid/MemoryHWIDHandler.java | 93 ++++++++++++++ .../auth/hwid/MysqlHWIDHandler.java | 5 + .../permissions/ConfigPermissionsHandler.java | 5 + .../DefaultPermissionsHandler.java | 5 + .../JsonFilePermissionsHandler.java | 15 ++- .../JsonLongFilePermissionsHandler.java | 15 ++- .../auth/permissions/PermissionsHandler.java | 2 + .../java/ru/gravit/launcher/OshiHWID.java | 31 +++++ .../gravit/utils/command/CommandHandler.java | 4 + 14 files changed, 300 insertions(+), 10 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MemoryHWIDHandler.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index f5ca38f7..f3299a08 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -68,6 +68,8 @@ public void reload() throws Exception { for (AuthProviderPair auth : config.auth) { auth.init(); } + config.permissionsHandler.init(); + config.hwidHandler.init(); } public static final class Config { @@ -460,6 +462,8 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE for (AuthProviderPair provider : config.auth) { provider.init(); } + config.permissionsHandler.init(); + config.hwidHandler.init(); if(config.protectHandler != null) { config.protectHandler.checkLaunchServerLicense(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/AcceptHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/AcceptHWIDHandler.java index ee657875..06f29c91 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/AcceptHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/AcceptHWIDHandler.java @@ -22,6 +22,11 @@ public void close() { //SKIP } + @Override + public void init() { + + } + @Override public List getHwid(String username) { return new ArrayList<>(); 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 2e816879..e72f7ef2 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 @@ -24,6 +24,7 @@ public static void registerHandlers() { registerHandler("accept", AcceptHWIDHandler.class); registerHandler("mysql", MysqlHWIDHandler.class); registerHandler("json", JsonHWIDHandler.class); + registerHandler("memory", MemoryHWIDHandler.class); registredHandl = true; } } @@ -40,6 +41,8 @@ public void check(HWID hwid, String username) throws HWIDException { @Override public abstract void close() throws Exception; + public abstract void init(); + public abstract List getHwid(String username) throws HWIDException; public abstract void unban(List hwid) throws HWIDException; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java new file mode 100644 index 00000000..d8b6c6a8 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonFileHWIDHandler.java @@ -0,0 +1,118 @@ +package ru.gravit.launchserver.auth.hwid; + +import com.google.gson.reflect.TypeToken; +import ru.gravit.launcher.ClientPermissions; +import ru.gravit.launcher.HWID; +import ru.gravit.launcher.Launcher; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +public class JsonFileHWIDHandler extends HWIDHandler { + public class Entry + { + public HWID hwid; + public String username; + public boolean isBanned = false; + + public Entry(HWID hwid) { + this.hwid = hwid; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Entry entry = (Entry) o; + return Objects.equals(hwid, entry.hwid); + } + + @Override + public int hashCode() { + return Objects.hash(hwid); + } + } + public String filename = "hwids.json"; + public transient LinkedList list = new LinkedList<>(); + public String banMessage = "You banned"; + @Override + public void ban(List hwid) throws HWIDException { + for(Entry e : list) + { + for(HWID banHWID : hwid) + { + if(e.hwid.equals(banHWID)) e.isBanned = true; + } + } + } + @Override + public void init() + { + Path path = Paths.get(filename); + Type type = new TypeToken>() { + }.getType(); + try (Reader reader = IOHelper.newReader(path)) { + list = Launcher.gson.fromJson(reader, type); + } catch (IOException e) { + LogHelper.error(e); + } + } + + @Override + public void check0(HWID hwid, String username) throws HWIDException { + boolean isOne = false; + for(Entry e : list) + { + if(e.hwid.equals(hwid)) + { + isOne = true; + if(e.isBanned) throw new HWIDException(banMessage); + } + } + if(!isOne) + { + list.add(new Entry(hwid)); + } + } + + @Override + public void close() throws Exception { + Path path = Paths.get(filename); + try(Writer writer = IOHelper.newWriter(path)) + { + LaunchServer.gson.toJson(list, writer); + } + } + + @Override + public List getHwid(String username) throws HWIDException { + LinkedList hwids = new LinkedList<>(); + for(Entry e : list) + { + if(e.username.equals(username)) hwids.add(e.hwid); + } + return hwids; + } + + @Override + public void unban(List hwid) throws HWIDException { + for(Entry e : list) + { + for(HWID banHWID : hwid) + { + if(e.hwid.equals(banHWID)) e.isBanned = false; + } + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java index 0b222231..de0d1536 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java @@ -111,6 +111,11 @@ public void close() { // pass } + @Override + public void init() { + + } + @Override public List getHwid(String username) throws HWIDException { ArrayList hwids = new ArrayList<>(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MemoryHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MemoryHWIDHandler.java new file mode 100644 index 00000000..07c7de02 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MemoryHWIDHandler.java @@ -0,0 +1,93 @@ +package ru.gravit.launchserver.auth.hwid; + +import ru.gravit.launcher.HWID; + +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +public class MemoryHWIDHandler extends HWIDHandler { + public class Entry + { + public HWID hwid; + public String username; + public boolean isBanned = false; + + public Entry(HWID hwid) { + this.hwid = hwid; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Entry entry = (Entry) o; + return Objects.equals(hwid, entry.hwid); + } + + @Override + public int hashCode() { + return Objects.hash(hwid); + } + } + public transient LinkedList list = new LinkedList<>(); + public String banMessage = "You banned"; + @Override + public void ban(List hwid) throws HWIDException { + for(Entry e : list) + { + for(HWID banHWID : hwid) + { + if(e.hwid.equals(banHWID)) e.isBanned = true; + } + } + } + + @Override + public void check0(HWID hwid, String username) throws HWIDException { + boolean isOne = false; + for(Entry e : list) + { + if(e.hwid.equals(hwid)) + { + isOne = true; + if(e.isBanned) throw new HWIDException(banMessage); + } + } + if(!isOne) + { + list.add(new Entry(hwid)); + } + } + + @Override + public void close() throws Exception { + + } + + @Override + public void init() { + + } + + @Override + public List getHwid(String username) throws HWIDException { + LinkedList hwids = new LinkedList<>(); + for(Entry e : list) + { + if(e.username.equals(username)) hwids.add(e.hwid); + } + return hwids; + } + + @Override + public void unban(List hwid) throws HWIDException { + for(Entry e : list) + { + for(HWID banHWID : hwid) + { + if(e.hwid.equals(banHWID)) e.isBanned = false; + } + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java index 1201815b..a7dbd40f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java @@ -250,4 +250,9 @@ public List getHwid(String username) { public void close() { mySQLHolder.close(); } + + @Override + public void init() { + + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java index 4ffe4a74..44f12eff 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/ConfigPermissionsHandler.java @@ -6,6 +6,11 @@ public class ConfigPermissionsHandler extends PermissionsHandler { public boolean isAdmin = false; public boolean isServer = false; + @Override + public void init() { + + } + @Override public ClientPermissions getPermissions(String username) { ClientPermissions permissions = new ClientPermissions(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java index b434bb5e..4b14873b 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/DefaultPermissionsHandler.java @@ -3,6 +3,11 @@ import ru.gravit.launcher.ClientPermissions; public class DefaultPermissionsHandler extends PermissionsHandler { + @Override + public void init() { + + } + @Override public ClientPermissions getPermissions(String username) { return ClientPermissions.DEFAULT; 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 c46c1857..bd3903f2 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 @@ -44,11 +44,7 @@ public static class Enity { } @Override - public ClientPermissions getPermissions(String username) { - return map.getOrDefault(username, ClientPermissions.DEFAULT); - } - - public JsonFilePermissionsHandler() { + public void init() { Type type = new TypeToken>() { }.getType(); Path path = Paths.get(filename); @@ -66,4 +62,13 @@ public JsonFilePermissionsHandler() { LogHelper.error(e); } } + + @Override + public ClientPermissions getPermissions(String username) { + return map.getOrDefault(username, ClientPermissions.DEFAULT); + } + + public JsonFilePermissionsHandler() { + + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java index d7d98402..ada99916 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonLongFilePermissionsHandler.java @@ -45,11 +45,7 @@ public static class Enity { } @Override - public ClientPermissions getPermissions(String username) { - return new ClientPermissions(map.getOrDefault(username, defaultPerms)); - } - - public JsonLongFilePermissionsHandler() { + public void init() { Type type = new TypeToken>() { }.getType(); Path path = Paths.get(filename); @@ -67,4 +63,13 @@ public JsonLongFilePermissionsHandler() { LogHelper.error(e); } } + + @Override + public ClientPermissions getPermissions(String username) { + return new ClientPermissions(map.getOrDefault(username, defaultPerms)); + } + + public JsonLongFilePermissionsHandler() { + + } } 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 index d7058b71..d3898138 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java @@ -38,5 +38,7 @@ public static void registerHandlers() { } } + public abstract void init(); + public abstract ClientPermissions getPermissions(String username); } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java b/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java index 5d52e491..dabdacea 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java @@ -2,6 +2,9 @@ import com.google.gson.Gson; +import java.util.Objects; +import java.util.StringJoiner; + public class OshiHWID implements HWID { public static Gson gson = new Gson(); @LauncherAPI @@ -53,4 +56,32 @@ public int compare(HWID hwid) { public boolean isNull() { return getLevel() < 15; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + OshiHWID oshiHWID = (OshiHWID) o; + return totalMemory == oshiHWID.totalMemory && + Objects.equals(serialNumber, oshiHWID.serialNumber) && + Objects.equals(HWDiskSerial, oshiHWID.HWDiskSerial) && + Objects.equals(processorID, oshiHWID.processorID) && + Objects.equals(macAddr, oshiHWID.macAddr); + } + + @Override + public int hashCode() { + return Objects.hash(totalMemory, serialNumber, HWDiskSerial, processorID, macAddr); + } + + @Override + public String toString() { + return new StringJoiner(", ", OshiHWID.class.getSimpleName() + "[", "]") + .add("totalMemory=" + totalMemory) + .add("serialNumber='" + serialNumber + "'") + .add("HWDiskSerial='" + HWDiskSerial + "'") + .add("processorID='" + processorID + "'") + .add("macAddr='" + macAddr + "'") + .toString(); + } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java index 8739cd42..0e2c5a0d 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java @@ -76,6 +76,10 @@ public void registerCommand(String name, Command command) { VerifyHelper.putIfAbsent(commands, name.toLowerCase(), Objects.requireNonNull(command, "command"), String.format("Command has been already registered: '%s'", name.toLowerCase())); } + public void unregisterCommand(String name) + { + commands.remove(name); + } @Override public void run() {