[FEATURE] MemoryHWIDHandler/JsonFileHWIDHandler

This commit is contained in:
Gravit 2019-03-28 17:13:27 +07:00
parent 695758aa18
commit 24aefd47f1
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
14 changed files with 300 additions and 10 deletions

View file

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

View file

@ -22,6 +22,11 @@ public void close() {
//SKIP
}
@Override
public void init() {
}
@Override
public List<HWID> getHwid(String username) {
return new ArrayList<>();

View file

@ -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<HWID> getHwid(String username) throws HWIDException;
public abstract void unban(List<HWID> hwid) throws HWIDException;

View file

@ -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<Entry> list = new LinkedList<>();
public String banMessage = "You banned";
@Override
public void ban(List<HWID> 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<LinkedList<Entry>>() {
}.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<HWID> getHwid(String username) throws HWIDException {
LinkedList<HWID> hwids = new LinkedList<>();
for(Entry e : list)
{
if(e.username.equals(username)) hwids.add(e.hwid);
}
return hwids;
}
@Override
public void unban(List<HWID> hwid) throws HWIDException {
for(Entry e : list)
{
for(HWID banHWID : hwid)
{
if(e.hwid.equals(banHWID)) e.isBanned = false;
}
}
}
}

View file

@ -111,6 +111,11 @@ public void close() {
// pass
}
@Override
public void init() {
}
@Override
public List<HWID> getHwid(String username) throws HWIDException {
ArrayList<HWID> hwids = new ArrayList<>();

View file

@ -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<Entry> list = new LinkedList<>();
public String banMessage = "You banned";
@Override
public void ban(List<HWID> 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<HWID> getHwid(String username) throws HWIDException {
LinkedList<HWID> hwids = new LinkedList<>();
for(Entry e : list)
{
if(e.username.equals(username)) hwids.add(e.hwid);
}
return hwids;
}
@Override
public void unban(List<HWID> hwid) throws HWIDException {
for(Entry e : list)
{
for(HWID banHWID : hwid)
{
if(e.hwid.equals(banHWID)) e.isBanned = false;
}
}
}
}

View file

@ -250,4 +250,9 @@ public List<HWID> getHwid(String username) {
public void close() {
mySQLHolder.close();
}
@Override
public void init() {
}
}

View file

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

View file

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

View file

@ -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<Map<String, ClientPermissions>>() {
}.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() {
}
}

View file

@ -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<Map<String, ClientPermissions>>() {
}.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() {
}
}

View file

@ -38,5 +38,7 @@ public static void registerHandlers() {
}
}
public abstract void init();
public abstract ClientPermissions getPermissions(String username);
}

View file

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

View file

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