From 6cb1c0f9eb6458b8fe9a1d70575dbf636a71ebfb Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Fri, 17 Apr 2020 14:32:22 +0800 Subject: [PATCH] update --- .../com/yutou/tools/nas/AdminManager.java | 25 +++ .../java/com/yutou/tools/nas/UpdateIp.java | 59 ++++++- .../java/com/yutou/tools/utils/Tools.java | 158 +++++++++++++++++- .../com/yutou/tools/web/userController.java | 73 +++++++- web/html/body/nas.html | 44 ----- web/html/body/nas/index.html | 45 +++++ web/html/body/nas/ip.html | 53 ++++++ web/html/body/nas/side.html | 28 ++++ web/html/body/nas/switchAdmin.html | 53 ++++++ web/html/header.html | 73 ++++---- web/index.html | 2 +- 11 files changed, 522 insertions(+), 91 deletions(-) create mode 100644 src/main/java/com/yutou/tools/nas/AdminManager.java delete mode 100644 web/html/body/nas.html create mode 100644 web/html/body/nas/index.html create mode 100644 web/html/body/nas/ip.html create mode 100644 web/html/body/nas/side.html create mode 100644 web/html/body/nas/switchAdmin.html diff --git a/src/main/java/com/yutou/tools/nas/AdminManager.java b/src/main/java/com/yutou/tools/nas/AdminManager.java new file mode 100644 index 0000000..7447d1a --- /dev/null +++ b/src/main/java/com/yutou/tools/nas/AdminManager.java @@ -0,0 +1,25 @@ +package com.yutou.tools.nas; + +import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.utils.RedisTools; +import org.springframework.stereotype.Controller; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +@Controller +public class AdminManager { + @Resource + RedisTools redisTools; + + public String getAdminAddress(HttpServletRequest request){ + JSONObject json=new JSONObject(); + + String address=redisTools.get("adminAddress"); + if(address==null){ + json.put("code",-1); + json.put("msg","暂未设置管理后台"); + } + return json.toJSONString(); + } +} diff --git a/src/main/java/com/yutou/tools/nas/UpdateIp.java b/src/main/java/com/yutou/tools/nas/UpdateIp.java index e3cedc9..487b1df 100644 --- a/src/main/java/com/yutou/tools/nas/UpdateIp.java +++ b/src/main/java/com/yutou/tools/nas/UpdateIp.java @@ -1,9 +1,13 @@ package com.yutou.tools.nas; +import com.alibaba.fastjson.JSONObject; import com.yutou.tools.mybatis.dao.UKeyDao; import com.yutou.tools.mybatis.model.UKey; +import com.yutou.tools.utils.RedisTools; +import com.yutou.tools.utils.Tools; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -19,6 +23,8 @@ import java.util.regex.Pattern; @Controller public class UpdateIp { private static List keys = new ArrayList<>(); + @Resource + RedisTools redisTools; /* static { keys.add("nas.yutou233.cn;"); @@ -71,6 +77,50 @@ public class UpdateIp { } } + @RequestMapping("/nas/getIp.do") + @ResponseBody + public String getIP(HttpServletRequest request) { + JSONObject json = new JSONObject(); + json.put("code", -1); + json.put("msg", "未登录"); + if (Tools.checkWebLogin(request, redisTools) != 1) { + // return json.toJSONString(); + } + updateList(); + File file = new File("/etc/nginx/nginx.conf"); + file = new File("D:\\nginx.conf"); + if (file.exists()) { + String testIp = "0.0.0.0"; + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line; + boolean isIp = false; + while ((line = reader.readLine()) != null) { + // System.out.println(line.trim().replace("server_name","").replace("upstream","").trim()); + if (keys.contains(line.trim().replace("server_name", "").replace("upstream", "").replace("{", "").trim())) { + isIp = true; + } + if (isIp) { + testIp = testIp(line); + if (testIp != null) + break; + } + } + reader.close(); + } catch (Exception e) { + e.printStackTrace(); + } + json.put("code", 0); + json.put("msg", "ok"); + json.put("data", testIp); + } else { + json.put("code", 0); + json.put("msg", "没有找到ip"); + json.put("data", "0.0.0.0"); + } + return json.toJSONString(); + } + public String testIp(String ip) { String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}"; Pattern p = Pattern.compile(pattern); @@ -87,12 +137,13 @@ public class UpdateIp { if (!file.exists()) { boolean create = file.createNewFile(); if (create) - System.out.println("创建文件完成:" +file.getAbsolutePath()); + System.out.println("创建文件完成:" + file.getAbsolutePath()); } - BufferedReader reader=new BufferedReader(new FileReader(file)); + BufferedReader reader = new BufferedReader(new FileReader(file)); String tmp; - while ((tmp=reader.readLine())!=null){ - keys.add(tmp.trim()); + while ((tmp = reader.readLine()) != null) { + if (!keys.contains(tmp.trim())) + keys.add(tmp.trim()); } reader.close(); } catch (Exception e) { diff --git a/src/main/java/com/yutou/tools/utils/Tools.java b/src/main/java/com/yutou/tools/utils/Tools.java index c9b0100..c570cfa 100644 --- a/src/main/java/com/yutou/tools/utils/Tools.java +++ b/src/main/java/com/yutou/tools/utils/Tools.java @@ -1,15 +1,167 @@ package com.yutou.tools.utils; +import com.alibaba.fastjson.JSONArray; + +import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Random; public class Tools { - public static Cookie getCookie(HttpServletRequest request,String key){ - for (Cookie cookie : request.getCookies()) { - if(cookie.getName().equals(key)){ + /** + * 设置Cookie + * @param response + * @param key + * @param value + * @param time + */ + public static void setCookie(HttpServletResponse response, String key,String value,int time) { + Cookie cookie = new Cookie(key, value); + if(time!=-1) { + cookie.setMaxAge(time); + } + response.addCookie(cookie); + } + /** + * 设置Cookie + * @param request + * @param response + * @param key + * @param time 生命周期,为0时即为删除 + * @return + */ + private static String setCookie(HttpServletRequest request, HttpServletResponse response, String key,int time) { + Cookie name = new Cookie("uname", key); + Cookie session = new Cookie("session", request.getSession().getId()); + if(time!=-1) { + name.setMaxAge(time); + session.setMaxAge(time); + } + response.addCookie(name); + response.addCookie(session); + return request.getSession().getId(); + } + /** + * 获取Cookie + * @param request + * @param key + * @return + */ + public static Cookie getCookie(HttpServletRequest request,String key) { + Cookie[] cookies = request.getCookies(); + for (Cookie cookie : cookies) { + if (key!=null&&cookie.getName().equals(key)) { return cookie; } } return null; } + /** + * 删除Cookie + * @param request + * @param response + * @param key + * @return + */ + public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) { + return setCookie(request, response, key, 0); + } + public static void sendServer(String title,String msg){ + try{ + System.out.println("title="+title+" msg="+msg); + /*HttpURLConnection connection= (HttpURLConnection) new URL("https://sc.ftqq.com/SCU64034T5adf5c5940dcecc016e0e9d0cf9b1e725da126ff47475.send?text=" + + URLEncoder.encode(title,"UTF-8")+"&desp="+URLEncoder.encode(msg,"UTF-8")).openConnection(); + connection.connect(); + InputStream inputStream=connection.getInputStream(); + int i=inputStream.read(); + inputStream.close(); + connection.disconnect();*/ + }catch (Exception e){ + e.printStackTrace(); + } + } + + /** + * 获取项目路径 + * @param request + * @return + */ + public static String getPath(HttpServletRequest request) { + return request.getServletContext().getRealPath("/") + "/"; + } + /** + * 获取客户端IP + * + * @param request + * @return + */ + public static String getRemoteAddress(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { + ip = request.getRemoteAddr(); + } + return ip; + } + /** + * N以内的不重复随机数 + * + * @param min + * 最小值 + * @param max + * 最大值 + * @param n + * @return + */ + public static int[] randomCommon(int min, int max, int n) { + int len = max - min + 1; + if (max < min || n > len) { + return new int[0]; + } + // 初始化给定范围的待选数组 + int[] source = new int[len]; + for (int i = min; i < min + len; i++) { + source[i - min] = i; + } + int[] result = new int[n]; + Random rd = new Random(); + int index = 0; + for (int i = 0; i < result.length; i++) { + // 待选数组0到(len-2)随机一个下标 + index = Math.abs(rd.nextInt() % len--); + // 将随机到的数放入结果集 + result[i] = source[index]; + // 将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换 + source[index] = source[len]; + } + return result; + } + public static int checkWebLogin(HttpServletRequest request,RedisTools redisTools){ + JSONArray array=new JSONArray(); + if(redisTools.get("bean")!=null){ + array=JSONArray.parseArray(redisTools.get("bean")); + } + if(array.contains(Tools.getRemoteAddress(request))){ + System.out.println("IP已被封禁"); + return -100; + } + Cookie cookie = Tools.getCookie(request, "user"); + if (cookie == null) { + return -1; + } + if (redisTools.get(cookie.getValue()).equals("ok")) { + return 1; + } + return 0; + } } diff --git a/src/main/java/com/yutou/tools/web/userController.java b/src/main/java/com/yutou/tools/web/userController.java index f923665..c276580 100644 --- a/src/main/java/com/yutou/tools/web/userController.java +++ b/src/main/java/com/yutou/tools/web/userController.java @@ -1,23 +1,82 @@ package com.yutou.tools.web; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.utils.RedisTools; import com.yutou.tools.utils.Tools; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Objects; +import java.util.UUID; @Controller public class userController { + @Resource + RedisTools redisTools; - public String getLoginState(HttpServletRequest request){ - JSONObject json=new JSONObject(); - Cookie cookie= Tools.getCookie(request,"user"); - if(cookie==null){ - json.put("code",-1); - json.put("msg","未登录"); + @RequestMapping("/login/check.do") + @ResponseBody + public String getLoginState(HttpServletRequest request) { + JSONObject json = new JSONObject(); + json.put("code", -1); + json.put("msg", "未登录"); + JSONArray array=new JSONArray(); + if(redisTools.get("bean")!=null){ + array=JSONArray.parseArray(redisTools.get("bean")); + } + if(array.contains(Tools.getRemoteAddress(request))){ + System.out.println("IP已被封禁"); return json.toJSONString(); } - return ""; + Cookie cookie = Tools.getCookie(request, "user"); + if (cookie == null) { + return json.toJSONString(); + } + if (redisTools.get(cookie.getValue()).equals("ok")) { + json.put("code", 0); + json.put("msg", "登录成功"); + return json.toJSONString(); + } + json.put("code", -1); + json.put("msg", "未登录"); + return json.toJSONString(); + } + + @RequestMapping("/login/sendCaptcha.do") + @ResponseBody + public String captcha(HttpServletRequest request) { + int[] captcha = Tools.randomCommon(0, 9, 5); + String cc = ""; + for (int value : captcha) { + cc += value; + } + redisTools.set("login",cc,5*60*1000); + Tools.sendServer("管理后台登录验证码", "本次登录验证码为:" + cc + + ",登录IP:" + Tools.getRemoteAddress(request) + + ",非正常登录,封禁IP:http://www.baidu.com"); + return "ok"; + } + @RequestMapping("/login/login.do") + @ResponseBody + public String login(HttpServletResponse response,String code){ + JSONObject json=new JSONObject(); + if(redisTools.get("login").equals(code.trim())){ + String uuid=UUID.randomUUID().toString(); + Tools.setCookie(response,"user",uuid.replace("-",""),30*24*60*60*1000); + redisTools.set(uuid.replace("-",""),"ok",30*24*60*60*1000); + json.put("code",0); + json.put("msg","登录成功"); + return json.toJSONString(); + } + json.put("code",-2); + json.put("msg","登录安全码错误"); + return json.toJSONString(); } } diff --git a/web/html/body/nas.html b/web/html/body/nas.html deleted file mode 100644 index 4513757..0000000 --- a/web/html/body/nas.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - NAS - - - - -
- -
- 123 -
- -
- - - - - - - - \ No newline at end of file diff --git a/web/html/body/nas/index.html b/web/html/body/nas/index.html new file mode 100644 index 0000000..1155ed9 --- /dev/null +++ b/web/html/body/nas/index.html @@ -0,0 +1,45 @@ + + + + + + + NAS + + + + +
+ +
+
+ + +
+ + + + + + + + \ No newline at end of file diff --git a/web/html/body/nas/ip.html b/web/html/body/nas/ip.html new file mode 100644 index 0000000..01e2ffe --- /dev/null +++ b/web/html/body/nas/ip.html @@ -0,0 +1,53 @@ + + + + + + + NAS + + + + +
+ +
+
+
当前IP:
+ +
+ + + + + + + + \ No newline at end of file diff --git a/web/html/body/nas/side.html b/web/html/body/nas/side.html new file mode 100644 index 0000000..ec26f89 --- /dev/null +++ b/web/html/body/nas/side.html @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/web/html/body/nas/switchAdmin.html b/web/html/body/nas/switchAdmin.html new file mode 100644 index 0000000..01e2ffe --- /dev/null +++ b/web/html/body/nas/switchAdmin.html @@ -0,0 +1,53 @@ + + + + + + + NAS + + + + +
+ +
+
+
当前IP:
+ +
+ + + + + + + + \ No newline at end of file diff --git a/web/html/header.html b/web/html/header.html index cb752f0..c609841 100644 --- a/web/html/header.html +++ b/web/html/header.html @@ -1,46 +1,55 @@ + Title + - +
  • + 页面集 +
    +
    管理后台
    +
    BT下载
    +
    +
  • +
  • + 登录 +
    +
    退了
    +
    +
  • + +
    + \ No newline at end of file diff --git a/web/index.html b/web/index.html index 988519b..63cf50d 100644 --- a/web/index.html +++ b/web/index.html @@ -27,7 +27,7 @@ }); $.ajax({cache: false}) - $('#header').load("../html/header.html"); + $('#header').load("/html/header.html"); $('#footer').load("../html/footer.html");