更新机器人功能

This commit is contained in:
Yutousama 2020-06-15 23:54:13 +08:00
parent 4dc019b200
commit d2b2e8d262
4 changed files with 177 additions and 36 deletions

View File

@ -10,7 +10,7 @@
</parent>
<groupId>com.yutou</groupId>
<artifactId>tools</artifactId>
<version>1.0.7.3</version>
<version>1.0.8</version>
<name>tools</name>
<description>Demo project for Spring Boot</description>

View File

@ -1,5 +1,6 @@
package com.yutou.tools;
import com.yutou.tools.utils.RedisTools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -8,6 +9,7 @@ public class ToolsApplication {
public static void main(String[] args) {
SpringApplication.run(ToolsApplication.class, args);
RedisTools.initRedisPoolSub();
}
}

View File

@ -1,11 +1,19 @@
package com.yutou.tools.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import javax.accessibility.AccessibleAction;
public class RedisTools {
private static String host;
@ -24,6 +32,7 @@ public class RedisTools {
host = "127.0.0.1";
port = 6379;
}
public static boolean set(int dbIndex, String key, String value) {
try {
Jedis jedis = getRedis();
@ -38,6 +47,7 @@ public class RedisTools {
}
return true;
}
public static boolean set(String key, String value) {
return set(0, key, value);
}
@ -58,6 +68,7 @@ public class RedisTools {
}
return true;
}
public static String get(String key, int dbIndex) {
String value = "-999";
Jedis jedis = null;
@ -75,9 +86,11 @@ public class RedisTools {
}
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);
@ -88,6 +101,7 @@ public class RedisTools {
return false;
}
}
public static void removeLoginState(String uid) {
Jedis jedis = getRedis();
Set<String> keys = jedis.keys("*");
@ -98,21 +112,25 @@ public class RedisTools {
}
}
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流里面
@ -130,8 +148,128 @@ public class RedisTools {
return properties;
}
}
private static Jedis getPoolRedis() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(0);
poolConfig.setMaxWaitMillis(1000);
JedisPool pool = new JedisPool(poolConfig, host);
return pool.getResource();
}
public static void pullMsg(String channel, String msg) {
System.out.println("1");
Jedis jedis = getPoolRedis();
System.out.println("2");
jedis.publish(channel, msg);
System.out.println("3");
jedis.close();
System.out.println("4");
}
private static boolean init = false;
public static void initRedisPoolSub() {
if (init)
return;
init = true;
System.out.println("初始化订阅");
new Thread(new Runnable() {
@Override
public void run() {
Jedis jedis = getPoolRedis();
jedis.psubscribe(new Consumer(), "*");
}
}).start();
}
private static class Consumer extends JedisPubSub {
@Override
public void onPMessage(String pattern, String channel, String message) {
super.onPMessage(pattern, channel, message);
System.out.println("onPMessage: channel=" + channel + " msg=" + message + " pattern=" + pattern);
switch (channel) {
case "system":
switch (message){
case "openPC":
system("openPC",null);
break;
case "updateIP":
system("updateIP",null);
break;
}
break;
case "bot":
bot(message);
break;
case "cmd":
system("cmd", message);
break;
}
}
@Override
public void onMessage(String channel, String message) {
super.onMessage(channel, message);
System.out.println("onMessage: channel=" + channel + " msg=" + message);
}
private void system(String type, String value) {
try {
String exec = null;
switch (type) {
case "openPC":
exec = "wakeonlan 00:D8:61:6F:02:2F";
break;
case "cmd":
exec = value;
break;
case "updateIP":
exec="python3 /media/yutou/4t/public/Python/tools/ip.py";
break;
}
if (exec != null) {
Process process = Runtime.getRuntime().exec(exec);
processOut(process.getInputStream());
processOut(process.getErrorStream());
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
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) {
case "getip":
JSONObject json = JSONObject.parseObject(Tools.get("https://api.asilu.com/ip/"));
String ip = json.getString("ip");
RedisTools.set(1, "msg_" + System.currentTimeMillis(), "服务器IP:\n" + ip);
break;
}
}
}
public static void main(String[] args) {
String str= "\"aaa\"";
str=str.replaceAll("\"aaa\"", "0");
RedisTools.pullMsg("msg", "abc");
}
}

View File

@ -187,6 +187,7 @@ public class Tools {
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;