From b785901d3b6984a592f29cff34c3d0a603eda81d Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 19 Feb 2021 20:58:03 +0300 Subject: [PATCH] Updating this --- WebInfoSponge.iml | 5 +- WebInfoSponge.ipr | 996 +++++++++++++++++- WebInfoSponge.iws | 339 ++++-- WebInfoSponge.main.iml | 29 +- WebInfoSponge.test.iml | 30 +- build.gradle | 1 + gradle.properties | 2 +- .../java/ru/redguy/webinfomod/Config.java | 2 - src/main/java/ru/redguy/webinfomod/User.java | 31 +- .../ru/redguy/webinfomod/UsersConfig.java | 63 +- .../ru/redguy/webinfomod/WebInfoSponge.java | 1 + .../java/ru/redguy/webinfomod/WebServer.java | 6 +- .../java/ru/redguy/webinfomod/WebStatic.java | 8 + .../redguy/webinfomod/pages/auth/Index.java | 3 +- .../redguy/webinfomod/pages/auth/Worker.java | 6 +- .../ru/redguy/webinfomod/pages/cp/Index.java | 17 +- .../redguy/webinfomod/pages/logout/Index.java | 5 +- .../webinfomod/utils/BasePlaceholders.java | 3 +- .../ru/redguy/webinfomod/utils/Jsonable.java | 9 + .../redguy/webinfomod/utils/SessionUtils.java | 17 + src/main/resources/resources/web/js/index.js | 4 +- src/main/resources/resources/web/web.iml | 8 + 22 files changed, 1374 insertions(+), 211 deletions(-) create mode 100644 src/main/java/ru/redguy/webinfomod/WebStatic.java create mode 100644 src/main/java/ru/redguy/webinfomod/utils/Jsonable.java create mode 100644 src/main/java/ru/redguy/webinfomod/utils/SessionUtils.java create mode 100644 src/main/resources/resources/web/web.iml diff --git a/WebInfoSponge.iml b/WebInfoSponge.iml index 60febdb..50350fd 100644 --- a/WebInfoSponge.iml +++ b/WebInfoSponge.iml @@ -1,12 +1,13 @@ - - + + + \ No newline at end of file diff --git a/WebInfoSponge.ipr b/WebInfoSponge.ipr index 33dff76..8f090a4 100644 --- a/WebInfoSponge.ipr +++ b/WebInfoSponge.ipr @@ -1,42 +1,14 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + @@ -44,6 +16,10 @@ + + + + - @@ -191,6 +166,7 @@ + @@ -222,6 +198,26 @@ @@ -236,29 +232,58 @@ + + + + + + + + + + + + + + + + + + + + + + - + + + - + - + + + + - + + + @@ -267,7 +292,9 @@ - + + + @@ -283,6 +310,17 @@ + + + + + + + + + + + @@ -297,12 +335,35 @@ + + + + + + + + + + + + + + + + + + + + + + + @@ -314,33 +375,466 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -352,15 +846,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -368,6 +993,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -379,11 +1143,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + @@ -403,7 +1202,9 @@ - + + + @@ -411,47 +1212,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + - @@ -463,14 +1335,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WebInfoSponge.iws b/WebInfoSponge.iws index 1a2a782..29dcafa 100644 --- a/WebInfoSponge.iws +++ b/WebInfoSponge.iws @@ -1,52 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WebInfoSponge.main.iml b/WebInfoSponge.main.iml index 0d4e8b7..cb5291e 100644 --- a/WebInfoSponge.main.iml +++ b/WebInfoSponge.main.iml @@ -1,5 +1,5 @@ - + @@ -9,7 +9,7 @@ - + @@ -22,29 +22,32 @@ + + + - - + + - - + + + + + + + - + - - - - - - + \ No newline at end of file diff --git a/WebInfoSponge.test.iml b/WebInfoSponge.test.iml index 96c8cac..bc4a55c 100644 --- a/WebInfoSponge.test.iml +++ b/WebInfoSponge.test.iml @@ -1,5 +1,5 @@ - + @@ -9,7 +9,7 @@ - + @@ -18,32 +18,12 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 7bae964..5e0dfdf 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ dependencies { compile 'org.nanohttpd:nanohttpd:2.3.1' compile 'org.reflections:reflections:0.9.12' compile 'org.apache.directory.studio:org.apache.commons.io:2.4' + compile group: 'org.json', name: 'json', version: '20201115' } sponge.plugin.id = pluginId diff --git a/gradle.properties b/gradle.properties index bba3aff..ffd1f5b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ pluginGroup=ru.redguy pluginId=webinfomod -pluginVersion=1.S3 +pluginVersion=1.0.4 diff --git a/src/main/java/ru/redguy/webinfomod/Config.java b/src/main/java/ru/redguy/webinfomod/Config.java index dad1f98..2cc71b3 100644 --- a/src/main/java/ru/redguy/webinfomod/Config.java +++ b/src/main/java/ru/redguy/webinfomod/Config.java @@ -11,7 +11,6 @@ import java.util.Map; public class Config { private JsonObject jsonObject; - public Map sessions; private Logger logger; public Config(Logger logger) throws IOException { @@ -35,7 +34,6 @@ public class Config { } jsonObject = (JsonObject) obj; jsonObject.addProperty("MineV",Sponge.getPlatform().getMinecraftVersion().getName()); - sessions = new HashMap(); } public String getString(String name) { diff --git a/src/main/java/ru/redguy/webinfomod/User.java b/src/main/java/ru/redguy/webinfomod/User.java index b6afcd2..8f5bd87 100644 --- a/src/main/java/ru/redguy/webinfomod/User.java +++ b/src/main/java/ru/redguy/webinfomod/User.java @@ -1,12 +1,39 @@ package ru.redguy.webinfomod; -public class User { +import org.json.JSONObject; +import ru.redguy.webinfomod.utils.Jsonable; + +public class User implements Jsonable { public String user; public String password; + public boolean isAdmin; - public User(String user, String password) { + public User(JSONObject json) { + user = json.optString("user","admin"); + password = json.optString("password","admin"); + isAdmin = json.optBoolean("isAdmin",false); + } + + public User(String user, String password, boolean isAdmin) { this.user = user; this.password = password; + this.isAdmin = isAdmin; } + + @Override + public String toJSON() { + return toJSONObject().toString(); + } + + @Override + public JSONObject toJSONObject() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("user",user); + jsonObject.put("password",password); + jsonObject.put("isAdmin",isAdmin); + return jsonObject; + } + + } diff --git a/src/main/java/ru/redguy/webinfomod/UsersConfig.java b/src/main/java/ru/redguy/webinfomod/UsersConfig.java index f915c04..de38a5f 100644 --- a/src/main/java/ru/redguy/webinfomod/UsersConfig.java +++ b/src/main/java/ru/redguy/webinfomod/UsersConfig.java @@ -1,48 +1,41 @@ package ru.redguy.webinfomod; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import org.apache.commons.io.FileUtils; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.*; public class UsersConfig { - private JsonArray rawUsers; + private static JSONArray users; - public UsersConfig() throws IOException { - JsonParser parser = new JsonParser(); - Object rawData; + public static void loadUsers() { try { - rawData = parser.parse(new FileReader("config/WebInfoUsers.json")); - } catch (FileNotFoundException e) { - File f = new File("config/WebInfoUsers.json"); - f.createNewFile(); - FileWriter fw = new FileWriter(f); - fw.write( - "[{"+System.lineSeparator()+ - "\"user\":\"admin\","+System.lineSeparator()+ - "\"password\":\"admin\""+System.lineSeparator()+ - "}]" - ); - fw.close(); - rawData = parser.parse(new FileReader("config/WebInfoMod.json")); - } - rawUsers = (JsonArray) rawData; - } - - public User getUser(String name) { - User result = null; - for (JsonElement rawUser : rawUsers) { - JsonObject userJson = rawUser.getAsJsonObject(); - if(userJson.get("user").getAsString().equals(name)) { - result = new User( - userJson.get("user").getAsString(), - userJson.get("password").getAsString() - ); + users = new JSONArray(FileUtils.readFileToString(new File("config/WebInfoUsers.json"))); + } catch (IOException e) { + try { + if(new File("config/WebInfoUsers.json").createNewFile()) { + JSONArray array = new JSONArray(); + array.put(new User("admin","password",false).toJSONObject()); + users = array; + FileUtils.writeStringToFile(new File("config/WebInfoUsers.json"),array.toString()); + } else { + //TODO: I cant load + } + } catch (IOException ioException) { + //TODO: I cant load } } - return result; + } + + public static User getUser(String name) { + for (Object user : users) { + JSONObject object = (JSONObject)user; + if(name.equals(object.getString("user"))) { + return new User(object); + } + } + return null; } } diff --git a/src/main/java/ru/redguy/webinfomod/WebInfoSponge.java b/src/main/java/ru/redguy/webinfomod/WebInfoSponge.java index a61e2d2..023df1b 100644 --- a/src/main/java/ru/redguy/webinfomod/WebInfoSponge.java +++ b/src/main/java/ru/redguy/webinfomod/WebInfoSponge.java @@ -35,6 +35,7 @@ public class WebInfoSponge { config = new Config(myLogger); Localization.loadLangFile(config); BasePlaceholders.registerBase(); + UsersConfig.loadUsers(); webServer = new WebServer(config.getInt("WebPort"), config); } } diff --git a/src/main/java/ru/redguy/webinfomod/WebServer.java b/src/main/java/ru/redguy/webinfomod/WebServer.java index 0760817..7b45a5b 100644 --- a/src/main/java/ru/redguy/webinfomod/WebServer.java +++ b/src/main/java/ru/redguy/webinfomod/WebServer.java @@ -44,14 +44,14 @@ public class WebServer extends NanoHTTPD { if(session.getUri().startsWith("/css/")||session.getUri().startsWith("/js/")) { String path = "/resources/web"+session.getUri(); try { - return newFixedLengthResponse( - IOUtils.toString(WebServer.class.getResourceAsStream(path), StandardCharsets.UTF_8)); + //TODO: 400 код при авторизоавном доступе + return newFixedLengthResponse(Response.Status.OK,session.getUri().startsWith("/css/") ? "text/css" : "application/javascript", IOUtils.toString(WebServer.class.getResourceAsStream(path), StandardCharsets.UTF_8)); } catch (IOException ignored) { } } else { for (Class mClass : reflections.getTypesAnnotatedWith(WebPage.class, true)) { - if (mClass.getAnnotation(WebPage.class).url().equals(session.getUri())) { + if (session.getUri().equals(mClass.getAnnotation(WebPage.class).url()) || session.getUri().equals(mClass.getAnnotation(WebPage.class).url()+"/")) { try { return (Response) mClass.getMethod("getPage", Config.class, IHTTPSession.class).invoke(mClass.newInstance(), config, session); } catch (IllegalAccessException e) { diff --git a/src/main/java/ru/redguy/webinfomod/WebStatic.java b/src/main/java/ru/redguy/webinfomod/WebStatic.java new file mode 100644 index 0000000..fb65b8d --- /dev/null +++ b/src/main/java/ru/redguy/webinfomod/WebStatic.java @@ -0,0 +1,8 @@ +package ru.redguy.webinfomod; + +import java.util.HashMap; +import java.util.Map; + +public class WebStatic { + public static Map sessions = new HashMap<>(); +} diff --git a/src/main/java/ru/redguy/webinfomod/pages/auth/Index.java b/src/main/java/ru/redguy/webinfomod/pages/auth/Index.java index 4a6d2ae..b0dff64 100644 --- a/src/main/java/ru/redguy/webinfomod/pages/auth/Index.java +++ b/src/main/java/ru/redguy/webinfomod/pages/auth/Index.java @@ -8,11 +8,12 @@ import ru.redguy.webinfomod.langs.enums.RootCategories; import ru.redguy.webinfomod.langs.enums.Titles; import ru.redguy.webinfomod.langs.enums.Words; import ru.redguy.webinfomod.utils.PlaceholdersUtils; +import ru.redguy.webinfomod.utils.SessionUtils; import java.io.IOException; import java.nio.charset.Charset; -@WebPage(url = "/auth/") +@WebPage(url = "/auth") public class Index implements IWebPage { public NanoHTTPD.Response getPage(Config config, NanoHTTPD.IHTTPSession session) throws IOException { diff --git a/src/main/java/ru/redguy/webinfomod/pages/auth/Worker.java b/src/main/java/ru/redguy/webinfomod/pages/auth/Worker.java index 7df4f20..382fda2 100644 --- a/src/main/java/ru/redguy/webinfomod/pages/auth/Worker.java +++ b/src/main/java/ru/redguy/webinfomod/pages/auth/Worker.java @@ -11,7 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -@WebPage(url = "/auth/worker/") +@WebPage(url = "/auth/worker") public class Worker implements IWebPage { public NanoHTTPD.Response getPage(Config config, NanoHTTPD.IHTTPSession session) throws IOException { try { @@ -46,8 +46,8 @@ public class Worker implements IWebPage { String token = WebUtils.MD5(date.getTime()+"MagickSalt"); WebUtils.CookieHandler ch = new WebUtils.CookieHandler(session.getHeaders()); ch.set("session",token,1); - config.sessions.put(token,user.user); - NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.REDIRECT,NanoHTTPD.MIME_PLAINTEXT,""); + WebStatic.sessions.put(token,user.user); + NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.TEMPORARY_REDIRECT,NanoHTTPD.MIME_PLAINTEXT,""); response.addHeader("Location", "/"); ch.unloadQueue(response); config.getLogger().info(LoggerType.Web,user.user + " login!"); diff --git a/src/main/java/ru/redguy/webinfomod/pages/cp/Index.java b/src/main/java/ru/redguy/webinfomod/pages/cp/Index.java index 8b18290..50b7e20 100644 --- a/src/main/java/ru/redguy/webinfomod/pages/cp/Index.java +++ b/src/main/java/ru/redguy/webinfomod/pages/cp/Index.java @@ -2,22 +2,21 @@ package ru.redguy.webinfomod.pages.cp; import fi.iki.elonen.NanoHTTPD; import org.apache.commons.io.IOUtils; -import ru.redguy.webinfomod.Config; -import ru.redguy.webinfomod.IWebPage; -import ru.redguy.webinfomod.WebPage; -import ru.redguy.webinfomod.WebServer; -import ru.redguy.webinfomod.langs.Localization; -import ru.redguy.webinfomod.langs.enums.Links; -import ru.redguy.webinfomod.langs.enums.RootCategories; +import ru.redguy.webinfomod.*; import ru.redguy.webinfomod.utils.PlaceholdersUtils; -import ru.redguy.webinfomod.utils.WebUtils; +import ru.redguy.webinfomod.utils.SessionUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; -@WebPage(url = "/cp/") +@WebPage(url = "/cp") public class Index implements IWebPage { public NanoHTTPD.Response getPage(Config config, NanoHTTPD.IHTTPSession session) throws IOException { + if(!SessionUtils.checkAdmin(session)) { + NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.TEMPORARY_REDIRECT, NanoHTTPD.MIME_PLAINTEXT, ""); + response.addHeader("Location", "/"); + return response; + } String path = "/resources/web/cp/index.html"; String page = IOUtils.toString(WebServer.class.getResourceAsStream(path), StandardCharsets.UTF_8); return NanoHTTPD.newFixedLengthResponse(new PlaceholdersUtils(session).work(page)); diff --git a/src/main/java/ru/redguy/webinfomod/pages/logout/Index.java b/src/main/java/ru/redguy/webinfomod/pages/logout/Index.java index e4c8e41..eaf5823 100644 --- a/src/main/java/ru/redguy/webinfomod/pages/logout/Index.java +++ b/src/main/java/ru/redguy/webinfomod/pages/logout/Index.java @@ -7,11 +7,12 @@ import ru.redguy.webinfomod.utils.WebUtils; import java.io.IOException; -@WebPage(url = "/logout/") +@WebPage(url = "/logout") public class Index implements IWebPage { public NanoHTTPD.Response getPage(Config config, NanoHTTPD.IHTTPSession session) throws IOException { WebUtils.CookieHandler ch = new WebUtils.CookieHandler(session.getHeaders()); - config.getLogger().info(LoggerType.Web,config.sessions.get(ch.read("session"))+ " has been logout!"); + config.getLogger().info(LoggerType.Web,WebStatic.sessions.get(ch.read("session"))+ " has been logout!"); + WebStatic.sessions.remove(ch.read("session")); ch.delete("session"); NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.REDIRECT,NanoHTTPD.MIME_PLAINTEXT,""); response.addHeader("Location", "/"); diff --git a/src/main/java/ru/redguy/webinfomod/utils/BasePlaceholders.java b/src/main/java/ru/redguy/webinfomod/utils/BasePlaceholders.java index 929a903..32c8da3 100644 --- a/src/main/java/ru/redguy/webinfomod/utils/BasePlaceholders.java +++ b/src/main/java/ru/redguy/webinfomod/utils/BasePlaceholders.java @@ -1,6 +1,7 @@ package ru.redguy.webinfomod.utils; import ru.redguy.webinfomod.Logger; +import ru.redguy.webinfomod.WebStatic; import ru.redguy.webinfomod.langs.Localization; import ru.redguy.webinfomod.langs.enums.Links; import ru.redguy.webinfomod.langs.enums.RootCategories; @@ -14,7 +15,7 @@ public class BasePlaceholders { @Override public String getContent(String[] args, PlaceholdersUtils placeholdersUtils) { WebUtils.CookieHandler ch = new WebUtils.CookieHandler(placeholdersUtils.getSession().getHeaders()); - if(ch.read("session") == null) { + if(!WebStatic.sessions.containsKey(ch.read("session"))) { return ""+ Localization.getCategory(RootCategories.Links).getString(Links.login)+""; } else { return ""+Localization.getCategory(RootCategories.Links).getString(Links.logout)+"
" + diff --git a/src/main/java/ru/redguy/webinfomod/utils/Jsonable.java b/src/main/java/ru/redguy/webinfomod/utils/Jsonable.java new file mode 100644 index 0000000..2e84bd5 --- /dev/null +++ b/src/main/java/ru/redguy/webinfomod/utils/Jsonable.java @@ -0,0 +1,9 @@ +package ru.redguy.webinfomod.utils; + +import org.json.JSONObject; + +public interface Jsonable { + String toJSON(); + + JSONObject toJSONObject(); +} diff --git a/src/main/java/ru/redguy/webinfomod/utils/SessionUtils.java b/src/main/java/ru/redguy/webinfomod/utils/SessionUtils.java new file mode 100644 index 0000000..b5b4dff --- /dev/null +++ b/src/main/java/ru/redguy/webinfomod/utils/SessionUtils.java @@ -0,0 +1,17 @@ +package ru.redguy.webinfomod.utils; + +import fi.iki.elonen.NanoHTTPD; +import ru.redguy.webinfomod.UsersConfig; +import ru.redguy.webinfomod.WebStatic; + +import java.util.Objects; + +public class SessionUtils { + public static boolean checkAdmin(NanoHTTPD.IHTTPSession session) { + WebUtils.CookieHandler ch = new WebUtils.CookieHandler(session.getHeaders()); + if(WebStatic.sessions.containsKey(ch.read("session"))) { + return Objects.requireNonNull(UsersConfig.getUser(WebStatic.sessions.get(ch.read("session")))).isAdmin; + } + return false; + } +} diff --git a/src/main/resources/resources/web/js/index.js b/src/main/resources/resources/web/js/index.js index b1073a9..3ad04a9 100644 --- a/src/main/resources/resources/web/js/index.js +++ b/src/main/resources/resources/web/js/index.js @@ -15,8 +15,8 @@ function updatePageInfo() { if (xhr.readyState !== 4) return; let data = JSON.parse(xhr.responseText); header.innerText = "Мод запущен на сервере"; - appendLine(stat, ["Версия игры", data.MineV]); - appendLine(stat, ["Игроки", data.Players]); + Tables.appendLine(stat, ["Версия игры", data.MineV]); + Tables.appendLine(stat, ["Игроки", data.Players]); } } diff --git a/src/main/resources/resources/web/web.iml b/src/main/resources/resources/web/web.iml new file mode 100644 index 0000000..80cc739 --- /dev/null +++ b/src/main/resources/resources/web/web.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file