Skip to content

Commit

Permalink
Fix config can't read
Browse files Browse the repository at this point in the history
  • Loading branch information
MlgmXyysd committed Jul 21, 2020
1 parent 44cd96a commit c53183a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
minSdkVersion 24
//noinspection OldTargetApi
targetSdkVersion 27
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}

buildTypes {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/org/meowcat/xposed/mipush/Enhancement.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
@Override
protected void afterHookedMethod(MethodHookParam param) {
final Context context = (Context) param.args[0];
final ApplicationInfo applicationInfo = context.getApplicationInfo();
if (applicationInfo == null) {
// null application info, exit
return;
}
final int userId = UserHandle.getUserHandleForUid(applicationInfo.uid).hashCode();
final boolean availability = Utils.getParamAvailability(param, Binder.getCallingPid());

if (packageName.equals(BuildConfig.APPLICATION_ID)) {
Expand All @@ -66,7 +72,7 @@ protected void afterHookedMethod(MethodHookParam param) {
}

try {
final IniConf conf = new IniConf(context);
final IniConf conf = new IniConf(userId);

switch (conf.get(MODULE_WORKING_MODE, "blacklist")) {
case MODE_BLACK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.text.Html;
import android.util.Pair;
import android.view.Menu;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
if (savedInstanceState == null) {
try {
mConf = new IniConf(requireContext());
mConf = new IniConf(UserHandle.getUserHandleForUid(requireContext().getApplicationInfo().uid).hashCode());
} catch (IOException e) {
// We have encountered an unrecoverable error.
throw new RuntimeException(e);
Expand Down
41 changes: 27 additions & 14 deletions app/src/main/java/top/trumeet/mipush/settings/ini/IniConf.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package top.trumeet.mipush.settings.ini;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.FileUtils;
import android.util.Log;

import androidx.annotation.NonNull;
Expand All @@ -18,6 +20,8 @@
import java.util.Map;
import java.util.Objects;

import static top.trumeet.mipush.settings.ini.IniConstants.CONF_FILE;
import static top.trumeet.mipush.settings.ini.IniConstants.CONF_PATH;
import static top.trumeet.mipush.settings.ini.IniConstants.INI_DEFAULT;
import static top.trumeet.mipush.settings.ini.IniConstants.TAG;
import static top.trumeet.mipush.settings.ini.IniConstants.rwxrwxrwx;
Expand Down Expand Up @@ -52,19 +56,28 @@ public class IniConf {
* If the file is absent, it will be created.
* If the file is null, write() will do nothing.
*/
public IniConf(@Nullable final File file) throws IOException {
if (file != null) {
setFilePermissionsFromMode(file, rwxrwxrwx);
@SuppressWarnings({"ResultOfMethodCallIgnored"})
public IniConf(final File file) throws IOException {
final File conf_path = new File(file, CONF_PATH);
final File conf_file = new File(conf_path, CONF_FILE);

FileUtils.setPermissions(file.getAbsolutePath() + "/", rwxrwxrwx, -1, -1);

if (!conf_path.exists()) {
conf_path.mkdir();
}
mIni = createDefaultConfig(file);

FileUtils.setPermissions(conf_path.getAbsolutePath() + "/", rwxrwxrwx, -1, -1);

mIni = createDefaultConfig(conf_file);
// TODO: File locking?
}

/**
* Construct using the default path
*/
public IniConf(Context context) throws IOException {
this(IniUtils.getConfigPath(context));
public IniConf(int userId) throws IOException {
this(IniUtils.getConfigPath(userId));
}

/**
Expand All @@ -74,15 +87,13 @@ public IniConf(Context context) throws IOException {
* @return The upgraded ini. It will not be stored in file system.
* @throws IOException In case there's errors.
*/
private Ini createDefaultConfig(@Nullable final File file) throws IOException {
private Ini createDefaultConfig(final File file) throws IOException {
final Ini defaultIni = new Ini();
defaultIni.load(new ByteArrayInputStream(INI_DEFAULT.getBytes(StandardCharsets.UTF_8)));
final Ini currentIni = new Ini();
if (file != null) {
currentIni.setFile(file);
if (file.exists()) {
currentIni.load(file);
}
currentIni.setFile(file);
if (file.exists()) {
currentIni.load(file);
}
// Instead of just copying, we compare and proceed the diffs.
// Check for absent options to be added.
Expand Down Expand Up @@ -139,19 +150,21 @@ public Map<IniKey, Object> getAll() {
/**
* Write all changes to the file system. If the file is absent, it will be created.
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
@SuppressLint("WorldReadableFiles")
@SuppressWarnings({"ResultOfMethodCallIgnored", "deprecation"})
public void write() throws IOException {
// Log.d(TAG, "Saving configuration");
if (mIni.getFile() != null) {
// Create the parent if needed.
final File parent = mIni.getFile().getParentFile();
FileUtils.setPermissions(parent.getParent() + "/", 511, -1, -1);
if (!Objects.requireNonNull(parent).exists()) {
parent.mkdir();
}
// Write, or create.
mIni.store();

setFilePermissionsFromMode(mIni.getFile(), rwxrwxrwx);
setFilePermissionsFromMode(mIni.getFile().getAbsolutePath(), Context.MODE_WORLD_READABLE);
} else {
Log.w(TAG, "File is null. Ignoring.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public final class IniConstants {
// linux permission rwx rwx rwx, OCT is 00777, DEX is 511
public static final int rwxrwxrwx = 511;

public static final String CONF_PATH = "etc";
public static final String CONF_FILE = "module.conf";

public static final IniKey MODULE_WORKING_MODE = new IniKey("module", "working_mode");
public static final IniKey MODULE_BLACKLIST = new IniKey("module", "blacklist");
public static final IniKey MODULE_WHITELIST = new IniKey("module", "whitelist");
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/top/trumeet/mipush/settings/ini/IniUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import androidx.annotation.NonNull;

import org.meowcat.xposed.mipush.BuildConfig;

import java.io.File;

public class IniUtils {
Expand All @@ -31,12 +33,12 @@ public static String convertKeyToSP(@NonNull final IniKey key) {
/**
* Change file permission with given permission mode
*
* @param file file to change permissions
* @param name file to change permissions
* @param mode file permission
*/
@SuppressWarnings("deprecation")
@SuppressLint({"WorldReadableFiles", "WorldWriteableFiles"})
public static void setFilePermissionsFromMode(File file, int mode) {
public static void setFilePermissionsFromMode(String name, int mode) {
int perms = FileUtils.S_IRUSR | FileUtils.S_IWUSR
| FileUtils.S_IRGRP | FileUtils.S_IWGRP;
if ((mode & Context.MODE_WORLD_READABLE) != 0) {
Expand All @@ -45,18 +47,22 @@ public static void setFilePermissionsFromMode(File file, int mode) {
if ((mode & Context.MODE_WORLD_WRITEABLE) != 0) {
perms |= FileUtils.S_IWOTH;
}
FileUtils.setPermissions(file.getAbsolutePath(), perms, -1, -1);
FileUtils.setPermissions(name, perms, -1, -1);
}

/**
* Get the configuration path
* The configuration may be deleted, but it can be simply recreated. In such cases,
* it will return a File with exists() = false.
*
* @param context Application context
* @param userId userId
* @return Path of the configuration
*/
public static File getConfigPath(Context context) {
return new File(context.getApplicationInfo().deviceProtectedDataDir + "/etc/module.conf");
public static File getConfigPath(int userId) {
if (userId < 0) {
// system user, try to load main user's conf
userId = 0;
}
return new File(String.format("/data/user_de/%s/%s/", userId, BuildConfig.APPLICATION_ID));
}
}

0 comments on commit c53183a

Please sign in to comment.