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"
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 {

View File

@ -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");

View File

@ -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() {

View File

@ -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);

View File

@ -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());
}

View File

@ -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();

View File

@ -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";
}
}
}

View File

@ -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());
}