From f53bc2d48d67f1a124f20ced19f7ce1c6de7b377 Mon Sep 17 00:00:00 2001 From: yutou Date: Fri, 25 Dec 2020 10:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B01.0.14=20=E6=95=B4?= =?UTF-8?q?=E5=90=88=E7=BD=91=E7=BB=9C=E8=AF=B7=E6=B1=82=E5=88=B0HttpTools?= =?UTF-8?q?=E7=B1=BB=20server=E9=85=B1=E8=AF=B7=E6=B1=82=E6=94=B9=E6=88=90?= =?UTF-8?q?post=20=E6=96=B0=E5=A2=9E!version=E6=93=8D=E4=BD=9C=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=AC=E5=9C=B0=E5=92=8C=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yutou/tools/ToolsApplication.java | 2 +- .../yutou/tools/bangumi/AnimationData.java | 14 +-- .../yutou/tools/home/nas/ToolsController.java | 4 +- .../java/com/yutou/tools/utils/HttpTools.java | 104 ++++++++++++++++++ .../utils/Interfaces/NetworkInterface.java | 16 +++ .../com/yutou/tools/utils/QQBotManager.java | 12 ++ .../com/yutou/tools/utils/RedisTools.java | 3 +- .../java/com/yutou/tools/utils/Tools.java | 62 ++++------- 8 files changed, 159 insertions(+), 58 deletions(-) create mode 100644 src/main/java/com/yutou/tools/utils/HttpTools.java create mode 100644 src/main/java/com/yutou/tools/utils/Interfaces/NetworkInterface.java diff --git a/src/main/java/com/yutou/tools/ToolsApplication.java b/src/main/java/com/yutou/tools/ToolsApplication.java index f0b44de..e186cbf 100644 --- a/src/main/java/com/yutou/tools/ToolsApplication.java +++ b/src/main/java/com/yutou/tools/ToolsApplication.java @@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ToolsApplication { - public static final String version="1.0.13"; + public static final String version="1.0.14"; public static void main(String[] args) { System.out.println("当前版本号:"+version); diff --git a/src/main/java/com/yutou/tools/bangumi/AnimationData.java b/src/main/java/com/yutou/tools/bangumi/AnimationData.java index e3a630a..4d0c8fa 100644 --- a/src/main/java/com/yutou/tools/bangumi/AnimationData.java +++ b/src/main/java/com/yutou/tools/bangumi/AnimationData.java @@ -1,17 +1,9 @@ package com.yutou.tools.bangumi; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.yutou.tools.utils.BiliBiliLiveTools; -import com.yutou.tools.utils.Tools; +import com.yutou.tools.utils.HttpTools; -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; import java.net.URLEncoder; public class AnimationData { @@ -32,7 +24,7 @@ public class AnimationData { title = title.substring(1, title.length() - 1); System.out.println(String.format(animUrl, "" + index, title,type,team)); System.out.println(rss2jsonUrl + URLEncoder.encode(String.format(animUrl, "" + index, title,type,team), "UTF-8") +String.format( "&api_key=%s&count=500",rss2jsonApi_2)); - String js=Tools.get(rss2jsonUrl+ URLEncoder.encode(String.format(animUrl,""+index,title,type,team),"UTF-8")+String.format( "&api_key=%s&count=500",rss2jsonApi_2)); + String js= HttpTools.get(rss2jsonUrl+ URLEncoder.encode(String.format(animUrl,""+index,title,type,team),"UTF-8")+String.format( "&api_key=%s&count=500",rss2jsonApi_2)); JSONObject json=JSONObject.parseObject(js); if(json.getString("status").equals("ok")){ return json.getJSONArray("items"); @@ -61,7 +53,7 @@ public class AnimationData { } public static void initData(){ String url="https://share.dmhy.org/topics/advanced-search?team_id=0&sort_id=0&orderby="; - String data=Tools.get(url); + String data=HttpTools.get(url); System.out.println(data); } public static void main(String[] args) { diff --git a/src/main/java/com/yutou/tools/home/nas/ToolsController.java b/src/main/java/com/yutou/tools/home/nas/ToolsController.java index f7b51f9..bcedde5 100644 --- a/src/main/java/com/yutou/tools/home/nas/ToolsController.java +++ b/src/main/java/com/yutou/tools/home/nas/ToolsController.java @@ -1,6 +1,7 @@ package com.yutou.tools.home.nas; import com.yutou.tools.nas.UpdateIp; +import com.yutou.tools.utils.HttpTools; import com.yutou.tools.utils.RedisTools; import com.yutou.tools.utils.Tools; import org.springframework.stereotype.Controller; @@ -8,7 +9,6 @@ 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; @@ -19,7 +19,7 @@ public class ToolsController { 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"); + HttpTools.get("http://" + UpdateIp.nas_ip + ":8000/tools/openpc.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor&type=nas"); } } else { if (type.equals("nas")) { diff --git a/src/main/java/com/yutou/tools/utils/HttpTools.java b/src/main/java/com/yutou/tools/utils/HttpTools.java new file mode 100644 index 0000000..7b02e0b --- /dev/null +++ b/src/main/java/com/yutou/tools/utils/HttpTools.java @@ -0,0 +1,104 @@ +package com.yutou.tools.utils; + +import com.alibaba.fastjson.JSONException; +import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.utils.Interfaces.NetworkInterface; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Set; + +public class HttpTools { + public static String get(String url){ + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestProperty("User-Agent", getExtUa()); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder str = new StringBuilder(); + String tmp; + while ((tmp = reader.readLine()) != null) { + str.append(tmp); + } + reader.close(); + connection.disconnect(); + return str.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + public static void post(final String url, final byte[] body, final NetworkInterface networkInterface) { + + new Thread(new Runnable() { + + @Override + public void run() { + String tmp; + StringBuilder str = new StringBuilder(); + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setConnectTimeout(5 * 1000); + connection.setReadTimeout(10 * 1000); + //connection.addRequestProperty("Connection", "keep-alive"); + //connection.addRequestProperty("User-Agent", getExtUa()); + //connection.addRequestProperty("content-type", "application/json"); + connection.addRequestProperty("charset", "UTF-8"); + OutputStream outputStream = connection.getOutputStream(); + + outputStream.write(body); + outputStream.flush(); + outputStream.close(); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + while ((tmp = reader.readLine()) != null) { + str.append(tmp); + } + final String finalStr = str.toString(); + + // Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")"); + if (networkInterface != null) { + try { + networkInterface.httpGetData(str.toString(), connection.getResponseCode()); + } catch (IOException e) { + e.printStackTrace(); + } + } + connection.disconnect(); + reader.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + private static String getExtUa() { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"; + } + public String toUrlParams(JSONObject json) { + StringBuilder string = new StringBuilder(); + Set keys = json.keySet(); + for (String key : keys) { + try { + string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8")); + } catch (Exception e) { + e.printStackTrace(); + try { + string.append("&").append(URLEncoder.encode(key, "UTF-8")).append("="); + // string += "&" + key + "="; + } catch (Exception e1) { + string.append("&").append(key).append("="); + } + } + } + + string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", "")); + return string.toString(); + } +} diff --git a/src/main/java/com/yutou/tools/utils/Interfaces/NetworkInterface.java b/src/main/java/com/yutou/tools/utils/Interfaces/NetworkInterface.java new file mode 100644 index 0000000..465477c --- /dev/null +++ b/src/main/java/com/yutou/tools/utils/Interfaces/NetworkInterface.java @@ -0,0 +1,16 @@ +package com.yutou.tools.utils.Interfaces; + +public interface NetworkInterface { + /** + * 请求成功 + * @param data 请求参数 + * @param state http状态 + */ + void httpGetData(Object data, int state); + + /** + * 请求异常 + * @param e 异常 + */ + void httpError(Exception e); +} diff --git a/src/main/java/com/yutou/tools/utils/QQBotManager.java b/src/main/java/com/yutou/tools/utils/QQBotManager.java index ddefebe..7b8f3ba 100644 --- a/src/main/java/com/yutou/tools/utils/QQBotManager.java +++ b/src/main/java/com/yutou/tools/utils/QQBotManager.java @@ -1,5 +1,6 @@ package com.yutou.tools.utils; +import com.yutou.tools.ToolsApplication; import kotlin.coroutines.CoroutineContext; import net.mamoe.mirai.Bot; import net.mamoe.mirai.BotFactoryJvm; @@ -127,6 +128,9 @@ public class QQBotManager { case "!ip": RedisTools.Consumer.bot("getip"); break; + case "!version": + sendVersion(); + break; default: if (msg.startsWith("!cmd")) { RedisTools.Consumer.system("cmd", msg.replace("!cmd", "")); @@ -138,6 +142,14 @@ public class QQBotManager { return ListeningStatus.LISTENING; } + private void sendVersion() { + String localVersion= ToolsApplication.version; + String serverVersion=HttpTools.get("http://tools.yutou233.cn:8000/public/version.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor"); + String msg="本地版本:"+localVersion+"\n"+"服务器版本:"+serverVersion; + QQBotManager.getInstance().sendMessage(msg); + Tools.sendServer("服务版本查询",msg); + } + @EventHandler public void onMessage(@NotNull MessageEvent event) throws Exception { // sendMessage("个人:"+event.getSender().getNick()+" 发来消息:"+event.getMessage().contentToString()); diff --git a/src/main/java/com/yutou/tools/utils/RedisTools.java b/src/main/java/com/yutou/tools/utils/RedisTools.java index 4601c7c..1ecfec4 100644 --- a/src/main/java/com/yutou/tools/utils/RedisTools.java +++ b/src/main/java/com/yutou/tools/utils/RedisTools.java @@ -13,7 +13,6 @@ import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPubSub; -import javax.accessibility.AccessibleAction; public class RedisTools { private static boolean isNotInstallRedis = false; @@ -268,7 +267,7 @@ public class RedisTools { public static void bot(String value) { switch (value) { case "getip": - JSONObject json = JSONObject.parseObject(Tools.get("https://api.asilu.com/ip/")); + JSONObject json = JSONObject.parseObject(HttpTools.get("https://api.asilu.com/ip/")); String ip = json.getString("ip"); QQBotManager.getInstance().sendMessage("服务器IP:\n" + ip); break; diff --git a/src/main/java/com/yutou/tools/utils/Tools.java b/src/main/java/com/yutou/tools/utils/Tools.java index a0c91ce..1e50700 100644 --- a/src/main/java/com/yutou/tools/utils/Tools.java +++ b/src/main/java/com/yutou/tools/utils/Tools.java @@ -19,6 +19,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.Base64; import java.util.Date; @@ -89,13 +90,9 @@ public class Tools { public static void sendServer(String title, String msg) { try { System.out.println("title=" + title + " msg=" + msg); - HttpURLConnection connection = (HttpURLConnection) new URL("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send?title=" - + 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(); + HttpTools.post("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send", + ("title="+URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).getBytes(StandardCharsets.UTF_8), + null); if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("nas")) { String img = null; msg = msg.replace("
", "\n"); @@ -148,12 +145,7 @@ public class Tools { + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor"; } System.out.println(url); - connection = (HttpURLConnection) new URL(url).openConnection(); - connection.connect(); - inputStream = connection.getInputStream(); - i = inputStream.read(); - inputStream.close(); - connection.disconnect(); + HttpTools.get(url); } } catch (Exception e) { e.printStackTrace(); @@ -241,25 +233,6 @@ public class Tools { return 0; } - public static String get(String url) { - try { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - StringBuilder str = new StringBuilder(); - String tmp; - while ((tmp = reader.readLine()) != null) { - str.append(tmp); - } - reader.close(); - connection.disconnect(); - return str.toString(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - /** * 保存上传的文件 * @@ -293,7 +266,7 @@ public class Tools { public static void download(String url, DownloadInterface downloadInterface) { try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"); + connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"); connection.disconnect(); new File("tmp").mkdirs(); File file = new File("tmp" + File.separator + url.trim().split("/")[url.trim().split("/").length - 1]); @@ -337,6 +310,7 @@ public class Tools { headers.add("ETag", String.valueOf(System.currentTimeMillis())); return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file)); } + public static String getFileMD5(File file) { if (!file.isFile()) { return null; @@ -358,6 +332,7 @@ public class Tools { } return bytesToHexString(digest.digest()); } + private static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { @@ -373,29 +348,32 @@ public class Tools { } return stringBuilder.toString(); } - public static String base64ToString(String base){ - base=base.replace(" ","+"); + + public static String base64ToString(String base) { + base = base.replace(" ", "+"); try { - base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); - }catch (Exception e){ + base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes())); + } catch (Exception e) { try { - base=URLDecoder.decode(base,"UTF-8"); - base=base.replace(" ","+"); - base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); + base = URLDecoder.decode(base, "UTF-8"); + base = base.replace(" ", "+"); + base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes())); } catch (Exception e1) { e1.printStackTrace(); } } return base; } + /** * 异常输出 + * * @param e 异常 * @return */ public static String getExceptionString(Exception e) { - StringWriter writer=new StringWriter(); - PrintWriter printWriter=new PrintWriter(writer); + StringWriter writer = new StringWriter(); + PrintWriter printWriter = new PrintWriter(writer); e.printStackTrace(printWriter); printWriter.close(); return writer.toString();