fix: 调整 android xapk 文件校验

This commit is contained in:
zhangshuai
2024-10-30 09:27:49 +08:00
parent b1e4a60c46
commit 5350efe1e0
3 changed files with 47 additions and 16 deletions

View File

@@ -100,7 +100,7 @@ public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> i
throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR); throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR);
} }
entity.setName(apkInfo.getLabel()); entity.setName(apkInfo.getLabel());
entity.setVersion(apkInfo.getSdkVersion()); entity.setVersion(apkInfo.getVersionName());
entity.setIdentifier(apkInfo.getPackageName()); entity.setIdentifier(apkInfo.getPackageName());
} }
} catch (Exception e) { } catch (Exception e) {

View File

@@ -38,11 +38,14 @@ public class ApkInfo {
private String versionCode; private String versionCode;
// 外部版本号 // 外部版本号
private String versionName; private String versionName;
// xapk 包含的 apk
private List<String> splitApks;
public ApkInfo() { public ApkInfo() {
this.features = new ArrayList<>(); this.features = new ArrayList<>();
this.icons = new HashMap<>(); this.icons = new HashMap<>();
this.usesPermissions = new ArrayList<>(); this.usesPermissions = new ArrayList<>();
this.splitApks = new ArrayList<>();
} }
public List<String> getFeatures() { public List<String> getFeatures() {
@@ -161,6 +164,14 @@ public class ApkInfo {
this.versionName = versionName; this.versionName = versionName;
} }
public List<String> getSplitApks() {
return splitApks;
}
public void setSplitApks(List<String> splitApks) {
this.splitApks = splitApks;
}
@Override @Override
public String toString() { public String toString() {
return "ApkInfo [features=" + features + ", icon=" + icon + ", icons=" + icons + ", label=" + label + ", launchableActivity=" + launchableActivity + ", minSdkVersion=" + minSdkVersion + ", packageName=" + packageName + ", sdkVersion=" + sdkVersion + ", size=" + size + ", targetSdkVersion=" + targetSdkVersion + ", usesPermissions=" + usesPermissions + ", versionCode=" + versionCode + ", versionName=" + versionName + "]"; return "ApkInfo [features=" + features + ", icon=" + icon + ", icons=" + icons + ", label=" + label + ", launchableActivity=" + launchableActivity + ", minSdkVersion=" + minSdkVersion + ", packageName=" + packageName + ", sdkVersion=" + sdkVersion + ", size=" + size + ", targetSdkVersion=" + targetSdkVersion + ", usesPermissions=" + usesPermissions + ", versionCode=" + versionCode + ", versionName=" + versionName + "]";

View File

@@ -1,11 +1,11 @@
package net.geedge.asw.module.app.util; package net.geedge.asw.module.app.util;
import cn.hutool.core.io.FileUtil;
import cn.hutool.log.Log; import cn.hutool.log.Log;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.T; import net.geedge.asw.common.util.T;
import java.io.*; import java.io.*;
import java.util.Enumeration;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@@ -84,10 +84,13 @@ public class ApkUtil {
public ApkInfo parseXapk(String xapkPath) { public ApkInfo parseXapk(String xapkPath) {
InputStream inputStream = null; InputStream inputStream = null;
BufferedReader reader = null; BufferedReader reader = null;
File tempFile = null; ZipFile zipFile = null;
try { try {
ZipFile zipFile = new ZipFile(T.FileUtil.file(xapkPath)); zipFile = new ZipFile(T.FileUtil.file(xapkPath));
ZipEntry entry = zipFile.getEntry("manifest.json"); ZipEntry entry = zipFile.getEntry("manifest.json");
if (entry == null){
return null;
}
inputStream = zipFile.getInputStream(entry); inputStream = zipFile.getInputStream(entry);
StringBuilder manifestJson = new StringBuilder(); StringBuilder manifestJson = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(inputStream));
@@ -95,29 +98,46 @@ public class ApkUtil {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
manifestJson.append(line).append("\n"); manifestJson.append(line).append("\n");
} }
Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class); Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class);
ZipEntry packageFile = zipFile.getEntry(T.StrUtil.concat(true, T.MapUtil.getStr(manifest, "package_name"), ".apk")); ApkInfo apkInfo = new ApkInfo();
tempFile = T.FileUtil.file(Constants.TEMP_PATH, packageFile.getName()); this.setXapkInfoProperty(manifest, apkInfo);
FileUtil.writeBytes(zipFile.getInputStream(packageFile).readAllBytes(), tempFile);
ApkInfo apkInfo = this.parseApk(tempFile.getPath()); String packageName = apkInfo.getPackageName();
if (apkInfo == null) { List<String> splitApks = apkInfo.getSplitApks();
return null; if (T.CollUtil.isNotEmpty(splitApks)){
} Enumeration<? extends ZipEntry> apkEntries = zipFile.entries();
if (!T.BooleanUtil.and(apkInfo.getVersionName().equals(T.MapUtil.getStr(manifest, "version_name")),
apkInfo.getPackageName().equals(T.MapUtil.getStr(manifest, "package_name")))) { while (apkEntries.hasMoreElements()) {
return null; ZipEntry apk = apkEntries.nextElement();
if (!apk.getName().endsWith(".apk")){
continue;
}
if (!T.StrUtil.equalsIgnoreCase(packageName + ".apk", apk.getName()) && !splitApks.contains(apk.getName())) {
return null;
}
}
} }
return apkInfo; return apkInfo;
} catch (Exception e) { } catch (Exception e) {
log.error(e, "[parseXapk] [error] [path: {}]", xapkPath); log.error(e, "[parseXapk] [error] [path: {}]", xapkPath);
return null; return null;
} finally { } finally {
T.FileUtil.del(tempFile);
T.IoUtil.close(inputStream); T.IoUtil.close(inputStream);
T.IoUtil.close(reader); T.IoUtil.close(reader);
T.IoUtil.close(zipFile);
} }
} }
private void setXapkInfoProperty(Map manifest, ApkInfo apkInfo) {
apkInfo.setLabel(T.MapUtil.getStr(manifest, "name"));
apkInfo.setVersionName(T.MapUtil.getStr(manifest, "version_name"));
apkInfo.setPackageName(T.MapUtil.getStr(manifest, "package_name"));
List<Map> splitApkMapList = T.MapUtil.get(manifest, "split_apks", List.class);
List<String> splitApkNames = splitApkMapList.stream().map(x -> T.MapUtil.getStr(x, "file")).toList();
apkInfo.setSplitApks(splitApkNames);
}
private void setApkInfoProperty(ApkInfo apkInfo, String source) { private void setApkInfoProperty(ApkInfo apkInfo, String source) {
if (source.startsWith(APPLICATION)) { if (source.startsWith(APPLICATION)) {
String[] rs = source.split("( icon=')|'"); String[] rs = source.split("( icon=')|'");