mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-01 15:26:47 +00:00
Update to dropwizard 5.0.1
This commit is contained in:
1 parent
0beeb8a935
commit
4c4282162f
41 files changed
+976
-764
No files matched your search
+7
-10
@@ -13,9 +13,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Callback;
|
||||
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;
|
||||
@@ -30,15 +29,13 @@ 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, RemoteEndpoint remoteEndpoint, WebSocketMessageFactory messageFactory,
|
||||
public WebSocketClient(Session session, WebSocketMessageFactory messageFactory,
|
||||
Map<Long, CompletableFuture<WebSocketResponseMessage>> pendingRequestMapper) {
|
||||
this.session = session;
|
||||
this.remoteEndpoint = remoteEndpoint;
|
||||
this.messageFactory = messageFactory;
|
||||
this.pendingRequestMapper = pendingRequestMapper;
|
||||
this.created = Instant.now();
|
||||
@@ -56,9 +53,9 @@ public class WebSocketClient {
|
||||
WebSocketMessage requestMessage = messageFactory.createRequest(Optional.of(requestId), verb, path, headers, body);
|
||||
|
||||
try {
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(requestMessage.toByteArray()), new WriteCallback() {
|
||||
session.sendBinary(ByteBuffer.wrap(requestMessage.toByteArray()), new Callback() {
|
||||
@Override
|
||||
public void writeFailed(Throwable x) {
|
||||
public void fail(Throwable x) {
|
||||
logger.debug("Write failed", x);
|
||||
pendingRequestMapper.remove(requestId);
|
||||
future.completeExceptionally(x);
|
||||
@@ -86,9 +83,9 @@ public class WebSocketClient {
|
||||
}
|
||||
|
||||
public void close(final int code, final String message) {
|
||||
session.close(code, message, new WriteCallback() {
|
||||
session.close(code, message, new Callback() {
|
||||
@Override
|
||||
public void writeFailed(final Throwable throwable) {
|
||||
public void fail(final Throwable throwable) {
|
||||
try {
|
||||
session.disconnect();
|
||||
} catch (final Exception e) {
|
||||
@@ -108,6 +105,6 @@ public class WebSocketClient {
|
||||
}
|
||||
|
||||
public SocketAddress getRemoteAddress() {
|
||||
return session.getRemoteAddress();
|
||||
return session.getRemoteSocketAddress();
|
||||
}
|
||||
}
|
||||
+31
-22
@@ -24,11 +24,8 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Callback;
|
||||
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;
|
||||
@@ -48,11 +45,11 @@ import org.whispersystems.websocket.setup.WebSocketConnectListener;
|
||||
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public class WebSocketResourceProvider<T extends Principal> implements WebSocketListener {
|
||||
public class WebSocketResourceProvider<T extends Principal> implements Session.Listener.AutoDemanding {
|
||||
|
||||
/**
|
||||
* A static exception instance passed to outstanding requests (via {@code completeExceptionally} in
|
||||
* {@link #onWebSocketClose(int, String)}
|
||||
* {@link #onWebSocketClose}
|
||||
*/
|
||||
public static final IOException CONNECTION_CLOSED_EXCEPTION = new IOException("Connection closed!");
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketResourceProvider.class);
|
||||
@@ -70,7 +67,6 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
private final int localPort;
|
||||
|
||||
private Session session;
|
||||
private RemoteEndpoint remoteEndpoint;
|
||||
private WebSocketSessionContext context;
|
||||
|
||||
private static final Set<String> EXCLUDED_UPGRADE_REQUEST_HEADERS = Set.of("connection", "upgrade");
|
||||
@@ -96,11 +92,10 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketConnect(Session session) {
|
||||
public void onWebSocketOpen(Session session) {
|
||||
this.session = session;
|
||||
this.remoteEndpoint = session.getRemote();
|
||||
this.context = new WebSocketSessionContext(
|
||||
new WebSocketClient(session, remoteEndpoint, messageFactory, requestMap));
|
||||
new WebSocketClient(session, messageFactory, requestMap));
|
||||
this.context.setAuthenticated(reusableAuth.orElse(null));
|
||||
this.session.setIdleTimeout(idleTimeout);
|
||||
|
||||
@@ -125,9 +120,19 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketBinary(byte[] payload, int offset, int length) {
|
||||
public void onWebSocketBinary(final ByteBuffer payload, final Callback callback) {
|
||||
try {
|
||||
WebSocketMessage webSocketMessage = messageFactory.parseMessage(payload, offset, length);
|
||||
onWebSocketBinary(payload);
|
||||
callback.succeed();
|
||||
} catch (RuntimeException e) {
|
||||
callback.fail(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void onWebSocketBinary(ByteBuffer payload) {
|
||||
try {
|
||||
final WebSocketMessage webSocketMessage = messageFactory.parseMessage(payload);
|
||||
|
||||
switch (webSocketMessage.getType()) {
|
||||
case REQUEST_MESSAGE:
|
||||
@@ -147,17 +152,21 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketClose(int statusCode, String reason) {
|
||||
if (context != null) {
|
||||
context.notifyClosed(statusCode, reason);
|
||||
public void onWebSocketClose(int statusCode, String reason, Callback callback) {
|
||||
try {
|
||||
if (context != null) {
|
||||
context.notifyClosed(statusCode, reason);
|
||||
|
||||
for (long requestId : requestMap.keySet()) {
|
||||
CompletableFuture<WebSocketResponseMessage> outstandingRequest = requestMap.remove(requestId);
|
||||
for (long requestId : requestMap.keySet()) {
|
||||
CompletableFuture<WebSocketResponseMessage> outstandingRequest = requestMap.remove(requestId);
|
||||
|
||||
if (outstandingRequest != null) {
|
||||
outstandingRequest.completeExceptionally(CONNECTION_CLOSED_EXCEPTION);
|
||||
if (outstandingRequest != null) {
|
||||
outstandingRequest.completeExceptionally(CONNECTION_CLOSED_EXCEPTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
callback.succeed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +278,7 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
}
|
||||
|
||||
private void close(Session session, int status, String message) {
|
||||
session.close(status, message);
|
||||
session.close(status, message, Callback.NOOP);
|
||||
}
|
||||
|
||||
private void sendResponse(WebSocketRequestMessage requestMessage, ContainerResponse response,
|
||||
@@ -288,7 +297,7 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
Optional.ofNullable(body))
|
||||
.toByteArray();
|
||||
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(responseBytes), WriteCallback.NOOP);
|
||||
session.sendBinary(ByteBuffer.wrap(responseBytes), Callback.NOOP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,7 +309,7 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
|
||||
getHeaderList(error.getStringHeaders()),
|
||||
Optional.empty());
|
||||
|
||||
remoteEndpoint.sendBytes(ByteBuffer.wrap(response.toByteArray()), WriteCallback.NOOP);
|
||||
session.sendBinary(ByteBuffer.wrap(response.toByteArray()), Callback.NOOP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-18
@@ -13,11 +13,9 @@ import java.security.Principal;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeResponse;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
||||
import org.glassfish.jersey.CommonProperties;
|
||||
import org.glassfish.jersey.server.ApplicationHandler;
|
||||
import org.slf4j.Logger;
|
||||
@@ -25,23 +23,20 @@ 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> extends JettyWebSocketServlet implements
|
||||
JettyWebSocketCreator {
|
||||
public class WebSocketResourceProviderFactory<T extends Principal> 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,
|
||||
WebSocketConfiguration configuration, String remoteAddressPropertyName) {
|
||||
String remoteAddressPropertyName) {
|
||||
this.environment = environment;
|
||||
|
||||
environment.jersey().register(new WebSocketSessionContextValueFactoryProvider.Binder());
|
||||
@@ -55,7 +50,6 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends Jetty
|
||||
|
||||
this.jerseyApplicationHandler = new ApplicationHandler(environment.jersey());
|
||||
|
||||
this.configuration = configuration;
|
||||
this.remoteAddressPropertyName = remoteAddressPropertyName;
|
||||
}
|
||||
|
||||
@@ -91,6 +85,7 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends Jetty
|
||||
// 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) {
|
||||
@@ -99,13 +94,6 @@ public class WebSocketResourceProviderFactory<T extends Principal> extends Jetty
|
||||
}
|
||||
}
|
||||
|
||||
@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.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
import org.eclipse.jetty.ee10.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.websocket.api.UpgradeRequest;
|
||||
import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest;
|
||||
|
||||
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(UpgradeRequest request) throws InvalidCredentialsException;
|
||||
Optional<T> authenticate(JettyServerUpgradeRequest request) throws InvalidCredentialsException;
|
||||
}
|
||||
+9
-8
@@ -5,22 +5,23 @@
|
||||
package org.whispersystems.websocket.messages;
|
||||
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public interface WebSocketMessageFactory {
|
||||
|
||||
public WebSocketMessage parseMessage(byte[] serialized, int offset, int len)
|
||||
WebSocketMessage parseMessage(ByteBuffer serialized)
|
||||
throws InvalidMessageException;
|
||||
|
||||
public WebSocketMessage createRequest(Optional<Long> requestId,
|
||||
String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
WebSocketMessage createRequest(Optional<Long> requestId,
|
||||
String verb, String path,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
|
||||
public WebSocketMessage createResponse(long requestId, int status, String message,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
WebSocketMessage createResponse(long requestId, int status, String message,
|
||||
List<String> headers,
|
||||
Optional<byte[]> body);
|
||||
|
||||
}
|
||||
+3
-2
@@ -10,14 +10,15 @@ 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(byte[] buffer, int offset, int length) throws InvalidMessageException {
|
||||
ProtobufWebSocketMessage(ByteBuffer buffer) throws InvalidMessageException {
|
||||
try {
|
||||
this.message = SubProtocol.WebSocketMessage.parseFrom(ByteString.copyFrom(buffer, offset, length));
|
||||
this.message = SubProtocol.WebSocketMessage.parseFrom(ByteString.copyFrom(buffer));
|
||||
|
||||
if (getType() == Type.REQUEST_MESSAGE) {
|
||||
if (!message.getRequest().hasVerb() || !message.getRequest().hasPath()) {
|
||||
|
||||
+3
-2
@@ -9,16 +9,17 @@ 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(byte[] serialized, int offset, int len)
|
||||
public WebSocketMessage parseMessage(ByteBuffer serialized)
|
||||
throws InvalidMessageException
|
||||
{
|
||||
return new ProtobufWebSocketMessage(serialized, offset, len);
|
||||
return new ProtobufWebSocketMessage(serialized);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user