131 lines
5.0 KiB
Java
Raw Normal View History

2016-08-23 05:18:28 +08:00
package com.topjohnwu.magisk.utils;
2016-08-23 03:50:46 +08:00
2016-08-29 06:35:07 +08:00
import android.Manifest;
import android.app.DownloadManager;
2016-08-28 03:52:03 +08:00
import android.content.Context;
2016-08-29 06:35:07 +08:00
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
2016-09-08 15:47:10 -05:00
import android.util.Base64;
2016-08-28 03:52:03 +08:00
import android.widget.Toast;
import com.topjohnwu.magisk.R;
2016-09-26 10:45:34 +08:00
import com.topjohnwu.magisk.receivers.DownloadReceiver;
2016-08-28 03:52:03 +08:00
2016-08-29 06:35:07 +08:00
import java.io.File;
2016-09-08 15:47:10 -05:00
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
2016-08-22 10:09:36 +02:00
import java.util.List;
2016-08-20 17:26:49 +02:00
2016-09-08 15:47:10 -05:00
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
2016-08-20 17:26:49 +02:00
public class Utils {
private static final String cryptoPass = "MagiskRox666";
private static final String secret = "GTYybRBTYf5his9kQ16ZNO7qgkBJ/5MyVe4CGceAOIoXgSnnk8FTd4F1dE9p5Eus";
public static boolean itemExist(String path) {
2016-09-30 10:52:04 +08:00
return itemExist(true, path);
}
public static boolean itemExist(boolean root, String path) {
String command = "if [ -e " + path + " ]; then echo true; else echo false; fi";
2016-09-30 10:52:04 +08:00
if (Shell.rootAccess() && root) {
return Boolean.parseBoolean(Shell.su(command).get(0));
} else {
2016-09-30 10:52:04 +08:00
return new File(path).exists();
}
}
public static boolean commandExists(String s) {
List<String> ret;
String command = "if [ -z $(which " + s + ") ]; then echo false; else echo true; fi";
ret = Shell.sh(command);
2016-09-18 02:32:08 +08:00
return Boolean.parseBoolean(ret.get(0));
}
public static boolean createFile(String path) {
2016-09-18 02:32:08 +08:00
String command = "touch " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo true; else echo false; fi";
2016-09-28 14:50:26 +08:00
return Shell.rootAccess() && Boolean.parseBoolean(Shell.su(command).get(0));
}
2016-08-20 17:26:49 +02:00
2016-10-17 10:11:26 +08:00
public static boolean removeItem(String path) {
String command = "rm -rf " + path + " 2>/dev/null; if [ -e " + path + " ]; then echo false; else echo true; fi";
2016-09-28 14:50:26 +08:00
return Shell.rootAccess() && Boolean.parseBoolean(Shell.su(command).get(0));
2016-09-18 02:32:08 +08:00
}
static List<String> getModList(String path) {
List<String> ret;
2016-09-21 09:22:36 -05:00
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
if (Shell.rootAccess()) {
ret = Shell.su(command);
} else {
ret = Shell.sh(command);
}
return ret;
2016-08-20 17:26:49 +02:00
}
public static List<String> readFile(String path) {
List<String> ret;
2016-09-21 09:22:36 -05:00
String command = "cat " + path;
if (Shell.rootAccess()) {
ret = Shell.su(command);
} else {
ret = Shell.sh(command);
}
return ret;
2016-08-23 01:44:34 +08:00
}
2016-09-28 14:50:26 +08:00
public static void downloadAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
2016-08-29 06:35:07 +08:00
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
return;
}
2016-09-28 14:50:26 +08:00
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || (file.exists() && !file.delete())) {
2016-09-29 23:24:31 +08:00
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
}
2016-08-29 06:35:07 +08:00
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
2016-09-28 14:50:26 +08:00
request.setDestinationUri(Uri.fromFile(file));
2016-08-29 06:35:07 +08:00
receiver.setDownloadID(downloadManager.enqueue(request));
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public static String getToken() {
2016-09-18 02:32:08 +08:00
try {
DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decode(secret, Base64.DEFAULT);
2016-09-18 02:32:08 +08:00
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
return new String(decrypedValueBytes);
} catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
| BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException
| InvalidKeySpecException e) {
e.printStackTrace();
}
return secret;
2016-09-18 02:32:08 +08:00
}
2016-09-21 09:22:36 -05:00
}