1 package org
.asamk
.signal
.manager
.storage
.sessions
;
3 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
4 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
5 import org
.asamk
.signal
.manager
.util
.IOUtils
;
6 import org
.asamk
.signal
.manager
.util
.Utils
;
7 import org
.slf4j
.Logger
;
8 import org
.slf4j
.LoggerFactory
;
9 import org
.whispersystems
.libsignal
.NoSessionException
;
10 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
11 import org
.whispersystems
.libsignal
.protocol
.CiphertextMessage
;
12 import org
.whispersystems
.libsignal
.state
.SessionRecord
;
13 import org
.whispersystems
.signalservice
.api
.SignalServiceSessionStore
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.IOException
;
19 import java
.nio
.file
.Files
;
20 import java
.util
.Arrays
;
21 import java
.util
.Collection
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
25 import java
.util
.Objects
;
26 import java
.util
.regex
.Matcher
;
27 import java
.util
.regex
.Pattern
;
28 import java
.util
.stream
.Collectors
;
30 public class SessionStore
implements SignalServiceSessionStore
{
32 private final static Logger logger
= LoggerFactory
.getLogger(SessionStore
.class);
34 private final Map
<Key
, SessionRecord
> cachedSessions
= new HashMap
<>();
36 private final File sessionsPath
;
38 private final RecipientResolver resolver
;
41 final File sessionsPath
, final RecipientResolver resolver
43 this.sessionsPath
= sessionsPath
;
44 this.resolver
= resolver
;
48 public SessionRecord
loadSession(SignalProtocolAddress address
) {
49 final var key
= getKey(address
);
51 synchronized (cachedSessions
) {
52 final var session
= loadSessionLocked(key
);
53 if (session
== null) {
54 return new SessionRecord();
61 public List
<SessionRecord
> loadExistingSessions(final List
<SignalProtocolAddress
> addresses
) throws NoSessionException
{
62 final var keys
= addresses
.stream().map(this::getKey
).collect(Collectors
.toList());
64 synchronized (cachedSessions
) {
65 final var sessions
= keys
.stream()
66 .map(this::loadSessionLocked
)
67 .filter(Objects
::nonNull
)
68 .collect(Collectors
.toList());
70 if (sessions
.size() != addresses
.size()) {
71 String message
= "Mismatch! Asked for "
73 + " sessions, but only found "
77 throw new NoSessionException(message
);
85 public List
<Integer
> getSubDeviceSessions(String name
) {
86 final var recipientId
= resolveRecipient(name
);
88 synchronized (cachedSessions
) {
89 return getKeysLocked(recipientId
).stream()
90 // get all sessions for recipient except main device session
91 .filter(key
-> key
.getDeviceId() != 1 && key
.getRecipientId().equals(recipientId
))
92 .map(Key
::getDeviceId
)
93 .collect(Collectors
.toList());
98 public void storeSession(SignalProtocolAddress address
, SessionRecord session
) {
99 final var key
= getKey(address
);
101 synchronized (cachedSessions
) {
102 storeSessionLocked(key
, session
);
107 public boolean containsSession(SignalProtocolAddress address
) {
108 final var key
= getKey(address
);
110 synchronized (cachedSessions
) {
111 final var session
= loadSessionLocked(key
);
112 if (session
== null) {
116 return session
.hasSenderChain() && session
.getSessionVersion() == CiphertextMessage
.CURRENT_VERSION
;
121 public void deleteSession(SignalProtocolAddress address
) {
122 final var key
= getKey(address
);
124 synchronized (cachedSessions
) {
125 deleteSessionLocked(key
);
130 public void deleteAllSessions(String name
) {
131 final var recipientId
= resolveRecipient(name
);
132 deleteAllSessions(recipientId
);
135 public void deleteAllSessions(RecipientId recipientId
) {
136 synchronized (cachedSessions
) {
137 final var keys
= getKeysLocked(recipientId
);
138 for (var key
: keys
) {
139 deleteSessionLocked(key
);
145 public void archiveSession(final SignalProtocolAddress address
) {
146 final var key
= getKey(address
);
148 synchronized (cachedSessions
) {
149 archiveSessionLocked(key
);
153 public void archiveAllSessions() {
154 synchronized (cachedSessions
) {
155 final var keys
= getKeysLocked();
156 for (var key
: keys
) {
157 archiveSessionLocked(key
);
162 public void archiveSessions(final RecipientId recipientId
) {
163 synchronized (cachedSessions
) {
164 getKeysLocked().stream()
165 .filter(key
-> key
.recipientId
.equals(recipientId
))
166 .forEach(this::archiveSessionLocked
);
170 public void mergeRecipients(RecipientId recipientId
, RecipientId toBeMergedRecipientId
) {
171 synchronized (cachedSessions
) {
172 final var otherHasSession
= getKeysLocked(toBeMergedRecipientId
).size() > 0;
173 if (!otherHasSession
) {
177 final var hasSession
= getKeysLocked(recipientId
).size() > 0;
179 logger
.debug("To be merged recipient had sessions, deleting.");
180 deleteAllSessions(toBeMergedRecipientId
);
182 logger
.debug("To be merged recipient had sessions, re-assigning to the new recipient.");
183 final var keys
= getKeysLocked(toBeMergedRecipientId
);
184 for (var key
: keys
) {
185 final var session
= loadSessionLocked(key
);
186 deleteSessionLocked(key
);
187 if (session
== null) {
190 final var newKey
= new Key(recipientId
, key
.getDeviceId());
191 storeSessionLocked(newKey
, session
);
198 * @param identifier can be either a serialized uuid or a e164 phone number
200 private RecipientId
resolveRecipient(String identifier
) {
201 return resolver
.resolveRecipient(Utils
.getSignalServiceAddressFromIdentifier(identifier
));
204 private Key
getKey(final SignalProtocolAddress address
) {
205 final var recipientId
= resolveRecipient(address
.getName());
206 return new Key(recipientId
, address
.getDeviceId());
209 private List
<Key
> getKeysLocked(RecipientId recipientId
) {
210 final var files
= sessionsPath
.listFiles((_file
, s
) -> s
.startsWith(recipientId
.getId() + "_"));
214 return parseFileNames(files
);
217 private Collection
<Key
> getKeysLocked() {
218 final var files
= sessionsPath
.listFiles();
222 return parseFileNames(files
);
225 final Pattern sessionFileNamePattern
= Pattern
.compile("([0-9]+)_([0-9]+)");
227 private List
<Key
> parseFileNames(final File
[] files
) {
228 return Arrays
.stream(files
)
229 .map(f
-> sessionFileNamePattern
.matcher(f
.getName()))
230 .filter(Matcher
::matches
)
231 .map(matcher
-> new Key(RecipientId
.of(Long
.parseLong(matcher
.group(1))),
232 Integer
.parseInt(matcher
.group(2))))
233 .collect(Collectors
.toList());
236 private File
getSessionFile(Key key
) {
238 IOUtils
.createPrivateDirectories(sessionsPath
);
239 } catch (IOException e
) {
240 throw new AssertionError("Failed to create sessions path", e
);
242 return new File(sessionsPath
, key
.getRecipientId().getId() + "_" + key
.getDeviceId());
245 private SessionRecord
loadSessionLocked(final Key key
) {
247 final var session
= cachedSessions
.get(key
);
248 if (session
!= null) {
253 final var file
= getSessionFile(key
);
254 if (!file
.exists()) {
257 try (var inputStream
= new FileInputStream(file
)) {
258 final var session
= new SessionRecord(inputStream
.readAllBytes());
259 cachedSessions
.put(key
, session
);
261 } catch (IOException e
) {
262 logger
.warn("Failed to load session, resetting session: {}", e
.getMessage());
267 private void storeSessionLocked(final Key key
, final SessionRecord session
) {
268 cachedSessions
.put(key
, session
);
270 final var file
= getSessionFile(key
);
272 try (var outputStream
= new FileOutputStream(file
)) {
273 outputStream
.write(session
.serialize());
275 } catch (IOException e
) {
276 logger
.warn("Failed to store session, trying to delete file and retry: {}", e
.getMessage());
278 Files
.delete(file
.toPath());
279 try (var outputStream
= new FileOutputStream(file
)) {
280 outputStream
.write(session
.serialize());
282 } catch (IOException e2
) {
283 logger
.error("Failed to store session file {}: {}", file
, e2
.getMessage());
288 private void archiveSessionLocked(final Key key
) {
289 final var session
= loadSessionLocked(key
);
290 if (session
== null) {
293 session
.archiveCurrentState();
294 storeSessionLocked(key
, session
);
297 private void deleteSessionLocked(final Key key
) {
298 cachedSessions
.remove(key
);
300 final var file
= getSessionFile(key
);
301 if (!file
.exists()) {
305 Files
.delete(file
.toPath());
306 } catch (IOException e
) {
307 logger
.error("Failed to delete session file {}: {}", file
, e
.getMessage());
311 private static final class Key
{
313 private final RecipientId recipientId
;
314 private final int deviceId
;
316 public Key(final RecipientId recipientId
, final int deviceId
) {
317 this.recipientId
= recipientId
;
318 this.deviceId
= deviceId
;
321 public RecipientId
getRecipientId() {
325 public int getDeviceId() {
330 public boolean equals(final Object o
) {
331 if (this == o
) return true;
332 if (o
== null || getClass() != o
.getClass()) return false;
334 final var key
= (Key
) o
;
336 if (deviceId
!= key
.deviceId
) return false;
337 return recipientId
.equals(key
.recipientId
);
341 public int hashCode() {
342 int result
= recipientId
.hashCode();
343 result
= 31 * result
+ deviceId
;