Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 6e5b53b

Browse files
committed
Fix Cloudflare captcha always failing in app (Fix #211)
1 parent bf92f58 commit 6e5b53b

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class AndroidacyActivity extends FoxActivity {
6161

6262
@SuppressWarnings("deprecation")
6363
@Override
64-
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
64+
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface", "RestrictedApi"})
6565
protected void onCreate(@Nullable Bundle savedInstanceState) {
6666
super.onCreate(savedInstanceState);
6767
Intent intent = this.getIntent();
@@ -126,6 +126,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
126126
webSettings.setDomStorageEnabled(true);
127127
webSettings.setJavaScriptEnabled(true);
128128
webSettings.setAllowFileAccess(false);
129+
// Attempt at fixing CloudFlare captcha.
130+
if (WebViewFeature.isFeatureSupported(WebViewFeature.REQUESTED_WITH_HEADER_CONTROL)) {
131+
WebSettingsCompat.setRequestedWithHeaderMode(
132+
webSettings, WebSettingsCompat.REQUESTED_WITH_HEADER_MODE_NO_HEADER);
133+
}
129134
// If API level is .= 33, allow setAlgorithmicDarkeningAllowed
130135
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
131136
try {

app/src/main/java/com/fox2code/mmm/utils/Http.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.system.Os;
88
import android.util.Log;
99
import android.webkit.CookieManager;
10+
import android.webkit.WebSettings;
1011

1112
import androidx.annotation.NonNull;
1213
import androidx.annotation.Nullable;
@@ -77,6 +78,16 @@ public class Http {
7778
}
7879
throw error;
7980
}
81+
CookieManager cookieManager;
82+
try {
83+
cookieManager = CookieManager.getInstance();
84+
cookieManager.setAcceptCookie(true);
85+
cookieManager.flush(); // Make sure the instance work
86+
} catch (Throwable t) {
87+
cookieManager = null;
88+
Log.e(TAG, "No WebView support!", t);
89+
}
90+
hasWebView = cookieManager != null;
8091
OkHttpClient.Builder httpclientBuilder = new OkHttpClient.Builder();
8192
// Default is 10, extend it a bit for slow mobile connections.
8293
httpclientBuilder.connectTimeout(15, TimeUnit.SECONDS);
@@ -112,10 +123,15 @@ public class Http {
112123
Log.e(TAG, "Failed to init DoH", e);
113124
}
114125
httpclientBuilder.cookieJar(CookieJar.NO_COOKIES);
115-
androidacyUA = // User-Agent format was agreed on telegram
116-
"Mozilla/5.0 (Linux; Android " + Build.VERSION.RELEASE + "; " + Build.DEVICE +")" +
117-
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36"
118-
+ " FoxMmm/" + BuildConfig.VERSION_CODE;
126+
// User-Agent format was agreed on telegram
127+
if (hasWebView) {
128+
androidacyUA = WebSettings.getDefaultUserAgent(mainApplication).replace("; wv", "")
129+
+ " FoxMmm/" + BuildConfig.VERSION_CODE;
130+
} else {
131+
androidacyUA = "Mozilla/5.0 (Linux; Android " + Build.VERSION.RELEASE + "; " + Build.DEVICE +")" +
132+
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36"
133+
+ " FoxMmm/" + BuildConfig.VERSION_CODE;
134+
}
119135
httpclientBuilder.addInterceptor(chain -> {
120136
Request.Builder request = chain.request().newBuilder();
121137
request.header("Upgrade-Insecure-Requests", "1");
@@ -141,16 +157,6 @@ public class Http {
141157
"camo.githubusercontent.com", "user-images.githubusercontent.com",
142158
"cdn.jsdelivr.net", "img.shields.io", "magisk-modules-repo.github.io",
143159
"www.androidacy.com", "api.androidacy.com");
144-
CookieManager cookieManager;
145-
try {
146-
cookieManager = CookieManager.getInstance();
147-
cookieManager.setAcceptCookie(true);
148-
cookieManager.flush(); // Make sure the instance work
149-
} catch (Throwable t) {
150-
cookieManager = null;
151-
Log.e(TAG, "No WebView support!", t);
152-
}
153-
hasWebView = cookieManager != null;
154160
httpclientBuilder.cookieJar(cookieJar = new CDNCookieJar(cookieManager));
155161
httpclientBuilder.dns(Dns.SYSTEM);
156162
httpClient = followRedirects(httpclientBuilder, true).build();

0 commit comments

Comments
 (0)