diff --git a/pom.xml b/pom.xml index e9f7276..f254abd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.yutou tools - 1.0.6 + 1.0.7 tools Demo project for Spring Boot diff --git a/src/main/java/com/yutou/tools/BiliBili/Live.java b/src/main/java/com/yutou/tools/BiliBili/Live.java index 165f846..088508e 100644 --- a/src/main/java/com/yutou/tools/BiliBili/Live.java +++ b/src/main/java/com/yutou/tools/BiliBili/Live.java @@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject; import com.yutou.tools.mybatis.dao.BilibiliLiveDao; import com.yutou.tools.mybatis.model.BilibiliLive; import com.yutou.tools.mybatis.model.BilibiliLiveExample; -import com.yutou.tools.utils.RedisTools; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/com/yutou/tools/home/nas/QQBot.java b/src/main/java/com/yutou/tools/home/nas/QQBot.java new file mode 100644 index 0000000..5567a98 --- /dev/null +++ b/src/main/java/com/yutou/tools/home/nas/QQBot.java @@ -0,0 +1,29 @@ +package com.yutou.tools.home.nas; + +import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.utils.RedisTools; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping("/localhome/bot") +public class QQBot { + + + @RequestMapping("send.do") + @ResponseBody + public String send(String msg){ + JSONObject json=new JSONObject(); + try { + boolean ret= RedisTools.set(1,"msg_"+System.currentTimeMillis(),msg); + json.put("code",0); + json.put("msg",ret); + }catch (Exception e){ + e.printStackTrace(); + json.put("code",-1); + json.put("msg",e.getMessage()); + } + return json.toJSONString(); + } +} diff --git a/src/main/java/com/yutou/tools/nas/NasManager.java b/src/main/java/com/yutou/tools/nas/NasManager.java index c43057e..52fe023 100644 --- a/src/main/java/com/yutou/tools/nas/NasManager.java +++ b/src/main/java/com/yutou/tools/nas/NasManager.java @@ -21,8 +21,6 @@ import java.util.List; @Controller public class NasManager { - @Resource - RedisTools redisTools; @Resource NasAdminAddressDao adminAddressDao; @ResponseBody @@ -30,7 +28,7 @@ public class NasManager { public String getAdminAddress(HttpServletRequest request){ JSONObject json=new JSONObject(); - String address=redisTools.get("adminAddress"); + String address=RedisTools.get("adminAddress"); if(address==null){ json.put("code",-1); json.put("msg","暂未设置管理后台"); @@ -136,7 +134,7 @@ public class NasManager { try { NasAdminAddress address=adminAddressDao.selectByPrimaryKey(Integer.parseInt(id)); if(address!=null){ - redisTools.set("adminAddress",JSONObject.toJSONString(address)); + RedisTools.set("adminAddress",JSONObject.toJSONString(address)); } json.put("code",0); json.put("msg","ok"); diff --git a/src/main/java/com/yutou/tools/nas/UpdateIp.java b/src/main/java/com/yutou/tools/nas/UpdateIp.java index 0b698a7..ad9fd36 100644 --- a/src/main/java/com/yutou/tools/nas/UpdateIp.java +++ b/src/main/java/com/yutou/tools/nas/UpdateIp.java @@ -24,9 +24,6 @@ import java.util.regex.Pattern; public class UpdateIp { private static List keys = new ArrayList<>(); public static String nas_ip=null; - @Resource - RedisTools redisTools; - /* static { keys.add("nas.yutou233.cn;"); keys.add("bt.yutou233.cn;"); @@ -64,7 +61,7 @@ public class UpdateIp { if (testIp != null) { System.out.println("检测到目标:" + line + " 修改IP " + testIp + " > " + ip); if(isNas){ - String adminAddress=redisTools.get("adminAddress"); + String adminAddress=RedisTools.get("adminAddress"); if(adminAddress!=null) { JSONObject json=JSONObject.parseObject(adminAddress); line = "proxy_pass http://" +ip+":"+json.getInteger("port")+"/;"; @@ -100,41 +97,12 @@ public class UpdateIp { JSONObject json = new JSONObject(); json.put("code", -1); json.put("msg", "未登录"); - if (Tools.checkWebLogin(request, redisTools) != 1) { + if (Tools.checkWebLogin(request) != 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"); - } + json.put("code", 0); + json.put("msg", "ok"); + json.put("data", nas_ip); return json.toJSONString(); } diff --git a/src/main/java/com/yutou/tools/utils/APIFilter.java b/src/main/java/com/yutou/tools/utils/APIFilter.java index adf18ec..437cced 100644 --- a/src/main/java/com/yutou/tools/utils/APIFilter.java +++ b/src/main/java/com/yutou/tools/utils/APIFilter.java @@ -19,8 +19,6 @@ import java.util.Enumeration; public class APIFilter implements Filter { @Resource UKeyDao keyDao; - @Resource - RedisTools redisTools; @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { @@ -39,18 +37,15 @@ public class APIFilter implements Filter { } } if (cookie != null) { - if ("ok".equals(redisTools.get(cookie.getValue()))) { + if ("ok".equals(RedisTools.get(cookie.getValue()))) { isCookie = true; } } - if (!isToken) { - System.out.println("token验证不通过:" + token); - } else if (!isCookie) { - System.out.println("请求无令牌,拦截"); - } + if (!isCookie && !isToken) { //response.sendRedirect("/"); - if(!request.getRequestURI().contains("/login/")){ + System.out.println("请求无令牌,拦截"); + if(!request.getRequestURI().contains("/login/")&&!request.getRequestURI().equals("/favicon.ico")){ response.sendRedirect("/"); return; } diff --git a/src/main/java/com/yutou/tools/utils/RedisConfig.java b/src/main/java/com/yutou/tools/utils/RedisConfig.java index cc90971..10805c4 100644 --- a/src/main/java/com/yutou/tools/utils/RedisConfig.java +++ b/src/main/java/com/yutou/tools/utils/RedisConfig.java @@ -12,8 +12,6 @@ import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; -@Configuration -@EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { diff --git a/src/main/java/com/yutou/tools/utils/RedisTools.java b/src/main/java/com/yutou/tools/utils/RedisTools.java index e0d8ac6..ffcb739 100644 --- a/src/main/java/com/yutou/tools/utils/RedisTools.java +++ b/src/main/java/com/yutou/tools/utils/RedisTools.java @@ -1,33 +1,137 @@ package com.yutou.tools.utils; -import org.springframework.context.annotation.Bean; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.stereotype.Component; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.Set; -import javax.annotation.Resource; -import java.util.concurrent.TimeUnit; +import redis.clients.jedis.Jedis; -@Component public class RedisTools { - @Resource - RedisTemplate redisTemplate; + private static String host; + private static int port; + private static int TOKEN_TIMEOUT_DEFAULT=360; + + private RedisTools() { - public void set(String key, String value) { - redisTemplate.opsForValue().set(key, value); } - public void set(String key, String value, long time) { - System.out.println("key=" + key + " value=" + value + " time=" + time); - redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); + // 写成静态代码块形式,只加载一次,节省资源 + static { + //Properties properties = PropertyUtil.loadProperties("jedis.properties"); + //host = properties.getProperty("redis.host"); + //port = Integer.valueOf(properties.getProperty("redis.port")); + host="127.0.0.1"; + port=6379; } - - public String get(String key) { + public static boolean set(int dbIndex,String key,String value){ try { - return redisTemplate.opsForValue().get(key); + Jedis jedis =getRedis(); + jedis.select(dbIndex); + String ret=jedis.set(key,value); + System.out.println("Redis set ="+ret); + jedis.close(); } catch (Exception e) { - return null; + // TODO: handle exception + e.printStackTrace(); + return false; + } + return true; + } + public static boolean set(String key, String value) { + return set(0,key,value); + } + + public static boolean set(String key,String value,int timeout) { + try { + Jedis jedis=getRedis(); + if(timeout==-1){ + jedis.set(key,value); + }else { + jedis.setex(key, timeout, value); + } + jedis.close(); + }catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + return false; + } + return true; + } + public static String get(String key,int dbIndex){ + String value = "-999"; + Jedis jedis=null; + try { + jedis = getRedis(); + jedis.select(dbIndex); + value = jedis.get(key); + jedis.close(); + } catch (Exception e) { + // TODO: handle exception + // e.printStackTrace(); + }finally { + if(jedis!=null) + jedis.close(); + } + return value; + } + public static String get(String key) { + return get(key,0); + } + public static boolean remove(String key) { + Jedis jedis=getRedis(); + Long i=jedis.del(key); + jedis.close(); + if(i>0) { + return true; + }else { + return false; + } + } + public static void removeLoginState(String uid) { + Jedis jedis=getRedis(); + Set keys=jedis.keys("*"); + for (String string : keys) { + if(string.equals(uid)) { + jedis.del(string); + } } } + public static String ping() { + Jedis jedis=getRedis(); + String tmp=jedis.ping(); + jedis.close(); + return tmp; + } + public static boolean exists(String key,String value) { + Jedis jedis=getRedis(); + boolean flag=value.equals(jedis.get(key)); + jedis.close(); + return flag; + } + public static Jedis getRedis() { + return new Jedis(host,port); + } + private static class PropertyUtil { + + // 加载property文件到io流里面 + public static Properties loadProperties(String propertyFile) { + Properties properties = new Properties(); + try { + InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile); + if (is == null) { + is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile); + } + properties.load(is); + } catch (IOException e) { + e.printStackTrace(); + } + return properties; + } + } + public static void main(String[] args) { + String str= "\"aaa\""; + str=str.replaceAll("\"aaa\"", "0"); + } } diff --git a/src/main/java/com/yutou/tools/utils/Tools.java b/src/main/java/com/yutou/tools/utils/Tools.java index 7423fe6..ec24398 100644 --- a/src/main/java/com/yutou/tools/utils/Tools.java +++ b/src/main/java/com/yutou/tools/utils/Tools.java @@ -1,6 +1,7 @@ package com.yutou.tools.utils; import com.alibaba.fastjson.JSONArray; +import com.yutou.tools.nas.UpdateIp; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; @@ -19,43 +20,48 @@ import java.util.Random; public class Tools { /** - * 设置Cookie + * 设置Cookie + * * @param response * @param key * @param value * @param time */ - public static void setCookie(HttpServletResponse response, String key,String value,int time) { + public static void setCookie(HttpServletResponse response, String key, String value, int time) { Cookie cookie = new Cookie(key, value); - if(time!=-1) { + if (time != -1) { cookie.setMaxAge(time); } cookie.setPath("/"); response.addCookie(cookie); } + /** * 获取Cookie + * * @param request * @param key * @return */ - public static Cookie getCookie(HttpServletRequest request,String key) { + public static Cookie getCookie(HttpServletRequest request, String key) { Cookie[] cookies = request.getCookies(); try { for (Cookie cookie : cookies) { - if (key!=null&&cookie.getName().equals(key)) { + if (key != null && cookie.getName().equals(key)) { return cookie; } } - }catch (Exception ignored){ + } catch (Exception ignored) { } return null; } + /** * 删除Cookie + * * @param request * @param response * @param key @@ -63,8 +69,8 @@ public class Tools { */ public static String deleteCookie(HttpServletRequest request, HttpServletResponse response, String key) { for (Cookie cookie : request.getCookies()) { - if(cookie.getName().equals(key)) { - System.out.println("删除key="+key); + if (cookie.getName().equals(key)) { + System.out.println("删除key=" + key); cookie.setMaxAge(0); cookie.setPath("/"); cookie.setValue(null); @@ -73,29 +79,36 @@ public class Tools { } return "ok"; } - 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(); + + 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 inputStream = connection.getInputStream(); + int i = inputStream.read(); inputStream.close(); connection.disconnect(); - }catch (Exception e){ + if (UpdateIp.nas_ip != null) { + connection = (HttpURLConnection) new URL("http://" + UpdateIp.nas_ip + ":8000/localhome/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8") + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor").openConnection(); + connection.connect(); + } + } catch (Exception e) { e.printStackTrace(); } } /** * 获取项目路径 + * * @param request * @return */ public static String getPath(HttpServletRequest request) { return request.getServletContext().getRealPath("/") + "/"; } + /** * 获取客户端IP * @@ -115,13 +128,12 @@ public class Tools { } return ip; } + /** * N以内的不重复随机数 * - * @param min - * 最小值 - * @param max - * 最大值 + * @param min 最小值 + * @param max 最大值 * @param n * @return */ @@ -148,12 +160,13 @@ public class Tools { } 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")); + + public static int checkWebLogin(HttpServletRequest request) { + JSONArray array = new JSONArray(); + if (RedisTools.get("bean") != null) { + array = JSONArray.parseArray(RedisTools.get("bean")); } - if(array.contains(Tools.getRemoteAddress(request))){ + if (array.contains(Tools.getRemoteAddress(request))) { System.out.println("IP已被封禁"); return -100; } @@ -161,57 +174,57 @@ public class Tools { if (cookie == null) { return -1; } - if (redisTools.get(cookie.getValue()).equals("ok")) { - return 1; + if (RedisTools.get(cookie.getValue()).equals("ok")) { + return 1; } return 0; } - public static String get(String url){ - try{ - HttpURLConnection connection= (HttpURLConnection) new URL(url).openConnection(); - BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream())); - StringBuilder str= new StringBuilder(); + + public static String get(String url) { + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder str = new StringBuilder(); String tmp; - while ((tmp=reader.readLine())!=null){ + while ((tmp = reader.readLine()) != null) { str.append(tmp); } reader.close(); connection.disconnect(); return str.toString(); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } return null; } + /** * 保存上传的文件 * - * @param path - * 路径 - * @param file - * 文件 + * @param path 路径 + * @param file 文件 * @return * @throws Exception */ public static String createFile(String path, MultipartFile file, String newFileName) throws Exception { - String savePath =new File("").getAbsolutePath()+File.separator +"html"+File.separator+ path; + String savePath = new File("").getAbsolutePath() + File.separator + "html" + File.separator + path; File servicePath = new File(savePath); if (!servicePath.exists()) { servicePath.mkdirs(); } - String fileName=file.getOriginalFilename(); - if(newFileName!=null) { - fileName=newFileName; + String fileName = file.getOriginalFilename(); + if (newFileName != null) { + fileName = newFileName; } File saveFile = new File(savePath + "/" + fileName); - if(saveFile.exists()) { - if(!saveFile.delete()) { - saveFile = new File(savePath + "/" + fileName.replace(".", "_"+new Date().getTime()+".")); + if (saveFile.exists()) { + if (!saveFile.delete()) { + saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + ".")); } } file.transferTo(saveFile); - fileName=URLEncoder.encode(fileName,"UTF-8"); - System.out.println("上传文件保存路径:"+saveFile.getAbsolutePath()); - return path+fileName; + fileName = URLEncoder.encode(fileName, "UTF-8"); + System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath()); + return path + fileName; } } diff --git a/src/main/java/com/yutou/tools/web/userController.java b/src/main/java/com/yutou/tools/web/userController.java index 6369374..3532d98 100644 --- a/src/main/java/com/yutou/tools/web/userController.java +++ b/src/main/java/com/yutou/tools/web/userController.java @@ -19,8 +19,6 @@ import java.util.UUID; @Controller public class userController { - @Resource - RedisTools redisTools; @RequestMapping("/login/check.do") @ResponseBody @@ -29,8 +27,8 @@ public class userController { json.put("code", -1); json.put("msg", "未登录"); JSONArray array = new JSONArray(); - if (redisTools.get("ban") != null) { - array = JSONArray.parseArray(redisTools.get("ban")); + if (RedisTools.get("ban") != null) { + array = JSONArray.parseArray(RedisTools.get("ban")); } if (array.contains(Tools.getRemoteAddress(request))) { json.put("code", -2); @@ -42,7 +40,7 @@ public class userController { if (cookie == null) { return json.toJSONString(); } - if ("ok".equals(redisTools.get(cookie.getValue()))) { + if ("ok".equals(RedisTools.get(cookie.getValue()))) { json.put("code", 0); json.put("msg", "登录成功"); return json.toJSONString(); @@ -56,8 +54,8 @@ public class userController { @ResponseBody public String captcha(HttpServletRequest request) { JSONArray array = new JSONArray(); - if (redisTools.get("ban") != null) { - array = JSONArray.parseArray(redisTools.get("ban")); + if (RedisTools.get("ban") != null) { + array = JSONArray.parseArray(RedisTools.get("ban")); } if (array.contains(Tools.getRemoteAddress(request))) { @@ -69,9 +67,9 @@ public class userController { for (int value : captcha) { cc += value; } - redisTools.set("login", cc, 5 * 60 * 1000); + RedisTools.set("login", cc, 5 * 60 * 1000); String token=UUID.randomUUID().toString().replace("-",""); - redisTools.set(token,Tools.getRemoteAddress(request),10 * 60 * 1000); + RedisTools.set(token,Tools.getRemoteAddress(request),10 * 60 * 1000); String url="http://tools.yutou233.cn/login/ban.do?token="+token; Tools.sendServer("管理后台登录验证码", "本次登录验证码为:" + cc + ",登录IP:" + Tools.getRemoteAddress(request) @@ -81,14 +79,14 @@ public class userController { @RequestMapping("/login/ban.do") @ResponseBody public String banIp(String token){ - String ip=redisTools.get(token); + String ip=RedisTools.get(token); if(ip!=null){ JSONArray array = new JSONArray(); - if (redisTools.get("ban") != null) { - array = JSONArray.parseArray(redisTools.get("bean")); + if (RedisTools.get("ban") != null) { + array = JSONArray.parseArray(RedisTools.get("bean")); } array.add(ip); - redisTools.set("ban",array.toJSONString()); + RedisTools.set("ban",array.toJSONString()); return "已封禁"; } return "ERROR"; @@ -98,10 +96,10 @@ public class userController { @ResponseBody public String login(HttpServletResponse response, String code) { JSONObject json = new JSONObject(); - if (redisTools.get("login").equals(code.trim())) { + if (RedisTools.get("login").equals(code.trim())) { String uuid = UUID.randomUUID().toString(); Tools.setCookie(response, "user", uuid.replace("-", ""), 30 * 24 * 60 * 60); - redisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60); + RedisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60); json.put("code", 0); json.put("msg", "登录成功"); return json.toJSONString(); @@ -119,8 +117,8 @@ public class userController { json.put("code", -1); json.put("msg", "退出失败"); if (cookie != null) { - if ("ok".equals(redisTools.get(cookie.getValue()))) { - redisTools.set(cookie.getValue(), "ok", 1); + if ("ok".equals(RedisTools.get(cookie.getValue()))) { + RedisTools.set(cookie.getValue(), "ok", 1); Tools.deleteCookie(request, response, "user"); json.put("code", 0); json.put("msg", "退出成功");