]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/storage/groups/JsonGroupStore.java
570282bf3affe9760864651cae06943baff62f21
[signal-cli] / src / main / java / org / asamk / signal / storage / groups / JsonGroupStore.java
1 package org.asamk.signal.storage.groups;
2
3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import com.fasterxml.jackson.core.JsonGenerator;
5 import com.fasterxml.jackson.core.JsonParser;
6 import com.fasterxml.jackson.databind.DeserializationContext;
7 import com.fasterxml.jackson.databind.JsonDeserializer;
8 import com.fasterxml.jackson.databind.JsonNode;
9 import com.fasterxml.jackson.databind.JsonSerializer;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializerProvider;
12 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
13 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
14
15 import org.asamk.signal.util.Hex;
16 import org.asamk.signal.util.IOUtils;
17 import org.signal.storageservice.protos.groups.local.DecryptedGroup;
18 import org.signal.zkgroup.InvalidInputException;
19 import org.signal.zkgroup.groups.GroupMasterKey;
20 import org.whispersystems.util.Base64;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 public class JsonGroupStore {
33
34 private static final ObjectMapper jsonProcessor = new ObjectMapper();
35 public File groupCachePath;
36
37 @JsonProperty("groups")
38 @JsonSerialize(using = GroupsSerializer.class)
39 @JsonDeserialize(using = GroupsDeserializer.class)
40 private final Map<String, GroupInfo> groups = new HashMap<>();
41
42 private JsonGroupStore() {
43 }
44
45 public JsonGroupStore(final File groupCachePath) {
46 this.groupCachePath = groupCachePath;
47 }
48
49 public void updateGroup(GroupInfo group) {
50 groups.put(Base64.encodeBytes(group.groupId), group);
51 if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() != null) {
52 try {
53 IOUtils.createPrivateDirectories(groupCachePath);
54 try (FileOutputStream stream = new FileOutputStream(getGroupFile(group.groupId))) {
55 ((GroupInfoV2) group).getGroup().writeTo(stream);
56 }
57 } catch (IOException e) {
58 System.err.println("Failed to cache group, ignoring ...");
59 }
60 }
61 }
62
63 public GroupInfo getGroup(byte[] groupId) {
64 final GroupInfo group = groups.get(Base64.encodeBytes(groupId));
65 loadDecryptedGroup(group);
66 return group;
67 }
68
69 private void loadDecryptedGroup(final GroupInfo group) {
70 if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() == null) {
71 try (FileInputStream stream = new FileInputStream(getGroupFile(group.groupId))) {
72 ((GroupInfoV2) group).setGroup(DecryptedGroup.parseFrom(stream));
73 } catch (IOException ignored) {
74 }
75 }
76 }
77
78 private File getGroupFile(final byte[] groupId) {
79 return new File(groupCachePath, Hex.toStringCondensed(groupId));
80 }
81
82 public GroupInfoV1 getOrCreateGroupV1(byte[] groupId) {
83 GroupInfo group = groups.get(Base64.encodeBytes(groupId));
84 if (group instanceof GroupInfoV1) {
85 return (GroupInfoV1) group;
86 }
87
88 if (group == null) {
89 return new GroupInfoV1(groupId);
90 }
91
92 return null;
93 }
94
95 public List<GroupInfo> getGroups() {
96 final Collection<GroupInfo> groups = this.groups.values();
97 for (GroupInfo group : groups) {
98 loadDecryptedGroup(group);
99 }
100 return new ArrayList<>(groups);
101 }
102
103 private static class GroupsSerializer extends JsonSerializer<Map<String, GroupInfo>> {
104
105 @Override
106 public void serialize(
107 final Map<String, GroupInfo> value, final JsonGenerator jgen, final SerializerProvider provider
108 ) throws IOException {
109 final Collection<GroupInfo> groups = value.values();
110 jgen.writeStartArray(groups.size());
111 for (GroupInfo group : groups) {
112 if (group instanceof GroupInfoV1) {
113 jgen.writeObject(group);
114 } else if (group instanceof GroupInfoV2) {
115 final GroupInfoV2 groupV2 = (GroupInfoV2) group;
116 jgen.writeStartObject();
117 jgen.writeStringField("groupId", Base64.encodeBytes(groupV2.groupId));
118 jgen.writeStringField("masterKey", Base64.encodeBytes(groupV2.getMasterKey().serialize()));
119 jgen.writeBooleanField("blocked", groupV2.isBlocked());
120 jgen.writeEndObject();
121 } else {
122 throw new AssertionError("Unknown group version");
123 }
124 }
125 jgen.writeEndArray();
126 }
127 }
128
129 private static class GroupsDeserializer extends JsonDeserializer<Map<String, GroupInfo>> {
130
131 @Override
132 public Map<String, GroupInfo> deserialize(
133 JsonParser jsonParser, DeserializationContext deserializationContext
134 ) throws IOException {
135 Map<String, GroupInfo> groups = new HashMap<>();
136 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
137 for (JsonNode n : node) {
138 GroupInfo g;
139 if (n.has("masterKey")) {
140 // a v2 group
141 byte[] groupId = Base64.decode(n.get("groupId").asText());
142 try {
143 GroupMasterKey masterKey = new GroupMasterKey(Base64.decode(n.get("masterKey").asText()));
144 g = new GroupInfoV2(groupId, masterKey);
145 } catch (InvalidInputException e) {
146 throw new AssertionError("Invalid master key for group " + Base64.encodeBytes(groupId));
147 }
148 g.setBlocked(n.get("blocked").asBoolean(false));
149 } else {
150 g = jsonProcessor.treeToValue(n, GroupInfoV1.class);
151 }
152 groups.put(Base64.encodeBytes(g.groupId), g);
153 }
154
155 return groups;
156 }
157 }
158 }