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);
}
entity.setName(apkInfo.getLabel());
entity.setVersion(apkInfo.getSdkVersion());
entity.setVersion(apkInfo.getVersionName());
entity.setIdentifier(apkInfo.getPackageName());
}
} catch (Exception e) {

View File

@@ -38,11 +38,14 @@ public class ApkInfo {
private String versionCode;
// 外部版本号
private String versionName;
// xapk 包含的 apk
private List<String> splitApks;
public ApkInfo() {
this.features = new ArrayList<>();
this.icons = new HashMap<>();
this.usesPermissions = new ArrayList<>();
this.splitApks = new ArrayList<>();
}
public List<String> getFeatures() {
@@ -161,6 +164,14 @@ public class ApkInfo {
this.versionName = versionName;
}
public List<String> getSplitApks() {
return splitApks;
}
public void setSplitApks(List<String> splitApks) {
this.splitApks = splitApks;
}
@Override
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 + "]";

View File

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