Skip to content

Commit

Permalink
对脚本的运行增加了些许判断
Browse files Browse the repository at this point in the history
  • Loading branch information
RainCat committed Jun 15, 2022
1 parent 84543cf commit f0aa6e5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.raincat.dolby_beta"
minSdkVersion 21
targetSdkVersion 29
versionCode 351
versionName "3.5.1"
versionCode 352
versionName "3.5.2"

ndk {
abiFilters "arm64-v8a"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/raincat/dolby_beta/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (!SettingHelper.getInstance().getSetting(SettingHelper.master_key))
return;
//音源代理
new ProxyHook(context, versionCode, false);
new ProxyHook(context, false);
//黑胶
if (SettingHelper.getInstance().isEnable(SettingHelper.black_key)) {
new BlackHook(context, versionCode);
Expand Down Expand Up @@ -155,7 +155,7 @@ public void onReceive(Context c, Intent intent) {
}, intentFilter);
} else if (processName.equals(PACKAGE_NAME + ":play") && SettingHelper.getInstance().getSetting(SettingHelper.master_key)) {
//音源代理
new ProxyHook(context, versionCode, true);
new ProxyHook(context, true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(msg_hook_play_process);
context.registerReceiver(new BroadcastReceiver() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/raincat/dolby_beta/Hooklite.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (!SettingHelper.getInstance().getSetting(SettingHelper.master_key))
return;
//音源代理
new ProxyHook(context, versionCode, false);
new ProxyHook(context, false);
//黑胶
if (SettingHelper.getInstance().isEnable(SettingHelper.black_key)) {
new BlackHook(context, versionCode);
Expand Down Expand Up @@ -134,7 +134,7 @@ public void onReceive(Context c, Intent intent) {
}, intentFilter);
} else if (processName.equals(PACKAGE_NAME + ":play") && SettingHelper.getInstance().getSetting(SettingHelper.master_key)) {
//音源代理
new ProxyHook(context, versionCode, true);
new ProxyHook(context, true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(msg_hook_play_process);
context.registerReceiver(new BroadcastReceiver() {
Expand Down
44 changes: 26 additions & 18 deletions app/src/main/java/com/raincat/dolby_beta/helper/ScriptHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public class ScriptHelper {
//node路径
private static String nodeLibPath;

private static final String STOP_PROXY = "killall -9 libnode.so >/dev/null 2>&1";
private static final String[] STOP_PROXY = new String[]{"node=$(ps -ef |grep \"libnode.so app.js\" |grep -v grep)",
"if [ -n \"$node\" ]; then",
"killall -9 libnode.so >/dev/null 2>&1",
"fi"};

@SuppressLint("StaticFieldLeak")
private static Context neteaseContext;
Expand All @@ -68,31 +71,37 @@ public static void initScript(Context context, boolean cover) {
FileHelper.unzipFiles(getScriptPath(context) + "/UnblockNeteaseMusic.zip", getScriptPath(context));
}
Command auth = new Command(0, "cd " + getScriptPath(context), "chmod 0777 *");
Tools.shell(context, auth);
Tools.shell(auth);
ExtraHelper.setExtraDate(ExtraHelper.APP_VERSION, BuildConfig.VERSION_CODE);
}
if (TextUtils.isEmpty(nodeLibPath)) {
nodeLibPath = TextUtils.isEmpty(modulePath) ? "" : modulePath.substring(0, modulePath.lastIndexOf('/'));
nodeLibPath = "export PATH=$PATH:" + nodeLibPath + "/lib/arm64:" + modulePath + "!/lib/arm64-v8a:" + context.getApplicationInfo().nativeLibraryDir;
}
}

/**
* 采用代理模式执行UnblockNeteaseMusic
*/
public static void startHttpProxyMode(final Context context) {
stopScript(context);
stopScript();
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "1");
Tools.showToastOnLooper(context, "服务器代理运行成功");
}

public static void startScript(final Context context) {
if (TextUtils.isEmpty(nodeLibPath)) {
nodeLibPath = TextUtils.isEmpty(modulePath) ? "" : modulePath.substring(0, modulePath.lastIndexOf('/'));
nodeLibPath = "export PATH=$PATH:" + nodeLibPath + "/lib/arm64:" + modulePath + "!/lib/arm64-v8a:" + context.getApplicationInfo().nativeLibraryDir;
}

String START_PROXY = String.format("export ENABLE_FLAC=%s&&export MIN_BR=%s&&libnode.so app.js -a 127.0.0.1 -o %s -p %s",
public static void startScript() {
String script = String.format("export ENABLE_FLAC=%s&&export MIN_BR=%s&&libnode.so app.js -a 127.0.0.1 -o %s -p %s",
SettingHelper.getInstance().getSetting(SettingHelper.proxy_flac_key), SettingHelper.getInstance().getSetting(SettingHelper.proxy_priority_key) ? "256000" : "96000",
SettingHelper.getInstance().getProxyOriginal(), SettingHelper.getInstance().getProxyPort() + ":" + (SettingHelper.getInstance().getProxyPort() + 1));

Command start = new Command(0, STOP_PROXY, "cd " + getScriptPath(context), nodeLibPath + "&&" + START_PROXY) {
String[] START_PROXY = new String[]{"node=$(ps -ef |grep \"libnode.so app.js\" |grep -v grep)",
"if [ ! \"$node\" ]; then",
"cd " + scriptPath, nodeLibPath + "&&" + script,
"else",
"echo \"RESTART\"",
"killall -9 libnode.so >/dev/null 2>&1",
"fi"};
Command start = new Command(0, START_PROXY) {
@Override
public void commandOutput(int id, String line) {
if ((!line.contains("mERROR") && line.contains("Error:")) || line.contains("Port ") || line.contains("Please ")) {
Expand All @@ -105,19 +114,18 @@ public void commandOutput(int id, String line) {
if (neteaseContext != null && ExtraHelper.getExtraDate(ExtraHelper.SCRIPT_STATUS).equals("0"))
Tools.showToastOnLooper(neteaseContext, "UnblockNeteaseMusic运行成功");
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "1");
} else if (line.contains("Killed")) {
} else if (line.equals("Killed ")) {
startScript();
} else if (line.equals("RESTART")) {
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "0");
if (neteaseContext != null) {
startScript(neteaseContext);
}
}
}
};
Tools.shell(context, start);
Tools.shell(start);
}

public static void stopScript(final Context context) {
Tools.shell(context, new Command(0, STOP_PROXY));
public static void stopScript() {
Tools.shell(new Command(0, STOP_PROXY));
}

/**
Expand Down
17 changes: 5 additions & 12 deletions app/src/main/java/com/raincat/dolby_beta/hook/ProxyHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ProxyHook {

private final List<String> whiteUrlList = Arrays.asList("song/enhance/player/url", "song/enhance/download/url");

public ProxyHook(Context context, int versionCode, boolean isPlayProcess) {
public ProxyHook(Context context, boolean isPlayProcess) {
if (!SettingHelper.getInstance().isEnable(SettingHelper.proxy_master_key)) {
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "0");
return;
Expand Down Expand Up @@ -111,26 +111,19 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
});
}

if (isPlayProcess)
findAndHookMethod("com.netease.cloudmusic.service.PlayService", context.getClassLoader(), "onCreate", new XC_MethodHook() {
if (!isPlayProcess)
findAndHookMethod("com.netease.cloudmusic.activity.LoadingActivity", context.getClassLoader(), "onCreate", Bundle.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "0");
ScriptHelper.initScript(context, false);
if (SettingHelper.getInstance().getSetting(SettingHelper.proxy_server_key)) {
ScriptHelper.startHttpProxyMode(context);
} else {
ScriptHelper.startScript(context);
ScriptHelper.startScript();
}
}
});

if (!isPlayProcess)
findAndHookMethod("com.netease.cloudmusic.activity.LoadingActivity", context.getClassLoader(), "onCreate", Bundle.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
ExtraHelper.setExtraDate(ExtraHelper.SCRIPT_STATUS, "0");
}
});
}

/**
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/raincat/dolby_beta/utils/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ public static long getTodayStartTime() {
/**
* ADB命令
*/
public static void shell(Context context, Command command) {
public static void shell(Command command) {
try {
RootTools.closeAllShells();
RootTools.getShell(false).add(command);
} catch (TimeoutException | RootDeniedException | IOException e) {
e.printStackTrace();
showToastOnLooper(context, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public void init(Context context, AttributeSet attrs) {

setOnClickListener(view -> {
ScriptHelper.initScript(context, true);
ScriptHelper.stopScript(context);
Tools.showToastOnLooper(context, "操作成功,脚本即将重新启动");
if (SettingHelper.getInstance().getSetting(SettingHelper.proxy_master_key)
&& !SettingHelper.getInstance().getSetting(SettingHelper.proxy_server_key)) {
ScriptHelper.startScript();
Tools.showToastOnLooper(context, "操作成功,脚本即将重新启动");
} else {
Tools.showToastOnLooper(context, "操作成功");
}
});
}
}

0 comments on commit f0aa6e5

Please sign in to comment.