Skip to content

Commit

Permalink
Merge branch 'zjyzip:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wfvw126 authored Jul 23, 2024
2 parents 37728cc + 982cdea commit 53c6a95
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 201 deletions.
13 changes: 1 addition & 12 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,7 @@ android {
}
}

sourceSets {
getByName("main") {
java.srcDirs("src/main/java", "src/main/aidl")
aidl.srcDirs("src/main/aidl")
assets.srcDirs("src/main/assets")
res.srcDirs("src/main/res")
}
}

buildFeatures {
aidl = true
viewBinding = true
buildConfig = true
}
Expand All @@ -137,7 +127,7 @@ configurations.configureEach {

dependencies {
compileOnly("de.robv.android.xposed:api:82")
implementation("org.luckypray:dexkit:2.0.0")
implementation("org.luckypray:dexkit:2.0.2")

implementation("androidx.appcompat:appcompat:1.6.1")
implementation("androidx.preference:preference-ktx:1.2.1")
Expand All @@ -149,7 +139,6 @@ dependencies {
runtimeOnly("androidx.lifecycle:lifecycle-livedata-ktx:2.7.0")
implementation("androidx.fragment:fragment-ktx:1.6.2")


implementation("com.google.android.material:material:1.10.0")
implementation("com.drakeet.about:about:2.5.2")
implementation("me.zhanghai.android.fastscroll:library:1.3.0")
Expand Down
1 change: 1 addition & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keep class okhttp3.** { *; }
-keep class com.aitsuki.swipe.** { *; }
-keep class com.close.hook.ads.** { *; }
22 changes: 7 additions & 15 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />

<permission
android:name="com.close.hook.ads.READ_WRITE"
android:name="com.close.hook.ads.provider.READ_WRITE"
android:protectionLevel="normal" />
<permission
android:name="com.close.hook.ads.WRITE_DATABASE"
android:name="com.close.hook.ads.provider.WRITE_DATABASE"
android:protectionLevel="normal" />

<application
Expand All @@ -27,15 +23,11 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<service
android:name=".service.AidlService"
<provider
android:name=".provider.UrlContentProvider"
android:authorities="com.close.hook.ads.provider"
android:enabled="true"
android:exported="true"
android:foregroundServiceType="dataSync">
<intent-filter>
<action android:name="com.close.hook.ads.service.AidlService" />
</intent-filter>
</service>
android:exported="true" />

<activity
android:name=".ui.activity.RequestInfoActivity"
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/aidl/com/close/hook/ads/BlockedBean.aidl

This file was deleted.

30 changes: 0 additions & 30 deletions app/src/main/aidl/com/close/hook/ads/BlockedBean.kt

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions app/src/main/java/com/close/hook/ads/data/DataSource.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.close.hook.ads.data

import android.content.Context
import com.close.hook.ads.BlockedBean
import com.close.hook.ads.data.database.UrlDatabase
import com.close.hook.ads.data.model.Url
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -63,15 +62,6 @@ class DataSource(context: Context) {
return urlDao.isExist(type, url)
}

fun checkIsBlocked(type: String, url: String): BlockedBean {
val urlEntry = urlDao.findMatchingUrl(type, url)
return if (urlEntry != null) {
BlockedBean(true, urlEntry.type, urlEntry.url)
} else {
BlockedBean(false, null, null)
}
}

companion object {
@Volatile
private var INSTANCE: DataSource? = null
Expand Down
101 changes: 38 additions & 63 deletions app/src/main/java/com/close/hook/ads/hook/gc/network/RequestHook.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.close.hook.ads.hook.gc.network;

import android.app.AndroidAppHelper;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.Nullable;

import com.close.hook.ads.IBlockedStatusProvider;
import com.close.hook.ads.BlockedBean;
import com.close.hook.ads.data.model.BlockedRequest;
import com.close.hook.ads.data.model.RequestDetails;
import com.close.hook.ads.data.model.Url;
import com.close.hook.ads.hook.util.HookUtil;
import com.close.hook.ads.hook.util.DexKitUtil;
import com.close.hook.ads.hook.util.StringFinderKit;

import java.io.IOException;
import com.close.hook.ads.provider.UrlContentProvider;

import org.luckypray.dexkit.result.MethodData;

Expand All @@ -31,22 +29,14 @@
import java.net.URL;
import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

import kotlin.Triple;

import android.os.ParcelFileDescriptor;

import java.io.ObjectInputStream;
import java.io.FileInputStream;

public class RequestHook {
private static final String LOG_PREFIX = "[RequestHook] ";

private static IBlockedStatusProvider mStub;

public static void init() {
try {
setupDNSRequestHook();
Expand Down Expand Up @@ -123,62 +113,43 @@ private static String formatUrlWithoutQuery(URL url) {
private static Triple<Boolean, String, String> queryContentProvider(String queryType, String queryValue) {
Context context = AndroidAppHelper.currentApplication();
if (context != null) {

bindService(context);

try {
if (mStub != null) {
ParcelFileDescriptor pfd = mStub.getData(
queryType.replace("host", "Domain").replace("url", "URL"),
queryValue
);
if (pfd != null) {
try (FileInputStream fis = new FileInputStream(pfd.getFileDescriptor());
ObjectInputStream ois = new ObjectInputStream(fis)) {

BlockedBean blockedBean = (BlockedBean) ois.readObject();
if (blockedBean.isBlocked()) {
return new Triple<>(true, blockedBean.getType(), blockedBean.getValue());
}
} catch (Exception e) {
e.printStackTrace();
Log.i(LOG_PREFIX, "Error reading data from memory file: " + e.getMessage());
} finally {
try {
pfd.close();
} catch (IOException e) {
Log.i(LOG_PREFIX, "Error closing ParcelFileDescriptor: " + e.getMessage());
}
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://" + UrlContentProvider.AUTHORITY + "/" + UrlContentProvider.URL_TABLE_NAME);
String[] projection = new String[]{Url.Companion.getURL_TYPE(), Url.Companion.getURL_ADDRESS()};
String selection = Url.Companion.getURL_TYPE() + " = ?";
String[] selectionArgs = new String[]{queryType};

try (Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
do {
String urlType = cursor.getString(cursor.getColumnIndex(Url.Companion.getURL_TYPE()));
String urlValue = cursor.getString(cursor.getColumnIndex(Url.Companion.getURL_ADDRESS()));

if (isQueryMatch(queryType, queryValue, urlType, urlValue)) {
return new Triple<>(true, formatUrlType(urlType), urlValue);
}
}
} while (cursor.moveToNext());
}
} catch (RemoteException e) {
}

}
return new Triple<>(false, null, null);
}

private static void bindService(Context context) {
if (mStub != null) {
return;
private static boolean isQueryMatch(String queryType, String queryValue, String urlType, String urlValue) {
switch (queryType) {
case "host":
return urlValue.equals(queryValue);
case "url":
case "keyword":
return queryValue.contains(urlValue);
default:
return false;
}
Intent intent = new Intent();
intent.setClassName("com.close.hook.ads", "com.close.hook.ads.service.AidlService");
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

private static final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mStub = IBlockedStatusProvider.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
mStub = null;
}
};
private static String formatUrlType(String urlType) {
return urlType.replace("url", "URL").replace("keyword", "KeyWord");
}

private static void setupHttpRequestHook() {
try {
Expand Down Expand Up @@ -298,11 +269,15 @@ private static Object createEmptyResponseForOkHttp(Object response) throws Excep
okhttp3.Response originalResponse = (okhttp3.Response) response;
okhttp3.Request request = originalResponse.request();

okhttp3.MediaType JSON = okhttp3.MediaType.get("application/json; charset=utf-8");
okhttp3.ResponseBody emptyBody = okhttp3.ResponseBody.create("{}", JSON);

return new okhttp3.Response.Builder()
.request(request)
.protocol(okhttp3.Protocol.HTTP_1_1)
.code(204) // 204 No Content
.message("No Content")
.code(200) // 200 OK
.message("OK")
.body(emptyBody)
.build();
}
return null;
Expand Down
Loading

0 comments on commit 53c6a95

Please sign in to comment.