feat: ASW-85 新增 API 前置拦截器,请求接口前执行前置操作
1. adb connect 2. adb root 3. start droid_ng 4. 判断 默认链中是否含有 ASW_OUTPUT 如果没有则添加
This commit is contained in:
24
src/main/java/net/geedge/api/interceptor/APIInterceptor.java
Normal file
24
src/main/java/net/geedge/api/interceptor/APIInterceptor.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
src/main/java/net/geedge/api/interceptor/WebConfig.java
Normal file
18
src/main/java/net/geedge/api/interceptor/WebConfig.java
Normal file
@@ -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/**");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -47,9 +47,19 @@ public class AdbUtil {
|
|||||||
this.port = adb.getPort();
|
this.port = adb.getPort();
|
||||||
this.vncPort = adb.getVncPort();
|
this.vncPort = adb.getVncPort();
|
||||||
// adb connect
|
// adb connect
|
||||||
this.connect();
|
if (!this.connect()) {
|
||||||
|
log.error("[connect error, program exit]");
|
||||||
|
Runtime.getRuntime().halt(1);
|
||||||
|
}
|
||||||
// init
|
// 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) {
|
public static AdbUtil getInstance(EnvApiYml.Adb connInfo) {
|
||||||
@@ -66,14 +76,13 @@ public class AdbUtil {
|
|||||||
/**
|
/**
|
||||||
* connect
|
* connect
|
||||||
*/
|
*/
|
||||||
private void connect() {
|
public boolean connect() {
|
||||||
if (T.StrUtil.isNotEmpty(this.serial)) {
|
if (T.StrUtil.isNotEmpty(this.serial)) {
|
||||||
// local
|
// local
|
||||||
AdbDevice adbDevice = this.getAdbDevice();
|
AdbDevice adbDevice = this.getAdbDevice();
|
||||||
log.info("[connect] [result: {}]", T.JSONUtil.toJsonStr(adbDevice));
|
log.info("[connect] [result: {}]", T.JSONUtil.toJsonStr(adbDevice));
|
||||||
if (null == adbDevice || !adbDevice.isAvailable()) {
|
if (null == adbDevice || !adbDevice.isAvailable()) {
|
||||||
log.error("[device is not available, program exit]");
|
return false;
|
||||||
Runtime.getRuntime().halt(1);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// remote
|
// remote
|
||||||
@@ -82,10 +91,10 @@ public class AdbUtil {
|
|||||||
.build());
|
.build());
|
||||||
log.info("[connect] [result: {}]", result);
|
log.info("[connect] [result: {}]", result);
|
||||||
if (!T.StrUtil.contains(result, "connected")) {
|
if (!T.StrUtil.contains(result, "connected")) {
|
||||||
log.error("[connect error, program exit]");
|
return false;
|
||||||
Runtime.getRuntime().halt(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,7 +102,7 @@ public class AdbUtil {
|
|||||||
* su root
|
* su root
|
||||||
* install droidVNC NG
|
* install droidVNC NG
|
||||||
*/
|
*/
|
||||||
private void init() {
|
public void init(boolean install) {
|
||||||
// adb root
|
// adb root
|
||||||
String result = CommandExec.exec(AdbCommandBuilder.builder()
|
String result = CommandExec.exec(AdbCommandBuilder.builder()
|
||||||
.serial(this.getSerial())
|
.serial(this.getSerial())
|
||||||
@@ -102,23 +111,27 @@ public class AdbUtil {
|
|||||||
);
|
);
|
||||||
log.info("[init] [adb root] [result: {}]", result);
|
log.info("[init] [adb root] [result: {}]", result);
|
||||||
|
|
||||||
// install droidVNC NG
|
if (install) {
|
||||||
CommandResult installed = this.install(DEFAULT_DROIDVNC_NG_APK_PATH, true, true);
|
// install droidVNC NG
|
||||||
log.info("[init] [install droidVNC NG] [result: {}]", installed);
|
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.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.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 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 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 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");
|
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");
|
||||||
|
|
||||||
// 添加自定义链
|
// 添加自定义链
|
||||||
|
|||||||
Reference in New Issue
Block a user