更新机器人功能
This commit is contained in:
parent
4dc019b200
commit
d2b2e8d262
2
pom.xml
2
pom.xml
@ -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>
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,24 @@
|
||||
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;
|
||||
private static int port;
|
||||
public static int TOKEN_TIMEOUT_DEFAULT=360;
|
||||
public static int TOKEN_TIMEOUT_DEFAULT = 360;
|
||||
|
||||
private RedisTools() {
|
||||
|
||||
@ -21,15 +29,16 @@ public class RedisTools {
|
||||
//Properties properties = PropertyUtil.loadProperties("jedis.properties");
|
||||
//host = properties.getProperty("redis.host");
|
||||
//port = Integer.valueOf(properties.getProperty("redis.port"));
|
||||
host="127.0.0.1";
|
||||
port=6379;
|
||||
host = "127.0.0.1";
|
||||
port = 6379;
|
||||
}
|
||||
public static boolean set(int dbIndex,String key,String value){
|
||||
|
||||
public static boolean set(int dbIndex, String key, String value) {
|
||||
try {
|
||||
Jedis jedis =getRedis();
|
||||
Jedis jedis = getRedis();
|
||||
jedis.select(dbIndex);
|
||||
String ret=jedis.set(key,value);
|
||||
System.out.println("Redis set ="+ret);
|
||||
String ret = jedis.set(key, value);
|
||||
System.out.println("Redis set =" + ret);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
@ -38,29 +47,31 @@ public class RedisTools {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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 {
|
||||
Jedis jedis=getRedis();
|
||||
if(timeout==-1){
|
||||
jedis.set(key,value);
|
||||
}else {
|
||||
Jedis jedis = getRedis();
|
||||
if (timeout == -1) {
|
||||
jedis.set(key, value);
|
||||
} else {
|
||||
jedis.setex(key, timeout, value);
|
||||
}
|
||||
jedis.close();
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static String get(String key,int dbIndex){
|
||||
|
||||
public static String get(String key, int dbIndex) {
|
||||
String value = "-999";
|
||||
Jedis jedis=null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getRedis();
|
||||
jedis.select(dbIndex);
|
||||
@ -69,50 +80,57 @@ public class RedisTools {
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
// e.printStackTrace();
|
||||
}finally {
|
||||
if(jedis!=null)
|
||||
} finally {
|
||||
if (jedis != null)
|
||||
jedis.close();
|
||||
}
|
||||
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) {
|
||||
Jedis jedis=getRedis();
|
||||
Long i=jedis.del(key);
|
||||
Jedis jedis = getRedis();
|
||||
Long i = jedis.del(key);
|
||||
jedis.close();
|
||||
if(i>0) {
|
||||
if (i > 0) {
|
||||
return true;
|
||||
}else {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeLoginState(String uid) {
|
||||
Jedis jedis=getRedis();
|
||||
Set<String> keys=jedis.keys("*");
|
||||
Jedis jedis = getRedis();
|
||||
Set<String> keys = jedis.keys("*");
|
||||
for (String string : keys) {
|
||||
if(string.equals(uid)) {
|
||||
if (string.equals(uid)) {
|
||||
jedis.del(string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String ping() {
|
||||
Jedis jedis=getRedis();
|
||||
String tmp=jedis.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));
|
||||
|
||||
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);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user