summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshizhendong <[email protected]>2024-09-29 11:21:54 +0800
committershizhendong <[email protected]>2024-09-29 11:21:54 +0800
commit28e34185c5ebcd01852452943af45f2b250916b7 (patch)
tree7673c31c121c9ea15fbc8d807a1fa68cebc66c08
parent8d886396556cb3fea693cbf3f7e9131499b809c0 (diff)
feat: ASW-85 新增 API 前置拦截器,请求接口前执行前置操作
1. adb connect 2. adb root 3. start droid_ng 4. 判断 默认链中是否含有 ASW_OUTPUT 如果没有则添加
-rw-r--r--src/main/java/net/geedge/api/interceptor/APIInterceptor.java24
-rw-r--r--src/main/java/net/geedge/api/interceptor/WebConfig.java18
-rw-r--r--src/main/java/net/geedge/api/util/AdbUtil.java57
3 files changed, 77 insertions, 22 deletions
diff --git a/src/main/java/net/geedge/api/interceptor/APIInterceptor.java b/src/main/java/net/geedge/api/interceptor/APIInterceptor.java
new file mode 100644
index 0000000..addb883
--- /dev/null
+++ b/src/main/java/net/geedge/api/interceptor/APIInterceptor.java
@@ -0,0 +1,24 @@
+package net.geedge.api.interceptor;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import net.geedge.api.util.AdbUtil;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+public class APIInterceptor implements HandlerInterceptor {
+
+ @Override
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+ AdbUtil adbUtil = AdbUtil.getInstance();
+ if (!adbUtil.connect()) {
+ // 记录日志或设置响应状态
+ response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "device connect error");
+ return false;
+ }
+
+ // init
+ adbUtil.init(false);
+ return true;
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/net/geedge/api/interceptor/WebConfig.java b/src/main/java/net/geedge/api/interceptor/WebConfig.java
new file mode 100644
index 0000000..61356a4
--- /dev/null
+++ b/src/main/java/net/geedge/api/interceptor/WebConfig.java
@@ -0,0 +1,18 @@
+package net.geedge.api.interceptor;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * WebMvc配置
+ */
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+ @Override
+ public void addInterceptors(InterceptorRegistry registry) {
+ registry.addInterceptor(new APIInterceptor()).addPathPatterns("/api/v1/env/**");
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/net/geedge/api/util/AdbUtil.java b/src/main/java/net/geedge/api/util/AdbUtil.java
index a9a609a..9c38683 100644
--- a/src/main/java/net/geedge/api/util/AdbUtil.java
+++ b/src/main/java/net/geedge/api/util/AdbUtil.java
@@ -47,9 +47,19 @@ public class AdbUtil {
this.port = adb.getPort();
this.vncPort = adb.getVncPort();
// adb connect
- this.connect();
+ if (!this.connect()) {
+ log.error("[connect error, program exit]");
+ Runtime.getRuntime().halt(1);
+ }
// init
- this.init();
+ this.init(true);
+ }
+
+ public static AdbUtil getInstance() {
+ if (instance == null) {
+ throw new IllegalArgumentException("Object has not been instantiated.");
+ }
+ return instance;
}
public static AdbUtil getInstance(EnvApiYml.Adb connInfo) {
@@ -66,14 +76,13 @@ public class AdbUtil {
/**
* connect
*/
- private void connect() {
+ public boolean connect() {
if (T.StrUtil.isNotEmpty(this.serial)) {
// local
AdbDevice adbDevice = this.getAdbDevice();
log.info("[connect] [result: {}]", T.JSONUtil.toJsonStr(adbDevice));
if (null == adbDevice || !adbDevice.isAvailable()) {
- log.error("[device is not available, program exit]");
- Runtime.getRuntime().halt(1);
+ return false;
}
} else {
// remote
@@ -82,10 +91,10 @@ public class AdbUtil {
.build());
log.info("[connect] [result: {}]", result);
if (!T.StrUtil.contains(result, "connected")) {
- log.error("[connect error, program exit]");
- Runtime.getRuntime().halt(1);
+ return false;
}
}
+ return true;
}
/**
@@ -93,7 +102,7 @@ public class AdbUtil {
* su root
* install droidVNC NG
*/
- private void init() {
+ public void init(boolean install) {
// adb root
String result = CommandExec.exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
@@ -102,23 +111,27 @@ public class AdbUtil {
);
log.info("[init] [adb root] [result: {}]", result);
- // install droidVNC NG
- CommandResult installed = this.install(DEFAULT_DROIDVNC_NG_APK_PATH, true, true);
- log.info("[init] [install droidVNC NG] [result: {}]", installed);
+ if (install) {
+ // install droidVNC NG
+ CommandResult installed = this.install(DEFAULT_DROIDVNC_NG_APK_PATH, true, true);
+ log.info("[init] [install droidVNC NG] [result: {}]", installed);
- // 上传默认配置
- this.execShellCommand("shell mkdir -p /storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files");
- this.push(DEFAULT_DROIDVNC_NG_DEFAULTS_JSON_PATH, "/storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files/defaults.json");
+ // 上传默认配置
+ this.execShellCommand("shell mkdir -p /storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files");
+ this.push(DEFAULT_DROIDVNC_NG_DEFAULTS_JSON_PATH, "/storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files/defaults.json");
- // 无障碍权限
- this.execShellCommand("shell settings put secure enabled_accessibility_services net.christianbeier.droidvnc_ng/.InputService:$(settings get secure enabled_accessibility_services)");
- // 存储空间权限
- this.execShellCommand("shell pm grant net.christianbeier.droidvnc_ng android.permission.WRITE_EXTERNAL_STORAGE");
- // 屏幕录制权限
- this.execShellCommand("shell appops set net.christianbeier.droidvnc_ng PROJECT_MEDIA allow");
+ // 无障碍权限
+ this.execShellCommand("shell settings put secure enabled_accessibility_services net.christianbeier.droidvnc_ng/.InputService:$(settings get secure enabled_accessibility_services)");
+ // 存储空间权限
+ this.execShellCommand("shell pm grant net.christianbeier.droidvnc_ng android.permission.WRITE_EXTERNAL_STORAGE");
+ // 屏幕录制权限
+ this.execShellCommand("shell appops set net.christianbeier.droidvnc_ng PROJECT_MEDIA allow");
+
+ // ACTION_STOP
+ this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_STOP --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0");
+ }
- // 后台启动
- this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_STOP --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0");
+ // ACTION_START
this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_START --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0");
// 添加自定义链