mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-02 04:20:38 +00:00
Add support for registration commands
This commit is contained in:
parent
0338befcf3
commit
869820bcce
1 changed files with 42 additions and 6 deletions
|
@ -9,23 +9,31 @@ import com.sun.net.httpserver.HttpServer;
|
|||
import org.asamk.signal.commands.Commands;
|
||||
import org.asamk.signal.commands.JsonRpcNamespace;
|
||||
import org.asamk.signal.commands.LocalCommand;
|
||||
import org.asamk.signal.commands.RegistrationCommand;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcException;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcRequest;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcResponse;
|
||||
import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.MultiAccountManager;
|
||||
import org.asamk.signal.manager.RegistrationManager;
|
||||
import org.asamk.signal.output.JsonWriterImpl;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.channels.OverlappingFileLockException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class HttpServerHandler {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
|
||||
|
||||
private final ObjectMapper objectMapper = Util.createJsonObjectMapper();
|
||||
|
||||
public void init(int port, MultiAccountManager m) {
|
||||
|
@ -49,23 +57,34 @@ public class HttpServerHandler {
|
|||
System.out.println("Command called " + request.getMethod());
|
||||
|
||||
final var command = Commands.getCommand(request.getMethod());
|
||||
final var manager = getManagerFromParams(request.getParams(), m);
|
||||
final var writer = new StringWriter();
|
||||
final var ns = new JsonRpcNamespace(params);
|
||||
|
||||
if (command instanceof LocalCommand) {
|
||||
final var writer = new StringWriter();
|
||||
final var ns = new JsonRpcNamespace(params);
|
||||
|
||||
final var manager = getManagerFromParams(request.getParams(), m);
|
||||
((LocalCommand) command).handleCommand(ns, manager, new JsonWriterImpl(writer));
|
||||
|
||||
sendResponse(200, writer.toString(), httpExchange);
|
||||
} else if (command instanceof RegistrationCommand) {
|
||||
final var registrationManager = getRegistrationManagerFromParams(request.getParams(), m);
|
||||
if (registrationManager != null) {
|
||||
((RegistrationCommand) command).handleCommand(ns, registrationManager);
|
||||
} else {
|
||||
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_PARAMS,
|
||||
"Method requires valid account parameter",
|
||||
null));
|
||||
}
|
||||
}
|
||||
else {
|
||||
sendResponse(404, "COMMAND NOT FOUND", httpExchange);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO if writer empty return some generic response
|
||||
sendResponse(200, writer.toString(), httpExchange);
|
||||
|
||||
}
|
||||
catch (Throwable aEx) {
|
||||
aEx.printStackTrace();
|
||||
//TODO if this is a JSON RPC Error serialize and return the error
|
||||
sendResponse(500, "ERROR", httpExchange);
|
||||
}
|
||||
});
|
||||
|
@ -108,4 +127,21 @@ public class HttpServerHandler {
|
|||
return null;
|
||||
}
|
||||
|
||||
private RegistrationManager getRegistrationManagerFromParams(final ContainerNode<?> params, MultiAccountManager c) {
|
||||
if (params != null && params.has("account")) {
|
||||
try {
|
||||
final var registrationManager = c.getNewRegistrationManager(params.get("account").asText());
|
||||
((ObjectNode) params).remove("account");
|
||||
return registrationManager;
|
||||
} catch (OverlappingFileLockException e) {
|
||||
logger.warn("Account is already in use");
|
||||
return null;
|
||||
} catch (IOException | IllegalStateException e) {
|
||||
logger.warn("Failed to load registration manager", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue