1 package org
.asamk
.signal
.jsonrpc
;
3 import com
.fasterxml
.jackson
.core
.TreeNode
;
4 import com
.fasterxml
.jackson
.core
.type
.TypeReference
;
5 import com
.fasterxml
.jackson
.databind
.JsonMappingException
;
6 import com
.fasterxml
.jackson
.databind
.JsonNode
;
7 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
8 import com
.fasterxml
.jackson
.databind
.node
.ContainerNode
;
9 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
11 import org
.asamk
.signal
.commands
.Command
;
12 import org
.asamk
.signal
.commands
.JsonRpcMultiCommand
;
13 import org
.asamk
.signal
.commands
.JsonRpcRegistrationCommand
;
14 import org
.asamk
.signal
.commands
.JsonRpcSingleCommand
;
15 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
16 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
17 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
18 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
19 import org
.asamk
.signal
.manager
.Manager
;
20 import org
.asamk
.signal
.manager
.MultiAccountManager
;
21 import org
.asamk
.signal
.manager
.RegistrationManager
;
22 import org
.asamk
.signal
.output
.JsonWriter
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
26 import java
.io
.IOException
;
27 import java
.nio
.channels
.OverlappingFileLockException
;
29 import java
.util
.function
.Function
;
31 public class SignalJsonRpcCommandHandler
{
33 private final static Logger logger
= LoggerFactory
.getLogger(SignalJsonRpcDispatcherHandler
.class);
35 private static final int USER_ERROR
= -1;
36 private static final int IO_ERROR
= -3;
37 private static final int UNTRUSTED_KEY_ERROR
= -4;
39 private final Manager m
;
40 private final MultiAccountManager c
;
41 private final Function
<String
, Command
> commandProvider
;
43 public SignalJsonRpcCommandHandler(final Manager m
, final Function
<String
, Command
> commandProvider
) {
46 this.commandProvider
= commandProvider
;
49 public SignalJsonRpcCommandHandler(final MultiAccountManager c
, final Function
<String
, Command
> commandProvider
) {
52 this.commandProvider
= commandProvider
;
55 public JsonNode
handleRequest(
56 final ObjectMapper objectMapper
, final String method
, ContainerNode
<?
> params
57 ) throws JsonRpcException
{
58 var command
= getCommand(method
);
60 if (command
instanceof JsonRpcSingleCommand
<?
> jsonRpcCommand
) {
61 final var manager
= getManagerFromParams(params
);
62 if (manager
!= null) {
63 return runCommand(objectMapper
, params
, new CommandRunnerImpl
<>(manager
, jsonRpcCommand
));
66 if (command
instanceof JsonRpcMultiCommand
<?
> jsonRpcCommand
) {
67 return runCommand(objectMapper
, params
, new MultiCommandRunnerImpl
<>(c
, jsonRpcCommand
));
69 if (command
instanceof JsonRpcRegistrationCommand
<?
> jsonRpcCommand
) {
70 try (var manager
= getRegistrationManagerFromParams(params
)) {
71 if (manager
!= null) {
72 return runCommand(objectMapper
,
74 new RegistrationCommandRunnerImpl
<>(manager
, c
, jsonRpcCommand
));
76 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INVALID_PARAMS
,
77 "Method requires valid account parameter",
80 } catch (IOException e
) {
81 logger
.warn("Failed to close registration manager", e
);
85 if (command
instanceof JsonRpcSingleCommand
<?
> jsonRpcCommand
) {
87 return runCommand(objectMapper
, params
, new CommandRunnerImpl
<>(m
, jsonRpcCommand
));
90 final var manager
= getManagerFromParams(params
);
91 if (manager
!= null) {
92 return runCommand(objectMapper
, params
, new CommandRunnerImpl
<>(manager
, jsonRpcCommand
));
94 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INVALID_PARAMS
,
95 "Method requires valid account parameter",
100 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.METHOD_NOT_FOUND
,
101 "Method not implemented",
105 private Manager
getManagerFromParams(final ContainerNode
<?
> params
) throws JsonRpcException
{
106 if (params
!= null && params
.hasNonNull("account")) {
107 final var manager
= c
.getManager(params
.get("account").asText());
108 ((ObjectNode
) params
).remove("account");
109 if (manager
== null) {
110 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INVALID_PARAMS
,
111 "Specified account does not exist",
119 private RegistrationManager
getRegistrationManagerFromParams(final ContainerNode
<?
> params
) {
120 if (params
!= null && params
.has("account")) {
122 final var registrationManager
= c
.getNewRegistrationManager(params
.get("account").asText());
123 ((ObjectNode
) params
).remove("account");
124 return registrationManager
;
125 } catch (OverlappingFileLockException e
) {
126 logger
.warn("Account is already in use");
128 } catch (IOException
| IllegalStateException e
) {
129 logger
.warn("Failed to load registration manager", e
);
136 private Command
getCommand(final String method
) {
137 return commandProvider
.apply(method
);
140 private record CommandRunnerImpl
<T
>(Manager m
, JsonRpcSingleCommand
<T
> command
) implements CommandRunner
<T
> {
143 public void handleCommand(final T request
, final JsonWriter jsonWriter
) throws CommandException
{
144 command
.handleCommand(request
, m
, jsonWriter
);
148 public TypeReference
<T
> getRequestType() {
149 return command
.getRequestType();
153 private record RegistrationCommandRunnerImpl
<T
>(
154 RegistrationManager m
, MultiAccountManager c
, JsonRpcRegistrationCommand
<T
> command
155 ) implements CommandRunner
<T
> {
158 public void handleCommand(final T request
, final JsonWriter jsonWriter
) throws CommandException
{
159 command
.handleCommand(request
, m
, jsonWriter
);
163 public TypeReference
<T
> getRequestType() {
164 return command
.getRequestType();
168 private record MultiCommandRunnerImpl
<T
>(
169 MultiAccountManager c
, JsonRpcMultiCommand
<T
> command
170 ) implements CommandRunner
<T
> {
173 public void handleCommand(final T request
, final JsonWriter jsonWriter
) throws CommandException
{
174 command
.handleCommand(request
, c
, jsonWriter
);
178 public TypeReference
<T
> getRequestType() {
179 return command
.getRequestType();
183 interface CommandRunner
<T
> {
185 void handleCommand(T request
, JsonWriter jsonWriter
) throws CommandException
;
187 TypeReference
<T
> getRequestType();
190 private JsonNode
runCommand(
191 final ObjectMapper objectMapper
, final ContainerNode
<?
> params
, final CommandRunner
<?
> command
192 ) throws JsonRpcException
{
193 final Object
[] result
= {null};
194 final JsonWriter commandJsonWriter
= s
-> {
195 if (result
[0] != null) {
196 throw new AssertionError("Command may only write one json result");
203 parseParamsAndRunCommand(objectMapper
, params
, commandJsonWriter
, command
);
204 } catch (JsonMappingException e
) {
205 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INVALID_REQUEST
,
208 } catch (UserErrorException e
) {
209 throw new JsonRpcException(new JsonRpcResponse
.Error(USER_ERROR
,
211 getErrorDataNode(objectMapper
, result
)));
212 } catch (IOErrorException e
) {
213 throw new JsonRpcException(new JsonRpcResponse
.Error(IO_ERROR
,
215 getErrorDataNode(objectMapper
, result
)));
216 } catch (UntrustedKeyErrorException e
) {
217 throw new JsonRpcException(new JsonRpcResponse
.Error(UNTRUSTED_KEY_ERROR
,
219 getErrorDataNode(objectMapper
, result
)));
220 } catch (Throwable e
) {
221 logger
.error("Command execution failed", e
);
222 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INTERNAL_ERROR
,
224 getErrorDataNode(objectMapper
, result
)));
227 Object output
= result
[0] == null ? Map
.of() : result
[0];
228 return objectMapper
.valueToTree(output
);
231 private JsonNode
getErrorDataNode(final ObjectMapper objectMapper
, final Object
[] result
) {
232 if (result
[0] == null) {
235 return objectMapper
.valueToTree(Map
.of("response", result
[0]));
238 private <T
> void parseParamsAndRunCommand(
239 final ObjectMapper objectMapper
,
240 final TreeNode params
,
241 final JsonWriter jsonWriter
,
242 final CommandRunner
<T
> command
243 ) throws CommandException
, JsonMappingException
{
244 T requestParams
= null;
245 final var requestType
= command
.getRequestType();
246 if (params
!= null && requestType
!= null) {
248 requestParams
= objectMapper
.readValue(objectMapper
.treeAsTokens(params
), requestType
);
249 } catch (JsonMappingException e
) {
251 } catch (IOException e
) {
252 throw new AssertionError(e
);
255 command
.handleCommand(requestParams
, jsonWriter
);