diff --git a/LaunchServer/source/LaunchServer.java b/LaunchServer/source/LaunchServer.java index e8525a7..44fe876 100644 --- a/LaunchServer/source/LaunchServer.java +++ b/LaunchServer/source/LaunchServer.java @@ -183,7 +183,7 @@ LogHelper.info("Reading IP Connection List file"); try { - AuthLimiterIPConfig.load(ipConfigFile.toFile()); + AuthLimiterIPConfig.load(ipConfigFile); } catch (Exception error) { diff --git a/LaunchServer/source/auth/limiter/AuthLimiterIPConfig.java b/LaunchServer/source/auth/limiter/AuthLimiterIPConfig.java index 5b0e53f..091de45 100644 --- a/LaunchServer/source/auth/limiter/AuthLimiterIPConfig.java +++ b/LaunchServer/source/auth/limiter/AuthLimiterIPConfig.java @@ -2,43 +2,44 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonIOException; +import com.google.gson.JsonSyntaxException; import com.google.gson.annotations.Expose; -import com.google.gson.stream.JsonReader; +import launcher.helper.IOHelper; import launcher.helper.LogHelper; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.*; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; public class AuthLimiterIPConfig { - public static File ipConfigFile; + public static Path ipConfigFile; public static Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); - public static AuthLimiterIPConfig Instance; + public static AuthLimiterIPConfig Instance; // С этим сами разбирайтесь) @Expose List allowIp = new ArrayList<>(); @Expose List blockIp = new ArrayList<>(); - public static void load(File file) throws Exception { + public static void load(Path file) throws Exception { ipConfigFile = file; - if (file.exists()) { + if (IOHelper.exists(ipConfigFile)) { LogHelper.subDebug("IP List file found! Loading..."); - if (file.length() > 2) { - try - { - Instance = gson.fromJson(new JsonReader(new FileReader(file)), AuthLimiterIPConfig.class); - return; - } - catch (FileNotFoundException error) - { - LogHelper.subWarning("Ip List not reading!"); - if (LogHelper.isDebugEnabled()) LogHelper.error(error); - } + try + { + Instance = gson.fromJson(IOHelper.newReader(ipConfigFile), AuthLimiterIPConfig.class); + return; + } + catch (JsonIOException | IOException error) { + LogHelper.subWarning("Ip List not reading!"); + if (LogHelper.isDebugEnabled()) LogHelper.error(error); + } + catch (JsonSyntaxException error) { + LogHelper.subWarning("Invalid file syntax!"); + if (LogHelper.isDebugEnabled()) LogHelper.error(error); } } @@ -49,11 +50,7 @@ public void saveIPConfig() throws Exception { - if (!ipConfigFile.exists()) ipConfigFile.createNewFile(); - - FileWriter fw = new FileWriter(ipConfigFile, false); - fw.write(gson.toJson(this)); - fw.close(); + gson.toJson(this, IOHelper.newWriter(ipConfigFile)); } public List getAllowIp() {