Skip to content

Commit

Permalink
Remove dependency on Gson
Browse files Browse the repository at this point in the history
Patch-loader might use Gson provided by the apk classLoader
  • Loading branch information
JingMatrix committed Jul 8, 2023
1 parent bd136d8 commit 91066a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import android.system.Os;
import android.util.Log;

import com.google.gson.Gson;

import org.lsposed.lspatch.loader.util.FileUtils;
import org.lsposed.lspatch.loader.util.XLog;
import org.lsposed.lspatch.service.LocalApplicationService;
import org.lsposed.lspatch.service.RemoteApplicationService;
import org.lsposed.lspatch.share.PatchConfig;
import org.lsposed.lspd.core.Startup;
import org.lsposed.lspd.service.ILSPApplicationService;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -39,6 +37,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;

import de.robv.android.xposed.XposedHelpers;
Expand All @@ -58,7 +57,7 @@ public class LSPApplication {
private static LoadedApk stubLoadedApk;
private static LoadedApk appLoadedApk;

private static PatchConfig config;
private static JSONObject config;

public static boolean isIsolated() {
return (android.os.Process.myUid() % PER_USER_RANGE) >= FIRST_APP_ZYGOTE_ISOLATED_UID;
Expand All @@ -78,7 +77,7 @@ public static void onLoad() throws RemoteException, IOException {

Log.d(TAG, "Initialize service client");
ILSPApplicationService service;
if (config.useManager) {
if (config.optBoolean("useManager")) {
service = new RemoteApplicationService(context);
} else {
service = new LocalApplicationService(context);
Expand All @@ -94,7 +93,7 @@ public static void onLoad() throws RemoteException, IOException {
Log.i(TAG, "Modules initialized");

switchAllClassLoader();
SigBypass.doSigBypass(context, config.sigBypassLevel);
SigBypass.doSigBypass(context, config.optInt("sigBypassLevel"));

Log.i(TAG, "LSPatch bootstrap completed");
}
Expand All @@ -110,13 +109,13 @@ private static Context createLoadedApkWithContext() {

try (var is = baseClassLoader.getResourceAsStream(CONFIG_ASSET_PATH)) {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
config = new Gson().fromJson(streamReader, PatchConfig.class);
} catch (IOException e) {
Log.e(TAG, "Failed to load config file");
config = new JSONObject(streamReader.lines().collect(Collectors.joining()));
} catch (Throwable e) {
Log.e(TAG, "Failed to parse config file", e);
return null;
}
Log.i(TAG, "Use manager: " + config.useManager);
Log.i(TAG, "Signature bypass level: " + config.sigBypassLevel);
Log.i(TAG, "Use manager: " + config.optBoolean("useManager"));
Log.i(TAG, "Signature bypass level: " + config.optInt("sigBypassLevel"));

Path originPath = Paths.get(appInfo.dataDir, "cache/lspatch/origin/");
Path cacheApkPath;
Expand All @@ -126,7 +125,9 @@ private static Context createLoadedApkWithContext() {

appInfo.sourceDir = cacheApkPath.toString();
appInfo.publicSourceDir = cacheApkPath.toString();
appInfo.appComponentFactory = config.appComponentFactory;
if (config.has("appComponentFactory")) {
appInfo.appComponentFactory = config.optString("appComponentFactory");
}

if (!Files.exists(cacheApkPath)) {
Log.i(TAG, "Extract original apk");
Expand Down Expand Up @@ -165,11 +166,11 @@ private static Context createLoadedApkWithContext() {
Log.i(TAG, "hooked app initialized: " + appLoadedApk);

var context = (Context) XposedHelpers.callStaticMethod(Class.forName("android.app.ContextImpl"), "createAppContext", activityThread, stubLoadedApk);
if (config.appComponentFactory != null) {
if (config.has("appComponentFactory")) {
try {
context.getClassLoader().loadClass(config.appComponentFactory);
context.getClassLoader().loadClass(appInfo.appComponentFactory);
} catch (ClassNotFoundException e) { // This will happen on some strange shells like 360
Log.w(TAG, "Original AppComponentFactory not found: " + config.appComponentFactory);
Log.w(TAG, "Original AppComponentFactory not found: " + appInfo.appComponentFactory);
appInfo.appComponentFactory = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.lsposed.lspatch.loader.util.XLog;
import org.lsposed.lspatch.share.Constants;
import org.lsposed.lspatch.share.PatchConfig;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -46,8 +46,12 @@ private static void replaceSignature(Context context, PackageInfo packageInfo) {
if (metaData != null) encoded = metaData.getString("lspatch");
if (encoded != null) {
var json = new String(Base64.decode(encoded, Base64.DEFAULT), StandardCharsets.UTF_8);
var patchConfig = new Gson().fromJson(json, PatchConfig.class);
replacement = patchConfig.originalSignature;
try {
var patchConfig = new JSONObject(json);
replacement = patchConfig.getString("originalSignature");
} catch (JSONException e) {
Log.w(TAG, "fail to get originalSignature", e);
}
}
} catch (PackageManager.NameNotFoundException | JsonSyntaxException ignored) {
}
Expand Down

0 comments on commit 91066a0

Please sign in to comment.