update
This commit is contained in:
25
src/main/java/com/yutou/tools/nas/AdminManager.java
Normal file
25
src/main/java/com/yutou/tools/nas/AdminManager.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user