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 Feb 19, 2024
2 parents 795cde8 + 4347b6b commit 1fcedd5
Show file tree
Hide file tree
Showing 26 changed files with 801 additions and 391 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@ dependencies {
implementation("androidx.room:room-runtime:2.6.1")
ksp("androidx.room:room-compiler:2.6.1")

implementation("com.github.aitsuki:SwipeMenuRecyclerView:2.1.5")
implementation("androidx.recyclerview:recyclerview-selection:1.1.0")

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/close/hook/ads/data/model/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.close.hook.ads.data.model

data class Item(
var type: String,
var url: String
var url: String,
)
122 changes: 43 additions & 79 deletions app/src/main/java/com/close/hook/ads/hook/gc/network/RequestHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,13 @@ private static boolean shouldBlockDnsRequest(String host) {
if (host == null) {
return false;
}
boolean shouldBlock = false;
String blockType = null;
Pair<Boolean, String> pair = queryHostContentProvider(host);
if (pair.first) {
shouldBlock = true;
blockType = pair.second;
}
sendBroadcast(" DNS", shouldBlock, blockType, host);
return shouldBlock;
}

private static void setupHttpConnectionHook() {
try {
Class<?> httpURLConnectionImpl = Class.forName("com.android.okhttp.internal.huc.HttpURLConnectionImpl");
Pair<Boolean, String> pair = queryContentProvider("host", host);
boolean shouldBlock = pair.first;
String blockType = pair.second;

XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "getInputStream", httpConnectionHook);
XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "getOutputStream", httpConnectionHook);
} catch (Exception e) {
XposedBridge.log(LOG_PREFIX + "Error setting up HTTP connection hook: " + e.getMessage());
}
sendBroadcast(" DNS", shouldBlock, blockType, host);
return shouldBlock;
}

private static final XC_MethodHook httpConnectionHook = new XC_MethodHook() {
Expand All @@ -149,13 +136,11 @@ private static boolean shouldBlockHttpsRequest(URL url) {
return false;
}
String formattedUrl = formatUrlWithoutQuery(url);
boolean shouldBlock = false;
String blockType = null;
Pair<Boolean, String> pair = queryURLContentProvider(formattedUrl);
if (pair.first) {
shouldBlock = true;
blockType = pair.second;
}

Pair<Boolean, String> pair = queryContentProvider("url", formattedUrl);
boolean shouldBlock = pair.first;
String blockType = pair.second;

sendBroadcast(" HTTP", shouldBlock, blockType, formattedUrl);
return shouldBlock;
}
Expand All @@ -180,18 +165,16 @@ private static boolean shouldBlockOkHttpsRequest(Object chain) {
if (chain == null) {
return false;
}

Object request = XposedHelpers.callMethod(chain, "request");
Object httpUrl = XposedHelpers.callMethod(request, "url");
URL url = new URL(httpUrl.toString());
String formattedUrl = formatUrlWithoutQuery(url);
boolean shouldBlock = false;
String blockType = null;
Pair<Boolean, String> pair = queryURLContentProvider(formattedUrl);
if (pair.first) {
shouldBlock = true;
blockType = pair.second;
}

Pair<Boolean, String> pair = queryContentProvider("url", formattedUrl);
boolean shouldBlock = pair.first;
String blockType = pair.second;

sendBroadcast(" OKHTTP", shouldBlock, blockType, formattedUrl);
return shouldBlock;
} catch (Exception e) {
Expand All @@ -200,40 +183,21 @@ private static boolean shouldBlockOkHttpsRequest(Object chain) {
}
}

private static Pair<Boolean, String> queryHostContentProvider(String host) {
private static Pair<Boolean, String> queryContentProvider(String queryType, String queryValue) {
Context context = AndroidAppHelper.currentApplication();
if (context != null) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://" + UrlContentProvider.AUTHORITY + "/" + UrlContentProvider.URL_TABLE_NAME);

try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
if (cursor != null) {
int urlTypeIndex = cursor.getColumnIndex(Url.Companion.getURL_TYPE());
int urlValueIndex = cursor.getColumnIndex(Url.Companion.getURL_ADDRESS());

while (cursor.moveToNext()) {
String urlType = cursor.getString(urlTypeIndex);
String urlValue = cursor.getString(urlValueIndex);

if (urlType.equalsIgnoreCase("host") && Objects.equals(urlValue, host)) {
return new Pair<>(true, "Host");
} else if ((urlType.equalsIgnoreCase("url") || urlType.equalsIgnoreCase("keyword")) && host.contains(urlValue)) {
return new Pair<>(true, urlType.replace("url", "URL").replace("keyword", "KeyWord"));
}
}
}
String[] projection = new String[]{Url.Companion.getURL_TYPE(), Url.Companion.getURL_ADDRESS()};
String selection = null;
String[] selectionArgs = null;

if ("host".equals(queryType)) {
selection = Url.Companion.getURL_TYPE() + " = ?";
selectionArgs = new String[]{"host"};
}
}
return new Pair<>(false, null);
}

private static Pair<Boolean, String> queryURLContentProvider(String formattedUrl) {
Context context = AndroidAppHelper.currentApplication();
if (context != null) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://" + UrlContentProvider.AUTHORITY + "/" + UrlContentProvider.URL_TABLE_NAME);

try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
try (Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)) {
if (cursor != null) {
int urlTypeIndex = cursor.getColumnIndex(Url.Companion.getURL_TYPE());
int urlValueIndex = cursor.getColumnIndex(Url.Companion.getURL_ADDRESS());
Expand All @@ -242,12 +206,10 @@ private static Pair<Boolean, String> queryURLContentProvider(String formattedUrl
String urlType = cursor.getString(urlTypeIndex);
String urlValue = cursor.getString(urlValueIndex);

String host = extractHost(formattedUrl);

if ("host".equalsIgnoreCase(urlType) && Objects.equals(urlValue, host)) {
if ("host".equals(queryType) && Objects.equals(urlValue, queryValue)) {
return new Pair<>(true, "Host");
} else if (("url".equalsIgnoreCase(urlType) || "keyword".equalsIgnoreCase(urlType)) && formattedUrl.contains(urlValue)) {
return new Pair<>(true, urlType.replace("url", "URL").replace("keyword", "KeyWord"));
} else if (("url".equals(queryType) || "keyword".equals(queryType)) && queryValue.contains(urlValue)) {
return new Pair<>(true, formatUrlType(urlType));
}
}
}
Expand All @@ -256,13 +218,8 @@ private static Pair<Boolean, String> queryURLContentProvider(String formattedUrl
return new Pair<>(false, null);
}

private static String extractHost(String url) {
String host = url.replace("https://", "").replace("http://", "");
int indexOfSlash = host.indexOf('/');
if (indexOfSlash != -1) {
host = host.substring(0, indexOfSlash);
}
return host;
private static String formatUrlType(String urlType) {
return urlType.replace("url", "URL").replace("keyword", "KeyWord");
}

private static String formatUrlWithoutQuery(URL url) {
Expand All @@ -275,21 +232,28 @@ private static String formatUrlWithoutQuery(URL url) {
}
}

private static String extractHost(String url) {
String host = url.replace("https://", "").replace("http://", "");
int indexOfSlash = host.indexOf('/');
if (indexOfSlash != -1) {
host = host.substring(0, indexOfSlash);
}
return host;
}

private static void setupHttpRequestHook() {
try {
Class<?> httpURLConnectionImpl = Class.forName("com.android.okhttp.internal.huc.HttpURLConnectionImpl");
XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "execute", boolean.class, new XC_MethodHook() {

@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
setupHttpConnectionHook();
}

XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "execute", boolean.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
processHttpRequestAsync(param);
}
});

XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "getInputStream", httpConnectionHook);
XposedHelpers.findAndHookMethod(httpURLConnectionImpl, "getOutputStream", httpConnectionHook);
} catch (Exception e) {
XposedBridge.log(LOG_PREFIX + "Error setting up HTTP connection hook: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ class AboutActivity : AbsAboutActivity() {
items.add(License("glide", "bumptech", License.APACHE_2, "https://github.com/bumptech/glide"))
items.add(License("RxJava", "ReactiveX", License.APACHE_2, "https://github.com/ReactiveX/RxJava"))
items.add(License("RxJava", "RxAndroid", License.APACHE_2, "https://github.com/ReactiveX/RxAndroid"))
items.add(License("SwipeMenuRecyclerView", "aitsuki", License.MIT, "https://github.com/aitsuki/SwipeMenuRecyclerView"))

}
}
Loading

0 comments on commit 1fcedd5

Please sign in to comment.