Split manager ServiceConfig from BaseConfig

This commit is contained in:
AsamK 2020-05-11 14:51:55 +02:00
parent 06caf4ebb3
commit 8163a42d3a
5 changed files with 55 additions and 43 deletions

View file

@ -0,0 +1,12 @@
package org.asamk.signal;
public class BaseConfig {
public final static String PROJECT_NAME = BaseConfig.class.getPackage().getImplementationTitle();
public final static String PROJECT_VERSION = BaseConfig.class.getPackage().getImplementationVersion();
final static String USER_AGENT = PROJECT_NAME == null ? "signal-cli" : PROJECT_NAME + " " + PROJECT_VERSION;
private BaseConfig() {
}
}

View file

@ -31,8 +31,8 @@ import org.asamk.signal.commands.Commands;
import org.asamk.signal.commands.DbusCommand; import org.asamk.signal.commands.DbusCommand;
import org.asamk.signal.commands.ExtendedDbusCommand; import org.asamk.signal.commands.ExtendedDbusCommand;
import org.asamk.signal.commands.LocalCommand; import org.asamk.signal.commands.LocalCommand;
import org.asamk.signal.manager.BaseConfig;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.ServiceConfig;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.SecurityProvider; import org.asamk.signal.util.SecurityProvider;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
@ -102,7 +102,7 @@ public class Main {
dataPath = getDefaultDataPath(); dataPath = getDefaultDataPath();
} }
m = new Manager(username, dataPath); m = new Manager(username, dataPath, ServiceConfig.createDefaultServiceConfiguration(BaseConfig.USER_AGENT), BaseConfig.USER_AGENT);
ts = m; ts = m;
try { try {
m.init(); m.init();

View file

@ -109,6 +109,7 @@ import org.whispersystems.signalservice.api.util.SleepTimer;
import org.whispersystems.signalservice.api.util.StreamDetails; import org.whispersystems.signalservice.api.util.StreamDetails;
import org.whispersystems.signalservice.api.util.UptimeSleepTimer; import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
import org.whispersystems.signalservice.api.util.UuidUtil; import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos; import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException; import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
import org.whispersystems.signalservice.internal.push.VerifyAccountResponse; import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
@ -128,13 +129,11 @@ import java.net.URLEncoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Deque;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -155,6 +154,8 @@ public class Manager implements Signal {
private final String attachmentsPath; private final String attachmentsPath;
private final String avatarsPath; private final String avatarsPath;
private final SleepTimer timer = new UptimeSleepTimer(); private final SleepTimer timer = new UptimeSleepTimer();
private final SignalServiceConfiguration serviceConfiguration;
private final String userAgent;
private SignalAccount account; private SignalAccount account;
private String username; private String username;
@ -162,13 +163,14 @@ public class Manager implements Signal {
private SignalServiceMessagePipe messagePipe = null; private SignalServiceMessagePipe messagePipe = null;
private SignalServiceMessagePipe unidentifiedMessagePipe = null; private SignalServiceMessagePipe unidentifiedMessagePipe = null;
public Manager(String username, String settingsPath) { public Manager(String username, String settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent) {
this.username = username; this.username = username;
this.settingsPath = settingsPath; this.settingsPath = settingsPath;
this.dataPath = this.settingsPath + "/data"; this.dataPath = this.settingsPath + "/data";
this.attachmentsPath = this.settingsPath + "/attachments"; this.attachmentsPath = this.settingsPath + "/attachments";
this.avatarsPath = this.settingsPath + "/avatars"; this.avatarsPath = this.settingsPath + "/avatars";
this.serviceConfiguration = serviceConfiguration;
this.userAgent = userAgent;
} }
public String getUsername() { public String getUsername() {
@ -180,7 +182,7 @@ public class Manager implements Signal {
} }
private SignalServiceAccountManager getSignalServiceAccountManager() { private SignalServiceAccountManager getSignalServiceAccountManager() {
return new SignalServiceAccountManager(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer); return new SignalServiceAccountManager(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), userAgent, timer);
} }
private IdentityKey getIdentity() { private IdentityKey getIdentity() {
@ -224,7 +226,7 @@ public class Manager implements Signal {
accountManager = getSignalServiceAccountManager(); accountManager = getSignalServiceAccountManager();
if (account.isRegistered()) { if (account.isRegistered()) {
if (accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) { if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
refreshPreKeys(); refreshPreKeys();
account.save(); account.save();
} }
@ -298,7 +300,7 @@ public class Manager implements Signal {
} }
public void updateAccountAttributes() throws IOException { public void updateAccountAttributes() throws IOException {
accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), account.getRegistrationLock(), getSelfUnidentifiedAccessKey(), false, BaseConfig.capabilities); accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), account.getRegistrationLock(), getSelfUnidentifiedAccessKey(), false, ServiceConfig.capabilities);
} }
public void setProfileName(String name) throws IOException { public void setProfileName(String name) throws IOException {
@ -401,10 +403,10 @@ public class Manager implements Signal {
} }
private List<PreKeyRecord> generatePreKeys() { private List<PreKeyRecord> generatePreKeys() {
List<PreKeyRecord> records = new ArrayList<>(BaseConfig.PREKEY_BATCH_SIZE); List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);
final int offset = account.getPreKeyIdOffset(); final int offset = account.getPreKeyIdOffset();
for (int i = 0; i < BaseConfig.PREKEY_BATCH_SIZE; i++) { for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
int preKeyId = (offset + i) % Medium.MAX_VALUE; int preKeyId = (offset + i) % Medium.MAX_VALUE;
ECKeyPair keyPair = Curve.generateKeyPair(); ECKeyPair keyPair = Curve.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair); PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
@ -437,7 +439,7 @@ public class Manager implements Signal {
verificationCode = verificationCode.replace("-", ""); verificationCode = verificationCode.replace("-", "");
account.setSignalingKey(KeyUtils.createSignalingKey()); account.setSignalingKey(KeyUtils.createSignalingKey());
// TODO make unrestricted unidentified access configurable // TODO make unrestricted unidentified access configurable
VerifyAccountResponse response = accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false, BaseConfig.capabilities); VerifyAccountResponse response = accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false, ServiceConfig.capabilities);
UUID uuid = UuidUtil.parseOrNull(response.getUuid()); UUID uuid = UuidUtil.parseOrNull(response.getUuid());
// TODO response.isStorageCapable() // TODO response.isStorageCapable()
@ -473,15 +475,15 @@ public class Manager implements Signal {
private SignalServiceMessageReceiver getMessageReceiver() { private SignalServiceMessageReceiver getMessageReceiver() {
// TODO implement ZkGroup support // TODO implement ZkGroup support
final ClientZkProfileOperations clientZkProfileOperations = null; final ClientZkProfileOperations clientZkProfileOperations = null;
return new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer, clientZkProfileOperations); return new SignalServiceMessageReceiver(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), userAgent, null, timer, clientZkProfileOperations);
} }
private SignalServiceMessageSender getMessageSender() { private SignalServiceMessageSender getMessageSender() {
// TODO implement ZkGroup support // TODO implement ZkGroup support
final ClientZkProfileOperations clientZkProfileOperations = null; final ClientZkProfileOperations clientZkProfileOperations = null;
final boolean attachmentsV3 = false; final boolean attachmentsV3 = false;
return new SignalServiceMessageSender(BaseConfig.serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), return new SignalServiceMessageSender(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(),
account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), attachmentsV3, Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations); account.getDeviceId(), account.getSignalProtocolStore(), userAgent, account.isMultiDevice(), attachmentsV3, Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations);
} }
private SignalServiceProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException { private SignalServiceProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException {
@ -1801,7 +1803,7 @@ public class Manager implements Signal {
final SignalServiceMessageReceiver messageReceiver = getMessageReceiver(); final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
File tmpFile = IOUtils.createTempFile(); File tmpFile = IOUtils.createTempFile();
try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE)) { try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE)) {
try (OutputStream output = new FileOutputStream(outputFile)) { try (OutputStream output = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int read; int read;
@ -1825,7 +1827,7 @@ public class Manager implements Signal {
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException, MissingConfigurationException { private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException, MissingConfigurationException {
final SignalServiceMessageReceiver messageReceiver = getMessageReceiver(); final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
return messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE); return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
} }
@Override @Override

View file

@ -18,12 +18,8 @@ import java.util.Map;
import okhttp3.Dns; import okhttp3.Dns;
import okhttp3.Interceptor; import okhttp3.Interceptor;
public class BaseConfig { public class ServiceConfig {
public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle();
public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion();
final static String USER_AGENT = PROJECT_NAME == null ? "signal-cli" : PROJECT_NAME + " " + PROJECT_VERSION;
final static String UNIDENTIFIED_SENDER_TRUST_ROOT = "BXu6QIKVz5MA8gstzfOgRQGqyLqOwNKHL6INkv3IHWMF"; final static String UNIDENTIFIED_SENDER_TRUST_ROOT = "BXu6QIKVz5MA8gstzfOgRQGqyLqOwNKHL6INkv3IHWMF";
final static int PREKEY_MINIMUM_COUNT = 20; final static int PREKEY_MINIMUM_COUNT = 20;
final static int PREKEY_BATCH_SIZE = 100; final static int PREKEY_BATCH_SIZE = 100;
@ -38,29 +34,28 @@ public class BaseConfig {
private final static Optional<Dns> dns = Optional.absent(); private final static Optional<Dns> dns = Optional.absent();
private final static Interceptor userAgentInterceptor = chain ->
chain.proceed(chain.request().newBuilder()
.header("User-Agent", USER_AGENT)
.build());
private final static List<Interceptor> interceptors = Collections.singletonList(userAgentInterceptor);
private final static byte[] zkGroupServerPublicParams = new byte[]{}; private final static byte[] zkGroupServerPublicParams = new byte[]{};
final static SignalServiceConfiguration serviceConfiguration = new SignalServiceConfiguration(
new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)},
makeSignalCdnUrlMapFor(new SignalCdnUrl[]{new SignalCdnUrl(CDN_URL, TRUST_STORE)}, new SignalCdnUrl[]{new SignalCdnUrl(CDN2_URL, TRUST_STORE)}),
new SignalContactDiscoveryUrl[0],
new SignalKeyBackupServiceUrl[]{new SignalKeyBackupServiceUrl(SIGNAL_KEY_BACKUP_URL, TRUST_STORE)},
new SignalStorageUrl[]{new SignalStorageUrl(STORAGE_URL, TRUST_STORE)},
interceptors,
dns,
zkGroupServerPublicParams
);
static final SignalServiceProfile.Capabilities capabilities = new SignalServiceProfile.Capabilities(false, false, false); static final SignalServiceProfile.Capabilities capabilities = new SignalServiceProfile.Capabilities(false, false, false);
private BaseConfig() { public static SignalServiceConfiguration createDefaultServiceConfiguration(String userAgent) {
final Interceptor userAgentInterceptor = chain ->
chain.proceed(chain.request().newBuilder()
.header("User-Agent", userAgent)
.build());
final List<Interceptor> interceptors = Collections.singletonList(userAgentInterceptor);
return new SignalServiceConfiguration(
new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)},
makeSignalCdnUrlMapFor(new SignalCdnUrl[]{new SignalCdnUrl(CDN_URL, TRUST_STORE)}, new SignalCdnUrl[]{new SignalCdnUrl(CDN2_URL, TRUST_STORE)}),
new SignalContactDiscoveryUrl[0],
new SignalKeyBackupServiceUrl[]{new SignalKeyBackupServiceUrl(SIGNAL_KEY_BACKUP_URL, TRUST_STORE)},
new SignalStorageUrl[]{new SignalStorageUrl(STORAGE_URL, TRUST_STORE)},
interceptors,
dns,
zkGroupServerPublicParams
);
} }
private static Map<Integer, SignalCdnUrl[]> makeSignalCdnUrlMapFor(SignalCdnUrl[] cdn0Urls, SignalCdnUrl[] cdn2Urls) { private static Map<Integer, SignalCdnUrl[]> makeSignalCdnUrlMapFor(SignalCdnUrl[] cdn0Urls, SignalCdnUrl[] cdn2Urls) {
@ -69,4 +64,7 @@ public class BaseConfig {
result.put(2, cdn2Urls); result.put(2, cdn2Urls);
return Collections.unmodifiableMap(result); return Collections.unmodifiableMap(result);
} }
private ServiceConfig() {
}
} }

View file

@ -97,7 +97,7 @@ class Utils {
static CertificateValidator getCertificateValidator() { static CertificateValidator getCertificateValidator() {
try { try {
ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(BaseConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0); ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(ServiceConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0);
return new CertificateValidator(unidentifiedSenderTrustRoot); return new CertificateValidator(unidentifiedSenderTrustRoot);
} catch (InvalidKeyException | IOException e) { } catch (InvalidKeyException | IOException e) {
throw new AssertionError(e); throw new AssertionError(e);
@ -246,7 +246,7 @@ class Utils {
byte[] ownId; byte[] ownId;
byte[] theirId; byte[] theirId;
if (BaseConfig.capabilities.isUuid() if (ServiceConfig.capabilities.isUuid()
&& ownAddress.getUuid().isPresent() && theirAddress.getUuid().isPresent()) { && ownAddress.getUuid().isPresent() && theirAddress.getUuid().isPresent()) {
// Version 2: UUID user // Version 2: UUID user
version = 2; version = 2;