Add User-Agent to all HTTP calls

This commit is contained in:
Daniel Gultsch 2021-04-30 10:54:36 +02:00
parent bc58fb0fbd
commit 9d9514a091
8 changed files with 42 additions and 38 deletions

View file

@ -98,8 +98,9 @@ android {
archivesBaseName += "-$versionName" archivesBaseName += "-$versionName"
applicationId "eu.siacs.conversations" applicationId "eu.siacs.conversations"
resValue "string", "applicationId", applicationId resValue "string", "applicationId", applicationId
resValue "string", "app_name", "Conversations" def appName = "Conversations"
buildConfigField "String", "LOGTAG", "\"conversations\"" resValue "string", "app_name", appName
buildConfigField "String", "APP_NAME", "\"$appName\"";
} }
splits { splits {
@ -135,9 +136,11 @@ android {
quicksy { quicksy {
dimension "mode" dimension "mode"
applicationId = "im.quicksy.client" applicationId = "im.quicksy.client"
resValue "string", "app_name", "Quicksy"
resValue "string", "applicationId", applicationId resValue "string", "applicationId", applicationId
buildConfigField "String", "LOGTAG", "\"quicksy\""
def appName = "Quicksy"
resValue "string", "app_name", appName
buildConfigField "String", "APP_NAME", "\"$appName\"";
} }
conversations { conversations {

View file

@ -6,6 +6,7 @@ import android.net.Uri;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import eu.siacs.conversations.crypto.XmppDomainVerifier; import eu.siacs.conversations.crypto.XmppDomainVerifier;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
@ -35,7 +36,7 @@ public final class Config {
return (ENCRYPTION_MASK & (ENCRYPTION_MASK - 1)) != 0; 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 Jid BUG_REPORTS = Jid.of("bugs@conversations.im");
public static final Uri HELP = Uri.parse("https://help.conversations.im"); public static final Uri HELP = Uri.parse("https://help.conversations.im");

View file

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.crypto.axolotl.AxolotlService;
@ -65,7 +66,6 @@ public abstract class AbstractGenerator {
Namespace.JINGLE_MESSAGE Namespace.JINGLE_MESSAGE
}; };
protected XmppConnectionService mXmppConnectionService; protected XmppConnectionService mXmppConnectionService;
private String mVersion = null;
AbstractGenerator(XmppConnectionService service) { AbstractGenerator(XmppConnectionService service) {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
@ -77,18 +77,11 @@ public abstract class AbstractGenerator {
} }
String getIdentityVersion() { String getIdentityVersion() {
if (mVersion == null) { return BuildConfig.VERSION_NAME;
this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService);
}
return this.mVersion;
} }
String getIdentityName() { String getIdentityName() {
return mXmppConnectionService.getString(R.string.app_name); return BuildConfig.APP_NAME;
}
public String getUserAgent() {
return mXmppConnectionService.getString(R.string.app_name) + '/' + getIdentityVersion();
} }
String getIdentityType() { String getIdentityType() {

View file

@ -19,10 +19,10 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -41,7 +41,24 @@ public class HttpConnectionManager extends AbstractConnectionManager {
public static final Executor EXECUTOR = Executors.newFixedThreadPool(4); 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) { public HttpConnectionManager(XmppConnectionService service) {
super(service); super(service);

View file

@ -50,7 +50,7 @@ public class ChannelDiscoveryService {
} }
void initializeMuclumbusService() { void initializeMuclumbusService() {
final OkHttpClient.Builder builder = new OkHttpClient.Builder(); final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder();
if (service.useTorToConnect()) { if (service.useTorToConnect()) {
builder.proxy(HttpConnectionManager.getProxy()); builder.proxy(HttpConnectionManager.getProxy());
} }

View file

@ -5,24 +5,26 @@ import android.content.Intent;
import android.preference.Preference; import android.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
public class AboutPreference extends Preference { public class AboutPreference extends Preference {
public AboutPreference(final Context context, final AttributeSet attrs, final int defStyle) { public AboutPreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
final String appName = context.getString(R.string.app_name); setSummaryAndTitle(context);
setSummary(appName +' '+ PhoneHelper.getVersionName(context));
setTitle(context.getString(R.string.title_activity_about_x, appName));
} }
public AboutPreference(final Context context, final AttributeSet attrs) { public AboutPreference(final Context context, final AttributeSet attrs) {
super(context, attrs); super(context, attrs);
final String appName = context.getString(R.string.app_name); setSummaryAndTitle(context);
setSummary(appName +' '+ PhoneHelper.getVersionName(context));
setTitle(context.getString(R.string.title_activity_about_x, appName));
} }
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 @Override
protected void onClick() { protected void onClick() {
super.onClick(); super.onClick();

View file

@ -35,17 +35,4 @@ public class PhoneHelper {
cursor.close(); cursor.close();
return uri == null ? null : Uri.parse(uri); 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";
}
}
} }

View file

@ -48,6 +48,7 @@ import eu.siacs.conversations.crypto.sasl.Plain;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Entry; import eu.siacs.conversations.entities.Entry;
import eu.siacs.conversations.http.HttpConnectionManager;
import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.AccountUtils;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
@ -260,7 +261,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
} }
private void setHeader(HttpURLConnection connection) { 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("Installation-Id", getInstallationId());
connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage()); connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage());
} }