From 9017c2bd7dd6d76619f0d5901208f7cfc2a221cd Mon Sep 17 00:00:00 2001 From: liuguijing <1440265357@qq.com> Date: Wed, 26 Mar 2025 17:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E7=BB=B4=E6=96=B0=E5=A2=9E=E6=94=AF?= =?UTF-8?q?=E6=8C=81adb=20=20shell=E5=91=BD=E4=BB=A4=20=E4=BF=AE=E5=A4=8Ds?= =?UTF-8?q?im=E5=8D=A12=20=E5=9C=A8=E8=BF=9E=E6=8E=A5=E4=B8=8D=E4=B8=8A?= =?UTF-8?q?=E8=BF=90=E7=BB=B4=E4=B8=BB=E7=AB=99=E6=97=B6=20=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=87=E6=8D=A2=E5=88=B0sim=E5=8D=A11=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + .../java/com/xypower/mpapp/MainActivity.java | 4 ++ mpmaster/build.gradle | 2 +- .../java/com/xypower/mpmaster/AppMaster.java | 49 ++++++++-------- .../com/xypower/mpmaster/sms/AdbUtil.java | 56 +++++++++++++++++++ .../com/xypower/mpmaster/sms/SimUtil.java | 28 +++++++++- .../com/xypower/mpmaster/sms/SmsTypeEnum.java | 1 + 7 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 58d9f162..855f3d95 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -68,6 +68,7 @@ android:name="android.permission.TETHER_PRIVILEGED" tools:ignore="ProtectedPermissions" /> + diff --git a/app/src/main/java/com/xypower/mpapp/MainActivity.java b/app/src/main/java/com/xypower/mpapp/MainActivity.java index cc2b8b32..8239d8b2 100644 --- a/app/src/main/java/com/xypower/mpapp/MainActivity.java +++ b/app/src/main/java/com/xypower/mpapp/MainActivity.java @@ -1,6 +1,7 @@ package com.xypower.mpapp; import android.Manifest; +import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; @@ -9,9 +10,11 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; +import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Messenger; +import android.os.PowerManager; import android.os.StrictMode; import androidx.appcompat.app.ActionBar; @@ -21,6 +24,7 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.SystemClock; +import android.provider.Settings; import android.telephony.SubscriptionManager; import android.text.TextUtils; import android.util.Log; diff --git a/mpmaster/build.gradle b/mpmaster/build.gradle index d43bbd68..14492f51 100644 --- a/mpmaster/build.gradle +++ b/mpmaster/build.gradle @@ -4,7 +4,7 @@ plugins { def AppMajorVersion = 1 def AppMinorVersion = 1 -def AppBuildNumber = 12 +def AppBuildNumber = 13 def AppVersionName = AppMajorVersion + "." + AppMinorVersion + "." + AppBuildNumber def AppVersionCode = AppMajorVersion * 100000 + AppMinorVersion * 1000 + AppBuildNumber diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 963ec29c..9914d88f 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -158,11 +158,15 @@ public class AppMaster { sleep(1000); } - + int masterHttpstatus = 0;//0--接口请求正常但未处理 1--访问运维服务器正常,处理也正常 -1----访问运维服务器异常 try { - runImpl(isCriticalTime); + masterHttpstatus= runImpl(isCriticalTime); } catch (Exception ex) { ex.printStackTrace(); + }finally { + if (masterHttpstatus == -1) { + mService.setMntnMode(false,false); + } } } catch (Exception ex) { ex.printStackTrace(); @@ -186,14 +190,14 @@ public class AppMaster { }).start(); } - private boolean runImpl(final boolean isCriticalTime) { + private int runImpl(final boolean isCriticalTime) { String masterUrl = mMasterUrl; + int res = 0; if (TextUtils.isEmpty(masterUrl)) { - return false; + return res; } - boolean res = false; HttpURLConnection httpURLConnection = null; InputStream inputStream = null; Date now = new Date(); @@ -336,13 +340,14 @@ public class AppMaster { String response = convertStreamToString(inputStream); process(response); } - res = true; + res = 1; } catch (Exception ex) { // ex.printStackTrace(); - mService.logger.warning(ex.getMessage()); + res = -1; + mService.logger.warning("运维服务器连接失败! " + ex.getMessage()); } - if (res) { + if (res == 1) { break; } @@ -435,13 +440,13 @@ public class AppMaster { if (items.length > 12 && items.length <= 18) { // v2 - long ts = (((long)items[12]) & 0xFFFFFFFFl) | ((((long)items[13]) << 32) & 0xFFFFFFFF00000000l); + long ts = (((long) items[12]) & 0xFFFFFFFFl) | ((((long) items[13]) << 32) & 0xFFFFFFFF00000000l); stats.add(new Pair("lastHbTime", Long.toString(ts))); - ts = (((long)items[14]) & 0xFFFFFFFFl) | ((((long)items[15]) << 32) & 0xFFFFFFFF00000000l); + ts = (((long) items[14]) & 0xFFFFFFFFl) | ((((long) items[15]) << 32) & 0xFFFFFFFF00000000l); stats.add(new Pair("lastHbRespTime", Long.toString(ts))); - ts = (((long)items[16]) & 0xFFFFFFFFl) | ((((long)items[17]) << 32) & 0xFFFFFFFF00000000l); + ts = (((long) items[16]) & 0xFFFFFFFFl) | ((((long) items[17]) << 32) & 0xFFFFFFFF00000000l); stats.add(new Pair("lastRecvTime", Long.toString(ts))); } @@ -472,7 +477,7 @@ public class AppMaster { cnts[0]++; } // if (pathname.lastModified() >= weekStartTime) { - cnts[1]++; + cnts[1]++; // } } return false; @@ -486,7 +491,6 @@ public class AppMaster { } - private String getImei(int number) { return (number == 1) ? SysApi.getImei(mService) : SysApi.getImei2(mService); } @@ -494,8 +498,8 @@ public class AppMaster { private String getFreeROM() { long[] flashInfo = SysApi.getFlashInfo(); if (flashInfo != null && flashInfo.length > 1) { - double fd = (double)(flashInfo[1] * 100) / (double)flashInfo[0]; - return Integer.toString((int)fd) + "%"; + double fd = (double) (flashInfo[1] * 100) / (double) flashInfo[0]; + return Integer.toString((int) fd) + "%"; } return ""; } @@ -628,7 +632,7 @@ public class AppMaster { } } } - } else if (TextUtils.equals(cmd, CMD_DOWNLOAD_FILE)) { + } else if (TextUtils.equals(cmd, CMD_DOWNLOAD_FILE)) { String path = jsonObject.optString("path", null); String url = jsonObject.optString("url", null); @@ -683,14 +687,14 @@ public class AppMaster { int enable = jsonObject.optInt("enable", 1); mService.logger.warning("Recv EN OTG: " + enable); SysApi.setOtgState((enable != 0)); - } else if (TextUtils.equals(cmd, CMD_PULL_FILE)) { + } else if (TextUtils.equals(cmd, CMD_PULL_FILE)) { pullFiles(jsonObject); - } else if (TextUtils.equals(cmd, CMD_DELETE_FILE)) { + } else if (TextUtils.equals(cmd, CMD_DELETE_FILE)) { String path = jsonObject.optString("path", null); mService.logger.warning("Recv Del File: " + path); deleteFile(path); - } else if (TextUtils.equals(cmd, CMD_LIST_FILES)) { + } else if (TextUtils.equals(cmd, CMD_LIST_FILES)) { String path = jsonObject.optString("path", null); String filter = jsonObject.optString("filter", null); mService.logger.warning("Recv List Files: " + path); @@ -904,7 +908,7 @@ public class AppMaster { if (packageName.equals(MicroPhotoContext.PACKAGE_NAME_MPMASTER)) { MpMasterService.restartMpMasterApp(mService.getApplicationContext(), "Config Updated"); } else { - MpMasterService.restartAppByPackage(mService.getApplicationContext(), packageName,"Config Updated"); + MpMasterService.restartAppByPackage(mService.getApplicationContext(), packageName, "Config Updated"); } } } @@ -957,7 +961,7 @@ public class AppMaster { } catch (Exception ex) { } - MpMasterService.restartMpApp(context,"CMA Updated"); + MpMasterService.restartMpApp(context, "CMA Updated"); } }); @@ -983,7 +987,7 @@ public class AppMaster { MicroPhotoContext.saveMpAppConfig(context, appConfig); - MpMasterService.restartMpApp(context.getApplicationContext(),"HB Duration Updated"); + MpMasterService.restartMpApp(context.getApplicationContext(), "HB Duration Updated"); return true; } @@ -1331,5 +1335,4 @@ public class AppMaster { } - } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java new file mode 100644 index 00000000..4e713818 --- /dev/null +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java @@ -0,0 +1,56 @@ +package com.xypower.mpmaster.sms; + +import android.util.Log; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +public class AdbUtil { + public static void Shell() { + try { + Process process1 = Runtime.getRuntime().exec("su"); + Process process = Runtime.getRuntime().exec("ps -ef|grep mp"); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + Log.d("Output", line); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void ShellList(ArrayList shellList) { + try { + // 使用 su 获取 root 权限 + Process process = Runtime.getRuntime().exec("su"); + + // 获取输出流,用于发送命令 + DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); + + for (int i = 0; i < shellList.size()-1; i++) { + outputStream.writeBytes(shellList.get(i)+"\n"); + } + // 执行命令(例如:列出 /data 目录) + + outputStream.writeBytes("exit\n"); // 退出 su + outputStream.flush(); + + // 读取命令输出 + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + Log.d("Output", line); // 打印输出 + } + + // 等待命令执行完成 + int i = process.waitFor(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index 5e79fc45..fb915cbe 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -562,10 +562,36 @@ public class SimUtil { UpdateSysConfigUtil.clearHistoryPic(context); UpdateSysConfigUtil.clearHistoryLogs(context); sendmessage = getSendString(content, ifmessageCorrect); + } else if (content.contains(SmsTypeEnum.SHELL.value())) { + ifmessageCorrect = true; + ArrayList shellList = new ArrayList<>(); + restartType = 1; + String[] split1 = StringUtils.splitString1(content); + if (split1 != null && split1.length > 1) { + ifmessageCorrect = true; + String s = split1[1]; + String[] strings = StringUtils.splitString2(s); + if (strings != null && strings.length > 0) { + for (int i = 0; i < strings.length; i++) { + shellList.add(strings[i]); + } + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + AdbUtil.ShellList(shellList); + } + }).start(); + } + } + sendmessage = getSendString(content, ifmessageCorrect); } sendSms(context, slot, sender, sendmessage, restartType); } - } private static int getUtcp(Integer utcp) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java index c8de2819..cf730902 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java @@ -64,6 +64,7 @@ public enum SmsTypeEnum { CLEAR_LOG("yw+at+clearLog"), //删除日志 CLEAR_ALL("yw+at+clearAll"), //清除图片、视频、日志 RESTORE("yw+at+Restore"), //恢复出厂设置 + SHELL("yw+at+shell"), //执行shell脚本 SIMCARD("at+str=sim"), SET_AUTO_TIME("at-auto-time"), // act=[on/off] type=[net/gps] UPD_CFG_FILE("at-updcfg"),//修改配置文件参数 // f=[1/2/91-99] c=[field count] n1=[field name] t1=[0/1/2] v1= GET_CFG_FILE("at-getcfg"), //获取配置文件参数// f=[0/1/2/91-99] p=[Absolute Path]