[*] script
This commit is contained in:
14
pom.xml
14
pom.xml
@@ -73,10 +73,6 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
</dependency>
|
||||
<!-- Jackson -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
@@ -111,6 +107,11 @@
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.acgist</groupId>
|
||||
<artifactId>taoyao-test</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.acgist</groupId>
|
||||
<artifactId>taoyao-boot</artifactId>
|
||||
@@ -121,11 +122,6 @@
|
||||
<artifactId>taoyao-live</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.acgist</groupId>
|
||||
<artifactId>taoyao-test</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.acgist</groupId>
|
||||
<artifactId>taoyao-media</artifactId>
|
||||
|
||||
@@ -22,6 +22,11 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -29,6 +29,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public final class ErrorUtils {
|
||||
|
||||
private ErrorUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常映射
|
||||
*/
|
||||
@@ -55,9 +58,6 @@ public final class ErrorUtils {
|
||||
*/
|
||||
public static final String EXCEPTION_SPRINGBOOT = "org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR";
|
||||
|
||||
private ErrorUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册异常(注意继承顺序)
|
||||
*
|
||||
|
||||
@@ -43,14 +43,20 @@
|
||||
|
||||
### 执行命令信令(1001)
|
||||
|
||||
终端->服务端:执行系统命令
|
||||
#### 消息主体
|
||||
|
||||
```
|
||||
{
|
||||
"script": "命令"
|
||||
}
|
||||
----
|
||||
{
|
||||
"result": "结果"
|
||||
}
|
||||
```
|
||||
|
||||
#### 消息流程:终端->服务端->终端
|
||||
|
||||
### 异常信令(1999)
|
||||
|
||||
服务端->终端:提示异常信息
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.acgist.taoyao.signal.listener.platform;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
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.listener.ApplicationListenerAdapter;
|
||||
|
||||
@@ -23,21 +26,29 @@ public class ScriptListener extends ApplicationListenerAdapter<ScriptEvent> {
|
||||
@Async
|
||||
@Override
|
||||
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);
|
||||
this.execute(script);
|
||||
final String result = this.execute(script);
|
||||
message.setBody(Map.of("result", result));
|
||||
session.push(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行命令
|
||||
*
|
||||
* @param script 命令
|
||||
*
|
||||
* @return 执行结果
|
||||
*/
|
||||
private void execute(String script) {
|
||||
private String execute(String script) {
|
||||
if(StringUtils.isEmpty(script)) {
|
||||
log.warn("执行命令失败:{}", script);
|
||||
return;
|
||||
return "命令为空";
|
||||
}
|
||||
String result = null;
|
||||
Process process = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(script);
|
||||
@@ -45,22 +56,27 @@ public class ScriptListener extends ApplicationListenerAdapter<ScriptEvent> {
|
||||
final InputStream input = process.getInputStream();
|
||||
final InputStream error = process.getErrorStream();
|
||||
) {
|
||||
final String inputValue = new String(input.readAllBytes());
|
||||
final String errorValue = new String(input.readAllBytes());
|
||||
log.info("""
|
||||
执行命令:{}
|
||||
执行结果:{}
|
||||
失败结果:{}
|
||||
""", script, new String(input.readAllBytes()), new String(error.readAllBytes()));
|
||||
""", script, inputValue, errorValue);
|
||||
result = StringUtils.isEmpty(inputValue) ? errorValue : inputValue;
|
||||
} catch (Exception e) {
|
||||
log.error("命令执行异常:{}", script, e);
|
||||
result = e.getMessage();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("执行命令异常:{}", script, e);
|
||||
result = e.getMessage();
|
||||
} finally {
|
||||
if(process != null) {
|
||||
process.destroy();
|
||||
// process.destroyForcibly();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user