From 9d9514a091dd76ac6a925e68feaa9b3ae754c6e5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 30 Apr 2021 10:54:36 +0200 Subject: [PATCH] Add User-Agent to all HTTP calls --- build.gradle | 11 ++++++---- .../java/eu/siacs/conversations/Config.java | 3 ++- .../generator/AbstractGenerator.java | 13 +++--------- .../http/HttpConnectionManager.java | 21 +++++++++++++++++-- .../services/ChannelDiscoveryService.java | 2 +- .../conversations/ui/AboutPreference.java | 14 +++++++------ .../conversations/utils/PhoneHelper.java | 13 ------------ .../services/QuickConversationsService.java | 3 ++- 8 files changed, 42 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 1ce027f3d..9c08550ba 100644 --- a/build.gradle +++ b/build.gradle @@ -98,8 +98,9 @@ android { archivesBaseName += "-$versionName" applicationId "eu.siacs.conversations" resValue "string", "applicationId", applicationId - resValue "string", "app_name", "Conversations" - buildConfigField "String", "LOGTAG", "\"conversations\"" + def appName = "Conversations" + resValue "string", "app_name", appName + buildConfigField "String", "APP_NAME", "\"$appName\""; } splits { @@ -135,9 +136,11 @@ android { quicksy { dimension "mode" applicationId = "im.quicksy.client" - resValue "string", "app_name", "Quicksy" resValue "string", "applicationId", applicationId - buildConfigField "String", "LOGTAG", "\"quicksy\"" + + def appName = "Quicksy" + resValue "string", "app_name", appName + buildConfigField "String", "APP_NAME", "\"$appName\""; } conversations { diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index 4ee4c1fb8..0d9b4270f 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -6,6 +6,7 @@ import android.net.Uri; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; import eu.siacs.conversations.crypto.XmppDomainVerifier; import eu.siacs.conversations.xmpp.Jid; @@ -35,7 +36,7 @@ public final class Config { return (ENCRYPTION_MASK & (ENCRYPTION_MASK - 1)) != 0; } - public static final String LOGTAG = BuildConfig.LOGTAG; + public static final String LOGTAG = BuildConfig.APP_NAME.toLowerCase(Locale.US); public static final Jid BUG_REPORTS = Jid.of("bugs@conversations.im"); public static final Uri HELP = Uri.parse("https://help.conversations.im"); diff --git a/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java b/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java index 7a3ce765d..706b50043 100644 --- a/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Locale; import java.util.TimeZone; +import eu.siacs.conversations.BuildConfig; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.axolotl.AxolotlService; @@ -65,7 +66,6 @@ public abstract class AbstractGenerator { Namespace.JINGLE_MESSAGE }; protected XmppConnectionService mXmppConnectionService; - private String mVersion = null; AbstractGenerator(XmppConnectionService service) { this.mXmppConnectionService = service; @@ -77,18 +77,11 @@ public abstract class AbstractGenerator { } String getIdentityVersion() { - if (mVersion == null) { - this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService); - } - return this.mVersion; + return BuildConfig.VERSION_NAME; } String getIdentityName() { - return mXmppConnectionService.getString(R.string.app_name); - } - - public String getUserAgent() { - return mXmppConnectionService.getString(R.string.app_name) + '/' + getIdentityVersion(); + return BuildConfig.APP_NAME; } String getIdentityType() { diff --git a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java index 7ee1da466..566ce1e6a 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java @@ -19,10 +19,10 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.X509TrustManager; +import eu.siacs.conversations.BuildConfig; import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Message; @@ -41,7 +41,24 @@ public class HttpConnectionManager extends AbstractConnectionManager { public static final Executor EXECUTOR = Executors.newFixedThreadPool(4); - private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); + public static final OkHttpClient OK_HTTP_CLIENT; + + static { + OK_HTTP_CLIENT = new OkHttpClient.Builder() + .addInterceptor(chain -> { + final Request original = chain.request(); + final Request modified = original.newBuilder() + .header("User-Agent", getUserAgent()) + .build(); + return chain.proceed(modified); + }) + .build(); + } + + + public static String getUserAgent() { + return String.format("%s/%s", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME); + } public HttpConnectionManager(XmppConnectionService service) { super(service); diff --git a/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java b/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java index 3d33eefa5..c46568c3e 100644 --- a/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java +++ b/src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java @@ -50,7 +50,7 @@ public class ChannelDiscoveryService { } void initializeMuclumbusService() { - final OkHttpClient.Builder builder = new OkHttpClient.Builder(); + final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder(); if (service.useTorToConnect()) { builder.proxy(HttpConnectionManager.getProxy()); } diff --git a/src/main/java/eu/siacs/conversations/ui/AboutPreference.java b/src/main/java/eu/siacs/conversations/ui/AboutPreference.java index 455058d01..f2cd1e348 100644 --- a/src/main/java/eu/siacs/conversations/ui/AboutPreference.java +++ b/src/main/java/eu/siacs/conversations/ui/AboutPreference.java @@ -5,24 +5,26 @@ import android.content.Intent; import android.preference.Preference; import android.util.AttributeSet; +import eu.siacs.conversations.BuildConfig; import eu.siacs.conversations.R; import eu.siacs.conversations.utils.PhoneHelper; public class AboutPreference extends Preference { public AboutPreference(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); - final String appName = context.getString(R.string.app_name); - setSummary(appName +' '+ PhoneHelper.getVersionName(context)); - setTitle(context.getString(R.string.title_activity_about_x, appName)); + setSummaryAndTitle(context); } public AboutPreference(final Context context, final AttributeSet attrs) { super(context, attrs); - final String appName = context.getString(R.string.app_name); - setSummary(appName +' '+ PhoneHelper.getVersionName(context)); - setTitle(context.getString(R.string.title_activity_about_x, appName)); + setSummaryAndTitle(context); } + private void setSummaryAndTitle(final Context context) { + setSummary(String.format("%s %s", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME)); + setTitle(context.getString(R.string.title_activity_about_x, BuildConfig.APP_NAME)); + } + @Override protected void onClick() { super.onClick(); diff --git a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java index 0238dc975..a894cab67 100644 --- a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java @@ -35,17 +35,4 @@ public class PhoneHelper { cursor.close(); return uri == null ? null : Uri.parse(uri); } - - public static String getVersionName(Context context) { - final String packageName = context == null ? null : context.getPackageName(); - if (packageName != null) { - try { - return context.getPackageManager().getPackageInfo(packageName, 0).versionName; - } catch (final PackageManager.NameNotFoundException | RuntimeException e) { - return "unknown"; - } - } else { - return "unknown"; - } - } } diff --git a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java index 05b076424..f977758f2 100644 --- a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java @@ -48,6 +48,7 @@ import eu.siacs.conversations.crypto.sasl.Plain; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Entry; +import eu.siacs.conversations.http.HttpConnectionManager; import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; @@ -260,7 +261,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService } private void setHeader(HttpURLConnection connection) { - connection.setRequestProperty("User-Agent", service.getIqGenerator().getUserAgent()); + connection.setRequestProperty("User-Agent", HttpConnectionManager.getUserAgent()); connection.setRequestProperty("Installation-Id", getInstallationId()); connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage()); }