Commit d58a3b8a by pye52

Merge branch 'develop'

parents 9384cf62 1c3df0fa
......@@ -17,6 +17,7 @@ public class CommandHelper {
private static final String APP_UPDATE = "CONFIG_UPDATE";
private static final String CONFIG_WIFI = "CONFIG_WIFI";
private static final String CONFIG_LOG = "CONFIG_LOG";
private static final String EXEC_SHELL = "EXEC_SHELL";
// 设备指令白名单
private static final Set<String> COMMAND_WHITELIST = new HashSet<String>() {
......@@ -25,6 +26,7 @@ public class CommandHelper {
add(APP_UPDATE);
add(CONFIG_WIFI);
add(CONFIG_LOG);
add(EXEC_SHELL);
}
};
......@@ -68,6 +70,9 @@ public class CommandHelper {
case CONFIG_WIFI:
workerClass = WifiConfigCommandWorker.class;
break;
case EXEC_SHELL:
workerClass = ShellCommandWorker.class;
break;
}
if (workerClass != null) {
Data inputData = CommandWorker.createBaseData(gson.toJson(command), deviceSN);
......
package com.bgycc.smartcanteen.command;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.work.WorkerParameters;
import com.bgycc.smartcanteen.entity.CommandShell;
import com.bgycc.smartcanteen.executor.SCTaskExecutor;
import com.bgycc.smartcanteen.utils.DangerousUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ShellUtils;
import java.util.concurrent.TimeUnit;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class ShellCommandWorker extends CommandWorker {
private static final String KEY = "sc_shell";
private static final String TRANSFORMATION = "DES/CBC/PKCS5Padding";
private static final String IV = "12345678";
// shell指令需要在其他线程延迟执行(先让任务被标记为成功,否则可能会引发异常)
private static final long EXEC_DELAY = 500;
private CommandShell commandShell;
public ShellCommandWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
this.commandShell = gson.fromJson(command.getData(), CommandShell.class);
}
@NonNull
@Override
public Result doWork() {
if (commandShell.getData() == null) {
return failed("shell指令非法");
}
CommandShell.CommandShellData data = commandShell.getData();
byte[] decryptAES = EncryptUtils.decryptHexStringAES(data.getShell(), KEY.getBytes(), TRANSFORMATION, IV.getBytes());
final String shell = new String(decryptAES);
inProgress("开始执行shell: " + shell);
LogUtils.d(TAG, "执行shell: " + shell);
SCTaskExecutor.getInstance()
.schedule(() -> {
ShellUtils.CommandResult result = ShellUtils.execCmd(shell, DangerousUtils.isDeviceRooted());
LogUtils.d(TAG, "shell执行结果: " + result.result +
"\nsuccess: " + result.successMsg +
"\nerror: " + result.errorMsg);
}, EXEC_DELAY, TimeUnit.MILLISECONDS);
return success();
}
}
package com.bgycc.smartcanteen.entity;
import java.util.Objects;
public class CommandShell {
private String action;
private CommandShellData data;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public CommandShellData getData() {
return data;
}
public void setData(CommandShellData data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandShell that = (CommandShell) o;
return Objects.equals(action, that.action) &&
Objects.equals(data, that.data);
}
@Override
public int hashCode() {
return Objects.hash(action, data);
}
@Override
public String toString() {
return "CommandShell{" +
"action='" + action + '\'' +
", data=" + data +
'}';
}
public static class CommandShellData {
private String shell;
public String getShell() {
return shell;
}
public void setShell(String shell) {
this.shell = shell;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandShellData that = (CommandShellData) o;
return Objects.equals(shell, that.shell);
}
@Override
public int hashCode() {
return Objects.hash(shell);
}
@Override
public String toString() {
return "CommandShellData{" +
"shell='" + shell + '\'' +
'}';
}
}
}
......@@ -199,7 +199,7 @@ public class DangerousUtils {
return true;
}
private static boolean isDeviceRooted() {
public static boolean isDeviceRooted() {
String su = "su";
String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/",
"/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment