mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 11:00:38 +00:00
Move storage package to manager
This commit is contained in:
parent
9e6a353427
commit
b738f5740c
34 changed files with 148 additions and 141 deletions
|
@ -28,15 +28,15 @@ import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
|||
import org.asamk.signal.manager.helper.GroupHelper;
|
||||
import org.asamk.signal.manager.helper.ProfileHelper;
|
||||
import org.asamk.signal.manager.helper.UnidentifiedAccessHelper;
|
||||
import org.asamk.signal.storage.SignalAccount;
|
||||
import org.asamk.signal.storage.contacts.ContactInfo;
|
||||
import org.asamk.signal.storage.groups.GroupInfo;
|
||||
import org.asamk.signal.storage.groups.GroupInfoV1;
|
||||
import org.asamk.signal.storage.groups.GroupInfoV2;
|
||||
import org.asamk.signal.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.storage.profiles.SignalProfileEntry;
|
||||
import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
|
||||
import org.asamk.signal.storage.stickers.Sticker;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.storage.contacts.ContactInfo;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfo;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV2;
|
||||
import org.asamk.signal.manager.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.manager.storage.profiles.SignalProfileEntry;
|
||||
import org.asamk.signal.manager.storage.protocol.IdentityInfo;
|
||||
import org.asamk.signal.manager.storage.stickers.Sticker;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.signal.libsignal.metadata.InvalidMetadataMessageException;
|
||||
|
@ -2422,8 +2422,7 @@ public class Manager implements Closeable {
|
|||
DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
|
||||
for (ContactInfo record : account.getContactStore().getContacts()) {
|
||||
VerifiedMessage verifiedMessage = null;
|
||||
JsonIdentityKeyStore.Identity currentIdentity = account.getSignalProtocolStore()
|
||||
.getIdentity(record.getAddress());
|
||||
IdentityInfo currentIdentity = account.getSignalProtocolStore().getIdentity(record.getAddress());
|
||||
if (currentIdentity != null) {
|
||||
verifiedMessage = new VerifiedMessage(record.getAddress(),
|
||||
currentIdentity.getIdentityKey(),
|
||||
|
@ -2517,11 +2516,11 @@ public class Manager implements Closeable {
|
|||
return account.getGroupStore().getGroup(groupId);
|
||||
}
|
||||
|
||||
public List<JsonIdentityKeyStore.Identity> getIdentities() {
|
||||
public List<IdentityInfo> getIdentities() {
|
||||
return account.getSignalProtocolStore().getIdentities();
|
||||
}
|
||||
|
||||
public List<JsonIdentityKeyStore.Identity> getIdentities(String number) throws InvalidNumberException {
|
||||
public List<IdentityInfo> getIdentities(String number) throws InvalidNumberException {
|
||||
return account.getSignalProtocolStore().getIdentities(canonicalizeAndResolveSignalServiceAddress(number));
|
||||
}
|
||||
|
||||
|
@ -2533,11 +2532,11 @@ public class Manager implements Closeable {
|
|||
*/
|
||||
public boolean trustIdentityVerified(String name, byte[] fingerprint) throws InvalidNumberException {
|
||||
SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
|
||||
List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
List<IdentityInfo> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
if (ids == null) {
|
||||
return false;
|
||||
}
|
||||
for (JsonIdentityKeyStore.Identity id : ids) {
|
||||
for (IdentityInfo id : ids) {
|
||||
if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
|
||||
continue;
|
||||
}
|
||||
|
@ -2563,11 +2562,11 @@ public class Manager implements Closeable {
|
|||
*/
|
||||
public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) throws InvalidNumberException {
|
||||
SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
|
||||
List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
List<IdentityInfo> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
if (ids == null) {
|
||||
return false;
|
||||
}
|
||||
for (JsonIdentityKeyStore.Identity id : ids) {
|
||||
for (IdentityInfo id : ids) {
|
||||
if (!safetyNumber.equals(computeSafetyNumber(address, id.getIdentityKey()))) {
|
||||
continue;
|
||||
}
|
||||
|
@ -2592,11 +2591,11 @@ public class Manager implements Closeable {
|
|||
*/
|
||||
public boolean trustIdentityAllKeys(String name) {
|
||||
SignalServiceAddress address = resolveSignalServiceAddress(name);
|
||||
List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
List<IdentityInfo> ids = account.getSignalProtocolStore().getIdentities(address);
|
||||
if (ids == null) {
|
||||
return false;
|
||||
}
|
||||
for (JsonIdentityKeyStore.Identity id : ids) {
|
||||
for (IdentityInfo id : ids) {
|
||||
if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
|
||||
account.getSignalProtocolStore()
|
||||
.setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
package org.asamk.signal.manager;
|
||||
|
||||
import org.asamk.signal.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.asamk.signal.manager.groups;
|
||||
|
||||
import org.asamk.signal.storage.groups.GroupInfo;
|
||||
import org.asamk.signal.storage.groups.GroupInfoV1;
|
||||
import org.asamk.signal.storage.groups.GroupInfoV2;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfo;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV2;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.signal.zkgroup.groups.GroupSecretParams;
|
||||
|
|
|
@ -5,8 +5,8 @@ import com.google.protobuf.InvalidProtocolBufferException;
|
|||
import org.asamk.signal.manager.groups.GroupIdV2;
|
||||
import org.asamk.signal.manager.groups.GroupLinkPassword;
|
||||
import org.asamk.signal.manager.groups.GroupUtils;
|
||||
import org.asamk.signal.storage.groups.GroupInfoV2;
|
||||
import org.asamk.signal.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV2;
|
||||
import org.asamk.signal.manager.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.signal.storageservice.protos.groups.AccessControl;
|
||||
import org.signal.storageservice.protos.groups.GroupChange;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.manager.storage.profiles.SignalProfile;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
public interface ProfileProvider {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.storage.profiles.SignalProfile;
|
||||
import org.asamk.signal.manager.storage.profiles.SignalProfile;
|
||||
import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
|
|
@ -0,0 +1,517 @@
|
|||
package org.asamk.signal.manager.storage;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.storage.contacts.ContactInfo;
|
||||
import org.asamk.signal.manager.storage.contacts.JsonContactsStore;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfo;
|
||||
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
|
||||
import org.asamk.signal.manager.storage.groups.JsonGroupStore;
|
||||
import org.asamk.signal.manager.storage.profiles.ProfileStore;
|
||||
import org.asamk.signal.manager.storage.protocol.IdentityInfo;
|
||||
import org.asamk.signal.manager.storage.protocol.JsonSignalProtocolStore;
|
||||
import org.asamk.signal.manager.storage.protocol.RecipientStore;
|
||||
import org.asamk.signal.manager.storage.protocol.SessionInfo;
|
||||
import org.asamk.signal.manager.storage.protocol.SignalServiceAddressResolver;
|
||||
import org.asamk.signal.manager.storage.stickers.StickerStore;
|
||||
import org.asamk.signal.manager.storage.threads.LegacyJsonThreadStore;
|
||||
import org.asamk.signal.manager.storage.threads.ThreadInfo;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.util.Medium;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SignalAccount implements Closeable {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(SignalAccount.class);
|
||||
|
||||
private final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
private final FileChannel fileChannel;
|
||||
private final FileLock lock;
|
||||
private String username;
|
||||
private UUID uuid;
|
||||
private int deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
|
||||
private boolean isMultiDevice = false;
|
||||
private String password;
|
||||
private String registrationLockPin;
|
||||
private String signalingKey;
|
||||
private ProfileKey profileKey;
|
||||
private int preKeyIdOffset;
|
||||
private int nextSignedPreKeyId;
|
||||
|
||||
private boolean registered = false;
|
||||
|
||||
private JsonSignalProtocolStore signalProtocolStore;
|
||||
private JsonGroupStore groupStore;
|
||||
private JsonContactsStore contactStore;
|
||||
private RecipientStore recipientStore;
|
||||
private ProfileStore profileStore;
|
||||
private StickerStore stickerStore;
|
||||
|
||||
private SignalAccount(final FileChannel fileChannel, final FileLock lock) {
|
||||
this.fileChannel = fileChannel;
|
||||
this.lock = lock;
|
||||
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); // disable autodetect
|
||||
jsonProcessor.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print, you can disable it.
|
||||
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
jsonProcessor.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
|
||||
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
||||
}
|
||||
|
||||
public static SignalAccount load(File dataPath, String username) throws IOException {
|
||||
final File fileName = getFileName(dataPath, username);
|
||||
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
|
||||
try {
|
||||
SignalAccount account = new SignalAccount(pair.first(), pair.second());
|
||||
account.load(dataPath);
|
||||
return account;
|
||||
} catch (Throwable e) {
|
||||
pair.second().close();
|
||||
pair.first().close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static SignalAccount create(
|
||||
File dataPath, String username, IdentityKeyPair identityKey, int registrationId, ProfileKey profileKey
|
||||
) throws IOException {
|
||||
IOUtils.createPrivateDirectories(dataPath);
|
||||
File fileName = getFileName(dataPath, username);
|
||||
if (!fileName.exists()) {
|
||||
IOUtils.createPrivateFile(fileName);
|
||||
}
|
||||
|
||||
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
|
||||
SignalAccount account = new SignalAccount(pair.first(), pair.second());
|
||||
|
||||
account.username = username;
|
||||
account.profileKey = profileKey;
|
||||
account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
|
||||
account.groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
|
||||
account.contactStore = new JsonContactsStore();
|
||||
account.recipientStore = new RecipientStore();
|
||||
account.profileStore = new ProfileStore();
|
||||
account.stickerStore = new StickerStore();
|
||||
account.registered = false;
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public static SignalAccount createLinkedAccount(
|
||||
File dataPath,
|
||||
String username,
|
||||
UUID uuid,
|
||||
String password,
|
||||
int deviceId,
|
||||
IdentityKeyPair identityKey,
|
||||
int registrationId,
|
||||
String signalingKey,
|
||||
ProfileKey profileKey
|
||||
) throws IOException {
|
||||
IOUtils.createPrivateDirectories(dataPath);
|
||||
File fileName = getFileName(dataPath, username);
|
||||
if (!fileName.exists()) {
|
||||
IOUtils.createPrivateFile(fileName);
|
||||
}
|
||||
|
||||
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
|
||||
SignalAccount account = new SignalAccount(pair.first(), pair.second());
|
||||
|
||||
account.username = username;
|
||||
account.uuid = uuid;
|
||||
account.password = password;
|
||||
account.profileKey = profileKey;
|
||||
account.deviceId = deviceId;
|
||||
account.signalingKey = signalingKey;
|
||||
account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
|
||||
account.groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
|
||||
account.contactStore = new JsonContactsStore();
|
||||
account.recipientStore = new RecipientStore();
|
||||
account.profileStore = new ProfileStore();
|
||||
account.stickerStore = new StickerStore();
|
||||
account.registered = true;
|
||||
account.isMultiDevice = true;
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public static File getFileName(File dataPath, String username) {
|
||||
return new File(dataPath, username);
|
||||
}
|
||||
|
||||
private static File getUserPath(final File dataPath, final String username) {
|
||||
return new File(dataPath, username + ".d");
|
||||
}
|
||||
|
||||
public static File getMessageCachePath(File dataPath, String username) {
|
||||
return new File(getUserPath(dataPath, username), "msg-cache");
|
||||
}
|
||||
|
||||
private static File getGroupCachePath(File dataPath, String username) {
|
||||
return new File(getUserPath(dataPath, username), "group-cache");
|
||||
}
|
||||
|
||||
public static boolean userExists(File dataPath, String username) {
|
||||
if (username == null) {
|
||||
return false;
|
||||
}
|
||||
File f = getFileName(dataPath, username);
|
||||
return !(!f.exists() || f.isDirectory());
|
||||
}
|
||||
|
||||
private void load(File dataPath) throws IOException {
|
||||
JsonNode rootNode;
|
||||
synchronized (fileChannel) {
|
||||
fileChannel.position(0);
|
||||
rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
|
||||
}
|
||||
|
||||
JsonNode uuidNode = rootNode.get("uuid");
|
||||
if (uuidNode != null && !uuidNode.isNull()) {
|
||||
try {
|
||||
uuid = UUID.fromString(uuidNode.asText());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IOException("Config file contains an invalid uuid, needs to be a valid UUID", e);
|
||||
}
|
||||
}
|
||||
JsonNode node = rootNode.get("deviceId");
|
||||
if (node != null) {
|
||||
deviceId = node.asInt();
|
||||
}
|
||||
if (rootNode.has("isMultiDevice")) {
|
||||
isMultiDevice = Util.getNotNullNode(rootNode, "isMultiDevice").asBoolean();
|
||||
}
|
||||
username = Util.getNotNullNode(rootNode, "username").asText();
|
||||
password = Util.getNotNullNode(rootNode, "password").asText();
|
||||
JsonNode pinNode = rootNode.get("registrationLockPin");
|
||||
registrationLockPin = pinNode == null || pinNode.isNull() ? null : pinNode.asText();
|
||||
if (rootNode.has("signalingKey")) {
|
||||
signalingKey = Util.getNotNullNode(rootNode, "signalingKey").asText();
|
||||
}
|
||||
if (rootNode.has("preKeyIdOffset")) {
|
||||
preKeyIdOffset = Util.getNotNullNode(rootNode, "preKeyIdOffset").asInt(0);
|
||||
} else {
|
||||
preKeyIdOffset = 0;
|
||||
}
|
||||
if (rootNode.has("nextSignedPreKeyId")) {
|
||||
nextSignedPreKeyId = Util.getNotNullNode(rootNode, "nextSignedPreKeyId").asInt();
|
||||
} else {
|
||||
nextSignedPreKeyId = 0;
|
||||
}
|
||||
if (rootNode.has("profileKey")) {
|
||||
try {
|
||||
profileKey = new ProfileKey(Base64.decode(Util.getNotNullNode(rootNode, "profileKey").asText()));
|
||||
} catch (InvalidInputException e) {
|
||||
throw new IOException(
|
||||
"Config file contains an invalid profileKey, needs to be base64 encoded array of 32 bytes",
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
signalProtocolStore = jsonProcessor.convertValue(Util.getNotNullNode(rootNode, "axolotlStore"),
|
||||
JsonSignalProtocolStore.class);
|
||||
registered = Util.getNotNullNode(rootNode, "registered").asBoolean();
|
||||
JsonNode groupStoreNode = rootNode.get("groupStore");
|
||||
if (groupStoreNode != null) {
|
||||
groupStore = jsonProcessor.convertValue(groupStoreNode, JsonGroupStore.class);
|
||||
groupStore.groupCachePath = getGroupCachePath(dataPath, username);
|
||||
}
|
||||
if (groupStore == null) {
|
||||
groupStore = new JsonGroupStore(getGroupCachePath(dataPath, username));
|
||||
}
|
||||
|
||||
JsonNode contactStoreNode = rootNode.get("contactStore");
|
||||
if (contactStoreNode != null) {
|
||||
contactStore = jsonProcessor.convertValue(contactStoreNode, JsonContactsStore.class);
|
||||
}
|
||||
if (contactStore == null) {
|
||||
contactStore = new JsonContactsStore();
|
||||
}
|
||||
|
||||
JsonNode recipientStoreNode = rootNode.get("recipientStore");
|
||||
if (recipientStoreNode != null) {
|
||||
recipientStore = jsonProcessor.convertValue(recipientStoreNode, RecipientStore.class);
|
||||
}
|
||||
if (recipientStore == null) {
|
||||
recipientStore = new RecipientStore();
|
||||
|
||||
recipientStore.resolveServiceAddress(getSelfAddress());
|
||||
|
||||
for (ContactInfo contact : contactStore.getContacts()) {
|
||||
recipientStore.resolveServiceAddress(contact.getAddress());
|
||||
}
|
||||
|
||||
for (GroupInfo group : groupStore.getGroups()) {
|
||||
if (group instanceof GroupInfoV1) {
|
||||
GroupInfoV1 groupInfoV1 = (GroupInfoV1) group;
|
||||
groupInfoV1.members = groupInfoV1.members.stream()
|
||||
.map(m -> recipientStore.resolveServiceAddress(m))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
for (SessionInfo session : signalProtocolStore.getSessions()) {
|
||||
session.address = recipientStore.resolveServiceAddress(session.address);
|
||||
}
|
||||
|
||||
for (IdentityInfo identity : signalProtocolStore.getIdentities()) {
|
||||
identity.setAddress(recipientStore.resolveServiceAddress(identity.getAddress()));
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode profileStoreNode = rootNode.get("profileStore");
|
||||
if (profileStoreNode != null) {
|
||||
profileStore = jsonProcessor.convertValue(profileStoreNode, ProfileStore.class);
|
||||
}
|
||||
if (profileStore == null) {
|
||||
profileStore = new ProfileStore();
|
||||
}
|
||||
|
||||
JsonNode stickerStoreNode = rootNode.get("stickerStore");
|
||||
if (stickerStoreNode != null) {
|
||||
stickerStore = jsonProcessor.convertValue(stickerStoreNode, StickerStore.class);
|
||||
}
|
||||
if (stickerStore == null) {
|
||||
stickerStore = new StickerStore();
|
||||
}
|
||||
|
||||
JsonNode threadStoreNode = rootNode.get("threadStore");
|
||||
if (threadStoreNode != null) {
|
||||
LegacyJsonThreadStore threadStore = jsonProcessor.convertValue(threadStoreNode,
|
||||
LegacyJsonThreadStore.class);
|
||||
// Migrate thread info to group and contact store
|
||||
for (ThreadInfo thread : threadStore.getThreads()) {
|
||||
if (thread.id == null || thread.id.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
ContactInfo contactInfo = contactStore.getContact(new SignalServiceAddress(null, thread.id));
|
||||
if (contactInfo != null) {
|
||||
contactInfo.messageExpirationTime = thread.messageExpirationTime;
|
||||
contactStore.updateContact(contactInfo);
|
||||
} else {
|
||||
GroupInfo groupInfo = groupStore.getGroup(GroupId.fromBase64(thread.id));
|
||||
if (groupInfo instanceof GroupInfoV1) {
|
||||
((GroupInfoV1) groupInfo).messageExpirationTime = thread.messageExpirationTime;
|
||||
groupStore.updateGroup(groupInfo);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (fileChannel == null) {
|
||||
return;
|
||||
}
|
||||
ObjectNode rootNode = jsonProcessor.createObjectNode();
|
||||
rootNode.put("username", username)
|
||||
.put("uuid", uuid == null ? null : uuid.toString())
|
||||
.put("deviceId", deviceId)
|
||||
.put("isMultiDevice", isMultiDevice)
|
||||
.put("password", password)
|
||||
.put("registrationLockPin", registrationLockPin)
|
||||
.put("signalingKey", signalingKey)
|
||||
.put("preKeyIdOffset", preKeyIdOffset)
|
||||
.put("nextSignedPreKeyId", nextSignedPreKeyId)
|
||||
.put("profileKey", Base64.encodeBytes(profileKey.serialize()))
|
||||
.put("registered", registered)
|
||||
.putPOJO("axolotlStore", signalProtocolStore)
|
||||
.putPOJO("groupStore", groupStore)
|
||||
.putPOJO("contactStore", contactStore)
|
||||
.putPOJO("recipientStore", recipientStore)
|
||||
.putPOJO("profileStore", profileStore)
|
||||
.putPOJO("stickerStore", stickerStore);
|
||||
try {
|
||||
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
||||
// Write to memory first to prevent corrupting the file in case of serialization errors
|
||||
jsonProcessor.writeValue(output, rootNode);
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
|
||||
synchronized (fileChannel) {
|
||||
fileChannel.position(0);
|
||||
input.transferTo(Channels.newOutputStream(fileChannel));
|
||||
fileChannel.truncate(fileChannel.position());
|
||||
fileChannel.force(false);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error saving file: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static Pair<FileChannel, FileLock> openFileChannel(File fileName) throws IOException {
|
||||
FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel();
|
||||
FileLock lock = fileChannel.tryLock();
|
||||
if (lock == null) {
|
||||
logger.info("Config file is in use by another instance, waiting…");
|
||||
lock = fileChannel.lock();
|
||||
logger.info("Config file lock acquired.");
|
||||
}
|
||||
return new Pair<>(fileChannel, lock);
|
||||
}
|
||||
|
||||
public void setResolver(final SignalServiceAddressResolver resolver) {
|
||||
signalProtocolStore.setResolver(resolver);
|
||||
}
|
||||
|
||||
public void addPreKeys(Collection<PreKeyRecord> records) {
|
||||
for (PreKeyRecord record : records) {
|
||||
signalProtocolStore.storePreKey(record.getId(), record);
|
||||
}
|
||||
preKeyIdOffset = (preKeyIdOffset + records.size()) % Medium.MAX_VALUE;
|
||||
}
|
||||
|
||||
public void addSignedPreKey(SignedPreKeyRecord record) {
|
||||
signalProtocolStore.storeSignedPreKey(record.getId(), record);
|
||||
nextSignedPreKeyId = (nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
|
||||
}
|
||||
|
||||
public JsonSignalProtocolStore getSignalProtocolStore() {
|
||||
return signalProtocolStore;
|
||||
}
|
||||
|
||||
public JsonGroupStore getGroupStore() {
|
||||
return groupStore;
|
||||
}
|
||||
|
||||
public JsonContactsStore getContactStore() {
|
||||
return contactStore;
|
||||
}
|
||||
|
||||
public RecipientStore getRecipientStore() {
|
||||
return recipientStore;
|
||||
}
|
||||
|
||||
public ProfileStore getProfileStore() {
|
||||
return profileStore;
|
||||
}
|
||||
|
||||
public StickerStore getStickerStore() {
|
||||
return stickerStore;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(final UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public SignalServiceAddress getSelfAddress() {
|
||||
return new SignalServiceAddress(uuid, username);
|
||||
}
|
||||
|
||||
public int getDeviceId() {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(final String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getRegistrationLockPin() {
|
||||
return registrationLockPin;
|
||||
}
|
||||
|
||||
public String getRegistrationLock() {
|
||||
return null; // TODO implement KBS
|
||||
}
|
||||
|
||||
public void setRegistrationLockPin(final String registrationLockPin) {
|
||||
this.registrationLockPin = registrationLockPin;
|
||||
}
|
||||
|
||||
public String getSignalingKey() {
|
||||
return signalingKey;
|
||||
}
|
||||
|
||||
public void setSignalingKey(final String signalingKey) {
|
||||
this.signalingKey = signalingKey;
|
||||
}
|
||||
|
||||
public ProfileKey getProfileKey() {
|
||||
return profileKey;
|
||||
}
|
||||
|
||||
public void setProfileKey(final ProfileKey profileKey) {
|
||||
this.profileKey = profileKey;
|
||||
}
|
||||
|
||||
public int getPreKeyIdOffset() {
|
||||
return preKeyIdOffset;
|
||||
}
|
||||
|
||||
public int getNextSignedPreKeyId() {
|
||||
return nextSignedPreKeyId;
|
||||
}
|
||||
|
||||
public boolean isRegistered() {
|
||||
return registered;
|
||||
}
|
||||
|
||||
public void setRegistered(final boolean registered) {
|
||||
this.registered = registered;
|
||||
}
|
||||
|
||||
public boolean isMultiDevice() {
|
||||
return isMultiDevice;
|
||||
}
|
||||
|
||||
public void setMultiDevice(final boolean multiDevice) {
|
||||
isMultiDevice = multiDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
synchronized (fileChannel) {
|
||||
try {
|
||||
lock.close();
|
||||
} catch (ClosedChannelException ignored) {
|
||||
}
|
||||
fileChannel.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package org.asamk.signal.manager.storage.contacts;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY;
|
||||
|
||||
public class ContactInfo {
|
||||
|
||||
@JsonProperty
|
||||
public String name;
|
||||
|
||||
@JsonProperty
|
||||
public String number;
|
||||
|
||||
@JsonProperty
|
||||
public UUID uuid;
|
||||
|
||||
@JsonProperty
|
||||
public String color;
|
||||
|
||||
@JsonProperty(defaultValue = "0")
|
||||
public int messageExpirationTime;
|
||||
|
||||
@JsonProperty(access = WRITE_ONLY)
|
||||
public String profileKey;
|
||||
|
||||
@JsonProperty(defaultValue = "false")
|
||||
public boolean blocked;
|
||||
|
||||
@JsonProperty
|
||||
public Integer inboxPosition;
|
||||
|
||||
@JsonProperty(defaultValue = "false")
|
||||
public boolean archived;
|
||||
|
||||
public ContactInfo() {
|
||||
}
|
||||
|
||||
public ContactInfo(SignalServiceAddress address) {
|
||||
this.number = address.getNumber().orNull();
|
||||
this.uuid = address.getUuid().orNull();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public SignalServiceAddress getAddress() {
|
||||
return new SignalServiceAddress(uuid, number);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package org.asamk.signal.manager.storage.contacts;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonContactsStore {
|
||||
|
||||
@JsonProperty("contacts")
|
||||
private List<ContactInfo> contacts = new ArrayList<>();
|
||||
|
||||
public void updateContact(ContactInfo contact) {
|
||||
final SignalServiceAddress contactAddress = contact.getAddress();
|
||||
for (int i = 0; i < contacts.size(); i++) {
|
||||
if (contacts.get(i).getAddress().matches(contactAddress)) {
|
||||
contacts.set(i, contact);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
contacts.add(contact);
|
||||
}
|
||||
|
||||
public ContactInfo getContact(SignalServiceAddress address) {
|
||||
for (ContactInfo contact : contacts) {
|
||||
if (contact.getAddress().matches(address)) {
|
||||
if (contact.uuid == null) {
|
||||
contact.uuid = address.getUuid().orNull();
|
||||
} else if (contact.number == null) {
|
||||
contact.number = address.getNumber().orNull();
|
||||
}
|
||||
|
||||
return contact;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<ContactInfo> getContacts() {
|
||||
return new ArrayList<>(contacts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all contacts from the store
|
||||
*/
|
||||
public void clear() {
|
||||
contacts.clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package org.asamk.signal.manager.storage.groups;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class GroupInfo {
|
||||
|
||||
@JsonIgnore
|
||||
public abstract GroupId getGroupId();
|
||||
|
||||
@JsonIgnore
|
||||
public abstract String getTitle();
|
||||
|
||||
@JsonIgnore
|
||||
public abstract GroupInviteLinkUrl getGroupInviteLink();
|
||||
|
||||
@JsonIgnore
|
||||
public abstract Set<SignalServiceAddress> getMembers();
|
||||
|
||||
@JsonIgnore
|
||||
public Set<SignalServiceAddress> getPendingMembers() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public Set<SignalServiceAddress> getRequestingMembers() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public abstract boolean isBlocked();
|
||||
|
||||
@JsonIgnore
|
||||
public abstract void setBlocked(boolean blocked);
|
||||
|
||||
@JsonIgnore
|
||||
public abstract int getMessageExpirationTime();
|
||||
|
||||
@JsonIgnore
|
||||
public Set<SignalServiceAddress> getMembersWithout(SignalServiceAddress address) {
|
||||
return getMembers().stream().filter(member -> !member.matches(address)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public Set<SignalServiceAddress> getMembersIncludingPendingWithout(SignalServiceAddress address) {
|
||||
return Stream.concat(getMembers().stream(), getPendingMembers().stream())
|
||||
.filter(member -> !member.matches(address))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isMember(SignalServiceAddress address) {
|
||||
for (SignalServiceAddress member : getMembers()) {
|
||||
if (member.matches(address)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isPendingMember(SignalServiceAddress address) {
|
||||
for (SignalServiceAddress member : getPendingMembers()) {
|
||||
if (member.matches(address)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,212 @@
|
|||
package org.asamk.signal.manager.storage.groups;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupIdV1;
|
||||
import org.asamk.signal.manager.groups.GroupIdV2;
|
||||
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
||||
import org.asamk.signal.manager.groups.GroupUtils;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class GroupInfoV1 extends GroupInfo {
|
||||
|
||||
private static final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
|
||||
private final GroupIdV1 groupId;
|
||||
|
||||
private GroupIdV2 expectedV2Id;
|
||||
|
||||
@JsonProperty
|
||||
public String name;
|
||||
|
||||
@JsonProperty
|
||||
@JsonDeserialize(using = MembersDeserializer.class)
|
||||
@JsonSerialize(using = MembersSerializer.class)
|
||||
public Set<SignalServiceAddress> members = new HashSet<>();
|
||||
@JsonProperty
|
||||
public String color;
|
||||
@JsonProperty(defaultValue = "0")
|
||||
public int messageExpirationTime;
|
||||
@JsonProperty(defaultValue = "false")
|
||||
public boolean blocked;
|
||||
@JsonProperty
|
||||
public Integer inboxPosition;
|
||||
@JsonProperty(defaultValue = "false")
|
||||
public boolean archived;
|
||||
|
||||
public GroupInfoV1(GroupIdV1 groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public GroupInfoV1(
|
||||
@JsonProperty("groupId") byte[] groupId,
|
||||
@JsonProperty("expectedV2Id") byte[] expectedV2Id,
|
||||
@JsonProperty("name") String name,
|
||||
@JsonProperty("members") Collection<SignalServiceAddress> members,
|
||||
@JsonProperty("avatarId") long _ignored_avatarId,
|
||||
@JsonProperty("color") String color,
|
||||
@JsonProperty("blocked") boolean blocked,
|
||||
@JsonProperty("inboxPosition") Integer inboxPosition,
|
||||
@JsonProperty("archived") boolean archived,
|
||||
@JsonProperty("messageExpirationTime") int messageExpirationTime,
|
||||
@JsonProperty("active") boolean _ignored_active
|
||||
) {
|
||||
this.groupId = GroupId.v1(groupId);
|
||||
this.expectedV2Id = GroupId.v2(expectedV2Id);
|
||||
this.name = name;
|
||||
this.members.addAll(members);
|
||||
this.color = color;
|
||||
this.blocked = blocked;
|
||||
this.inboxPosition = inboxPosition;
|
||||
this.archived = archived;
|
||||
this.messageExpirationTime = messageExpirationTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
@JsonIgnore
|
||||
public GroupIdV1 getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
@JsonProperty("groupId")
|
||||
private byte[] getGroupIdJackson() {
|
||||
return groupId.serialize();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public GroupIdV2 getExpectedV2Id() {
|
||||
if (expectedV2Id == null) {
|
||||
expectedV2Id = GroupUtils.getGroupIdV2(groupId);
|
||||
}
|
||||
return expectedV2Id;
|
||||
}
|
||||
|
||||
@JsonProperty("expectedV2Id")
|
||||
private byte[] getExpectedV2IdJackson() {
|
||||
return expectedV2Id.serialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupInviteLinkUrl getGroupInviteLink() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public Set<SignalServiceAddress> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlocked() {
|
||||
return blocked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlocked(final boolean blocked) {
|
||||
this.blocked = blocked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMessageExpirationTime() {
|
||||
return messageExpirationTime;
|
||||
}
|
||||
|
||||
public void addMembers(Collection<SignalServiceAddress> addresses) {
|
||||
for (SignalServiceAddress address : addresses) {
|
||||
if (this.members.contains(address)) {
|
||||
continue;
|
||||
}
|
||||
removeMember(address);
|
||||
this.members.add(address);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeMember(SignalServiceAddress address) {
|
||||
this.members.removeIf(member -> member.matches(address));
|
||||
}
|
||||
|
||||
private static final class JsonSignalServiceAddress {
|
||||
|
||||
@JsonProperty
|
||||
private UUID uuid;
|
||||
|
||||
@JsonProperty
|
||||
private String number;
|
||||
|
||||
JsonSignalServiceAddress(@JsonProperty("uuid") final UUID uuid, @JsonProperty("number") final String number) {
|
||||
this.uuid = uuid;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
JsonSignalServiceAddress(SignalServiceAddress address) {
|
||||
this.uuid = address.getUuid().orNull();
|
||||
this.number = address.getNumber().orNull();
|
||||
}
|
||||
|
||||
SignalServiceAddress toSignalServiceAddress() {
|
||||
return new SignalServiceAddress(uuid, number);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MembersSerializer extends JsonSerializer<Set<SignalServiceAddress>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final Set<SignalServiceAddress> value, final JsonGenerator jgen, final SerializerProvider provider
|
||||
) throws IOException {
|
||||
jgen.writeStartArray(value.size());
|
||||
for (SignalServiceAddress address : value) {
|
||||
if (address.getUuid().isPresent()) {
|
||||
jgen.writeObject(new JsonSignalServiceAddress(address));
|
||||
} else {
|
||||
jgen.writeString(address.getNumber().get());
|
||||
}
|
||||
}
|
||||
jgen.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static class MembersDeserializer extends JsonDeserializer<Set<SignalServiceAddress>> {
|
||||
|
||||
@Override
|
||||
public Set<SignalServiceAddress> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
Set<SignalServiceAddress> addresses = new HashSet<>();
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
for (JsonNode n : node) {
|
||||
if (n.isTextual()) {
|
||||
addresses.add(new SignalServiceAddress(null, n.textValue()));
|
||||
} else {
|
||||
JsonSignalServiceAddress address = jsonProcessor.treeToValue(n, JsonSignalServiceAddress.class);
|
||||
addresses.add(address.toSignalServiceAddress());
|
||||
}
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
package org.asamk.signal.manager.storage.groups;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupIdV2;
|
||||
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
||||
import org.signal.storageservice.protos.groups.AccessControl;
|
||||
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GroupInfoV2 extends GroupInfo {
|
||||
|
||||
private final GroupIdV2 groupId;
|
||||
private final GroupMasterKey masterKey;
|
||||
|
||||
private boolean blocked;
|
||||
private DecryptedGroup group; // stored as a file with hexadecimal groupId as name
|
||||
|
||||
public GroupInfoV2(final GroupIdV2 groupId, final GroupMasterKey masterKey) {
|
||||
this.groupId = groupId;
|
||||
this.masterKey = masterKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupIdV2 getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public GroupMasterKey getMasterKey() {
|
||||
return masterKey;
|
||||
}
|
||||
|
||||
public void setGroup(final DecryptedGroup group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public DecryptedGroup getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
if (this.group == null) {
|
||||
return null;
|
||||
}
|
||||
return this.group.getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupInviteLinkUrl getGroupInviteLink() {
|
||||
if (this.group == null || this.group.getInviteLinkPassword() == null || (
|
||||
this.group.getAccessControl().getAddFromInviteLink() != AccessControl.AccessRequired.ANY
|
||||
&& this.group.getAccessControl().getAddFromInviteLink()
|
||||
!= AccessControl.AccessRequired.ADMINISTRATOR
|
||||
)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return GroupInviteLinkUrl.forGroup(masterKey, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<SignalServiceAddress> getMembers() {
|
||||
if (this.group == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return group.getMembersList()
|
||||
.stream()
|
||||
.map(m -> new SignalServiceAddress(UuidUtil.parseOrThrow(m.getUuid().toByteArray()), null))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<SignalServiceAddress> getPendingMembers() {
|
||||
if (this.group == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return group.getPendingMembersList()
|
||||
.stream()
|
||||
.map(m -> new SignalServiceAddress(UuidUtil.parseOrThrow(m.getUuid().toByteArray()), null))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<SignalServiceAddress> getRequestingMembers() {
|
||||
if (this.group == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return group.getRequestingMembersList()
|
||||
.stream()
|
||||
.map(m -> new SignalServiceAddress(UuidUtil.parseOrThrow(m.getUuid().toByteArray()), null))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlocked() {
|
||||
return blocked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlocked(final boolean blocked) {
|
||||
this.blocked = blocked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMessageExpirationTime() {
|
||||
return this.group != null && this.group.hasDisappearingMessagesTimer()
|
||||
? this.group.getDisappearingMessagesTimer().getDuration()
|
||||
: 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,205 @@
|
|||
package org.asamk.signal.manager.storage.groups;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.groups.GroupIdV1;
|
||||
import org.asamk.signal.manager.groups.GroupIdV2;
|
||||
import org.asamk.signal.manager.groups.GroupUtils;
|
||||
import org.asamk.signal.util.Hex;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonGroupStore {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(JsonGroupStore.class);
|
||||
|
||||
private static final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
public File groupCachePath;
|
||||
|
||||
@JsonProperty("groups")
|
||||
@JsonSerialize(using = GroupsSerializer.class)
|
||||
@JsonDeserialize(using = GroupsDeserializer.class)
|
||||
private final Map<GroupId, GroupInfo> groups = new HashMap<>();
|
||||
|
||||
private JsonGroupStore() {
|
||||
}
|
||||
|
||||
public JsonGroupStore(final File groupCachePath) {
|
||||
this.groupCachePath = groupCachePath;
|
||||
}
|
||||
|
||||
public void updateGroup(GroupInfo group) {
|
||||
groups.put(group.getGroupId(), group);
|
||||
if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() != null) {
|
||||
try {
|
||||
IOUtils.createPrivateDirectories(groupCachePath);
|
||||
try (FileOutputStream stream = new FileOutputStream(getGroupFile(group.getGroupId()))) {
|
||||
((GroupInfoV2) group).getGroup().writeTo(stream);
|
||||
}
|
||||
final File groupFileLegacy = getGroupFileLegacy(group.getGroupId());
|
||||
if (groupFileLegacy.exists()) {
|
||||
groupFileLegacy.delete();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to cache group, ignoring: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteGroup(GroupId groupId) {
|
||||
groups.remove(groupId);
|
||||
}
|
||||
|
||||
public GroupInfo getGroup(GroupId groupId) {
|
||||
GroupInfo group = groups.get(groupId);
|
||||
if (group == null) {
|
||||
if (groupId instanceof GroupIdV1) {
|
||||
group = groups.get(GroupUtils.getGroupIdV2((GroupIdV1) groupId));
|
||||
} else if (groupId instanceof GroupIdV2) {
|
||||
group = getGroupV1ByV2Id((GroupIdV2) groupId);
|
||||
}
|
||||
}
|
||||
loadDecryptedGroup(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
private GroupInfoV1 getGroupV1ByV2Id(GroupIdV2 groupIdV2) {
|
||||
for (GroupInfo g : groups.values()) {
|
||||
if (g instanceof GroupInfoV1) {
|
||||
final GroupInfoV1 gv1 = (GroupInfoV1) g;
|
||||
if (groupIdV2.equals(gv1.getExpectedV2Id())) {
|
||||
return gv1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void loadDecryptedGroup(final GroupInfo group) {
|
||||
if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() == null) {
|
||||
File groupFile = getGroupFile(group.getGroupId());
|
||||
if (!groupFile.exists()) {
|
||||
groupFile = getGroupFileLegacy(group.getGroupId());
|
||||
}
|
||||
if (!groupFile.exists()) {
|
||||
return;
|
||||
}
|
||||
try (FileInputStream stream = new FileInputStream(groupFile)) {
|
||||
((GroupInfoV2) group).setGroup(DecryptedGroup.parseFrom(stream));
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private File getGroupFileLegacy(final GroupId groupId) {
|
||||
return new File(groupCachePath, Hex.toStringCondensed(groupId.serialize()));
|
||||
}
|
||||
|
||||
private File getGroupFile(final GroupId groupId) {
|
||||
return new File(groupCachePath, groupId.toBase64().replace("/", "_"));
|
||||
}
|
||||
|
||||
public GroupInfoV1 getOrCreateGroupV1(GroupIdV1 groupId) {
|
||||
GroupInfo group = getGroup(groupId);
|
||||
if (group instanceof GroupInfoV1) {
|
||||
return (GroupInfoV1) group;
|
||||
}
|
||||
|
||||
if (group == null) {
|
||||
return new GroupInfoV1(groupId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<GroupInfo> getGroups() {
|
||||
final Collection<GroupInfo> groups = this.groups.values();
|
||||
for (GroupInfo group : groups) {
|
||||
loadDecryptedGroup(group);
|
||||
}
|
||||
return new ArrayList<>(groups);
|
||||
}
|
||||
|
||||
private static class GroupsSerializer extends JsonSerializer<Map<String, GroupInfo>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final Map<String, GroupInfo> value, final JsonGenerator jgen, final SerializerProvider provider
|
||||
) throws IOException {
|
||||
final Collection<GroupInfo> groups = value.values();
|
||||
jgen.writeStartArray(groups.size());
|
||||
for (GroupInfo group : groups) {
|
||||
if (group instanceof GroupInfoV1) {
|
||||
jgen.writeObject(group);
|
||||
} else if (group instanceof GroupInfoV2) {
|
||||
final GroupInfoV2 groupV2 = (GroupInfoV2) group;
|
||||
jgen.writeStartObject();
|
||||
jgen.writeStringField("groupId", groupV2.getGroupId().toBase64());
|
||||
jgen.writeStringField("masterKey", Base64.encodeBytes(groupV2.getMasterKey().serialize()));
|
||||
jgen.writeBooleanField("blocked", groupV2.isBlocked());
|
||||
jgen.writeEndObject();
|
||||
} else {
|
||||
throw new AssertionError("Unknown group version");
|
||||
}
|
||||
}
|
||||
jgen.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static class GroupsDeserializer extends JsonDeserializer<Map<GroupId, GroupInfo>> {
|
||||
|
||||
@Override
|
||||
public Map<GroupId, GroupInfo> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
Map<GroupId, GroupInfo> groups = new HashMap<>();
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
for (JsonNode n : node) {
|
||||
GroupInfo g;
|
||||
if (n.has("masterKey")) {
|
||||
// a v2 group
|
||||
GroupIdV2 groupId = GroupIdV2.fromBase64(n.get("groupId").asText());
|
||||
try {
|
||||
GroupMasterKey masterKey = new GroupMasterKey(Base64.decode(n.get("masterKey").asText()));
|
||||
g = new GroupInfoV2(groupId, masterKey);
|
||||
} catch (InvalidInputException e) {
|
||||
throw new AssertionError("Invalid master key for group " + groupId.toBase64());
|
||||
}
|
||||
g.setBlocked(n.get("blocked").asBoolean(false));
|
||||
} else {
|
||||
GroupInfoV1 gv1 = jsonProcessor.treeToValue(n, GroupInfoV1.class);
|
||||
g = gv1;
|
||||
}
|
||||
groups.put(g.getGroupId(), g);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
package org.asamk.signal.manager.storage.profiles;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.signal.zkgroup.profiles.ProfileKeyCredential;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ProfileStore {
|
||||
|
||||
private static final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
|
||||
@JsonProperty("profiles")
|
||||
@JsonDeserialize(using = ProfileStoreDeserializer.class)
|
||||
@JsonSerialize(using = ProfileStoreSerializer.class)
|
||||
private final List<SignalProfileEntry> profiles = new ArrayList<>();
|
||||
|
||||
public SignalProfileEntry getProfileEntry(SignalServiceAddress serviceAddress) {
|
||||
for (SignalProfileEntry entry : profiles) {
|
||||
if (entry.getServiceAddress().matches(serviceAddress)) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProfileKey getProfileKey(SignalServiceAddress serviceAddress) {
|
||||
for (SignalProfileEntry entry : profiles) {
|
||||
if (entry.getServiceAddress().matches(serviceAddress)) {
|
||||
return entry.getProfileKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void updateProfile(
|
||||
SignalServiceAddress serviceAddress,
|
||||
ProfileKey profileKey,
|
||||
long now,
|
||||
SignalProfile profile,
|
||||
ProfileKeyCredential profileKeyCredential
|
||||
) {
|
||||
SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress,
|
||||
profileKey,
|
||||
now,
|
||||
profile,
|
||||
profileKeyCredential);
|
||||
for (int i = 0; i < profiles.size(); i++) {
|
||||
if (profiles.get(i).getServiceAddress().matches(serviceAddress)) {
|
||||
profiles.set(i, newEntry);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
profiles.add(newEntry);
|
||||
}
|
||||
|
||||
public void storeProfileKey(SignalServiceAddress serviceAddress, ProfileKey profileKey) {
|
||||
SignalProfileEntry newEntry = new SignalProfileEntry(serviceAddress, profileKey, 0, null, null);
|
||||
for (int i = 0; i < profiles.size(); i++) {
|
||||
if (profiles.get(i).getServiceAddress().matches(serviceAddress)) {
|
||||
if (!profiles.get(i).getProfileKey().equals(profileKey)) {
|
||||
profiles.set(i, newEntry);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
profiles.add(newEntry);
|
||||
}
|
||||
|
||||
public static class ProfileStoreDeserializer extends JsonDeserializer<List<SignalProfileEntry>> {
|
||||
|
||||
@Override
|
||||
public List<SignalProfileEntry> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
List<SignalProfileEntry> addresses = new ArrayList<>();
|
||||
|
||||
if (node.isArray()) {
|
||||
for (JsonNode entry : node) {
|
||||
String name = entry.hasNonNull("name") ? entry.get("name").asText() : null;
|
||||
UUID uuid = entry.hasNonNull("uuid") ? UuidUtil.parseOrNull(entry.get("uuid").asText()) : null;
|
||||
final SignalServiceAddress serviceAddress = new SignalServiceAddress(uuid, name);
|
||||
ProfileKey profileKey = null;
|
||||
try {
|
||||
profileKey = new ProfileKey(Base64.decode(entry.get("profileKey").asText()));
|
||||
} catch (InvalidInputException ignored) {
|
||||
}
|
||||
ProfileKeyCredential profileKeyCredential = null;
|
||||
if (entry.hasNonNull("profileKeyCredential")) {
|
||||
try {
|
||||
profileKeyCredential = new ProfileKeyCredential(Base64.decode(entry.get(
|
||||
"profileKeyCredential").asText()));
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
long lastUpdateTimestamp = entry.get("lastUpdateTimestamp").asLong();
|
||||
SignalProfile profile = jsonProcessor.treeToValue(entry.get("profile"), SignalProfile.class);
|
||||
addresses.add(new SignalProfileEntry(serviceAddress,
|
||||
profileKey,
|
||||
lastUpdateTimestamp,
|
||||
profile,
|
||||
profileKeyCredential));
|
||||
}
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProfileStoreSerializer extends JsonSerializer<List<SignalProfileEntry>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
List<SignalProfileEntry> profiles, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartArray();
|
||||
for (SignalProfileEntry profileEntry : profiles) {
|
||||
final SignalServiceAddress address = profileEntry.getServiceAddress();
|
||||
json.writeStartObject();
|
||||
if (address.getNumber().isPresent()) {
|
||||
json.writeStringField("name", address.getNumber().get());
|
||||
}
|
||||
if (address.getUuid().isPresent()) {
|
||||
json.writeStringField("uuid", address.getUuid().get().toString());
|
||||
}
|
||||
json.writeStringField("profileKey", Base64.encodeBytes(profileEntry.getProfileKey().serialize()));
|
||||
json.writeNumberField("lastUpdateTimestamp", profileEntry.getLastUpdateTimestamp());
|
||||
json.writeObjectField("profile", profileEntry.getProfile());
|
||||
if (profileEntry.getProfileKeyCredential() != null) {
|
||||
json.writeStringField("profileKeyCredential",
|
||||
Base64.encodeBytes(profileEntry.getProfileKeyCredential().serialize()));
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
package org.asamk.signal.manager.storage.profiles;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class SignalProfile {
|
||||
|
||||
@JsonProperty
|
||||
private final String identityKey;
|
||||
|
||||
@JsonProperty
|
||||
private final String name;
|
||||
|
||||
private final File avatarFile;
|
||||
|
||||
@JsonProperty
|
||||
private final String unidentifiedAccess;
|
||||
|
||||
@JsonProperty
|
||||
private final boolean unrestrictedUnidentifiedAccess;
|
||||
|
||||
@JsonProperty
|
||||
private final Capabilities capabilities;
|
||||
|
||||
public SignalProfile(
|
||||
final String identityKey,
|
||||
final String name,
|
||||
final File avatarFile,
|
||||
final String unidentifiedAccess,
|
||||
final boolean unrestrictedUnidentifiedAccess,
|
||||
final SignalServiceProfile.Capabilities capabilities
|
||||
) {
|
||||
this.identityKey = identityKey;
|
||||
this.name = name;
|
||||
this.avatarFile = avatarFile;
|
||||
this.unidentifiedAccess = unidentifiedAccess;
|
||||
this.unrestrictedUnidentifiedAccess = unrestrictedUnidentifiedAccess;
|
||||
this.capabilities = new Capabilities();
|
||||
this.capabilities.storage = capabilities.isStorage();
|
||||
this.capabilities.gv1Migration = capabilities.isGv1Migration();
|
||||
this.capabilities.gv2 = capabilities.isGv2();
|
||||
}
|
||||
|
||||
public SignalProfile(
|
||||
@JsonProperty("identityKey") final String identityKey,
|
||||
@JsonProperty("name") final String name,
|
||||
@JsonProperty("unidentifiedAccess") final String unidentifiedAccess,
|
||||
@JsonProperty("unrestrictedUnidentifiedAccess") final boolean unrestrictedUnidentifiedAccess,
|
||||
@JsonProperty("capabilities") final Capabilities capabilities
|
||||
) {
|
||||
this.identityKey = identityKey;
|
||||
this.name = name;
|
||||
this.avatarFile = null;
|
||||
this.unidentifiedAccess = unidentifiedAccess;
|
||||
this.unrestrictedUnidentifiedAccess = unrestrictedUnidentifiedAccess;
|
||||
this.capabilities = capabilities;
|
||||
}
|
||||
|
||||
public String getIdentityKey() {
|
||||
return identityKey;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public File getAvatarFile() {
|
||||
return avatarFile;
|
||||
}
|
||||
|
||||
public String getUnidentifiedAccess() {
|
||||
return unidentifiedAccess;
|
||||
}
|
||||
|
||||
public boolean isUnrestrictedUnidentifiedAccess() {
|
||||
return unrestrictedUnidentifiedAccess;
|
||||
}
|
||||
|
||||
public Capabilities getCapabilities() {
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SignalProfile{"
|
||||
+ "identityKey='"
|
||||
+ identityKey
|
||||
+ '\''
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", avatarFile="
|
||||
+ avatarFile
|
||||
+ ", unidentifiedAccess='"
|
||||
+ unidentifiedAccess
|
||||
+ '\''
|
||||
+ ", unrestrictedUnidentifiedAccess="
|
||||
+ unrestrictedUnidentifiedAccess
|
||||
+ ", capabilities="
|
||||
+ capabilities
|
||||
+ '}';
|
||||
}
|
||||
|
||||
public static class Capabilities {
|
||||
|
||||
@JsonIgnore
|
||||
public boolean uuid;
|
||||
|
||||
@JsonProperty
|
||||
public boolean gv2;
|
||||
|
||||
@JsonProperty
|
||||
public boolean storage;
|
||||
|
||||
@JsonProperty
|
||||
public boolean gv1Migration;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package org.asamk.signal.manager.storage.profiles;
|
||||
|
||||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.signal.zkgroup.profiles.ProfileKeyCredential;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
public class SignalProfileEntry {
|
||||
|
||||
private final SignalServiceAddress serviceAddress;
|
||||
|
||||
private final ProfileKey profileKey;
|
||||
|
||||
private final long lastUpdateTimestamp;
|
||||
|
||||
private final SignalProfile profile;
|
||||
|
||||
private final ProfileKeyCredential profileKeyCredential;
|
||||
|
||||
private boolean requestPending;
|
||||
|
||||
public SignalProfileEntry(
|
||||
final SignalServiceAddress serviceAddress,
|
||||
final ProfileKey profileKey,
|
||||
final long lastUpdateTimestamp,
|
||||
final SignalProfile profile,
|
||||
final ProfileKeyCredential profileKeyCredential
|
||||
) {
|
||||
this.serviceAddress = serviceAddress;
|
||||
this.profileKey = profileKey;
|
||||
this.lastUpdateTimestamp = lastUpdateTimestamp;
|
||||
this.profile = profile;
|
||||
this.profileKeyCredential = profileKeyCredential;
|
||||
}
|
||||
|
||||
public SignalServiceAddress getServiceAddress() {
|
||||
return serviceAddress;
|
||||
}
|
||||
|
||||
public ProfileKey getProfileKey() {
|
||||
return profileKey;
|
||||
}
|
||||
|
||||
public long getLastUpdateTimestamp() {
|
||||
return lastUpdateTimestamp;
|
||||
}
|
||||
|
||||
public SignalProfile getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public ProfileKeyCredential getProfileKeyCredential() {
|
||||
return profileKeyCredential;
|
||||
}
|
||||
|
||||
public boolean isRequestPending() {
|
||||
return requestPending;
|
||||
}
|
||||
|
||||
public void setRequestPending(final boolean requestPending) {
|
||||
this.requestPending = requestPending;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import org.asamk.signal.manager.TrustLevel;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class IdentityInfo {
|
||||
|
||||
SignalServiceAddress address;
|
||||
IdentityKey identityKey;
|
||||
TrustLevel trustLevel;
|
||||
Date added;
|
||||
|
||||
public IdentityInfo(SignalServiceAddress address, IdentityKey identityKey, TrustLevel trustLevel) {
|
||||
this.address = address;
|
||||
this.identityKey = identityKey;
|
||||
this.trustLevel = trustLevel;
|
||||
this.added = new Date();
|
||||
}
|
||||
|
||||
IdentityInfo(SignalServiceAddress address, IdentityKey identityKey, TrustLevel trustLevel, Date added) {
|
||||
this.address = address;
|
||||
this.identityKey = identityKey;
|
||||
this.trustLevel = trustLevel;
|
||||
this.added = added;
|
||||
}
|
||||
|
||||
public SignalServiceAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(final SignalServiceAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
boolean isTrusted() {
|
||||
return trustLevel == TrustLevel.TRUSTED_UNVERIFIED || trustLevel == TrustLevel.TRUSTED_VERIFIED;
|
||||
}
|
||||
|
||||
public IdentityKey getIdentityKey() {
|
||||
return this.identityKey;
|
||||
}
|
||||
|
||||
public TrustLevel getTrustLevel() {
|
||||
return this.trustLevel;
|
||||
}
|
||||
|
||||
public Date getDateAdded() {
|
||||
return this.added;
|
||||
}
|
||||
|
||||
public byte[] getFingerprint() {
|
||||
return identityKey.getPublicKey().serialize();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,267 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import org.asamk.signal.manager.TrustLevel;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.InvalidKeyException;
|
||||
import org.whispersystems.libsignal.SignalProtocolAddress;
|
||||
import org.whispersystems.libsignal.state.IdentityKeyStore;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class JsonIdentityKeyStore implements IdentityKeyStore {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(JsonIdentityKeyStore.class);
|
||||
|
||||
private final List<IdentityInfo> identities = new ArrayList<>();
|
||||
|
||||
private final IdentityKeyPair identityKeyPair;
|
||||
private final int localRegistrationId;
|
||||
|
||||
private SignalServiceAddressResolver resolver;
|
||||
|
||||
public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) {
|
||||
this.identityKeyPair = identityKeyPair;
|
||||
this.localRegistrationId = localRegistrationId;
|
||||
}
|
||||
|
||||
public void setResolver(final SignalServiceAddressResolver resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
private SignalServiceAddress resolveSignalServiceAddress(String identifier) {
|
||||
if (resolver != null) {
|
||||
return resolver.resolveSignalServiceAddress(identifier);
|
||||
} else {
|
||||
return Util.getSignalServiceAddressFromIdentifier(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKeyPair getIdentityKeyPair() {
|
||||
return identityKeyPair;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalRegistrationId() {
|
||||
return localRegistrationId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey) {
|
||||
return saveIdentity(resolveSignalServiceAddress(address.getName()),
|
||||
identityKey,
|
||||
TrustLevel.TRUSTED_UNVERIFIED,
|
||||
null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given identityKey for the user name and sets the trustLevel and added timestamp.
|
||||
* If the identityKey already exists, the trustLevel and added timestamp are NOT updated.
|
||||
*
|
||||
* @param serviceAddress User address, i.e. phone number and/or uuid
|
||||
* @param identityKey The user's public key
|
||||
* @param trustLevel Level of trust: untrusted, trusted, trusted and verified
|
||||
* @param added Added timestamp, if null and the key is newly added, the current time is used.
|
||||
*/
|
||||
public boolean saveIdentity(
|
||||
SignalServiceAddress serviceAddress, IdentityKey identityKey, TrustLevel trustLevel, Date added
|
||||
) {
|
||||
for (IdentityInfo id : identities) {
|
||||
if (!id.address.matches(serviceAddress) || !id.identityKey.equals(identityKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!id.address.getUuid().isPresent() || !id.address.getNumber().isPresent()) {
|
||||
id.address = serviceAddress;
|
||||
}
|
||||
// Identity already exists, not updating the trust level
|
||||
return true;
|
||||
}
|
||||
|
||||
identities.add(new IdentityInfo(serviceAddress, identityKey, trustLevel, added != null ? added : new Date()));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update trustLevel for the given identityKey for the user name.
|
||||
*
|
||||
* @param serviceAddress User address, i.e. phone number and/or uuid
|
||||
* @param identityKey The user's public key
|
||||
* @param trustLevel Level of trust: untrusted, trusted, trusted and verified
|
||||
*/
|
||||
public void setIdentityTrustLevel(
|
||||
SignalServiceAddress serviceAddress, IdentityKey identityKey, TrustLevel trustLevel
|
||||
) {
|
||||
for (IdentityInfo id : identities) {
|
||||
if (!id.address.matches(serviceAddress) || !id.identityKey.equals(identityKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!id.address.getUuid().isPresent() || !id.address.getNumber().isPresent()) {
|
||||
id.address = serviceAddress;
|
||||
}
|
||||
id.trustLevel = trustLevel;
|
||||
return;
|
||||
}
|
||||
|
||||
identities.add(new IdentityInfo(serviceAddress, identityKey, trustLevel, new Date()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey, Direction direction) {
|
||||
// TODO implement possibility for different handling of incoming/outgoing trust decisions
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
boolean trustOnFirstUse = true;
|
||||
|
||||
for (IdentityInfo id : identities) {
|
||||
if (!id.address.matches(serviceAddress)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (id.identityKey.equals(identityKey)) {
|
||||
return id.isTrusted();
|
||||
} else {
|
||||
trustOnFirstUse = false;
|
||||
}
|
||||
}
|
||||
|
||||
return trustOnFirstUse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKey getIdentity(SignalProtocolAddress address) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
IdentityInfo identity = getIdentity(serviceAddress);
|
||||
return identity == null ? null : identity.getIdentityKey();
|
||||
}
|
||||
|
||||
public IdentityInfo getIdentity(SignalServiceAddress serviceAddress) {
|
||||
long maxDate = 0;
|
||||
IdentityInfo maxIdentity = null;
|
||||
for (IdentityInfo id : this.identities) {
|
||||
if (!id.address.matches(serviceAddress)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final long time = id.getDateAdded().getTime();
|
||||
if (maxIdentity == null || maxDate <= time) {
|
||||
maxDate = time;
|
||||
maxIdentity = id;
|
||||
}
|
||||
}
|
||||
return maxIdentity;
|
||||
}
|
||||
|
||||
public List<IdentityInfo> getIdentities() {
|
||||
// TODO deep copy
|
||||
return identities;
|
||||
}
|
||||
|
||||
public List<IdentityInfo> getIdentities(SignalServiceAddress serviceAddress) {
|
||||
List<IdentityInfo> identities = new ArrayList<>();
|
||||
for (IdentityInfo identity : this.identities) {
|
||||
if (identity.address.matches(serviceAddress)) {
|
||||
identities.add(identity);
|
||||
}
|
||||
}
|
||||
return identities;
|
||||
}
|
||||
|
||||
public static class JsonIdentityKeyStoreDeserializer extends JsonDeserializer<JsonIdentityKeyStore> {
|
||||
|
||||
@Override
|
||||
public JsonIdentityKeyStore deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
try {
|
||||
int localRegistrationId = node.get("registrationId").asInt();
|
||||
IdentityKeyPair identityKeyPair = new IdentityKeyPair(Base64.decode(node.get("identityKey").asText()));
|
||||
|
||||
JsonIdentityKeyStore keyStore = new JsonIdentityKeyStore(identityKeyPair, localRegistrationId);
|
||||
|
||||
JsonNode trustedKeysNode = node.get("trustedKeys");
|
||||
if (trustedKeysNode.isArray()) {
|
||||
for (JsonNode trustedKey : trustedKeysNode) {
|
||||
String trustedKeyName = trustedKey.hasNonNull("name") ? trustedKey.get("name").asText() : null;
|
||||
|
||||
if (UuidUtil.isUuid(trustedKeyName)) {
|
||||
// Ignore identities that were incorrectly created with UUIDs as name
|
||||
continue;
|
||||
}
|
||||
|
||||
UUID uuid = trustedKey.hasNonNull("uuid") ? UuidUtil.parseOrNull(trustedKey.get("uuid")
|
||||
.asText()) : null;
|
||||
final SignalServiceAddress serviceAddress = uuid == null
|
||||
? Util.getSignalServiceAddressFromIdentifier(trustedKeyName)
|
||||
: new SignalServiceAddress(uuid, trustedKeyName);
|
||||
try {
|
||||
IdentityKey id = new IdentityKey(Base64.decode(trustedKey.get("identityKey").asText()), 0);
|
||||
TrustLevel trustLevel = trustedKey.has("trustLevel") ? TrustLevel.fromInt(trustedKey.get(
|
||||
"trustLevel").asInt()) : TrustLevel.TRUSTED_UNVERIFIED;
|
||||
Date added = trustedKey.has("addedTimestamp") ? new Date(trustedKey.get("addedTimestamp")
|
||||
.asLong()) : new Date();
|
||||
keyStore.saveIdentity(serviceAddress, id, trustLevel, added);
|
||||
} catch (InvalidKeyException | IOException e) {
|
||||
logger.warn("Error while decoding key for {}: {}", trustedKeyName, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return keyStore;
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JsonIdentityKeyStoreSerializer extends JsonSerializer<JsonIdentityKeyStore> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
JsonIdentityKeyStore jsonIdentityKeyStore, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartObject();
|
||||
json.writeNumberField("registrationId", jsonIdentityKeyStore.getLocalRegistrationId());
|
||||
json.writeStringField("identityKey",
|
||||
Base64.encodeBytes(jsonIdentityKeyStore.getIdentityKeyPair().serialize()));
|
||||
json.writeArrayFieldStart("trustedKeys");
|
||||
for (IdentityInfo trustedKey : jsonIdentityKeyStore.identities) {
|
||||
json.writeStartObject();
|
||||
if (trustedKey.getAddress().getNumber().isPresent()) {
|
||||
json.writeStringField("name", trustedKey.getAddress().getNumber().get());
|
||||
}
|
||||
if (trustedKey.getAddress().getUuid().isPresent()) {
|
||||
json.writeStringField("uuid", trustedKey.getAddress().getUuid().get().toString());
|
||||
}
|
||||
json.writeStringField("identityKey", Base64.encodeBytes(trustedKey.identityKey.serialize()));
|
||||
json.writeNumberField("trustLevel", trustedKey.trustLevel.ordinal());
|
||||
json.writeNumberField("addedTimestamp", trustedKey.added.getTime());
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
json.writeEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.libsignal.InvalidKeyIdException;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.PreKeyStore;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class JsonPreKeyStore implements PreKeyStore {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(JsonPreKeyStore.class);
|
||||
|
||||
private final Map<Integer, byte[]> store = new HashMap<>();
|
||||
|
||||
public JsonPreKeyStore() {
|
||||
|
||||
}
|
||||
|
||||
private void addPreKeys(Map<Integer, byte[]> preKeys) {
|
||||
store.putAll(preKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException {
|
||||
try {
|
||||
if (!store.containsKey(preKeyId)) {
|
||||
throw new InvalidKeyIdException("No such prekeyrecord!");
|
||||
}
|
||||
|
||||
return new PreKeyRecord(store.get(preKeyId));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storePreKey(int preKeyId, PreKeyRecord record) {
|
||||
store.put(preKeyId, record.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPreKey(int preKeyId) {
|
||||
return store.containsKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePreKey(int preKeyId) {
|
||||
store.remove(preKeyId);
|
||||
}
|
||||
|
||||
public static class JsonPreKeyStoreDeserializer extends JsonDeserializer<JsonPreKeyStore> {
|
||||
|
||||
@Override
|
||||
public JsonPreKeyStore deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
Map<Integer, byte[]> preKeyMap = new HashMap<>();
|
||||
if (node.isArray()) {
|
||||
for (JsonNode preKey : node) {
|
||||
Integer preKeyId = preKey.get("id").asInt();
|
||||
try {
|
||||
preKeyMap.put(preKeyId, Base64.decode(preKey.get("record").asText()));
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error while decoding prekey for {}: {}", preKeyId, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonPreKeyStore keyStore = new JsonPreKeyStore();
|
||||
keyStore.addPreKeys(preKeyMap);
|
||||
|
||||
return keyStore;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class JsonPreKeyStoreSerializer extends JsonSerializer<JsonPreKeyStore> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
JsonPreKeyStore jsonPreKeyStore, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartArray();
|
||||
for (Map.Entry<Integer, byte[]> preKey : jsonPreKeyStore.store.entrySet()) {
|
||||
json.writeStartObject();
|
||||
json.writeNumberField("id", preKey.getKey());
|
||||
json.writeStringField("record", Base64.encodeBytes(preKey.getValue()));
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.libsignal.SignalProtocolAddress;
|
||||
import org.whispersystems.libsignal.state.SessionRecord;
|
||||
import org.whispersystems.libsignal.state.SessionStore;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
class JsonSessionStore implements SessionStore {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(JsonSessionStore.class);
|
||||
|
||||
private final List<SessionInfo> sessions = new ArrayList<>();
|
||||
|
||||
private SignalServiceAddressResolver resolver;
|
||||
|
||||
public JsonSessionStore() {
|
||||
}
|
||||
|
||||
public void setResolver(final SignalServiceAddressResolver resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
private SignalServiceAddress resolveSignalServiceAddress(String identifier) {
|
||||
if (resolver != null) {
|
||||
return resolver.resolveSignalServiceAddress(identifier);
|
||||
} else {
|
||||
return Util.getSignalServiceAddressFromIdentifier(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized SessionRecord loadSession(SignalProtocolAddress address) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
for (SessionInfo info : sessions) {
|
||||
if (info.address.matches(serviceAddress) && info.deviceId == address.getDeviceId()) {
|
||||
try {
|
||||
return new SessionRecord(info.sessionRecord);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to load session, resetting session: {}", e.getMessage());
|
||||
final SessionRecord sessionRecord = new SessionRecord();
|
||||
info.sessionRecord = sessionRecord.serialize();
|
||||
return sessionRecord;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new SessionRecord();
|
||||
}
|
||||
|
||||
public synchronized List<SessionInfo> getSessions() {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<Integer> getSubDeviceSessions(String name) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(name);
|
||||
|
||||
List<Integer> deviceIds = new LinkedList<>();
|
||||
for (SessionInfo info : sessions) {
|
||||
if (info.address.matches(serviceAddress) && info.deviceId != 1) {
|
||||
deviceIds.add(info.deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
return deviceIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void storeSession(SignalProtocolAddress address, SessionRecord record) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
for (SessionInfo info : sessions) {
|
||||
if (info.address.matches(serviceAddress) && info.deviceId == address.getDeviceId()) {
|
||||
if (!info.address.getUuid().isPresent() || !info.address.getNumber().isPresent()) {
|
||||
info.address = serviceAddress;
|
||||
}
|
||||
info.sessionRecord = record.serialize();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sessions.add(new SessionInfo(serviceAddress, address.getDeviceId(), record.serialize()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean containsSession(SignalProtocolAddress address) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
for (SessionInfo info : sessions) {
|
||||
if (info.address.matches(serviceAddress) && info.deviceId == address.getDeviceId()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void deleteSession(SignalProtocolAddress address) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(address.getName());
|
||||
sessions.removeIf(info -> info.address.matches(serviceAddress) && info.deviceId == address.getDeviceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void deleteAllSessions(String name) {
|
||||
SignalServiceAddress serviceAddress = resolveSignalServiceAddress(name);
|
||||
deleteAllSessions(serviceAddress);
|
||||
}
|
||||
|
||||
public synchronized void deleteAllSessions(SignalServiceAddress serviceAddress) {
|
||||
sessions.removeIf(info -> info.address.matches(serviceAddress));
|
||||
}
|
||||
|
||||
public static class JsonSessionStoreDeserializer extends JsonDeserializer<JsonSessionStore> {
|
||||
|
||||
@Override
|
||||
public JsonSessionStore deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
JsonSessionStore sessionStore = new JsonSessionStore();
|
||||
|
||||
if (node.isArray()) {
|
||||
for (JsonNode session : node) {
|
||||
String sessionName = session.hasNonNull("name") ? session.get("name").asText() : null;
|
||||
if (UuidUtil.isUuid(sessionName)) {
|
||||
// Ignore sessions that were incorrectly created with UUIDs as name
|
||||
continue;
|
||||
}
|
||||
|
||||
UUID uuid = session.hasNonNull("uuid") ? UuidUtil.parseOrNull(session.get("uuid").asText()) : null;
|
||||
final SignalServiceAddress serviceAddress = uuid == null
|
||||
? Util.getSignalServiceAddressFromIdentifier(sessionName)
|
||||
: new SignalServiceAddress(uuid, sessionName);
|
||||
final int deviceId = session.get("deviceId").asInt();
|
||||
final String record = session.get("record").asText();
|
||||
try {
|
||||
SessionInfo sessionInfo = new SessionInfo(serviceAddress, deviceId, Base64.decode(record));
|
||||
sessionStore.sessions.add(sessionInfo);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error while decoding session for {}: {}", sessionName, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sessionStore;
|
||||
}
|
||||
}
|
||||
|
||||
public static class JsonSessionStoreSerializer extends JsonSerializer<JsonSessionStore> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
JsonSessionStore jsonSessionStore, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartArray();
|
||||
for (SessionInfo sessionInfo : jsonSessionStore.sessions) {
|
||||
json.writeStartObject();
|
||||
if (sessionInfo.address.getNumber().isPresent()) {
|
||||
json.writeStringField("name", sessionInfo.address.getNumber().get());
|
||||
}
|
||||
if (sessionInfo.address.getUuid().isPresent()) {
|
||||
json.writeStringField("uuid", sessionInfo.address.getUuid().get().toString());
|
||||
}
|
||||
json.writeNumberField("deviceId", sessionInfo.deviceId);
|
||||
json.writeStringField("record", Base64.encodeBytes(sessionInfo.sessionRecord));
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,198 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.asamk.signal.manager.TrustLevel;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.InvalidKeyIdException;
|
||||
import org.whispersystems.libsignal.SignalProtocolAddress;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SessionRecord;
|
||||
import org.whispersystems.libsignal.state.SignalProtocolStore;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JsonSignalProtocolStore implements SignalProtocolStore {
|
||||
|
||||
@JsonProperty("preKeys")
|
||||
@JsonDeserialize(using = JsonPreKeyStore.JsonPreKeyStoreDeserializer.class)
|
||||
@JsonSerialize(using = JsonPreKeyStore.JsonPreKeyStoreSerializer.class)
|
||||
private JsonPreKeyStore preKeyStore;
|
||||
|
||||
@JsonProperty("sessionStore")
|
||||
@JsonDeserialize(using = JsonSessionStore.JsonSessionStoreDeserializer.class)
|
||||
@JsonSerialize(using = JsonSessionStore.JsonSessionStoreSerializer.class)
|
||||
private JsonSessionStore sessionStore;
|
||||
|
||||
@JsonProperty("signedPreKeyStore")
|
||||
@JsonDeserialize(using = JsonSignedPreKeyStore.JsonSignedPreKeyStoreDeserializer.class)
|
||||
@JsonSerialize(using = JsonSignedPreKeyStore.JsonSignedPreKeyStoreSerializer.class)
|
||||
private JsonSignedPreKeyStore signedPreKeyStore;
|
||||
|
||||
@JsonProperty("identityKeyStore")
|
||||
@JsonDeserialize(using = JsonIdentityKeyStore.JsonIdentityKeyStoreDeserializer.class)
|
||||
@JsonSerialize(using = JsonIdentityKeyStore.JsonIdentityKeyStoreSerializer.class)
|
||||
private JsonIdentityKeyStore identityKeyStore;
|
||||
|
||||
public JsonSignalProtocolStore() {
|
||||
}
|
||||
|
||||
public JsonSignalProtocolStore(
|
||||
JsonPreKeyStore preKeyStore,
|
||||
JsonSessionStore sessionStore,
|
||||
JsonSignedPreKeyStore signedPreKeyStore,
|
||||
JsonIdentityKeyStore identityKeyStore
|
||||
) {
|
||||
this.preKeyStore = preKeyStore;
|
||||
this.sessionStore = sessionStore;
|
||||
this.signedPreKeyStore = signedPreKeyStore;
|
||||
this.identityKeyStore = identityKeyStore;
|
||||
}
|
||||
|
||||
public JsonSignalProtocolStore(IdentityKeyPair identityKeyPair, int registrationId) {
|
||||
preKeyStore = new JsonPreKeyStore();
|
||||
sessionStore = new JsonSessionStore();
|
||||
signedPreKeyStore = new JsonSignedPreKeyStore();
|
||||
this.identityKeyStore = new JsonIdentityKeyStore(identityKeyPair, registrationId);
|
||||
}
|
||||
|
||||
public void setResolver(final SignalServiceAddressResolver resolver) {
|
||||
sessionStore.setResolver(resolver);
|
||||
identityKeyStore.setResolver(resolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKeyPair getIdentityKeyPair() {
|
||||
return identityKeyStore.getIdentityKeyPair();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalRegistrationId() {
|
||||
return identityKeyStore.getLocalRegistrationId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey) {
|
||||
return identityKeyStore.saveIdentity(address, identityKey);
|
||||
}
|
||||
|
||||
public void saveIdentity(SignalServiceAddress serviceAddress, IdentityKey identityKey, TrustLevel trustLevel) {
|
||||
identityKeyStore.saveIdentity(serviceAddress, identityKey, trustLevel, null);
|
||||
}
|
||||
|
||||
public void setIdentityTrustLevel(
|
||||
SignalServiceAddress serviceAddress, IdentityKey identityKey, TrustLevel trustLevel
|
||||
) {
|
||||
identityKeyStore.setIdentityTrustLevel(serviceAddress, identityKey, trustLevel);
|
||||
}
|
||||
|
||||
public List<IdentityInfo> getIdentities() {
|
||||
return identityKeyStore.getIdentities();
|
||||
}
|
||||
|
||||
public List<IdentityInfo> getIdentities(SignalServiceAddress serviceAddress) {
|
||||
return identityKeyStore.getIdentities(serviceAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey, Direction direction) {
|
||||
return identityKeyStore.isTrustedIdentity(address, identityKey, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKey getIdentity(SignalProtocolAddress address) {
|
||||
return identityKeyStore.getIdentity(address);
|
||||
}
|
||||
|
||||
public IdentityInfo getIdentity(SignalServiceAddress serviceAddress) {
|
||||
return identityKeyStore.getIdentity(serviceAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException {
|
||||
return preKeyStore.loadPreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storePreKey(int preKeyId, PreKeyRecord record) {
|
||||
preKeyStore.storePreKey(preKeyId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPreKey(int preKeyId) {
|
||||
return preKeyStore.containsPreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePreKey(int preKeyId) {
|
||||
preKeyStore.removePreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionRecord loadSession(SignalProtocolAddress address) {
|
||||
return sessionStore.loadSession(address);
|
||||
}
|
||||
|
||||
public List<SessionInfo> getSessions() {
|
||||
return sessionStore.getSessions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getSubDeviceSessions(String name) {
|
||||
return sessionStore.getSubDeviceSessions(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSession(SignalProtocolAddress address, SessionRecord record) {
|
||||
sessionStore.storeSession(address, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSession(SignalProtocolAddress address) {
|
||||
return sessionStore.containsSession(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSession(SignalProtocolAddress address) {
|
||||
sessionStore.deleteSession(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllSessions(String name) {
|
||||
sessionStore.deleteAllSessions(name);
|
||||
}
|
||||
|
||||
public void deleteAllSessions(SignalServiceAddress serviceAddress) {
|
||||
sessionStore.deleteAllSessions(serviceAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
|
||||
return signedPreKeyStore.loadSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignedPreKeyRecord> loadSignedPreKeys() {
|
||||
return signedPreKeyStore.loadSignedPreKeys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
|
||||
signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSignedPreKey(int signedPreKeyId) {
|
||||
return signedPreKeyStore.containsSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSignedPreKey(int signedPreKeyId) {
|
||||
signedPreKeyStore.removeSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.libsignal.InvalidKeyIdException;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyStore;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class JsonSignedPreKeyStore implements SignedPreKeyStore {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(JsonSignedPreKeyStore.class);
|
||||
|
||||
private final Map<Integer, byte[]> store = new HashMap<>();
|
||||
|
||||
public JsonSignedPreKeyStore() {
|
||||
|
||||
}
|
||||
|
||||
private void addSignedPreKeys(Map<Integer, byte[]> preKeys) {
|
||||
store.putAll(preKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
|
||||
try {
|
||||
if (!store.containsKey(signedPreKeyId)) {
|
||||
throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId);
|
||||
}
|
||||
|
||||
return new SignedPreKeyRecord(store.get(signedPreKeyId));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignedPreKeyRecord> loadSignedPreKeys() {
|
||||
try {
|
||||
List<SignedPreKeyRecord> results = new LinkedList<>();
|
||||
|
||||
for (byte[] serialized : store.values()) {
|
||||
results.add(new SignedPreKeyRecord(serialized));
|
||||
}
|
||||
|
||||
return results;
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
|
||||
store.put(signedPreKeyId, record.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSignedPreKey(int signedPreKeyId) {
|
||||
return store.containsKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSignedPreKey(int signedPreKeyId) {
|
||||
store.remove(signedPreKeyId);
|
||||
}
|
||||
|
||||
public static class JsonSignedPreKeyStoreDeserializer extends JsonDeserializer<JsonSignedPreKeyStore> {
|
||||
|
||||
@Override
|
||||
public JsonSignedPreKeyStore deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
Map<Integer, byte[]> preKeyMap = new HashMap<>();
|
||||
if (node.isArray()) {
|
||||
for (JsonNode preKey : node) {
|
||||
Integer preKeyId = preKey.get("id").asInt();
|
||||
try {
|
||||
preKeyMap.put(preKeyId, Base64.decode(preKey.get("record").asText()));
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error while decoding prekey for {}: {}", preKeyId, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonSignedPreKeyStore keyStore = new JsonSignedPreKeyStore();
|
||||
keyStore.addSignedPreKeys(preKeyMap);
|
||||
|
||||
return keyStore;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class JsonSignedPreKeyStoreSerializer extends JsonSerializer<JsonSignedPreKeyStore> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
JsonSignedPreKeyStore jsonPreKeyStore, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartArray();
|
||||
for (Map.Entry<Integer, byte[]> signedPreKey : jsonPreKeyStore.store.entrySet()) {
|
||||
json.writeStartObject();
|
||||
json.writeNumberField("id", signedPreKey.getKey());
|
||||
json.writeStringField("record", Base64.encodeBytes(signedPreKey.getValue()));
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RecipientStore {
|
||||
|
||||
@JsonProperty("recipientStore")
|
||||
@JsonDeserialize(using = RecipientStoreDeserializer.class)
|
||||
@JsonSerialize(using = RecipientStoreSerializer.class)
|
||||
private final Set<SignalServiceAddress> addresses = new HashSet<>();
|
||||
|
||||
public SignalServiceAddress resolveServiceAddress(SignalServiceAddress serviceAddress) {
|
||||
if (addresses.contains(serviceAddress)) {
|
||||
// If the Set already contains the exact address with UUID and Number,
|
||||
// we can just return it here.
|
||||
return serviceAddress;
|
||||
}
|
||||
|
||||
for (SignalServiceAddress address : addresses) {
|
||||
if (address.matches(serviceAddress)) {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
|
||||
if (serviceAddress.getNumber().isPresent() && serviceAddress.getUuid().isPresent()) {
|
||||
addresses.add(serviceAddress);
|
||||
}
|
||||
|
||||
return serviceAddress;
|
||||
}
|
||||
|
||||
public static class RecipientStoreDeserializer extends JsonDeserializer<Set<SignalServiceAddress>> {
|
||||
|
||||
@Override
|
||||
public Set<SignalServiceAddress> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
|
||||
Set<SignalServiceAddress> addresses = new HashSet<>();
|
||||
|
||||
if (node.isArray()) {
|
||||
for (JsonNode recipient : node) {
|
||||
String recipientName = recipient.get("name").asText();
|
||||
UUID uuid = UuidUtil.parseOrThrow(recipient.get("uuid").asText());
|
||||
final SignalServiceAddress serviceAddress = new SignalServiceAddress(uuid, recipientName);
|
||||
addresses.add(serviceAddress);
|
||||
}
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RecipientStoreSerializer extends JsonSerializer<Set<SignalServiceAddress>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
Set<SignalServiceAddress> addresses, JsonGenerator json, SerializerProvider serializerProvider
|
||||
) throws IOException {
|
||||
json.writeStartArray();
|
||||
for (SignalServiceAddress address : addresses) {
|
||||
json.writeStartObject();
|
||||
json.writeStringField("name", address.getNumber().get());
|
||||
json.writeStringField("uuid", address.getUuid().get().toString());
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
public class SessionInfo {
|
||||
|
||||
public SignalServiceAddress address;
|
||||
|
||||
public int deviceId;
|
||||
|
||||
public byte[] sessionRecord;
|
||||
|
||||
public SessionInfo(final SignalServiceAddress address, final int deviceId, final byte[] sessionRecord) {
|
||||
this.address = address;
|
||||
this.deviceId = deviceId;
|
||||
this.sessionRecord = sessionRecord;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.asamk.signal.manager.storage.protocol;
|
||||
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
public interface SignalServiceAddressResolver {
|
||||
|
||||
/**
|
||||
* Get a SignalServiceAddress with number and/or uuid from an identifier name.
|
||||
*
|
||||
* @param identifier can be either a serialized uuid or a e164 phone number
|
||||
*/
|
||||
SignalServiceAddress resolveSignalServiceAddress(String identifier);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package org.asamk.signal.manager.storage.stickers;
|
||||
|
||||
public class Sticker {
|
||||
|
||||
private final byte[] packId;
|
||||
private final byte[] packKey;
|
||||
private boolean installed;
|
||||
|
||||
public Sticker(final byte[] packId, final byte[] packKey) {
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
}
|
||||
|
||||
public Sticker(final byte[] packId, final byte[] packKey, final boolean installed) {
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
this.installed = installed;
|
||||
}
|
||||
|
||||
public byte[] getPackId() {
|
||||
return packId;
|
||||
}
|
||||
|
||||
public byte[] getPackKey() {
|
||||
return packKey;
|
||||
}
|
||||
|
||||
public boolean isInstalled() {
|
||||
return installed;
|
||||
}
|
||||
|
||||
public void setInstalled(final boolean installed) {
|
||||
this.installed = installed;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package org.asamk.signal.manager.storage.stickers;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class StickerStore {
|
||||
|
||||
private static final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
|
||||
@JsonSerialize(using = StickersSerializer.class)
|
||||
@JsonDeserialize(using = StickersDeserializer.class)
|
||||
private final Map<byte[], Sticker> stickers = new HashMap<>();
|
||||
|
||||
public Sticker getSticker(byte[] packId) {
|
||||
return stickers.get(packId);
|
||||
}
|
||||
|
||||
public void updateSticker(Sticker sticker) {
|
||||
stickers.put(sticker.getPackId(), sticker);
|
||||
}
|
||||
|
||||
private static class StickersSerializer extends JsonSerializer<Map<byte[], Sticker>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final Map<byte[], Sticker> value, final JsonGenerator jgen, final SerializerProvider provider
|
||||
) throws IOException {
|
||||
final Collection<Sticker> stickers = value.values();
|
||||
jgen.writeStartArray(stickers.size());
|
||||
for (Sticker sticker : stickers) {
|
||||
jgen.writeStartObject();
|
||||
jgen.writeStringField("packId", Base64.encodeBytes(sticker.getPackId()));
|
||||
jgen.writeStringField("packKey", Base64.encodeBytes(sticker.getPackKey()));
|
||||
jgen.writeBooleanField("installed", sticker.isInstalled());
|
||||
jgen.writeEndObject();
|
||||
}
|
||||
jgen.writeEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static class StickersDeserializer extends JsonDeserializer<Map<byte[], Sticker>> {
|
||||
|
||||
@Override
|
||||
public Map<byte[], Sticker> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
Map<byte[], Sticker> stickers = new HashMap<>();
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
for (JsonNode n : node) {
|
||||
byte[] packId = Base64.decode(n.get("packId").asText());
|
||||
byte[] packKey = Base64.decode(n.get("packKey").asText());
|
||||
boolean installed = n.get("installed").asBoolean(false);
|
||||
stickers.put(packId, new Sticker(packId, packKey, installed));
|
||||
}
|
||||
|
||||
return stickers;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.asamk.signal.manager.storage.threads;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class LegacyJsonThreadStore {
|
||||
|
||||
private static final ObjectMapper jsonProcessor = new ObjectMapper();
|
||||
|
||||
@JsonProperty("threads")
|
||||
@JsonSerialize(using = MapToListSerializer.class)
|
||||
@JsonDeserialize(using = ThreadsDeserializer.class)
|
||||
private Map<String, ThreadInfo> threads = new HashMap<>();
|
||||
|
||||
public List<ThreadInfo> getThreads() {
|
||||
return new ArrayList<>(threads.values());
|
||||
}
|
||||
|
||||
private static class MapToListSerializer extends JsonSerializer<Map<?, ?>> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider
|
||||
) throws IOException {
|
||||
jgen.writeObject(value.values());
|
||||
}
|
||||
}
|
||||
|
||||
private static class ThreadsDeserializer extends JsonDeserializer<Map<String, ThreadInfo>> {
|
||||
|
||||
@Override
|
||||
public Map<String, ThreadInfo> deserialize(
|
||||
JsonParser jsonParser, DeserializationContext deserializationContext
|
||||
) throws IOException {
|
||||
Map<String, ThreadInfo> threads = new HashMap<>();
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
for (JsonNode n : node) {
|
||||
ThreadInfo t = jsonProcessor.treeToValue(n, ThreadInfo.class);
|
||||
threads.put(t.id, t);
|
||||
}
|
||||
|
||||
return threads;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.asamk.signal.manager.storage.threads;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ThreadInfo {
|
||||
|
||||
@JsonProperty
|
||||
public String id;
|
||||
|
||||
@JsonProperty
|
||||
public int messageExpirationTime;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue