From ea0483a842065e004dca9c072cbba35d7253503e Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 4 Jun 2022 11:30:55 +0200 Subject: [PATCH] Added support for User-Agent Client Hints --- .../main/java/eu/faircode/email/ActivityView.java | 2 +- app/src/main/java/eu/faircode/email/Bimi.java | 4 ++-- .../java/eu/faircode/email/ConnectionHelper.java | 14 ++++++++++++++ .../main/java/eu/faircode/email/ContactInfo.java | 10 +++++----- app/src/main/java/eu/faircode/email/DeepL.java | 4 ++-- .../eu/faircode/email/DisconnectBlacklist.java | 2 +- .../main/java/eu/faircode/email/EmailProvider.java | 2 +- .../faircode/email/FragmentDialogInsertLink.java | 2 +- .../main/java/eu/faircode/email/FragmentOAuth.java | 2 +- app/src/main/java/eu/faircode/email/Helper.java | 2 +- app/src/main/java/eu/faircode/email/IPInfo.java | 2 +- .../main/java/eu/faircode/email/LanguageTool.java | 2 +- 12 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 4715b3ee9c..f279af21d8 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -1510,7 +1510,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB urlConnection.setReadTimeout(UPDATE_TIMEOUT); urlConnection.setConnectTimeout(UPDATE_TIMEOUT); urlConnection.setDoOutput(false); - urlConnection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, urlConnection); urlConnection.connect(); int status = urlConnection.getResponseCode(); diff --git a/app/src/main/java/eu/faircode/email/Bimi.java b/app/src/main/java/eu/faircode/email/Bimi.java index 88ed15d688..a7590cc46a 100644 --- a/app/src/main/java/eu/faircode/email/Bimi.java +++ b/app/src/main/java/eu/faircode/email/Bimi.java @@ -136,7 +136,7 @@ public class Bimi { connection.setReadTimeout(READ_TIMEOUT); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setInstanceFollowRedirects(true); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { @@ -174,7 +174,7 @@ public class Bimi { connection.setReadTimeout(READ_TIMEOUT); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setInstanceFollowRedirects(true); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); // Fetch PEM objects diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 016811c07a..5b88847f6e 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -37,6 +37,7 @@ import com.sun.mail.iap.ConnectionException; import com.sun.mail.util.FolderClosedIOException; import java.io.IOException; +import java.net.HttpURLConnection; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; @@ -623,4 +624,17 @@ public class ConnectionHelper { } return result; } + + static void setUserAgent(Context context, HttpURLConnection connection) { + connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + + // https://web.dev/migrate-to-ua-ch/ + + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA + connection.setRequestProperty("Sec-CH-UA", "\"Chromium\""); + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile + connection.setRequestProperty("Sec-CH-UA-Mobile", "?1"); + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Platform + connection.setRequestProperty("Sec-CH-UA-Platform", "Android"); + } } diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index eb60d207a6..2c1e015b63 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -571,7 +571,7 @@ public class ContactInfo { return true; } }); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); Document doc; @@ -627,7 +627,7 @@ public class ContactInfo { m.setReadTimeout(FAVICON_READ_TIMEOUT); m.setConnectTimeout(FAVICON_CONNECT_TIMEOUT); m.setInstanceFollowRedirects(true); - m.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, m); m.connect(); try { @@ -859,7 +859,7 @@ public class ContactInfo { return true; } }); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { @@ -888,7 +888,7 @@ public class ContactInfo { urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(GRAVATAR_READ_TIMEOUT); urlConnection.setConnectTimeout(GRAVATAR_CONNECT_TIMEOUT); - urlConnection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, urlConnection); urlConnection.connect(); try { @@ -934,7 +934,7 @@ public class ContactInfo { urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(LIBRAVATAR_READ_TIMEOUT); urlConnection.setConnectTimeout(LIBRAVATAR_CONNECT_TIMEOUT); - urlConnection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, urlConnection); urlConnection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/DeepL.java b/app/src/main/java/eu/faircode/email/DeepL.java index 13d2682799..380777a17c 100644 --- a/app/src/main/java/eu/faircode/email/DeepL.java +++ b/app/src/main/java/eu/faircode/email/DeepL.java @@ -254,7 +254,7 @@ public class DeepL { connection.setDoOutput(true); connection.setReadTimeout(DEEPL_TIMEOUT * 1000); connection.setConnectTimeout(DEEPL_TIMEOUT * 1000); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Content-Length", Integer.toString(request.length())); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -315,7 +315,7 @@ public class DeepL { HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setReadTimeout(DEEPL_TIMEOUT * 1000); connection.setConnectTimeout(DEEPL_TIMEOUT * 1000); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/DisconnectBlacklist.java b/app/src/main/java/eu/faircode/email/DisconnectBlacklist.java index 1a88a614b2..36d78a0bdf 100644 --- a/app/src/main/java/eu/faircode/email/DisconnectBlacklist.java +++ b/app/src/main/java/eu/faircode/email/DisconnectBlacklist.java @@ -117,7 +117,7 @@ public class DisconnectBlacklist { connection.setRequestMethod("GET"); connection.setReadTimeout(FETCH_TIMEOUT); connection.setConnectTimeout(FETCH_TIMEOUT); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index fcf85a181e..6983bf789c 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -597,7 +597,7 @@ public class EmailProvider implements Parcelable { request.setReadTimeout(ISPDB_TIMEOUT); request.setConnectTimeout(ISPDB_TIMEOUT); request.setDoInput(true); - request.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, request); request.connect(); int status = request.getResponseCode(); diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java index 51dab47792..e1c8722ee9 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java @@ -133,7 +133,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase { connection.setReadTimeout(METADATA_READ_TIMEOUT); connection.setConnectTimeout(METADATA_CONNECT_TIMEOUT); connection.setInstanceFollowRedirects(true); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/FragmentOAuth.java b/app/src/main/java/eu/faircode/email/FragmentOAuth.java index fae7e2f07a..cf8e52d497 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOAuth.java +++ b/app/src/main/java/eu/faircode/email/FragmentOAuth.java @@ -694,7 +694,7 @@ public class FragmentOAuth extends FragmentBase { connection.setRequestMethod("GET"); connection.setReadTimeout(MAILRU_TIMEOUT); connection.setConnectTimeout(MAILRU_TIMEOUT); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 6c96072736..dd78c92f1d 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -2063,7 +2063,7 @@ public class Helper { urlConnection.setReadTimeout(timeout); urlConnection.setConnectTimeout(timeout); urlConnection.setInstanceFollowRedirects(true); - urlConnection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, urlConnection); urlConnection.connect(); try { diff --git a/app/src/main/java/eu/faircode/email/IPInfo.java b/app/src/main/java/eu/faircode/email/IPInfo.java index db8193db3c..cb1a2e1eda 100644 --- a/app/src/main/java/eu/faircode/email/IPInfo.java +++ b/app/src/main/java/eu/faircode/email/IPInfo.java @@ -75,7 +75,7 @@ public class IPInfo { connection.setRequestMethod("GET"); connection.setReadTimeout(FETCH_TIMEOUT); connection.setConnectTimeout(FETCH_TIMEOUT); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.connect(); Organization organization = new Organization(); diff --git a/app/src/main/java/eu/faircode/email/LanguageTool.java b/app/src/main/java/eu/faircode/email/LanguageTool.java index 8414f8c129..a841d8b099 100644 --- a/app/src/main/java/eu/faircode/email/LanguageTool.java +++ b/app/src/main/java/eu/faircode/email/LanguageTool.java @@ -75,7 +75,7 @@ public class LanguageTool { connection.setDoOutput(true); connection.setReadTimeout(LT_TIMEOUT * 1000); connection.setConnectTimeout(LT_TIMEOUT * 1000); - connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context)); + ConnectionHelper.setUserAgent(context, connection); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Length", Integer.toString(request.length())); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");