mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 17:56:53 +00:00
1 parent
bb94975d74
commit
faa74469ea
37 files changed
+711
-698
No files matched your search
@@ -12,8 +12,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.eclipse.jetty.websocket.api.Callback;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||
import org.eclipse.jetty.websocket.api.exceptions.WebSocketException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -28,13 +29,15 @@ public class WebSocketClient {
|
||||
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
||||
|
||||
private final Session session;
|
||||
private final RemoteEndpoint remoteEndpoint;
|
||||
private final WebSocketMessageFactory messageFactory;
|
||||
private final Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper;
|
||||
private final Instant created;
|
||||
|
||||
public WebSocketClient(Session session, WebSocketMessageFactory messageFactory,
|
||||
public WebSocketClient(Session session, RemoteEndpoint remoteEndpoint, WebSocketMessageFactory messageFactory,
|
||||
Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper) {
|
||||
this.session = session;
|
||||
this.remoteEndpoint = remoteEndpoint;
|
||||
this.messageFactory = messageFactory;
|
||||
this.pendingRequestMapper = pendingRequestMapper;
|
||||
this.created = Instant.now();
|
||||
@@ -52,9 +55,9 @@ public class WebSocketClient {
|
||||
WebSocketMessage requestMessage = messageFactory.createRequest(Optional.of(requestId), verb, path, headers, body);
|
||||
|
||||
try {
|
||||
session.sendBinary(ByteBuffer.wrap(requestMessage.toByteArray()), new Callback() {
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(requestMessage.toByteArray()), new WriteCallback() {
|
||||
@Override
|
||||
public void fail(Throwable x) {
|
||||
public void writeFailed(Throwable x) {
|
||||
logger.debug("Write failed", x);
|
||||
pendingRequestMapper.remove(requestId);
|
||||
future.completeExceptionally(x);
|
||||
@@ -82,9 +85,9 @@ public class WebSocketClient {
|
||||
}
|
||||
|
||||
public void close(final int code, final String message) {
|
||||
session.close(code, message, new Callback() {
|
||||
session.close(code, message, new WriteCallback() {
|
||||
@Override
|
||||
public void fail(final Throwable throwable) {
|
||||
public void writeFailed(final Throwable throwable) {
|
||||
try {
|
||||
session.disconnect();
|
||||
} catch (final Exception e) {
|
||||
|
||||
+13
-9
@@ -24,8 +24,10 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.eclipse.jetty.websocket.api.Callback;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||
import org.eclipse.jetty.websocket.api.exceptions.MessageTooLargeException;
|
||||
import org.glassfish.jersey.internal.MapPropertiesDelegate;
|
||||
import org.glassfish.jersey.server.ApplicationHandler;
|
||||
@@ -45,7 +47,7 @@ import org.whispersystems.websocket.setup.WebSocketConnectListener;
|
||||
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public class WebSocketResourceProvider<T extends Principal> implements Session.Listener.AutoDemanding {
|
||||
public class WebSocketResourceProvider<T extends Principal> implements WebSocketListener {
|
||||
|
||||
/**
|
||||
* A static exception instance passed to outstanding requests (via {@code completeExceptionally} in
|
||||
@@ -66,6 +68,7 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
private final String remoteAddressPropertyName;
|
||||
|
||||
private Session session;
|
||||
private RemoteEndpoint remoteEndpoint;
|
||||
private WebSocketSessionContext context;
|
||||
|
||||
private static final Set<String> EXCLUDED_UPGRADE_REQUEST_HEADERS = Set.of("connection", "upgrade");
|
||||
@@ -89,10 +92,11 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketOpen(Session session) {
|
||||
public void onWebSocketConnect(Session session) {
|
||||
this.session = session;
|
||||
this.remoteEndpoint = session.getRemote();
|
||||
this.context = new WebSocketSessionContext(
|
||||
new WebSocketClient(session, messageFactory, requestMap));
|
||||
new WebSocketClient(session, remoteEndpoint, messageFactory, requestMap));
|
||||
this.context.setAuthenticated(reusableAuth.orElse(null));
|
||||
this.session.setIdleTimeout(idleTimeout);
|
||||
|
||||
@@ -117,9 +121,9 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketBinary(final ByteBuffer payload, final Callback callback) {
|
||||
public void onWebSocketBinary(byte[] payload, int offset, int length) {
|
||||
try {
|
||||
WebSocketMessage webSocketMessage = messageFactory.parseMessage(payload);
|
||||
WebSocketMessage webSocketMessage = messageFactory.parseMessage(payload, offset, length);
|
||||
|
||||
switch (webSocketMessage.getType()) {
|
||||
case REQUEST_MESSAGE:
|
||||
@@ -254,7 +258,7 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
}
|
||||
|
||||
private void close(Session session, int status, String message) {
|
||||
session.close(status, message, Callback.NOOP);
|
||||
session.close(status, message);
|
||||
}
|
||||
|
||||
private void sendResponse(WebSocketRequestMessage requestMessage, ContainerResponse response,
|
||||
@@ -273,7 +277,7 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
Optional.ofNullable(body))
|
||||
.toByteArray();
|
||||
|
||||
session.sendBinary(ByteBuffer.wrap(responseBytes), Callback.NOOP);
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(responseBytes), WriteCallback.NOOP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +289,7 @@ public class WebSocketResourceProvider<T extends Principal> implements Session.L
|
||||
getHeaderList(error.getStringHeaders()),
|
||||
Optional.empty());
|
||||
|
||||
session.sendBinary(ByteBuffer.wrap(response.toByteArray()), Callback.NOOP);
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(response.toByteArray()), WriteCallback.NOOP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+18
-6
@@ -13,9 +13,11 @@ import java.security.Principal;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeResponse;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
||||
import org.eclipse.jetty.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.server.JettyWebSocketCreator;
|
||||
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
|
||||
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
|
||||
import org.glassfish.jersey.CommonProperties;
|
||||
import org.glassfish.jersey.server.ApplicationHandler;
|
||||
import org.slf4j.Logger;
|
||||
@@ -23,20 +25,23 @@ import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.websocket.auth.InvalidCredentialsException;
|
||||
import org.whispersystems.websocket.auth.WebSocketAuthenticator;
|
||||
import org.whispersystems.websocket.auth.WebsocketAuthValueFactoryProvider;
|
||||
import org.whispersystems.websocket.configuration.WebSocketConfiguration;
|
||||
import org.whispersystems.websocket.session.WebSocketSessionContextValueFactoryProvider;
|
||||
import org.whispersystems.websocket.setup.WebSocketEnvironment;
|
||||
|
||||
public class WebSocketResourceProviderFactory<T extends Principal> implements JettyWebSocketCreator {
|
||||
public class WebSocketResourceProviderFactory<T extends Principal> extends JettyWebSocketServlet implements
|
||||
JettyWebSocketCreator {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketResourceProviderFactory.class);
|
||||
|
||||
private final WebSocketEnvironment<T> environment;
|
||||
private final ApplicationHandler jerseyApplicationHandler;
|
||||
private final WebSocketConfiguration configuration;
|
||||
|
||||
private final String remoteAddressPropertyName;
|
||||
|
||||
public WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass,
|
||||
String remoteAddressPropertyName) {
|
||||
WebSocketConfiguration configuration, String remoteAddressPropertyName) {
|
||||
this.environment = environment;
|
||||
|
||||
environment.jersey().register(new WebSocketSessionContextValueFactoryProvider.Binder());
|
||||
@@ -50,6 +55,7 @@ public class WebSocketResourceProviderFactory<T extends Principal> implements Je
|
||||
|
||||
this.jerseyApplicationHandler = new ApplicationHandler(environment.jersey());
|
||||
|
||||
this.configuration = configuration;
|
||||
this.remoteAddressPropertyName = remoteAddressPropertyName;
|
||||
}
|
||||
|
||||
@@ -83,7 +89,6 @@ public class WebSocketResourceProviderFactory<T extends Principal> implements Je
|
||||
// Authentication may fail for non-incorrect-credential reasons (e.g. we couldn't read from the account database).
|
||||
// If that happens, we don't want to incorrectly tell clients that they provided bad credentials.
|
||||
logger.warn("Authentication failure", e);
|
||||
|
||||
try {
|
||||
response.sendError(500, "Failure");
|
||||
} catch (final IOException ignored) {
|
||||
@@ -92,6 +97,13 @@ public class WebSocketResourceProviderFactory<T extends Principal> implements Je
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(JettyWebSocketServletFactory factory) {
|
||||
factory.setCreator(this);
|
||||
factory.setMaxBinaryMessageSize(configuration.getMaxBinaryMessageSize());
|
||||
factory.setMaxTextMessageSize(configuration.getMaxTextMessageSize());
|
||||
}
|
||||
|
||||
private String getRemoteAddress(JettyServerUpgradeRequest request) {
|
||||
final String remoteAddress = (String) request.getHttpServletRequest().getAttribute(remoteAddressPropertyName);
|
||||
if (StringUtils.isBlank(remoteAddress)) {
|
||||
|
||||
+2
-2
@@ -7,8 +7,8 @@ package org.whispersystems.websocket.auth;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse;
|
||||
|
||||
public interface AuthenticatedWebSocketUpgradeFilter<T extends Principal> {
|
||||
|
||||
|
||||
+2
-2
@@ -6,7 +6,7 @@ package org.whispersystems.websocket.auth;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||
|
||||
public interface WebSocketAuthenticator<T extends Principal> {
|
||||
|
||||
@@ -20,5 +20,5 @@ public interface WebSocketAuthenticator<T extends Principal> {
|
||||
*
|
||||
* @throws InvalidCredentialsException if credentials were provided, but could not be authenticated
|
||||
*/
|
||||
Optional<T> authenticate(JettyServerUpgradeRequest request) throws InvalidCredentialsException;
|
||||
Optional<T> authenticate(UpgradeRequest request) throws InvalidCredentialsException;
|
||||
}
|
||||
+8
-9
@@ -5,23 +5,22 @@
|
||||
package org.whispersystems.websocket.messages;
|
||||
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public interface WebSocketMessageFactory {
|
||||
|
||||
WebSocketMessage parseMessage(ByteBuffer serialized)
|
||||
public WebSocketMessage parseMessage(byte[] serialized, int offset, int len)
|
||||
throws InvalidMessageException;
|
||||
|
||||
WebSocketMessage createRequest(Optional<Long> requestId,
|
||||
String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
public WebSocketMessage createRequest(Optional<Long> requestId,
|
||||
String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
|
||||
WebSocketMessage createResponse(long requestId, int status, String message,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
public WebSocketMessage createResponse(long requestId, int status, String message,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
|
||||
}
|
||||
+2
-3
@@ -10,15 +10,14 @@ import org.whispersystems.websocket.messages.InvalidMessageException;
|
||||
import org.whispersystems.websocket.messages.WebSocketMessage;
|
||||
import org.whispersystems.websocket.messages.WebSocketRequestMessage;
|
||||
import org.whispersystems.websocket.messages.WebSocketResponseMessage;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class ProtobufWebSocketMessage implements WebSocketMessage {
|
||||
|
||||
private final SubProtocol.WebSocketMessage message;
|
||||
|
||||
ProtobufWebSocketMessage(ByteBuffer buffer) throws InvalidMessageException {
|
||||
ProtobufWebSocketMessage(byte[] buffer, int offset, int length) throws InvalidMessageException {
|
||||
try {
|
||||
this.message = SubProtocol.WebSocketMessage.parseFrom(ByteString.copyFrom(buffer));
|
||||
this.message = SubProtocol.WebSocketMessage.parseFrom(ByteString.copyFrom(buffer, offset, length));
|
||||
|
||||
if (getType() == Type.REQUEST_MESSAGE) {
|
||||
if (!message.getRequest().hasVerb() || !message.getRequest().hasPath()) {
|
||||
|
||||
+2
-3
@@ -9,17 +9,16 @@ import org.whispersystems.websocket.messages.InvalidMessageException;
|
||||
import org.whispersystems.websocket.messages.WebSocketMessage;
|
||||
import org.whispersystems.websocket.messages.WebSocketMessageFactory;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ProtobufWebSocketMessageFactory implements WebSocketMessageFactory {
|
||||
|
||||
@Override
|
||||
public WebSocketMessage parseMessage(ByteBuffer serialized)
|
||||
public WebSocketMessage parseMessage(byte[] serialized, int offset, int len)
|
||||
throws InvalidMessageException
|
||||
{
|
||||
return new ProtobufWebSocketMessage(serialized);
|
||||
return new ProtobufWebSocketMessage(serialized, offset, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user