更新机器人功能

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> </parent>
<groupId>com.yutou</groupId> <groupId>com.yutou</groupId>
<artifactId>tools</artifactId> <artifactId>tools</artifactId>
<version>1.0.7.3</version> <version>1.0.8</version>
<name>tools</name> <name>tools</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>

View File

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

View File

@ -1,16 +1,24 @@
package com.yutou.tools.utils; package com.yutou.tools.utils;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis; 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 { public class RedisTools {
private static String host; private static String host;
private static int port; private static int port;
public static int TOKEN_TIMEOUT_DEFAULT=360; public static int TOKEN_TIMEOUT_DEFAULT = 360;
private RedisTools() { private RedisTools() {
@ -21,15 +29,16 @@ public class RedisTools {
//Properties properties = PropertyUtil.loadProperties("jedis.properties"); //Properties properties = PropertyUtil.loadProperties("jedis.properties");
//host = properties.getProperty("redis.host"); //host = properties.getProperty("redis.host");
//port = Integer.valueOf(properties.getProperty("redis.port")); //port = Integer.valueOf(properties.getProperty("redis.port"));
host="127.0.0.1"; host = "127.0.0.1";
port=6379; port = 6379;
} }
public static boolean set(int dbIndex,String key,String value){
public static boolean set(int dbIndex, String key, String value) {
try { try {
Jedis jedis =getRedis(); Jedis jedis = getRedis();
jedis.select(dbIndex); jedis.select(dbIndex);
String ret=jedis.set(key,value); String ret = jedis.set(key, value);
System.out.println("Redis set ="+ret); System.out.println("Redis set =" + ret);
jedis.close(); jedis.close();
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
@ -38,29 +47,31 @@ public class RedisTools {
} }
return true; return true;
} }
public static boolean set(String key, String value) { public static boolean set(String key, String value) {
return set(0,key,value); return set(0, key, value);
} }
public static boolean set(String key,String value,int timeout) { public static boolean set(String key, String value, int timeout) {
try { try {
Jedis jedis=getRedis(); Jedis jedis = getRedis();
if(timeout==-1){ if (timeout == -1) {
jedis.set(key,value); jedis.set(key, value);
}else { } else {
jedis.setex(key, timeout, value); jedis.setex(key, timeout, value);
} }
jedis.close(); jedis.close();
}catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
return true; return true;
} }
public static String get(String key,int dbIndex){
public static String get(String key, int dbIndex) {
String value = "-999"; String value = "-999";
Jedis jedis=null; Jedis jedis = null;
try { try {
jedis = getRedis(); jedis = getRedis();
jedis.select(dbIndex); jedis.select(dbIndex);
@ -69,50 +80,57 @@ public class RedisTools {
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
// e.printStackTrace(); // e.printStackTrace();
}finally { } finally {
if(jedis!=null) if (jedis != null)
jedis.close(); jedis.close();
} }
return value; return value;
} }
public static String get(String key) {
return get(key,0); public static String get(String key) {
return get(key, 0);
} }
public static boolean remove(String key) { public static boolean remove(String key) {
Jedis jedis=getRedis(); Jedis jedis = getRedis();
Long i=jedis.del(key); Long i = jedis.del(key);
jedis.close(); jedis.close();
if(i>0) { if (i > 0) {
return true; return true;
}else { } else {
return false; return false;
} }
} }
public static void removeLoginState(String uid) { public static void removeLoginState(String uid) {
Jedis jedis=getRedis(); Jedis jedis = getRedis();
Set<String> keys=jedis.keys("*"); Set<String> keys = jedis.keys("*");
for (String string : keys) { for (String string : keys) {
if(string.equals(uid)) { if (string.equals(uid)) {
jedis.del(string); jedis.del(string);
} }
} }
} }
public static String ping() { public static String ping() {
Jedis jedis=getRedis(); Jedis jedis = getRedis();
String tmp=jedis.ping(); String tmp = jedis.ping();
jedis.close(); jedis.close();
return tmp; return tmp;
} }
public static boolean exists(String key,String value) {
Jedis jedis=getRedis(); public static boolean exists(String key, String value) {
boolean flag=value.equals(jedis.get(key)); Jedis jedis = getRedis();
boolean flag = value.equals(jedis.get(key));
jedis.close(); jedis.close();
return flag; return flag;
} }
public static Jedis getRedis() { public static Jedis getRedis() {
return new Jedis(host,port); return new Jedis(host, port);
} }
private static class PropertyUtil { private static class PropertyUtil {
// 加载property文件到io流里面 // 加载property文件到io流里面
@ -130,8 +148,128 @@ public class RedisTools {
return properties; 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) { public static void main(String[] args) {
String str= "\"aaa\""; RedisTools.pullMsg("msg", "abc");
str=str.replaceAll("\"aaa\"", "0");
} }
} }

View File

@ -187,6 +187,7 @@ public class Tools {
public static String get(String url) { public static String get(String url) {
try { try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 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())); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
String tmp; String tmp;