[*] script

This commit is contained in:
acgist
2022-11-20 09:13:32 +08:00
parent a08449cb8b
commit f6c6674113
5 changed files with 42 additions and 19 deletions

14
pom.xml
View File

@@ -73,10 +73,6 @@
<artifactId>commons-collections4</artifactId> <artifactId>commons-collections4</artifactId>
</dependency> </dependency>
<!-- Jackson --> <!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
@@ -111,6 +107,11 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>com.acgist</groupId>
<artifactId>taoyao-test</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.acgist</groupId> <groupId>com.acgist</groupId>
<artifactId>taoyao-boot</artifactId> <artifactId>taoyao-boot</artifactId>
@@ -121,11 +122,6 @@
<artifactId>taoyao-live</artifactId> <artifactId>taoyao-live</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.acgist</groupId>
<artifactId>taoyao-test</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.acgist</groupId> <groupId>com.acgist</groupId>
<artifactId>taoyao-media</artifactId> <artifactId>taoyao-media</artifactId>

View File

@@ -22,6 +22,11 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -29,6 +29,9 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public final class ErrorUtils { public final class ErrorUtils {
private ErrorUtils() {
}
/** /**
* 异常映射 * 异常映射
*/ */
@@ -54,9 +57,6 @@ public final class ErrorUtils {
* SpringBoot异常 * SpringBoot异常
*/ */
public static final String EXCEPTION_SPRINGBOOT = "org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR"; public static final String EXCEPTION_SPRINGBOOT = "org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR";
private ErrorUtils() {
}
/** /**
* 注册异常(注意继承顺序) * 注册异常(注意继承顺序)

View File

@@ -43,14 +43,20 @@
### 执行命令信令1001 ### 执行命令信令1001
终端->服务端:执行系统命令 #### 消息主体
``` ```
{ {
"script": "命令" "script": "命令"
} }
----
{
"result": "结果"
}
``` ```
#### 消息流程:终端->服务端->终端
### 异常信令1999 ### 异常信令1999
服务端->终端:提示异常信息 服务端->终端:提示异常信息

View File

@@ -1,11 +1,14 @@
package com.acgist.taoyao.signal.listener.platform; package com.acgist.taoyao.signal.listener.platform;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.acgist.taoyao.boot.model.Message;
import com.acgist.taoyao.signal.client.ClientSession;
import com.acgist.taoyao.signal.event.platform.ScriptEvent; import com.acgist.taoyao.signal.event.platform.ScriptEvent;
import com.acgist.taoyao.signal.listener.ApplicationListenerAdapter; import com.acgist.taoyao.signal.listener.ApplicationListenerAdapter;
@@ -23,21 +26,29 @@ public class ScriptListener extends ApplicationListenerAdapter<ScriptEvent> {
@Async @Async
@Override @Override
public void onApplicationEvent(ScriptEvent event) { public void onApplicationEvent(ScriptEvent event) {
final String script = (String) event.getBody().get("script"); final Message message = event.getMessage();
final ClientSession session = event.getSession();
final Map<?, ?> body = event.getBody();
final String script = (String) body.get("script");
log.debug("执行命令:{}", script); log.debug("执行命令:{}", script);
this.execute(script); final String result = this.execute(script);
message.setBody(Map.of("result", result));
session.push(message);
} }
/** /**
* 执行命令 * 执行命令
* *
* @param script 命令 * @param script 命令
*
* @return 执行结果
*/ */
private void execute(String script) { private String execute(String script) {
if(StringUtils.isEmpty(script)) { if(StringUtils.isEmpty(script)) {
log.warn("执行命令失败:{}", script); log.warn("执行命令失败:{}", script);
return; return "命令为空";
} }
String result = null;
Process process = null; Process process = null;
try { try {
process = Runtime.getRuntime().exec(script); process = Runtime.getRuntime().exec(script);
@@ -45,22 +56,27 @@ public class ScriptListener extends ApplicationListenerAdapter<ScriptEvent> {
final InputStream input = process.getInputStream(); final InputStream input = process.getInputStream();
final InputStream error = process.getErrorStream(); final InputStream error = process.getErrorStream();
) { ) {
final String inputValue = new String(input.readAllBytes());
final String errorValue = new String(input.readAllBytes());
log.info(""" log.info("""
执行命令:{} 执行命令:{}
执行结果:{} 执行结果:{}
失败结果:{} 失败结果:{}
""", script, new String(input.readAllBytes()), new String(error.readAllBytes())); """, script, inputValue, errorValue);
result = StringUtils.isEmpty(inputValue) ? errorValue : inputValue;
} catch (Exception e) { } catch (Exception e) {
log.error("命令执行异常:{}", script, e); log.error("命令执行异常:{}", script, e);
result = e.getMessage();
} }
} catch (Exception e) { } catch (Exception e) {
log.error("执行命令异常:{}", script, e); log.error("执行命令异常:{}", script, e);
result = e.getMessage();
} finally { } finally {
if(process != null) { if(process != null) {
process.destroy(); process.destroy();
// process.destroyForcibly();
} }
} }
return result;
} }
} }