From 71d8a52decd09ec90cac64eed2097b645db1c110 Mon Sep 17 00:00:00 2001
From: Yutousama <583819556@qq.com>
Date: Tue, 29 Sep 2020 08:52:16 +0800
Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E7=99=BB=E9=99=86=E6=9B=BF?=
=?UTF-8?q?=E6=8D=A2=E6=88=90Google=E8=BA=AB=E4=BB=BD=E9=AA=8C=E8=AF=81?=
=?UTF-8?q?=E5=99=A8=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E5=B7=A5?=
=?UTF-8?q?=E5=85=B7=E7=B1=BB=20=E6=9B=B4=E6=96=B0=E9=A1=B5=E9=9D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../com/yutou/tools/Tools/GoogleAccount.java | 10 ++-
.../yutou/tools/services/ServerManager.java | 81 +++++++++++++++++++
.../com/yutou/tools/utils/ConfigTools.java | 79 ++++++++++++++++++
.../com/yutou/tools/utils/RedisTools.java | 35 ++++----
.../com/yutou/tools/web/ToolsController.java | 45 +++++++++++
.../com/yutou/tools/web/userController.java | 74 +++++++++++++----
web/html/header.html | 38 ++++++++-
web/js/qrcode.min.js | 1 +
9 files changed, 325 insertions(+), 40 deletions(-)
create mode 100644 src/main/java/com/yutou/tools/services/ServerManager.java
create mode 100644 src/main/java/com/yutou/tools/utils/ConfigTools.java
create mode 100644 src/main/java/com/yutou/tools/web/ToolsController.java
create mode 100644 web/js/qrcode.min.js
diff --git a/pom.xml b/pom.xml
index 0a9998c..c8908e6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
com.yutou
tools
- 1.0.8
+ 1.0.9.1
tools
Demo project for Spring Boot
diff --git a/src/main/java/com/yutou/tools/Tools/GoogleAccount.java b/src/main/java/com/yutou/tools/Tools/GoogleAccount.java
index 227cf7a..0036af6 100644
--- a/src/main/java/com/yutou/tools/Tools/GoogleAccount.java
+++ b/src/main/java/com/yutou/tools/Tools/GoogleAccount.java
@@ -10,12 +10,13 @@ import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
public class GoogleAccount {
+ public static final boolean isDev=false;
// 生成的key长度( Generate secret key length)
- public static final int SECRET_SIZE = 10;
+ private static final int SECRET_SIZE = 10;
- public static final String SEED = "g8GjEvTbW5oVSV7avL47357438reyhreyuryetredLDVKs2m0QN7vxRs2im5MDaNCWGmcD2rvcZx";
+ private static final String SEED = "g8GjEvTbW5oVSV7avL47357438reyhreyuryetredLDVKs2m0QN7vxRs2im5MDaNCWGmcD2rvcZx";
// Java实现随机数算法
- public static final String RANDOM_NUMBER_ALGORITHM = "SHA1PRNG";
+ private static final String RANDOM_NUMBER_ALGORITHM = "SHA1PRNG";
// 最多可偏移的时间
int window_size = 3; // default 3 - max 17
@@ -145,7 +146,8 @@ public class GoogleAccount {
public static void main(String[] args) {
String secret=GoogleAccount.generateSecretKey();
- String qrcode = GoogleAccount.getQRBarcode("yutou", secret);
+ String uname=isDev?"yutou(dev)":"yutou";
+ String qrcode = GoogleAccount.getQRBarcode(uname, secret);
System.out.println("qrcode:" + qrcode + ",key:" + secret);
while (true){
String code=new Scanner(System.in).nextLine();
diff --git a/src/main/java/com/yutou/tools/services/ServerManager.java b/src/main/java/com/yutou/tools/services/ServerManager.java
new file mode 100644
index 0000000..fb49383
--- /dev/null
+++ b/src/main/java/com/yutou/tools/services/ServerManager.java
@@ -0,0 +1,81 @@
+package com.yutou.tools.services;
+
+import java.io.*;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.List;
+
+public class ServerManager {
+ private static ServerManager manager;
+ private ServerSocket server;
+ private ServerManager(){
+ try {
+ server=new ServerSocket(8100);
+ while (true){
+ new MyThread(server.accept());
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ public void restart(){
+ try {
+ server.close();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ try {
+ server=new ServerSocket(8100);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ private OutputStream nasOutPutStream;
+ private InputStream nasInputStream;
+ public static ServerManager getManager() {
+ if(manager==null){
+ manager=new ServerManager();
+ }
+ return manager;
+ }
+ public void send(String msg){
+ if(nasOutPutStream!=null){
+ try {
+ nasOutPutStream.write(msg.getBytes());
+ nasOutPutStream.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ private class MyThread extends Thread{
+ Socket socket;
+ private MyThread(Socket socket){
+ this.socket=socket;
+ start();
+ }
+
+ @Override
+ public void run() {
+ super.run();
+ try {
+ nasOutPutStream=socket.getOutputStream();
+ nasInputStream=socket.getInputStream();
+ BufferedReader reader=new BufferedReader(new InputStreamReader(nasInputStream));
+ String tmp,str="";
+ while (true){
+ tmp=reader.readLine();
+ if(tmp!=null){
+ System.out.println(tmp);
+ }
+
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ nasOutPutStream=null;
+ nasInputStream=null;
+ }
+
+ }
+}
diff --git a/src/main/java/com/yutou/tools/utils/ConfigTools.java b/src/main/java/com/yutou/tools/utils/ConfigTools.java
new file mode 100644
index 0000000..81910ba
--- /dev/null
+++ b/src/main/java/com/yutou/tools/utils/ConfigTools.java
@@ -0,0 +1,79 @@
+package com.yutou.tools.utils;
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.io.*;
+
+/**
+ * 配置和参数
+ */
+public class ConfigTools {
+ public static final String CONFIG="config.json";
+ public static final String DATA="data.json";
+ static {
+ try {
+ File file=new File(CONFIG);
+ if(!file.exists()){
+ file.createNewFile();
+ }
+ file=new File(DATA);
+ if(!file.exists()){
+ file.createNewFile();
+ }
+ file=null;
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+
+ }
+ public static Object load(String type,String key){
+ File file=new File(type);
+ String src=readFile(file);
+ if(src!=null){
+ JSONObject json=JSONObject.parseObject(src);
+ if(json==null){
+ json=new JSONObject();
+ saveFile(file,json.toJSONString());
+ }
+ return json.getOrDefault(key, "");
+ }
+ return null;
+ }
+ public static boolean save(String type,String key,Object data){
+ File file=new File(type);
+ String src=readFile(file);
+ if(src!=null){
+ JSONObject json=JSONObject.parseObject(src);
+ json.put(key,data);
+ saveFile(file,json.toJSONString());
+ }
+ return false;
+ }
+ public static boolean saveFile(File file,String data){
+ try {
+ FileWriter writer=new FileWriter(file);
+ writer.write(data);
+ writer.flush();
+ writer.close();
+ return true;
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ public static String readFile(File file){
+ try {
+ BufferedReader reader=new BufferedReader(new FileReader(file));
+ String tmp;
+ StringBuilder str= new StringBuilder();
+ while ((tmp=reader.readLine())!=null){
+ str.append(tmp);
+ }
+ reader.close();
+ return str.toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/com/yutou/tools/utils/RedisTools.java b/src/main/java/com/yutou/tools/utils/RedisTools.java
index afcacb2..7355750 100644
--- a/src/main/java/com/yutou/tools/utils/RedisTools.java
+++ b/src/main/java/com/yutou/tools/utils/RedisTools.java
@@ -226,6 +226,9 @@ public class RedisTools {
case "cmd":
system("cmd", message);
break;
+ case "msg":
+ Tools.sendServer("来自服务姬的通知~",message);
+ break;
}
}
@@ -260,23 +263,7 @@ public class RedisTools {
}
}
- private void processOut(InputStream inputStream) {
- String tmp, str = "null";
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
- while ((tmp = reader.readLine()) != null) {
- str += tmp + "\n";
- }
- reader.close();
- inputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("cmd > " + str);
- RedisTools.set(1, "msg_" + System.currentTimeMillis(), str);
- System.out.println("线程结束");
- }
private void bot(String value) {
switch (value) {
@@ -288,7 +275,23 @@ public class RedisTools {
}
}
}
+ public static void processOut(InputStream inputStream) {
+ String tmp, str = "null";
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+ while ((tmp = reader.readLine()) != null) {
+ str += tmp + "\n";
+ }
+ reader.close();
+ inputStream.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ System.out.println("cmd > " + str);
+ RedisTools.set(1, "msg_" + System.currentTimeMillis(), str);
+ System.out.println("线程结束");
+ }
public static void main(String[] args) {
RedisTools.pullMsg("msg", "abc");
}
diff --git a/src/main/java/com/yutou/tools/web/ToolsController.java b/src/main/java/com/yutou/tools/web/ToolsController.java
new file mode 100644
index 0000000..d48af7f
--- /dev/null
+++ b/src/main/java/com/yutou/tools/web/ToolsController.java
@@ -0,0 +1,45 @@
+package com.yutou.tools.web;
+
+import com.yutou.tools.nas.UpdateIp;
+import com.yutou.tools.utils.RedisTools;
+import com.yutou.tools.utils.Tools;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+
+@Controller
+public class ToolsController {
+ @ResponseBody
+ @RequestMapping("/tools/openpc.do")
+ public String open_pc(HttpServletRequest request, String type) {
+ if (StringUtils.isEmpty(type)) {
+ if (Tools.checkWebLogin(request) == 1) {
+ Tools.get("http://" + UpdateIp.nas_ip + ":8000/tools/openpc.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor&type=nas");
+ }
+ } else {
+ if (type.equals("nas")) {
+ try {
+ Process process = Runtime.getRuntime().exec("wakeonlan 00:D8:61:6F:02:2F");
+ RedisTools.processOut(process.getInputStream());
+ RedisTools.processOut(process.getErrorStream());
+ process.destroy();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return "ok";
+ }
+
+ @ResponseBody
+ @RequestMapping("/tools/server.do")
+ public String sendServerManager(String title, String msg) {
+ Tools.sendServer(title, msg);
+ return "ok";
+ }
+}
diff --git a/src/main/java/com/yutou/tools/web/userController.java b/src/main/java/com/yutou/tools/web/userController.java
index d22ae6e..f030953 100644
--- a/src/main/java/com/yutou/tools/web/userController.java
+++ b/src/main/java/com/yutou/tools/web/userController.java
@@ -3,9 +3,12 @@ package com.yutou.tools.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+import com.yutou.tools.Tools.GoogleAccount;
+import com.yutou.tools.utils.ConfigTools;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -56,15 +59,18 @@ public class userController {
@ResponseBody
public String captcha(HttpServletRequest request) {
JSONArray array = new JSONArray();
+ JSONObject json = new JSONObject();
if (RedisTools.get("ban") != null) {
array = JSONArray.parseArray(RedisTools.get("ban"));
}
if (array.contains(Tools.getRemoteAddress(request))) {
-
System.out.println("IP已被封禁");
- return "ERROR!";
+ json.put("msg", "IP已被封禁");
+ json.put("code", -1);
+ return json.toJSONString();
}
- int[] captcha = Tools.randomCommon(0, 9, 6);
+ /* //原验证码方案
+ int[] captcha = Tools.randomCommon(0, 9, 6);
String cc = "";
for (int value : captcha) {
cc += value;
@@ -75,8 +81,21 @@ public class userController {
String url = "http://tools.yutou233.cn/login/ban.do?token=" + token;
Tools.sendServer("管理后台登录验证码", "本次登录验证码为:" + cc
+ ",登录IP:" + Tools.getRemoteAddress(request)
- + ",非正常登录,封禁IP:" + url);
- return "ok";
+ + ",非正常登录,封禁IP:" + url);*/
+ String secret = (String) ConfigTools.load(ConfigTools.DATA, "secret");
+ if (StringUtils.isEmpty(secret)) {
+ secret = GoogleAccount.generateSecretKey();
+ String uname=GoogleAccount.isDev?"yutou(dev)":"yutou";
+ String code = GoogleAccount.getQRBarcode(uname, secret);
+ ConfigTools.save(ConfigTools.DATA,"secret_tmp",secret);
+ json.put("msg", "绑定连接");
+ json.put("code", 1);
+ json.put("data", code);
+ return json.toJSONString();
+ }
+ json.put("msg", "ok");
+ json.put("code", 0);
+ return json.toJSONString();
}
@RequestMapping("/login/ban.do")
@@ -88,8 +107,8 @@ public class userController {
if (RedisTools.get("ban") != null) {
array = JSONArray.parseArray(RedisTools.get("bean"));
}
- if(array==null){
- array=new JSONArray();
+ if (array == null) {
+ array = new JSONArray();
}
array.add(ip);
RedisTools.set("ban", array.toJSONString());
@@ -107,16 +126,39 @@ public class userController {
@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);
- RedisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60);
- json.put("code", 0);
- json.put("msg", "登录成功");
- return json.toJSONString();
+ String secret= (String) ConfigTools.load(ConfigTools.DATA,"secret");
+ if(StringUtils.isEmpty(secret)){
+ secret= (String) ConfigTools.load(ConfigTools.DATA,"secret_tmp");
+ if(StringUtils.isEmpty(secret)){
+ json.put("code",-2);
+ json.put("msg","未绑定");
+ return json.toJSONString();
+ }
+ if(new GoogleAccount().check_code(secret,Long.parseLong(code),System.currentTimeMillis())){
+ json.put("code", 0);
+ json.put("msg", "登录成功");
+ ConfigTools.save(ConfigTools.DATA,"secret",secret);
+ ConfigTools.save(ConfigTools.DATA,"secret_tmp","");
+ }else {
+ json.put("code", -2);
+ json.put("msg", "登录失败");
+ return json.toJSONString();
+ }
+ }else{
+ if(new GoogleAccount().check_code(secret,Long.parseLong(code),System.currentTimeMillis())){
+ json.put("code", 0);
+ json.put("msg", "登录成功");
+ }else {
+ json.put("code", -2);
+ json.put("msg", "登录失败");
+ return json.toJSONString();
+ }
}
- json.put("code", -2);
- json.put("msg", "登录安全码错误");
+ String uuid = UUID.randomUUID().toString();
+ Tools.setCookie(response, "user", uuid.replace("-", ""), 30 * 24 * 60 * 60);
+ RedisTools.set(uuid.replace("-", ""), "ok", 30 * 24 * 60 * 60);
+ json.put("code", 0);
+ json.put("msg", "登录成功");
return json.toJSONString();
}
diff --git a/web/html/header.html b/web/html/header.html
index a058372..599136e 100644
--- a/web/html/header.html
+++ b/web/html/header.html
@@ -19,6 +19,7 @@
工具集
- 密码管理器
+ - 远程开机
@@ -38,11 +39,12 @@