This commit is contained in:
Yutousama 2020-04-17 14:32:22 +08:00
parent b796a1a839
commit 6cb1c0f9eb
11 changed files with 522 additions and 91 deletions

View 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();
}
}

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -1,44 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>NAS</title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
<div id="header"></div>
<div class="layui-body" style="left: 0px;">
123
</div>
<div id="footer"></div>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script>
layui.use(['layer', 'form', 'element'], function () {
var layer = layui.layer
, form = layui.form;
});
$.ajax({cache: false})
$('#header').load("/html/header.html");
$('#footer').load("/html/footer.html");
</script>
<style>
#icon {
float: right;
}
.body {
bottom: 0;
}
</style>
</body>
</html>

View File

@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>NAS</title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
<div id="header"></div>
<div class="layui-body" style="left: 0px;">
<div id="side"></div>
<div id="footer"></div>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script>
layui.use(['layer', 'form', 'element'], function () {
var layer = layui.layer
, form = layui.form;
});
$.ajax({ cache: false })
$('#header').load("/html/header.html");
$('#footer').load("/html/footer.html");
$('#side').load("/html/body/nas/side.html");
</script>
<style>
#icon {
float: right;
}
.body {
bottom: 0;
}
</style>
</body>
</html>

53
web/html/body/nas/ip.html Normal file
View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>NAS</title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
<div id="header"></div>
<div class="layui-body" style="left: 200px;">
<div id="side"></div>
<blockquote class="layui-elem-quote" ><span id="ip">当前IP:</span></blockquote>
<div id="footer"></div>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script>
layui.use(['layer', 'form', 'element'], function () {
var layer = layui.layer
, form = layui.form;
});
$.ajax({ cache: false })
$('#header').load("/html/header.html");
$('#footer').load("/html/footer.html");
$('#side').load("/html/body/nas/side.html");
$.post("/nas/getIp.do",function(data){
var json=JSON.parse(data);
if(json.code!=0){
$('#ip').html(json.msg);
}else{
$('#ip').html("当前服务器IP:"+json.data);
}
})
</script>
<style>
#icon {
float: right;
}
.body {
bottom: 0;
}
</style>
</body>
</html>

View File

@ -0,0 +1,28 @@
<div class="layui-side layui-bg-black">
<div class="layui-side-scroll layui-bg-blue">
<!-- 左侧导航区域可配合layui已有的垂直导航 -->
<ul class="layui-nav layui-nav-tree layui-bg-blue" lay-filter="test">
<li class="layui-nav-item">
<a class="" href="javascript:;">本体管理</a>
<dl class="layui-nav-child">
<dd><a href="/html/body/nas/ip.html">查看IP</a></dd>
</dl>
</li>
<li class="layui-nav-item">
<a class="" href="javascript:;">管理后台</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">切换管理后台</a></dd>
<dd><a href="javascript:;">管理后台</a></dd>
<dd><a href="javascript:;">Jellyfin</a></dd>
</dl>
</li>
<li class="layui-nav-item">
<a href="javascript:;">功能管理</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">B站直播下载器</a></dd>
</dl>
</li>
</ul>
</div>
</div>

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>NAS</title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
<div id="header"></div>
<div class="layui-body" style="left: 200px;">
<div id="side"></div>
<blockquote class="layui-elem-quote" ><span id="ip">当前IP:</span></blockquote>
<div id="footer"></div>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script>
layui.use(['layer', 'form', 'element'], function () {
var layer = layui.layer
, form = layui.form;
});
$.ajax({ cache: false })
$('#header').load("/html/header.html");
$('#footer').load("/html/footer.html");
$('#side').load("/html/body/nas/side.html");
$.post("/nas/getIp.do",function(data){
var json=JSON.parse(data);
if(json.code!=0){
$('#ip').html(json.msg);
}else{
$('#ip').html("当前服务器IP:"+json.data);
}
})
</script>
<style>
#icon {
float: right;
}
.body {
bottom: 0;
}
</style>
</body>
</html>

View File

@ -1,46 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="layui-header">
<ul class="layui-nav layui-bg-blue" lay-filter="">
<li class="layui-nav-item"><a href="http://blog.yutou233.cn">博客</a></li>
<div id="admin" style="display: inline-block; font-size: 0px;">
<li class="layui-nav-item"><a href="javascript:;">NAS管理</a>
<div class="layui-header">
<ul class="layui-nav layui-bg-blue" lay-filter="">
<li class="layui-nav-item"><a href="http://blog.yutou233.cn">博客</a></li>
<div id="admin" style="display: inline-block; font-size: 0px;">
<li class="layui-nav-item"><a href="/html/body/nas/index.html">NAS管理</a>
</li>
</div>
<li class="layui-nav-item">
<a href="javascript:;">工具集</a>
<dl class="layui-nav-child">
<dd><a href="/html/body/nas.html">查看NAS IP</a></dd>
<dd><a href="#">切换NAS页面</a></dd>
<dd><a href="#">B站直播管理</a></dd>
<dd><a href="javascript:;" target="_blank">密码管理器</a></dd>
</dl>
</li>
</div>
<li class="layui-nav-item">
<a href="javascript:;">工具集</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;" target="_blank">密码管理器</a></dd>
</dl>
</li>
<li class="layui-nav-item">
<a href="javascript:;">页面集</a>
<dl class="layui-nav-child">
<dd><a href="http://nas.yutou233.cn/" target="_blank">管理后台</a></dd>
<dd><a href="http://bt.yutou233.cn" target="_blank">BT下载</a></dd>
</dl>
</li>
<li class="layui-nav-item" id='icon'>
<a href=""><img src="//t.cn/RCzsdCq" class="layui-nav-img">登录</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">修改信息</a></dd>
<dd><a href="javascript:;">安全管理</a></dd>
<dd><a href="javascript:;">退了</a></dd>
</dl>
</li>
</ul>
</div>
<li class="layui-nav-item">
<a href="javascript:;">页面集</a>
<dl class="layui-nav-child">
<dd><a href="http://nas.yutou233.cn/" target="_blank">管理后台</a></dd>
<dd><a href="http://bt.yutou233.cn" target="_blank">BT下载</a></dd>
</dl>
</li>
<li class="layui-nav-item" id='icon'>
<a href="javascript:;" id="login"><img src="//t.cn/RCzsdCq" class="layui-nav-img">登录</a>
<dl class="layui-nav-child">
<dd><a href="javascript:;">退了</a></dd>
</dl>
</li>
</ul>
</div>
</body>
<script>
$('#login').click(function () {
$.post('/login/sendCaptcha.do', function () {
layer.prompt({
title: '安全登录码'
}, function (value, index, elem) {
alert(value); //得到value
layer.close(index);
})
})
})
</script>
</html>

View File

@ -27,7 +27,7 @@
});
$.ajax({cache: false})
$('#header').load("../html/header.html");
$('#header').load("/html/header.html");
$('#footer').load("../html/footer.html");
</script>
<style>