From 3c75aedcecb298d2311faf3246867f1963daf4bf Mon Sep 17 00:00:00 2001 From: lijiahangmax Date: Sat, 23 Aug 2025 15:05:03 +0800 Subject: [PATCH] =?UTF-8?q?:hammer:=20=E4=BC=98=E5=8C=96=E9=94=81=E9=80=BB?= =?UTF-8?q?=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{interfaces => cipher}/AesEncryptor.java | 2 +- .../{interfaces => cipher}/RsaDecryptor.java | 2 +- .../{interfaces => file}/FileClient.java | 2 +- .../visor/common/interfaces/Locker.java | 55 ------- .../visor/common/lock/EmptyLocker.java | 80 ++++++++++ .../org/dromara/visor/common/lock/Locker.java | 116 ++++++++++++++ .../visor/common/utils/AesEncryptUtils.java | 2 +- .../visor/common/utils/LockerUtils.java | 96 ++++++++++-- .../common/utils/RsaParamDecryptUtils.java | 2 +- .../OrionEncryptAutoConfiguration.java | 4 +- .../encrypt/core/BaseAesEncryptor.java | 2 +- .../encrypt/core/impl/RsaDecryptorImpl.java | 2 +- .../OrionMockRedisAutoConfiguration.java | 17 +- .../OrionRedisAutoConfiguration.java | 2 +- .../redis/core/lock/RedisLocker.java | 145 ++++++++++++++++-- .../OrionStorageAutoConfiguration.java | 2 +- .../core/client/AbstractFileClient.java | 2 +- .../OrionMockRedisTestConfiguration.java | 17 +- .../handler/BaseExecCommandHandler.java | 2 +- .../exec/log/tracker/ExecLogTracker.java | 2 +- .../handler/upload/uploader/FileUploader.java | 2 +- .../exec/service/impl/ExecLogServiceImpl.java | 2 +- .../service/impl/UploadTaskServiceImpl.java | 2 +- .../exec/task/ExecLogFileAutoClearTask.java | 2 +- .../upload/FileUploadMessageDispatcher.java | 2 +- .../upload/handler/FileUploadHandler.java | 2 +- .../module/infra/task/TagAutoClearTask.java | 2 +- .../CommandSnippetGroupAutoClearTask.java | 2 +- .../task/PathBookmarkGroupAutoClearTask.java | 2 +- .../task/TerminalConnectLogAutoClearTask.java | 2 +- 30 files changed, 443 insertions(+), 131 deletions(-) rename orion-visor-common/src/main/java/org/dromara/visor/common/{interfaces => cipher}/AesEncryptor.java (97%) rename orion-visor-common/src/main/java/org/dromara/visor/common/{interfaces => cipher}/RsaDecryptor.java (95%) rename orion-visor-common/src/main/java/org/dromara/visor/common/{interfaces => file}/FileClient.java (98%) delete mode 100644 orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/Locker.java create mode 100644 orion-visor-common/src/main/java/org/dromara/visor/common/lock/EmptyLocker.java create mode 100644 orion-visor-common/src/main/java/org/dromara/visor/common/lock/Locker.java diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/AesEncryptor.java b/orion-visor-common/src/main/java/org/dromara/visor/common/cipher/AesEncryptor.java similarity index 97% rename from orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/AesEncryptor.java rename to orion-visor-common/src/main/java/org/dromara/visor/common/cipher/AesEncryptor.java index bf3f1360..326908f1 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/AesEncryptor.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/cipher/AesEncryptor.java @@ -20,7 +20,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dromara.visor.common.interfaces; +package org.dromara.visor.common.cipher; import cn.orionsec.kit.lang.utils.codec.Base62s; import cn.orionsec.kit.lang.utils.crypto.symmetric.SymmetricCrypto; diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/RsaDecryptor.java b/orion-visor-common/src/main/java/org/dromara/visor/common/cipher/RsaDecryptor.java similarity index 95% rename from orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/RsaDecryptor.java rename to orion-visor-common/src/main/java/org/dromara/visor/common/cipher/RsaDecryptor.java index a0bec1ae..d64e4e0c 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/RsaDecryptor.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/cipher/RsaDecryptor.java @@ -20,7 +20,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dromara.visor.common.interfaces; +package org.dromara.visor.common.cipher; /** * rsa 解密器 diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/FileClient.java b/orion-visor-common/src/main/java/org/dromara/visor/common/file/FileClient.java similarity index 98% rename from orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/FileClient.java rename to orion-visor-common/src/main/java/org/dromara/visor/common/file/FileClient.java index d6f62d4a..e9ea011b 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/FileClient.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/file/FileClient.java @@ -20,7 +20,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dromara.visor.common.interfaces; +package org.dromara.visor.common.file; import java.io.InputStream; import java.io.OutputStream; diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/Locker.java b/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/Locker.java deleted file mode 100644 index d08705ea..00000000 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/interfaces/Locker.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023 - present Dromara, All rights reserved. - * - * https://visor.dromara.org - * https://visor.dromara.org.cn - * https://visor.orionsec.cn - * - * Members: - * Jiahang Li - ljh1553488six@139.com - author - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.dromara.visor.common.interfaces; - -import java.util.function.Supplier; - -/** - * 分布式锁 - * - * @author Jiahang Li - * @version 1.0.0 - * @since 2024/5/16 12:24 - */ -public interface Locker { - - /** - * 尝试获取锁 - * - * @param key key - * @param run run - * @return 是否获取到锁 - */ - boolean tryLock(String key, Runnable run); - - /** - * 尝试获取锁 - * - * @param key key - * @param call call - * @param T - * @return 执行结果 - */ - T tryLock(String key, Supplier call); - -} diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/lock/EmptyLocker.java b/orion-visor-common/src/main/java/org/dromara/visor/common/lock/EmptyLocker.java new file mode 100644 index 00000000..b2e16b7c --- /dev/null +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/lock/EmptyLocker.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.common.lock; + +import cn.orionsec.kit.lang.able.Executable; + +import java.util.function.Supplier; + +/** + * 空实现的锁 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/8/23 13:59 + */ +public class EmptyLocker implements Locker { + + @Override + public boolean tryLockExecute(String key, Executable executable) { + executable.exec(); + return true; + } + + @Override + public boolean tryLockExecute(String key, long timeout, Executable executable) { + executable.exec(); + return true; + } + + @Override + public T tryLockExecute(String key, Supplier callable) { + return callable.get(); + } + + @Override + public T tryLockExecute(String key, long timeout, Supplier callable) { + return callable.get(); + } + + @Override + public void lockExecute(String key, Executable executable) { + executable.exec(); + } + + @Override + public void lockExecute(String key, long timeout, Executable executable) { + executable.exec(); + } + + @Override + public T lockExecute(String key, Supplier callable) { + return callable.get(); + } + + @Override + public T lockExecute(String key, long timeout, Supplier callable) { + return callable.get(); + } + +} diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/lock/Locker.java b/orion-visor-common/src/main/java/org/dromara/visor/common/lock/Locker.java new file mode 100644 index 00000000..12e87471 --- /dev/null +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/lock/Locker.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.common.lock; + +import cn.orionsec.kit.lang.able.Executable; + +import java.util.function.Supplier; + +/** + * 分布式锁 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/5/16 12:24 + */ +public interface Locker { + + /** + * 尝试获取锁并执行 + * + * @param key key + * @param executable exec + * @return 是否获取到锁 + */ + boolean tryLockExecute(String key, Executable executable); + + /** + * 尝试获取锁并执行 + * + * @param key key + * @param timeout timeout + * @param executable exec + * @return 是否获取到锁 + */ + boolean tryLockExecute(String key, long timeout, Executable executable); + + /** + * 尝试获取锁并执行 未获取到锁则抛出异常 + * + * @param key key + * @param callable callable + * @param T + * @return 执行结果 + */ + T tryLockExecute(String key, Supplier callable); + + /** + * 尝试获取锁并执行 未获取到锁则抛出异常 + * + * @param key key + * @param timeout timeout + * @param callable callable + * @param T + * @return 执行结果 + */ + T tryLockExecute(String key, long timeout, Supplier callable); + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param executable exec + */ + void lockExecute(String key, Executable executable); + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param timeout timeout + * @param executable exec + */ + void lockExecute(String key, long timeout, Executable executable); + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param callable callable + * @param T + * @return 执行结果 + */ + T lockExecute(String key, Supplier callable); + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param timeout timeout + * @param callable callable + * @param T + * @return 执行结果 + */ + T lockExecute(String key, long timeout, Supplier callable); + +} diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/AesEncryptUtils.java b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/AesEncryptUtils.java index cf8cfaf1..56446750 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/AesEncryptUtils.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/AesEncryptUtils.java @@ -23,7 +23,7 @@ package org.dromara.visor.common.utils; import cn.orionsec.kit.lang.utils.Exceptions; -import org.dromara.visor.common.interfaces.AesEncryptor; +import org.dromara.visor.common.cipher.AesEncryptor; /** * aes 数据加密工具类 diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/LockerUtils.java b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/LockerUtils.java index 01ce1219..e72ba3f2 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/LockerUtils.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/LockerUtils.java @@ -22,9 +22,10 @@ */ package org.dromara.visor.common.utils; +import cn.orionsec.kit.lang.able.Executable; import cn.orionsec.kit.lang.utils.Exceptions; import lombok.extern.slf4j.Slf4j; -import org.dromara.visor.common.interfaces.Locker; +import org.dromara.visor.common.lock.Locker; import java.util.function.Supplier; @@ -44,26 +45,97 @@ public class LockerUtils { } /** - * 尝试获取锁 + * 尝试获取锁并执行 * - * @param key key - * @param run run + * @param key key + * @param executable exec * @return 是否获取到锁 */ - public static boolean tryLock(String key, Runnable run) { - return delegate.tryLock(key, run); + public static boolean tryLockExecute(String key, Executable executable) { + return delegate.tryLockExecute(key, executable); } /** - * 尝试获取锁 + * 尝试获取锁并执行 * - * @param key key - * @param call call - * @param T + * @param key key + * @param timeout timeout + * @param executable exec + * @return 是否获取到锁 + */ + public static boolean tryLockExecute(String key, long timeout, Executable executable) { + return delegate.tryLockExecute(key, timeout, executable); + } + + /** + * 尝试获取锁并执行 未获取到锁则抛出异常 + * + * @param key key + * @param callable callable + * @param T * @return 执行结果 */ - public static T tryLock(String key, Supplier call) { - return delegate.tryLock(key, call); + public static T tryLockExecute(String key, Supplier callable) { + return delegate.tryLockExecute(key, callable); + } + + /** + * 尝试获取锁并执行 未获取到锁则抛出异常 + * + * @param key key + * @param timeout timeout + * @param callable callable + * @param T + * @return 执行结果 + */ + public static T tryLockExecute(String key, long timeout, Supplier callable) { + return delegate.tryLockExecute(key, timeout, callable); + } + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param executable exec + */ + public static void lockExecute(String key, Executable executable) { + delegate.lockExecute(key, executable); + } + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param timeout timeout + * @param executable exec + */ + public static void lockExecute(String key, long timeout, Executable executable) { + delegate.lockExecute(key, timeout, executable); + } + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param callable callable + * @param T + * @return 执行结果 + */ + public static T lockExecute(String key, Supplier callable) { + return delegate.lockExecute(key, callable); + } + + /** + * 阻塞获取锁并执行 + * + * @param key key + * @param timeout timeout + * @param callable callable + * @param T + * @return 执行结果 + */ + public static T lockExecute(String key, long timeout, Supplier callable) { + return delegate.lockExecute(key, timeout, callable); } public static void setDelegate(Locker delegate) { diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/RsaParamDecryptUtils.java b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/RsaParamDecryptUtils.java index 56b24887..f7d14ff6 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/utils/RsaParamDecryptUtils.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/utils/RsaParamDecryptUtils.java @@ -23,7 +23,7 @@ package org.dromara.visor.common.utils; import cn.orionsec.kit.lang.utils.Exceptions; -import org.dromara.visor.common.interfaces.RsaDecryptor; +import org.dromara.visor.common.cipher.RsaDecryptor; /** * rsa 参数解密工具类 diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/configuration/OrionEncryptAutoConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/configuration/OrionEncryptAutoConfiguration.java index f6e942be..36110dfa 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/configuration/OrionEncryptAutoConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/configuration/OrionEncryptAutoConfiguration.java @@ -24,8 +24,8 @@ package org.dromara.visor.framework.encrypt.configuration; import org.dromara.visor.common.config.ConfigStore; import org.dromara.visor.common.constant.AutoConfigureOrderConst; -import org.dromara.visor.common.interfaces.AesEncryptor; -import org.dromara.visor.common.interfaces.RsaDecryptor; +import org.dromara.visor.common.cipher.AesEncryptor; +import org.dromara.visor.common.cipher.RsaDecryptor; import org.dromara.visor.common.utils.AesEncryptUtils; import org.dromara.visor.common.utils.RsaParamDecryptUtils; import org.dromara.visor.framework.encrypt.configuration.config.AesEncryptConfig; diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/BaseAesEncryptor.java b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/BaseAesEncryptor.java index 27e0a390..4bd331a8 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/BaseAesEncryptor.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/BaseAesEncryptor.java @@ -22,7 +22,7 @@ */ package org.dromara.visor.framework.encrypt.core; -import org.dromara.visor.common.interfaces.AesEncryptor; +import org.dromara.visor.common.cipher.AesEncryptor; /** * 数据加密器 diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaDecryptorImpl.java b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaDecryptorImpl.java index 1b482252..7c9cfff3 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaDecryptorImpl.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-cipher/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaDecryptorImpl.java @@ -26,7 +26,7 @@ import cn.orionsec.kit.lang.utils.crypto.RSA; import org.dromara.visor.common.config.ConfigRef; import org.dromara.visor.common.config.ConfigStore; import org.dromara.visor.common.constant.ConfigKeys; -import org.dromara.visor.common.interfaces.RsaDecryptor; +import org.dromara.visor.common.cipher.RsaDecryptor; import java.security.interfaces.RSAPrivateKey; import java.util.Arrays; diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionMockRedisAutoConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionMockRedisAutoConfiguration.java index be3188ec..559ac1c6 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionMockRedisAutoConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionMockRedisAutoConfiguration.java @@ -24,7 +24,8 @@ package org.dromara.visor.framework.redis.configuration; import com.github.fppt.jedismock.RedisServer; import org.dromara.visor.common.constant.AutoConfigureOrderConst; -import org.dromara.visor.common.interfaces.Locker; +import org.dromara.visor.common.lock.EmptyLocker; +import org.dromara.visor.common.lock.Locker; import org.dromara.visor.common.utils.LockerUtils; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; @@ -35,7 +36,6 @@ import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import java.net.InetAddress; -import java.util.function.Supplier; /** * MockRedis @@ -79,18 +79,7 @@ public class OrionMockRedisAutoConfiguration { */ @Bean public Locker redisLocker() { - Locker locker = new Locker() { - @Override - public boolean tryLock(String key, Runnable run) { - run.run(); - return true; - } - - @Override - public T tryLock(String key, Supplier call) { - return call.get(); - } - }; + EmptyLocker locker = new EmptyLocker(); LockerUtils.setDelegate(locker); return locker; } diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionRedisAutoConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionRedisAutoConfiguration.java index 55ca9b83..49016f60 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionRedisAutoConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/configuration/OrionRedisAutoConfiguration.java @@ -24,7 +24,7 @@ package org.dromara.visor.framework.redis.configuration; import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine; import org.dromara.visor.common.constant.AutoConfigureOrderConst; -import org.dromara.visor.common.interfaces.Locker; +import org.dromara.visor.common.lock.Locker; import org.dromara.visor.common.utils.LockerUtils; import org.dromara.visor.framework.redis.configuration.config.RedissonConfig; import org.dromara.visor.framework.redis.core.lock.RedisLocker; diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/core/lock/RedisLocker.java b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/core/lock/RedisLocker.java index 03d83c75..876fc88f 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/core/lock/RedisLocker.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-redis/src/main/java/org/dromara/visor/framework/redis/core/lock/RedisLocker.java @@ -22,12 +22,14 @@ */ package org.dromara.visor.framework.redis.core.lock; +import cn.orionsec.kit.lang.able.Executable; import cn.orionsec.kit.lang.utils.Exceptions; import lombok.extern.slf4j.Slf4j; -import org.dromara.visor.common.interfaces.Locker; +import org.dromara.visor.common.lock.Locker; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; /** @@ -40,6 +42,8 @@ import java.util.function.Supplier; @Slf4j public class RedisLocker implements Locker { + private static final String LOCK_KEY_PREFIX = "lock:"; + private final RedissonClient redissonClient; public RedisLocker(RedissonClient redissonClient) { @@ -47,37 +51,154 @@ public class RedisLocker implements Locker { } @Override - public boolean tryLock(String key, Runnable run) { + public boolean tryLockExecute(String key, Executable executable) { + return this.tryLockExecute(key, 0, executable); + } + + @Override + public boolean tryLockExecute(String key, long timeout, Executable executable) { // 获取锁 - RLock lock = redissonClient.getLock(key); + RLock lock = this.getLock(key); // 未获取到直接返回 - if (!lock.tryLock()) { - log.info("RedisLocker.tryLock failed {}", key); + if (this.tryLock(lock, timeout)) { return false; } // 执行 try { - run.run(); + executable.exec(); } finally { - lock.unlock(); + this.unlockSafe(lock); } return true; } @Override - public T tryLock(String key, Supplier call) { + public T tryLockExecute(String key, Supplier callable) { + return this.tryLockExecute(key, 0, callable); + } + + @Override + public T tryLockExecute(String key, long timeout, Supplier callable) { // 获取锁 - RLock lock = redissonClient.getLock(key); + RLock lock = this.getLock(key); // 未获取到直接返回 - if (!lock.tryLock()) { - log.info("RedisLocker.tryLock failed {}", key); + if (this.tryLock(lock, timeout)) { throw Exceptions.lock(); } // 执行 try { - return call.get(); + return callable.get(); } finally { + this.unlockSafe(lock); + } + } + + @Override + public void lockExecute(String key, Executable executable) { + this.lockExecute(key, 0, executable); + } + + @Override + public void lockExecute(String key, long timeout, Executable executable) { + // 获取锁 + RLock lock = this.getLock(key); + this.lock(lock, timeout); + // 执行 + try { + executable.exec(); + } finally { + this.unlockSafe(lock); + } + } + + @Override + public T lockExecute(String key, Supplier callable) { + return this.lockExecute(key, 0, callable); + } + + @Override + public T lockExecute(String key, long timeout, Supplier callable) { + // 获取锁 + RLock lock = this.getLock(key); + this.lock(lock, timeout); + // 执行 + try { + return callable.get(); + } finally { + this.unlockSafe(lock); + } + } + + /** + * 获取锁 + * + * @param key key + * @return lock + */ + private RLock getLock(String key) { + return redissonClient.getLock(LOCK_KEY_PREFIX + key); + } + + /** + * 尝试上锁 + * + * @param lock lock + * @param timeout timeout + * @return locked + */ + private boolean tryLock(RLock lock, long timeout) { + boolean result; + try { + if (timeout == 0) { + result = lock.tryLock(); + } else { + result = lock.tryLock(timeout, TimeUnit.MILLISECONDS); + } + if (!result) { + log.warn("RedisLocker.tryLock failed {}", lock.getName()); + } + } catch (InterruptedException e) { + log.error("RedisLocker.tryLock timed out {}", lock.getName(), e); + throw Exceptions.lock(e); + } catch (Exception e) { + log.error("RedisLocker.tryLock error {}", lock.getName(), e); + throw Exceptions.lock(e); + } + return result; + } + + /** + * 上锁 + * + * @param lock lock + * @param timeout timeout + */ + private void lock(RLock lock, long timeout) { + try { + if (timeout == 0) { + lock.lock(); + } else { + lock.lock(timeout, TimeUnit.MILLISECONDS); + } + } catch (Exception e) { + log.error("RedisLocker.lock lock error {}", lock.getName(), e); + throw Exceptions.lock(e); + } + } + + /** + * 安全的释放锁 + * + * @param lock lock + */ + private void unlockSafe(RLock lock) { + if (!lock.isHeldByCurrentThread()) { + return; + } + try { lock.unlock(); + } catch (Exception e) { + log.warn("RedisLocker.unlock failed {}", lock.getName(), e); } } diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/configuration/OrionStorageAutoConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/configuration/OrionStorageAutoConfiguration.java index 195ab9fb..9ef12898 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/configuration/OrionStorageAutoConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/configuration/OrionStorageAutoConfiguration.java @@ -23,7 +23,7 @@ package org.dromara.visor.framework.storage.configuration; import org.dromara.visor.common.constant.AutoConfigureOrderConst; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.framework.storage.configuration.config.LocalStorageConfig; import org.dromara.visor.framework.storage.configuration.config.LogsStorageConfig; import org.dromara.visor.framework.storage.core.client.local.LocalFileClient; diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/core/client/AbstractFileClient.java b/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/core/client/AbstractFileClient.java index 05f7a896..506701ec 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/core/client/AbstractFileClient.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-storage/src/main/java/org/dromara/visor/framework/storage/core/client/AbstractFileClient.java @@ -26,7 +26,7 @@ import cn.orionsec.kit.lang.utils.io.Files1; import cn.orionsec.kit.lang.utils.io.Streams; import cn.orionsec.kit.lang.utils.time.Dates; import org.dromara.visor.common.constant.Const; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import java.io.InputStream; import java.io.OutputStream; diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-test/src/main/java/org/dromara/visor/framework/test/configuration/OrionMockRedisTestConfiguration.java b/orion-visor-framework/orion-visor-spring-boot-starter-test/src/main/java/org/dromara/visor/framework/test/configuration/OrionMockRedisTestConfiguration.java index 7ffe8d85..b94e4231 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-test/src/main/java/org/dromara/visor/framework/test/configuration/OrionMockRedisTestConfiguration.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-test/src/main/java/org/dromara/visor/framework/test/configuration/OrionMockRedisTestConfiguration.java @@ -23,7 +23,8 @@ package org.dromara.visor.framework.test.configuration; import com.github.fppt.jedismock.RedisServer; -import org.dromara.visor.common.interfaces.Locker; +import org.dromara.visor.common.lock.EmptyLocker; +import org.dromara.visor.common.lock.Locker; import org.dromara.visor.common.utils.LockerUtils; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -33,7 +34,6 @@ import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Profile; import java.net.InetAddress; -import java.util.function.Supplier; /** * 单元测试 redis mock server 初始化 @@ -66,18 +66,7 @@ public class OrionMockRedisTestConfiguration { */ @Bean public Locker unitTestLocker() { - Locker locker = new Locker() { - @Override - public boolean tryLock(String key, Runnable run) { - run.run(); - return true; - } - - @Override - public T tryLock(String key, Supplier call) { - return call.get(); - } - }; + EmptyLocker locker = new EmptyLocker(); LockerUtils.setDelegate(locker); return locker; } diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/command/handler/BaseExecCommandHandler.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/command/handler/BaseExecCommandHandler.java index f40411cc..93d11313 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/command/handler/BaseExecCommandHandler.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/command/handler/BaseExecCommandHandler.java @@ -44,7 +44,7 @@ import org.dromara.visor.common.constant.ErrorMessage; import org.dromara.visor.common.constant.FileConst; import org.dromara.visor.common.enums.BooleanBit; import org.dromara.visor.common.enums.EndpointDefine; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.common.session.config.SshConnectConfig; import org.dromara.visor.common.session.ssh.SessionStores; import org.dromara.visor.common.utils.PathUtils; diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/log/tracker/ExecLogTracker.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/log/tracker/ExecLogTracker.java index 483c8360..a261a12b 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/log/tracker/ExecLogTracker.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/exec/log/tracker/ExecLogTracker.java @@ -38,7 +38,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.dromara.visor.common.constant.Const; import org.dromara.visor.common.constant.ErrorMessage; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.common.utils.Valid; import org.dromara.visor.framework.websocket.core.utils.WebSockets; import org.dromara.visor.module.common.config.AppLogConfig; diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/upload/uploader/FileUploader.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/upload/uploader/FileUploader.java index c18b7080..49cb140c 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/upload/uploader/FileUploader.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/handler/upload/uploader/FileUploader.java @@ -35,7 +35,7 @@ import org.dromara.visor.common.constant.Const; import org.dromara.visor.common.constant.ErrorMessage; import org.dromara.visor.common.constant.ExtraFieldConst; import org.dromara.visor.common.enums.EndpointDefine; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.common.session.config.SshConnectConfig; import org.dromara.visor.common.session.ssh.SessionStores; import org.dromara.visor.common.utils.PathUtils; diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/ExecLogServiceImpl.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/ExecLogServiceImpl.java index 6c7483ff..e872a4f4 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/ExecLogServiceImpl.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/ExecLogServiceImpl.java @@ -39,7 +39,7 @@ import org.dromara.visor.common.constant.Const; import org.dromara.visor.common.constant.ErrorMessage; import org.dromara.visor.common.constant.FileConst; import org.dromara.visor.common.enums.EndpointDefine; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.common.utils.SqlUtils; import org.dromara.visor.common.utils.Valid; import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs; diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/UploadTaskServiceImpl.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/UploadTaskServiceImpl.java index 26e1531a..1ef5955f 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/UploadTaskServiceImpl.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/service/impl/UploadTaskServiceImpl.java @@ -35,7 +35,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.visor.common.constant.Const; import org.dromara.visor.common.constant.ErrorMessage; import org.dromara.visor.common.enums.EndpointDefine; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.common.security.LoginUser; import org.dromara.visor.common.utils.SqlUtils; import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs; diff --git a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/task/ExecLogFileAutoClearTask.java b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/task/ExecLogFileAutoClearTask.java index 8e9e9c83..5f243940 100644 --- a/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/task/ExecLogFileAutoClearTask.java +++ b/orion-visor-modules/orion-visor-module-exec/orion-visor-module-exec-service/src/main/java/org/dromara/visor/module/exec/task/ExecLogFileAutoClearTask.java @@ -70,7 +70,7 @@ public class ExecLogFileAutoClearTask { return; } // 获取锁并执行 - LockerUtils.tryLock(LOCK_KEY, this::doClear); + LockerUtils.tryLockExecute(LOCK_KEY, this::doClear); log.info("ExecLogFileAutoClearTask.clear finish"); } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/FileUploadMessageDispatcher.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/FileUploadMessageDispatcher.java index b901508c..ccb09581 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/FileUploadMessageDispatcher.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/FileUploadMessageDispatcher.java @@ -27,7 +27,7 @@ import cn.orionsec.kit.lang.utils.io.Streams; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.dromara.visor.common.constant.ExtraFieldConst; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.framework.websocket.core.utils.WebSockets; import org.dromara.visor.module.infra.entity.dto.FileUploadTokenDTO; import org.dromara.visor.module.infra.handler.upload.enums.FileUploadOperatorType; diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/handler/FileUploadHandler.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/handler/FileUploadHandler.java index 92fb91dc..7d9182b9 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/handler/FileUploadHandler.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/upload/handler/FileUploadHandler.java @@ -25,7 +25,7 @@ package org.dromara.visor.module.infra.handler.upload.handler; import cn.orionsec.kit.lang.utils.io.Streams; import com.alibaba.fastjson.JSON; import org.dromara.visor.common.constant.Const; -import org.dromara.visor.common.interfaces.FileClient; +import org.dromara.visor.common.file.FileClient; import org.dromara.visor.framework.websocket.core.utils.WebSockets; import org.dromara.visor.module.infra.handler.upload.enums.FileUploadReceiverType; import org.dromara.visor.module.infra.handler.upload.model.FileUploadResponse; diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/task/TagAutoClearTask.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/task/TagAutoClearTask.java index 3c617637..a82ac8c3 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/task/TagAutoClearTask.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/task/TagAutoClearTask.java @@ -56,7 +56,7 @@ public class TagAutoClearTask { public void clear() { log.info("TagAutoClearTask.clear start"); // 获取锁并执行 - LockerUtils.tryLock(LOCK_KEY, tagService::clearUnusedTag); + LockerUtils.tryLockExecute(LOCK_KEY, tagService::clearUnusedTag); log.info("TagAutoClearTask.clear finish"); } diff --git a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/CommandSnippetGroupAutoClearTask.java b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/CommandSnippetGroupAutoClearTask.java index a0d77072..a75fdba3 100644 --- a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/CommandSnippetGroupAutoClearTask.java +++ b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/CommandSnippetGroupAutoClearTask.java @@ -56,7 +56,7 @@ public class CommandSnippetGroupAutoClearTask { public void clear() { log.info("CommandSnippetGroupAutoClearTask.clear start"); // 获取锁并执行 - LockerUtils.tryLock(LOCK_KEY, commandSnippetGroupService::clearUnusedGroup); + LockerUtils.tryLockExecute(LOCK_KEY, commandSnippetGroupService::clearUnusedGroup); log.info("CommandSnippetGroupAutoClearTask.clear finish"); } diff --git a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/PathBookmarkGroupAutoClearTask.java b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/PathBookmarkGroupAutoClearTask.java index f1af9525..4e763d21 100644 --- a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/PathBookmarkGroupAutoClearTask.java +++ b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/PathBookmarkGroupAutoClearTask.java @@ -56,7 +56,7 @@ public class PathBookmarkGroupAutoClearTask { public void clear() { log.info("PathBookmarkGroupAutoClearTask.clear start"); // 获取锁并执行 - LockerUtils.tryLock(LOCK_KEY, pathBookmarkGroupService::clearUnusedGroup); + LockerUtils.tryLockExecute(LOCK_KEY, pathBookmarkGroupService::clearUnusedGroup); log.info("PathBookmarkGroupAutoClearTask.clear finish"); } diff --git a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/TerminalConnectLogAutoClearTask.java b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/TerminalConnectLogAutoClearTask.java index 806f7820..73ba13dd 100644 --- a/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/TerminalConnectLogAutoClearTask.java +++ b/orion-visor-modules/orion-visor-module-terminal/orion-visor-module-terminal-service/src/main/java/org/dromara/visor/module/terminal/task/TerminalConnectLogAutoClearTask.java @@ -70,7 +70,7 @@ public class TerminalConnectLogAutoClearTask { return; } // 获取锁并执行 - LockerUtils.tryLock(LOCK_KEY, this::doClear); + LockerUtils.tryLockExecute(LOCK_KEY, this::doClear); log.info("TerminalConnectLogAutoClearTask.clear finish"); }