mirror of
https://github.com/signalapp/Signal-Server.git
synced 2025-12-13 01:50:34 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9de015eab | ||
|
|
dd36c861ba | ||
|
|
b34e46af93 | ||
|
|
405802c492 | ||
|
|
e15f3c9d2b | ||
|
|
885af064c9 | ||
|
|
40529dc41f | ||
|
|
2452f6ef8a | ||
|
|
4c543e6f06 | ||
|
|
bc5fd5d441 | ||
|
|
7a33cef27e | ||
|
|
b433b9c879 | ||
|
|
5d169c523f | ||
|
|
98d277368f | ||
|
|
3bd58bf25e | ||
|
|
ba05e577ae | ||
|
|
4206f6af45 | ||
|
|
0c5da1cc47 | ||
|
|
d9bd1c679e | ||
|
|
437eb8de37 | ||
|
|
f14c181840 | ||
|
|
d46c9fb157 | ||
|
|
6913e4dfd2 | ||
|
|
aea3f299a0 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@ run.sh
|
|||||||
local.yml
|
local.yml
|
||||||
config/production.yml
|
config/production.yml
|
||||||
config/federated.yml
|
config/federated.yml
|
||||||
|
config/staging.yml
|
||||||
|
.opsmanage
|
||||||
|
|||||||
@@ -56,9 +56,6 @@ graphite:
|
|||||||
host:
|
host:
|
||||||
port:
|
port:
|
||||||
|
|
||||||
http:
|
|
||||||
shutdownGracePeriod: 0s
|
|
||||||
|
|
||||||
database:
|
database:
|
||||||
# the name of your JDBC driver
|
# the name of your JDBC driver
|
||||||
driverClass: org.postgresql.Driver
|
driverClass: org.postgresql.Driver
|
||||||
@@ -75,24 +72,3 @@ database:
|
|||||||
# any properties specific to your JDBC driver:
|
# any properties specific to your JDBC driver:
|
||||||
properties:
|
properties:
|
||||||
charSet: UTF-8
|
charSet: UTF-8
|
||||||
|
|
||||||
# the maximum amount of time to wait on an empty pool before throwing an exception
|
|
||||||
maxWaitForConnection: 1s
|
|
||||||
|
|
||||||
# the SQL query to run when validating a connection's liveness
|
|
||||||
validationQuery: "/* MyService Health Check */ SELECT 1"
|
|
||||||
|
|
||||||
# the minimum number of connections to keep open
|
|
||||||
minSize: 8
|
|
||||||
|
|
||||||
# the maximum number of connections to keep open
|
|
||||||
maxSize: 32
|
|
||||||
|
|
||||||
# whether or not idle connections should be validated
|
|
||||||
checkConnectionWhileIdle: false
|
|
||||||
|
|
||||||
# how long a connection must be held before it can be validated
|
|
||||||
checkConnectionHealthWhenIdleFor: 10s
|
|
||||||
|
|
||||||
# the maximum lifetime of an idle connection
|
|
||||||
closeConnectionIfIdleFor: 1 minute
|
|
||||||
|
|||||||
128
pom.xml
128
pom.xml
@@ -9,24 +9,72 @@
|
|||||||
|
|
||||||
<groupId>org.whispersystems.textsecure</groupId>
|
<groupId>org.whispersystems.textsecure</groupId>
|
||||||
<artifactId>TextSecureServer</artifactId>
|
<artifactId>TextSecureServer</artifactId>
|
||||||
<version>0.7</version>
|
<version>0.17</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<dropwizard.version>0.7.0</dropwizard.version>
|
||||||
|
<jackson.api.version>2.3.3</jackson.api.version>
|
||||||
|
<commons-codec.version>1.6</commons-codec.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-core</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-jdbi</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-auth</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-client</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-migrations</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-testing</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-metrics-graphite</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.jersey</groupId>
|
||||||
|
<artifactId>jersey-json</artifactId>
|
||||||
|
<version>1.18.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.codahale.metrics</groupId>
|
||||||
|
<artifactId>metrics-graphite</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-server</artifactId>
|
||||||
|
<version>9.0.7.v20131107</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>bouncycastle</groupId>
|
<groupId>bouncycastle</groupId>
|
||||||
<artifactId>bcprov-jdk16</artifactId>
|
<artifactId>bcprov-jdk16</artifactId>
|
||||||
<version>140</version>
|
<version>140</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-core</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.metrics</groupId>
|
|
||||||
<artifactId>metrics-graphite</artifactId>
|
|
||||||
<version>2.2.0</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.android.gcm</groupId>
|
<groupId>com.google.android.gcm</groupId>
|
||||||
<artifactId>gcm-server</artifactId>
|
<artifactId>gcm-server</artifactId>
|
||||||
@@ -66,35 +114,10 @@
|
|||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-jdbi</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-auth</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-client</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-migrations</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.yammer.dropwizard</groupId>
|
|
||||||
<artifactId>dropwizard-testing</artifactId>
|
|
||||||
<version>0.6.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.twilio.sdk</groupId>
|
<groupId>com.twilio.sdk</groupId>
|
||||||
<artifactId>twilio-java-sdk</artifactId>
|
<artifactId>twilio-java-sdk</artifactId>
|
||||||
<version>3.4.1</version>
|
<version>3.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -103,25 +126,24 @@
|
|||||||
<version>9.1-901.jdbc4</version>
|
<version>9.1-901.jdbc4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.sun.jersey</groupId>
|
|
||||||
<artifactId>jersey-json</artifactId>
|
|
||||||
<version>1.17.1</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
|
||||||
<artifactId>jetty-websocket</artifactId>
|
|
||||||
<version>8.1.14.v20131031</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.coursera</groupId>
|
|
||||||
<artifactId>metrics-datadog</artifactId>
|
|
||||||
<version>0.1.5</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>${commons-codec.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -17,14 +17,12 @@
|
|||||||
package org.whispersystems.textsecuregcm;
|
package org.whispersystems.textsecuregcm;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.yammer.dropwizard.config.Configuration;
|
|
||||||
import com.yammer.dropwizard.db.DatabaseConfiguration;
|
|
||||||
import org.whispersystems.textsecuregcm.configuration.ApnConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.ApnConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DataDogConfiguration;
|
|
||||||
import org.whispersystems.textsecuregcm.configuration.FederationConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.FederationConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.GcmConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.GcmConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.GraphiteConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.GraphiteConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.MemcacheConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.MemcacheConfiguration;
|
||||||
|
import org.whispersystems.textsecuregcm.configuration.MetricsConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.RateLimitsConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.RateLimitsConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.RedisConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.RedisConfiguration;
|
||||||
@@ -35,6 +33,9 @@ import org.whispersystems.textsecuregcm.configuration.WebsocketConfiguration;
|
|||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
import io.dropwizard.Configuration;
|
||||||
|
import io.dropwizard.db.DataSourceFactory;
|
||||||
|
|
||||||
public class WhisperServerConfiguration extends Configuration {
|
public class WhisperServerConfiguration extends Configuration {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -74,7 +75,7 @@ public class WhisperServerConfiguration extends Configuration {
|
|||||||
@Valid
|
@Valid
|
||||||
@NotNull
|
@NotNull
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private DatabaseConfiguration database = new DatabaseConfiguration();
|
private DataSourceFactory database = new DataSourceFactory();
|
||||||
|
|
||||||
@Valid
|
@Valid
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -87,7 +88,7 @@ public class WhisperServerConfiguration extends Configuration {
|
|||||||
|
|
||||||
@Valid
|
@Valid
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private DataDogConfiguration datadog = new DataDogConfiguration();
|
private MetricsConfiguration viz = new MetricsConfiguration();
|
||||||
|
|
||||||
@Valid
|
@Valid
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
@@ -125,7 +126,7 @@ public class WhisperServerConfiguration extends Configuration {
|
|||||||
return redis;
|
return redis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseConfiguration getDatabaseConfiguration() {
|
public DataSourceFactory getDataSourceFactory() {
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +142,7 @@ public class WhisperServerConfiguration extends Configuration {
|
|||||||
return graphite;
|
return graphite;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataDogConfiguration getDataDogConfiguration() {
|
public MetricsConfiguration getMetricsConfiguration() {
|
||||||
return datadog;
|
return viz;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm;
|
package org.whispersystems.textsecuregcm;
|
||||||
|
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
|
import com.codahale.metrics.graphite.GraphiteReporter;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.Service;
|
|
||||||
import com.yammer.dropwizard.config.Bootstrap;
|
|
||||||
import com.yammer.dropwizard.config.Environment;
|
|
||||||
import com.yammer.dropwizard.config.HttpConfiguration;
|
|
||||||
import com.yammer.dropwizard.db.DatabaseConfiguration;
|
|
||||||
import com.yammer.dropwizard.jdbi.DBIFactory;
|
|
||||||
import com.yammer.dropwizard.migrations.MigrationsBundle;
|
|
||||||
import com.yammer.metrics.core.Clock;
|
|
||||||
import com.yammer.metrics.core.MetricPredicate;
|
|
||||||
import com.yammer.metrics.reporting.DatadogReporter;
|
|
||||||
import com.yammer.metrics.reporting.GraphiteReporter;
|
|
||||||
import net.spy.memcached.MemcachedClient;
|
import net.spy.memcached.MemcachedClient;
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.eclipse.jetty.servlets.CrossOriginFilter;
|
||||||
import org.skife.jdbi.v2.DBI;
|
import org.skife.jdbi.v2.DBI;
|
||||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||||
import org.whispersystems.textsecuregcm.auth.FederatedPeerAuthenticator;
|
import org.whispersystems.textsecuregcm.auth.FederatedPeerAuthenticator;
|
||||||
@@ -42,12 +35,16 @@ import org.whispersystems.textsecuregcm.controllers.DirectoryController;
|
|||||||
import org.whispersystems.textsecuregcm.controllers.FederationController;
|
import org.whispersystems.textsecuregcm.controllers.FederationController;
|
||||||
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
||||||
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
||||||
import org.whispersystems.textsecuregcm.controllers.WebsocketControllerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.federation.FederatedClientManager;
|
import org.whispersystems.textsecuregcm.federation.FederatedClientManager;
|
||||||
import org.whispersystems.textsecuregcm.federation.FederatedPeer;
|
import org.whispersystems.textsecuregcm.federation.FederatedPeer;
|
||||||
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||||
import org.whispersystems.textsecuregcm.mappers.IOExceptionMapper;
|
import org.whispersystems.textsecuregcm.mappers.IOExceptionMapper;
|
||||||
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
|
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.CpuUsageGauge;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.FreeMemoryGauge;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.JsonMetricsReporter;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.NetworkReceivedGauge;
|
||||||
|
import org.whispersystems.textsecuregcm.metrics.NetworkSentGauge;
|
||||||
import org.whispersystems.textsecuregcm.providers.MemcacheHealthCheck;
|
import org.whispersystems.textsecuregcm.providers.MemcacheHealthCheck;
|
||||||
import org.whispersystems.textsecuregcm.providers.MemcachedClientFactory;
|
import org.whispersystems.textsecuregcm.providers.MemcachedClientFactory;
|
||||||
import org.whispersystems.textsecuregcm.providers.RedisClientFactory;
|
import org.whispersystems.textsecuregcm.providers.RedisClientFactory;
|
||||||
@@ -66,18 +63,30 @@ import org.whispersystems.textsecuregcm.storage.PendingAccountsManager;
|
|||||||
import org.whispersystems.textsecuregcm.storage.PendingDevices;
|
import org.whispersystems.textsecuregcm.storage.PendingDevices;
|
||||||
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
import org.whispersystems.textsecuregcm.util.CORSHeaderFilter;
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
import org.whispersystems.textsecuregcm.util.UrlSigner;
|
import org.whispersystems.textsecuregcm.util.UrlSigner;
|
||||||
|
import org.whispersystems.textsecuregcm.websocket.WebsocketControllerFactory;
|
||||||
import org.whispersystems.textsecuregcm.workers.DirectoryCommand;
|
import org.whispersystems.textsecuregcm.workers.DirectoryCommand;
|
||||||
|
|
||||||
|
import javax.servlet.DispatcherType;
|
||||||
|
import javax.servlet.FilterRegistration;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
import java.security.Security;
|
import java.security.Security;
|
||||||
|
import java.util.EnumSet;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import io.dropwizard.Application;
|
||||||
|
import io.dropwizard.db.DataSourceFactory;
|
||||||
|
import io.dropwizard.jdbi.DBIFactory;
|
||||||
|
import io.dropwizard.metrics.graphite.GraphiteReporterFactory;
|
||||||
|
import io.dropwizard.migrations.MigrationsBundle;
|
||||||
|
import io.dropwizard.setup.Bootstrap;
|
||||||
|
import io.dropwizard.setup.Environment;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
public class WhisperServerService extends Application<WhisperServerConfiguration> {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
@@ -85,41 +94,45 @@ public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(Bootstrap<WhisperServerConfiguration> bootstrap) {
|
public void initialize(Bootstrap<WhisperServerConfiguration> bootstrap) {
|
||||||
bootstrap.setName("whisper-server");
|
|
||||||
bootstrap.addCommand(new DirectoryCommand());
|
bootstrap.addCommand(new DirectoryCommand());
|
||||||
bootstrap.addBundle(new MigrationsBundle<WhisperServerConfiguration>() {
|
bootstrap.addBundle(new MigrationsBundle<WhisperServerConfiguration>() {
|
||||||
@Override
|
@Override
|
||||||
public DatabaseConfiguration getDatabaseConfiguration(WhisperServerConfiguration configuration) {
|
public DataSourceFactory getDataSourceFactory(WhisperServerConfiguration configuration) {
|
||||||
return configuration.getDatabaseConfiguration();
|
return configuration.getDataSourceFactory();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "whisper-server";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(WhisperServerConfiguration config, Environment environment)
|
public void run(WhisperServerConfiguration config, Environment environment)
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
config.getHttpConfiguration().setConnectorType(HttpConfiguration.ConnectorType.NONBLOCKING);
|
SharedMetricRegistries.add(Constants.METRICS_NAME, environment.metrics());
|
||||||
|
environment.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
|
||||||
DBIFactory dbiFactory = new DBIFactory();
|
DBIFactory dbiFactory = new DBIFactory();
|
||||||
DBI jdbi = dbiFactory.build(environment, config.getDatabaseConfiguration(), "postgresql");
|
DBI jdbi = dbiFactory.build(environment, config.getDataSourceFactory(), "postgresql");
|
||||||
|
|
||||||
Accounts accounts = jdbi.onDemand(Accounts.class);
|
Accounts accounts = jdbi.onDemand(Accounts.class);
|
||||||
PendingAccounts pendingAccounts = jdbi.onDemand(PendingAccounts.class);
|
PendingAccounts pendingAccounts = jdbi.onDemand(PendingAccounts.class);
|
||||||
PendingDevices pendingDevices = jdbi.onDemand(PendingDevices.class);
|
PendingDevices pendingDevices = jdbi.onDemand(PendingDevices.class);
|
||||||
Keys keys = jdbi.onDemand(Keys.class);
|
Keys keys = jdbi.onDemand(Keys.class);
|
||||||
StoredMessages storedMessages = jdbi.onDemand(StoredMessages.class );
|
|
||||||
|
|
||||||
MemcachedClient memcachedClient = new MemcachedClientFactory(config.getMemcacheConfiguration()).getClient();
|
MemcachedClient memcachedClient = new MemcachedClientFactory(config.getMemcacheConfiguration()).getClient();
|
||||||
JedisPool redisClient = new RedisClientFactory(config.getRedisConfiguration()).getRedisClientPool();
|
JedisPool redisClient = new RedisClientFactory(config.getRedisConfiguration()).getRedisClientPool();
|
||||||
|
|
||||||
DirectoryManager directory = new DirectoryManager(redisClient);
|
DirectoryManager directory = new DirectoryManager(redisClient);
|
||||||
PendingAccountsManager pendingAccountsManager = new PendingAccountsManager(pendingAccounts, memcachedClient);
|
PendingAccountsManager pendingAccountsManager = new PendingAccountsManager(pendingAccounts, memcachedClient);
|
||||||
PendingDevicesManager pendingDevicesManager = new PendingDevicesManager(pendingDevices, memcachedClient);
|
PendingDevicesManager pendingDevicesManager = new PendingDevicesManager (pendingDevices, memcachedClient );
|
||||||
AccountsManager accountsManager = new AccountsManager(accounts, directory, memcachedClient);
|
AccountsManager accountsManager = new AccountsManager(accounts, directory, memcachedClient);
|
||||||
FederatedClientManager federatedClientManager = new FederatedClientManager(config.getFederationConfiguration());
|
FederatedClientManager federatedClientManager = new FederatedClientManager(config.getFederationConfiguration());
|
||||||
PubSubManager pubSubManager = new PubSubManager(redisClient);
|
StoredMessages storedMessages = new StoredMessages(redisClient);
|
||||||
StoredMessageManager storedMessageManager = new StoredMessageManager(storedMessages, pubSubManager);
|
PubSubManager pubSubManager = new PubSubManager(redisClient);
|
||||||
|
|
||||||
AccountAuthenticator deviceAuthenticator = new AccountAuthenticator(accountsManager);
|
AccountAuthenticator deviceAuthenticator = new AccountAuthenticator(accountsManager);
|
||||||
RateLimiters rateLimiters = new RateLimiters(config.getLimitsConfiguration(), memcachedClient);
|
RateLimiters rateLimiters = new RateLimiters(config.getLimitsConfiguration(), memcachedClient);
|
||||||
@@ -130,49 +143,70 @@ public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
|||||||
UrlSigner urlSigner = new UrlSigner(config.getS3Configuration());
|
UrlSigner urlSigner = new UrlSigner(config.getS3Configuration());
|
||||||
PushSender pushSender = new PushSender(config.getGcmConfiguration(),
|
PushSender pushSender = new PushSender(config.getGcmConfiguration(),
|
||||||
config.getApnConfiguration(),
|
config.getApnConfiguration(),
|
||||||
storedMessageManager,
|
storedMessages, pubSubManager,
|
||||||
accountsManager);
|
accountsManager);
|
||||||
|
|
||||||
AttachmentController attachmentController = new AttachmentController(rateLimiters, federatedClientManager, urlSigner);
|
AttachmentController attachmentController = new AttachmentController(rateLimiters, federatedClientManager, urlSigner);
|
||||||
KeysController keysController = new KeysController(rateLimiters, keys, accountsManager, federatedClientManager);
|
KeysController keysController = new KeysController(rateLimiters, keys, accountsManager, federatedClientManager);
|
||||||
MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
||||||
|
|
||||||
environment.addProvider(new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(config.getFederationConfiguration()),
|
environment.jersey().register(new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(config.getFederationConfiguration()),
|
||||||
FederatedPeer.class,
|
FederatedPeer.class,
|
||||||
deviceAuthenticator,
|
deviceAuthenticator,
|
||||||
Device.class, "WhisperServer"));
|
Device.class, "WhisperServer"));
|
||||||
|
|
||||||
environment.addResource(new AccountController(pendingAccountsManager, accountsManager, rateLimiters, smsSender));
|
environment.jersey().register(new AccountController(pendingAccountsManager, accountsManager, rateLimiters, smsSender));
|
||||||
environment.addResource(new DeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
environment.jersey().register(new DeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
||||||
environment.addResource(new DirectoryController(rateLimiters, directory));
|
environment.jersey().register(new DirectoryController(rateLimiters, directory));
|
||||||
environment.addResource(new FederationController(accountsManager, attachmentController, keysController, messageController));
|
environment.jersey().register(new FederationController(accountsManager, attachmentController, keysController, messageController));
|
||||||
environment.addResource(attachmentController);
|
environment.jersey().register(attachmentController);
|
||||||
environment.addResource(keysController);
|
environment.jersey().register(keysController);
|
||||||
environment.addResource(messageController);
|
environment.jersey().register(messageController);
|
||||||
|
|
||||||
if (config.getWebsocketConfiguration().isEnabled()) {
|
if (config.getWebsocketConfiguration().isEnabled()) {
|
||||||
environment.addServlet(new WebsocketControllerFactory(deviceAuthenticator, storedMessageManager, pubSubManager),
|
WebsocketControllerFactory servlet = new WebsocketControllerFactory(deviceAuthenticator,
|
||||||
"/v1/websocket/");
|
pushSender,
|
||||||
environment.addFilter(new CORSHeaderFilter(), "/*");
|
storedMessages,
|
||||||
|
pubSubManager);
|
||||||
|
|
||||||
|
ServletRegistration.Dynamic websocket = environment.servlets().addServlet("WebSocket", servlet);
|
||||||
|
websocket.addMapping("/v1/websocket/*");
|
||||||
|
websocket.setAsyncSupported(true);
|
||||||
|
|
||||||
|
FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
|
||||||
|
filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
|
||||||
|
filter.setInitParameter("allowedOrigins", "*");
|
||||||
|
filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
|
||||||
|
filter.setInitParameter("allowedMethods", "GET,PUT,POST,DELETE,OPTIONS");
|
||||||
|
filter.setInitParameter("preflightMaxAge", "5184000");
|
||||||
|
filter.setInitParameter("allowCredentials", "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
environment.addHealthCheck(new RedisHealthCheck(redisClient));
|
environment.healthChecks().register("redis", new RedisHealthCheck(redisClient));
|
||||||
environment.addHealthCheck(new MemcacheHealthCheck(memcachedClient));
|
environment.healthChecks().register("memcache", new MemcacheHealthCheck(memcachedClient));
|
||||||
|
|
||||||
environment.addProvider(new IOExceptionMapper());
|
environment.jersey().register(new IOExceptionMapper());
|
||||||
environment.addProvider(new RateLimitExceededExceptionMapper());
|
environment.jersey().register(new RateLimitExceededExceptionMapper());
|
||||||
|
|
||||||
|
environment.metrics().register(name(CpuUsageGauge.class, "cpu"), new CpuUsageGauge());
|
||||||
|
environment.metrics().register(name(FreeMemoryGauge.class, "free_memory"), new FreeMemoryGauge());
|
||||||
|
environment.metrics().register(name(NetworkSentGauge.class, "bytes_sent"), new NetworkSentGauge());
|
||||||
|
environment.metrics().register(name(NetworkReceivedGauge.class, "bytes_received"), new NetworkReceivedGauge());
|
||||||
|
|
||||||
if (config.getGraphiteConfiguration().isEnabled()) {
|
if (config.getGraphiteConfiguration().isEnabled()) {
|
||||||
GraphiteReporter.enable(15, TimeUnit.SECONDS,
|
GraphiteReporterFactory graphiteReporterFactory = new GraphiteReporterFactory();
|
||||||
config.getGraphiteConfiguration().getHost(),
|
graphiteReporterFactory.setHost(config.getGraphiteConfiguration().getHost());
|
||||||
config.getGraphiteConfiguration().getPort());
|
graphiteReporterFactory.setPort(config.getGraphiteConfiguration().getPort());
|
||||||
|
|
||||||
|
GraphiteReporter graphiteReporter = (GraphiteReporter) graphiteReporterFactory.build(environment.metrics());
|
||||||
|
graphiteReporter.start(15, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.getDataDogConfiguration().isEnabled()) {
|
if (config.getMetricsConfiguration().isEnabled()) {
|
||||||
new DatadogReporter.Builder().withApiKey(config.getDataDogConfiguration().getApiKey())
|
new JsonMetricsReporter(environment.metrics(),
|
||||||
.withVmMetricsEnabled(true)
|
config.getMetricsConfiguration().getToken(),
|
||||||
.build()
|
config.getMetricsConfiguration().getHost())
|
||||||
.start(15, TimeUnit.SECONDS);
|
.start(60, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,29 +16,27 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.auth;
|
package org.whispersystems.textsecuregcm.auth;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.AuthenticationException;
|
|
||||||
import com.yammer.dropwizard.auth.Authenticator;
|
|
||||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
|
||||||
import com.yammer.metrics.Metrics;
|
|
||||||
import com.yammer.metrics.core.Meter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import io.dropwizard.auth.AuthenticationException;
|
||||||
|
import io.dropwizard.auth.Authenticator;
|
||||||
|
import io.dropwizard.auth.basic.BasicCredentials;
|
||||||
|
|
||||||
public class AccountAuthenticator implements Authenticator<BasicCredentials, Account> {
|
public class AccountAuthenticator implements Authenticator<BasicCredentials, Account> {
|
||||||
|
|
||||||
private final Meter authenticationFailedMeter = Metrics.newMeter(AccountAuthenticator.class,
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
"authentication", "failed",
|
private final Meter authenticationFailedMeter = metricRegistry.meter(name(getClass(), "authentication", "failed" ));
|
||||||
TimeUnit.MINUTES);
|
private final Meter authenticationSucceededMeter = metricRegistry.meter(name(getClass(), "authentication", "succeeded"));
|
||||||
|
|
||||||
private final Meter authenticationSucceededMeter = Metrics.newMeter(AccountAuthenticator.class,
|
|
||||||
"authentication", "succeeded",
|
|
||||||
TimeUnit.MINUTES);
|
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AccountAuthenticator.class);
|
private final Logger logger = LoggerFactory.getLogger(AccountAuthenticator.class);
|
||||||
|
|
||||||
|
|||||||
@@ -25,13 +25,13 @@ import java.io.IOException;
|
|||||||
public class AuthorizationHeader {
|
public class AuthorizationHeader {
|
||||||
|
|
||||||
private final String number;
|
private final String number;
|
||||||
private final long accountId;
|
private final long accountId;
|
||||||
private final String password;
|
private final String password;
|
||||||
|
|
||||||
private AuthorizationHeader(String number, long accountId, String password) {
|
private AuthorizationHeader(String number, long accountId, String password) {
|
||||||
this.number = number;
|
this.number = number;
|
||||||
this.accountId = accountId;
|
this.accountId = accountId;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AuthorizationHeader fromUserAndPassword(String user, String password) throws InvalidAuthorizationHeaderException {
|
public static AuthorizationHeader fromUserAndPassword(String user, String password) throws InvalidAuthorizationHeaderException {
|
||||||
|
|||||||
@@ -16,30 +16,35 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.auth;
|
package org.whispersystems.textsecuregcm.auth;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.AuthenticationException;
|
|
||||||
import com.yammer.dropwizard.auth.Authenticator;
|
|
||||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
|
||||||
import com.yammer.metrics.Metrics;
|
|
||||||
import com.yammer.metrics.core.Meter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.configuration.FederationConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.FederationConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.federation.FederatedPeer;
|
import org.whispersystems.textsecuregcm.federation.FederatedPeer;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import io.dropwizard.auth.AuthenticationException;
|
||||||
|
import io.dropwizard.auth.Authenticator;
|
||||||
|
import io.dropwizard.auth.basic.BasicCredentials;
|
||||||
|
|
||||||
|
|
||||||
public class FederatedPeerAuthenticator implements Authenticator<BasicCredentials, FederatedPeer> {
|
public class FederatedPeerAuthenticator implements Authenticator<BasicCredentials, FederatedPeer> {
|
||||||
|
|
||||||
private final Meter authenticationFailedMeter = Metrics.newMeter(FederatedPeerAuthenticator.class,
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
"authentication", "failed",
|
|
||||||
TimeUnit.MINUTES);
|
|
||||||
|
|
||||||
private final Meter authenticationSucceededMeter = Metrics.newMeter(FederatedPeerAuthenticator.class,
|
private final Meter authenticationFailedMeter = metricRegistry.meter(name(getClass(),
|
||||||
"authentication", "succeeded",
|
"authentication",
|
||||||
TimeUnit.MINUTES);
|
"failed"));
|
||||||
|
|
||||||
|
private final Meter authenticationSucceededMeter = metricRegistry.meter(name(getClass(),
|
||||||
|
"authentication",
|
||||||
|
"succeeded"));
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(FederatedPeerAuthenticator.class);
|
private final Logger logger = LoggerFactory.getLogger(FederatedPeerAuthenticator.class);
|
||||||
|
|
||||||
|
|||||||
@@ -21,17 +21,14 @@ import com.sun.jersey.core.spi.component.ComponentContext;
|
|||||||
import com.sun.jersey.core.spi.component.ComponentScope;
|
import com.sun.jersey.core.spi.component.ComponentScope;
|
||||||
import com.sun.jersey.spi.inject.Injectable;
|
import com.sun.jersey.spi.inject.Injectable;
|
||||||
import com.sun.jersey.spi.inject.InjectableProvider;
|
import com.sun.jersey.spi.inject.InjectableProvider;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.dropwizard.auth.Authenticator;
|
import io.dropwizard.auth.Auth;
|
||||||
import com.yammer.dropwizard.auth.basic.BasicAuthProvider;
|
import io.dropwizard.auth.Authenticator;
|
||||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
import io.dropwizard.auth.basic.BasicAuthProvider;
|
||||||
import org.slf4j.Logger;
|
import io.dropwizard.auth.basic.BasicCredentials;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class MultiBasicAuthProvider<T1,T2> implements InjectableProvider<Auth, Parameter> {
|
public class MultiBasicAuthProvider<T1,T2> implements InjectableProvider<Auth, Parameter> {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(MultiBasicAuthProvider.class);
|
|
||||||
|
|
||||||
private final BasicAuthProvider<T1> provider1;
|
private final BasicAuthProvider<T1> provider1;
|
||||||
private final BasicAuthProvider<T2> provider2;
|
private final BasicAuthProvider<T2> provider2;
|
||||||
|
|
||||||
@@ -44,8 +41,8 @@ public class MultiBasicAuthProvider<T1,T2> implements InjectableProvider<Auth, P
|
|||||||
Class<?> clazz2,
|
Class<?> clazz2,
|
||||||
String realm)
|
String realm)
|
||||||
{
|
{
|
||||||
this.provider1 = new BasicAuthProvider<T1>(authenticator1, realm);
|
this.provider1 = new BasicAuthProvider<>(authenticator1, realm);
|
||||||
this.provider2 = new BasicAuthProvider<T2>(authenticator2, realm);
|
this.provider2 = new BasicAuthProvider<>(authenticator2, realm);
|
||||||
this.clazz1 = clazz1;
|
this.clazz1 = clazz1;
|
||||||
this.clazz2 = clazz2;
|
this.clazz2 = clazz2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package org.whispersystems.textsecuregcm.configuration;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
public class DataDogConfiguration {
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
private String apiKey;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
private boolean enabled = false;
|
|
||||||
|
|
||||||
public String getApiKey() {
|
|
||||||
return apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return enabled && apiKey != null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.configuration;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
public class MetricsConfiguration {
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private boolean enabled = false;
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getToken() {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled && token != null && host != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,10 +16,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.auth.AuthenticationCredentials;
|
import org.whispersystems.textsecuregcm.auth.AuthenticationCredentials;
|
||||||
@@ -55,6 +54,8 @@ import java.io.IOException;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/accounts")
|
@Path("/v1/accounts")
|
||||||
public class AccountController {
|
public class AccountController {
|
||||||
|
|
||||||
|
|||||||
@@ -17,9 +17,8 @@
|
|||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
import com.amazonaws.HttpMethod;
|
import com.amazonaws.HttpMethod;
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.AttachmentDescriptor;
|
import org.whispersystems.textsecuregcm.entities.AttachmentDescriptor;
|
||||||
@@ -44,6 +43,8 @@ import java.net.URL;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
|
|
||||||
@Path("/v1/attachments")
|
@Path("/v1/attachments")
|
||||||
public class AttachmentController {
|
public class AttachmentController {
|
||||||
|
|||||||
@@ -16,10 +16,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.auth.AuthenticationCredentials;
|
import org.whispersystems.textsecuregcm.auth.AuthenticationCredentials;
|
||||||
@@ -29,8 +28,8 @@ import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
|||||||
import org.whispersystems.textsecuregcm.entities.DeviceResponse;
|
import org.whispersystems.textsecuregcm.entities.DeviceResponse;
|
||||||
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||||
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||||
|
|
||||||
@@ -48,6 +47,8 @@ import javax.ws.rs.core.Response;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/devices")
|
@Path("/v1/devices")
|
||||||
public class DeviceController {
|
public class DeviceController {
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Histogram;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
||||||
@@ -28,6 +30,7 @@ import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
|||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||||
import org.whispersystems.textsecuregcm.util.Base64;
|
import org.whispersystems.textsecuregcm.util.Base64;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
@@ -43,10 +46,15 @@ import java.io.IOException;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/directory")
|
@Path("/v1/directory")
|
||||||
public class DirectoryController {
|
public class DirectoryController {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(DirectoryController.class);
|
private final Logger logger = LoggerFactory.getLogger(DirectoryController.class);
|
||||||
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
|
private final Histogram contactsHistogram = metricRegistry.histogram(name(getClass(), "contacts"));
|
||||||
|
|
||||||
private final RateLimiters rateLimiters;
|
private final RateLimiters rateLimiters;
|
||||||
private final DirectoryManager directory;
|
private final DirectoryManager directory;
|
||||||
@@ -56,7 +64,7 @@ public class DirectoryController {
|
|||||||
this.rateLimiters = rateLimiters;
|
this.rateLimiters = rateLimiters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Timed()
|
@Timed
|
||||||
@GET
|
@GET
|
||||||
@Path("/{token}")
|
@Path("/{token}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@@ -77,7 +85,7 @@ public class DirectoryController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Timed()
|
@Timed
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/tokens")
|
@Path("/tokens")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@@ -86,6 +94,7 @@ public class DirectoryController {
|
|||||||
throws RateLimitExceededException
|
throws RateLimitExceededException
|
||||||
{
|
{
|
||||||
rateLimiters.getContactsLimiter().validate(account.getNumber(), contacts.getContacts().size());
|
rateLimiters.getContactsLimiter().validate(account.getNumber(), contacts.getContacts().size());
|
||||||
|
contactsHistogram.update(contacts.getContacts().size());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<byte[]> tokens = new LinkedList<>();
|
List<byte[]> tokens = new LinkedList<>();
|
||||||
|
|||||||
@@ -16,9 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.AccountCount;
|
import org.whispersystems.textsecuregcm.entities.AccountCount;
|
||||||
@@ -45,6 +44,8 @@ import java.io.IOException;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/federation")
|
@Path("/v1/federation")
|
||||||
public class FederationController {
|
public class FederationController {
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||||
@@ -47,6 +46,8 @@ import javax.ws.rs.core.Response;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/keys")
|
@Path("/v1/keys")
|
||||||
public class KeysController {
|
public class KeysController {
|
||||||
|
|
||||||
@@ -70,13 +71,19 @@ public class KeysController {
|
|||||||
@PUT
|
@PUT
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
public void setKeys(@Auth Account account, @Valid PreKeyList preKeys) {
|
public void setKeys(@Auth Account account, @Valid PreKeyList preKeys) {
|
||||||
Device device = account.getAuthenticatedDevice().get();
|
Device device = account.getAuthenticatedDevice().get();
|
||||||
|
String identityKey = preKeys.getLastResortKey().getIdentityKey();
|
||||||
|
|
||||||
|
if (!identityKey.equals(account.getIdentityKey())) {
|
||||||
|
account.setIdentityKey(identityKey);
|
||||||
|
accounts.update(account);
|
||||||
|
}
|
||||||
|
|
||||||
keys.store(account.getNumber(), device.getId(), preKeys.getKeys(), preKeys.getLastResortKey());
|
keys.store(account.getNumber(), device.getId(), preKeys.getKeys(), preKeys.getLastResortKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Timed
|
@Timed
|
||||||
@GET
|
@GET
|
||||||
@Path("/")
|
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public PreKeyStatus getStatus(@Auth Account account) {
|
public PreKeyStatus getStatus(@Auth Account account) {
|
||||||
int count = keys.getCount(account.getNumber(), account.getAuthenticatedDevice().get().getId());
|
int count = keys.getCount(account.getNumber(), account.getAuthenticatedDevice().get().getId());
|
||||||
@@ -167,6 +174,7 @@ public class KeysController {
|
|||||||
|
|
||||||
if (device.isPresent() && device.get().isActive()) {
|
if (device.isPresent() && device.get().isActive()) {
|
||||||
preKey.setRegistrationId(device.get().getRegistrationId());
|
preKey.setRegistrationId(device.get().getRegistrationId());
|
||||||
|
preKey.setIdentityKey(destination.getIdentityKey());
|
||||||
filteredKeys.add(preKey);
|
filteredKeys.add(preKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.yammer.dropwizard.auth.Auth;
|
|
||||||
import com.yammer.metrics.annotation.Timed;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.IncomingMessage;
|
import org.whispersystems.textsecuregcm.entities.IncomingMessage;
|
||||||
@@ -56,6 +55,8 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/messages")
|
@Path("/v1/messages")
|
||||||
public class MessageController {
|
public class MessageController {
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class MismatchedDevicesException extends Exception {
|
public class MismatchedDevicesException extends Exception {
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
import org.whispersystems.textsecuregcm.federation.NoSuchPeerException;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -27,7 +25,7 @@ public class NoSuchUserException extends Exception {
|
|||||||
|
|
||||||
public NoSuchUserException(String user) {
|
public NoSuchUserException(String user) {
|
||||||
super(user);
|
super(user);
|
||||||
missing = new LinkedList<String>();
|
missing = new LinkedList<>();
|
||||||
missing.add(user);
|
missing.add(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,26 @@
|
|||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.eclipse.jetty.websocket.WebSocket;
|
import com.google.common.base.Optional;
|
||||||
|
import org.eclipse.jetty.websocket.api.CloseStatus;
|
||||||
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||||
|
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||||
import org.whispersystems.textsecuregcm.entities.AcknowledgeWebsocketMessage;
|
import org.whispersystems.textsecuregcm.entities.AcknowledgeWebsocketMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||||
import org.whispersystems.textsecuregcm.entities.IncomingWebsocketMessage;
|
import org.whispersystems.textsecuregcm.entities.IncomingWebsocketMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
|
||||||
|
import org.whispersystems.textsecuregcm.push.PushSender;
|
||||||
|
import org.whispersystems.textsecuregcm.push.TransientPushFailureException;
|
||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubListener;
|
import org.whispersystems.textsecuregcm.storage.PubSubListener;
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubMessage;
|
import org.whispersystems.textsecuregcm.storage.PubSubMessage;
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
||||||
import org.whispersystems.textsecuregcm.websocket.WebsocketMessage;
|
import org.whispersystems.textsecuregcm.websocket.WebsocketMessage;
|
||||||
|
|
||||||
@@ -22,65 +31,132 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class WebsocketController implements WebSocket.OnTextMessage, PubSubListener {
|
import io.dropwizard.auth.AuthenticationException;
|
||||||
|
import io.dropwizard.auth.basic.BasicCredentials;
|
||||||
|
|
||||||
|
public class WebsocketController implements WebSocketListener, PubSubListener {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(WebsocketController.class);
|
private static final Logger logger = LoggerFactory.getLogger(WebsocketController.class);
|
||||||
private static final ObjectMapper mapper = new ObjectMapper();
|
private static final ObjectMapper mapper = new ObjectMapper();
|
||||||
private static final Map<Long, String> pendingMessages = new HashMap<>();
|
private static final Map<Long, String> pendingMessages = new HashMap<>();
|
||||||
|
|
||||||
private final StoredMessageManager storedMessageManager;
|
private final AccountAuthenticator accountAuthenticator;
|
||||||
private final PubSubManager pubSubManager;
|
private final PubSubManager pubSubManager;
|
||||||
|
private final StoredMessages storedMessages;
|
||||||
|
private final PushSender pushSender;
|
||||||
|
|
||||||
private final Account account;
|
private WebsocketAddress address;
|
||||||
private final Device device;
|
private Account account;
|
||||||
|
private Device device;
|
||||||
|
private Session session;
|
||||||
|
|
||||||
private Connection connection;
|
private long pendingMessageSequence;
|
||||||
private long pendingMessageSequence;
|
|
||||||
|
|
||||||
public WebsocketController(StoredMessageManager storedMessageManager,
|
public WebsocketController(AccountAuthenticator accountAuthenticator,
|
||||||
PubSubManager pubSubManager,
|
PushSender pushSender,
|
||||||
Account account)
|
PubSubManager pubSubManager,
|
||||||
|
StoredMessages storedMessages)
|
||||||
{
|
{
|
||||||
this.storedMessageManager = storedMessageManager;
|
this.accountAuthenticator = accountAuthenticator;
|
||||||
|
this.pushSender = pushSender;
|
||||||
this.pubSubManager = pubSubManager;
|
this.pubSubManager = pubSubManager;
|
||||||
this.account = account;
|
this.storedMessages = storedMessages;
|
||||||
this.device = account.getAuthenticatedDevice().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onOpen(Connection connection) {
|
|
||||||
this.connection = connection;
|
|
||||||
pubSubManager.subscribe(new WebsocketAddress(this.account.getId(), this.device.getId()), this);
|
|
||||||
handleQueryDatabase();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int i, String s) {
|
public void onWebSocketConnect(Session session) {
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMessage(String body) {
|
|
||||||
try {
|
try {
|
||||||
IncomingWebsocketMessage incomingMessage = mapper.readValue(body, IncomingWebsocketMessage.class);
|
UpgradeRequest request = session.getUpgradeRequest();
|
||||||
|
Map<String, String[]> parameters = request.getParameterMap();
|
||||||
|
String[] usernames = parameters.get("login" );
|
||||||
|
String[] passwords = parameters.get("password");
|
||||||
|
|
||||||
switch (incomingMessage.getType()) {
|
if (usernames == null || usernames.length == 0 ||
|
||||||
case IncomingWebsocketMessage.TYPE_ACKNOWLEDGE_MESSAGE: handleMessageAck(body); break;
|
passwords == null || passwords.length == 0)
|
||||||
case IncomingWebsocketMessage.TYPE_PING_MESSAGE: handlePing(); break;
|
{
|
||||||
default: handleClose(); break;
|
session.close(new CloseStatus(4001, "Unauthorized"));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
logger.debug("Parse", e);
|
BasicCredentials credentials = new BasicCredentials(usernames[0], passwords[0]);
|
||||||
handleClose();
|
Optional<Account> account = accountAuthenticator.authenticate(credentials);
|
||||||
|
|
||||||
|
if (!account.isPresent()) {
|
||||||
|
session.close(new CloseStatus(4001, "Unauthorized"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.account = account.get();
|
||||||
|
this.device = account.get().getAuthenticatedDevice().get();
|
||||||
|
this.address = new WebsocketAddress(this.account.getId(), this.device.getId());
|
||||||
|
this.session = session;
|
||||||
|
|
||||||
|
this.session.setIdleTimeout(10 * 60 * 1000);
|
||||||
|
this.pubSubManager.subscribe(this.address, this);
|
||||||
|
|
||||||
|
handleQueryDatabase();
|
||||||
|
} catch (AuthenticationException e) {
|
||||||
|
try { session.close(1011, "Server Error");} catch (IOException e1) {}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
logger.info("Abrupt session close.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketText(String body) {
|
||||||
|
try {
|
||||||
|
IncomingWebsocketMessage incomingMessage = mapper.readValue(body, IncomingWebsocketMessage.class);
|
||||||
|
|
||||||
|
switch (incomingMessage.getType()) {
|
||||||
|
case IncomingWebsocketMessage.TYPE_ACKNOWLEDGE_MESSAGE:
|
||||||
|
handleMessageAck(body);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
close(new CloseStatus(1008, "Unknown Type"));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.debug("Parse", e);
|
||||||
|
close(new CloseStatus(1008, "Badly Formatted"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketClose(int i, String s) {
|
||||||
|
pubSubManager.unsubscribe(this.address, this);
|
||||||
|
|
||||||
|
List<String> remainingMessages = new LinkedList<>();
|
||||||
|
|
||||||
|
synchronized (pendingMessages) {
|
||||||
|
Long[] pendingKeys = pendingMessages.keySet().toArray(new Long[0]);
|
||||||
|
Arrays.sort(pendingKeys);
|
||||||
|
|
||||||
|
for (long pendingKey : pendingKeys) {
|
||||||
|
remainingMessages.add(pendingMessages.get(pendingKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingMessages.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String remainingMessage : remainingMessages) {
|
||||||
|
try {
|
||||||
|
pushSender.sendMessage(account, device, new EncryptedOutgoingMessage(remainingMessage));
|
||||||
|
} catch (NotPushRegisteredException | TransientPushFailureException e) {
|
||||||
|
logger.warn("onWebSocketClose", e);
|
||||||
|
storedMessages.insert(account.getId(), device.getId(), remainingMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPubSubMessage(PubSubMessage outgoingMessage) {
|
public void onPubSubMessage(PubSubMessage outgoingMessage) {
|
||||||
switch (outgoingMessage.getType()) {
|
switch (outgoingMessage.getType()) {
|
||||||
case PubSubMessage.TYPE_DELIVER: handleDeliverOutgoingMessage(outgoingMessage.getContents()); break;
|
case PubSubMessage.TYPE_DELIVER:
|
||||||
case PubSubMessage.TYPE_QUERY_DB: handleQueryDatabase(); break;
|
handleDeliverOutgoingMessage(outgoingMessage.getContents());
|
||||||
|
break;
|
||||||
|
case PubSubMessage.TYPE_QUERY_DB:
|
||||||
|
handleQueryDatabase();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
logger.warn("Unknown pubsub message: " + outgoingMessage.getType());
|
logger.warn("Unknown pubsub message: " + outgoingMessage.getType());
|
||||||
}
|
}
|
||||||
@@ -95,10 +171,11 @@ public class WebsocketController implements WebSocket.OnTextMessage, PubSubListe
|
|||||||
pendingMessages.put(messageSequence, message);
|
pendingMessages.put(messageSequence, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.sendMessage(mapper.writeValueAsString(new WebsocketMessage(messageSequence, message)));
|
WebsocketMessage websocketMessage = new WebsocketMessage(messageSequence, message);
|
||||||
|
session.getRemote().sendStringByFuture(mapper.writeValueAsString(websocketMessage));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.debug("Response failed", e);
|
logger.debug("Response failed", e);
|
||||||
handleClose();
|
close(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,42 +191,33 @@ public class WebsocketController implements WebSocket.OnTextMessage, PubSubListe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handlePing() {
|
|
||||||
try {
|
|
||||||
IncomingWebsocketMessage pongMessage = new IncomingWebsocketMessage(IncomingWebsocketMessage.TYPE_PONG_MESSAGE);
|
|
||||||
connection.sendMessage(mapper.writeValueAsString(pongMessage));
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.warn("Pong failed", e);
|
|
||||||
handleClose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleClose() {
|
|
||||||
pubSubManager.unsubscribe(new WebsocketAddress(account.getId(), device.getId()), this);
|
|
||||||
connection.close();
|
|
||||||
|
|
||||||
List<String> remainingMessages = new LinkedList<>();
|
|
||||||
|
|
||||||
synchronized (pendingMessages) {
|
|
||||||
Long[] pendingKeys = pendingMessages.keySet().toArray(new Long[0]);
|
|
||||||
Arrays.sort(pendingKeys);
|
|
||||||
|
|
||||||
for (long pendingKey : pendingKeys) {
|
|
||||||
remainingMessages.add(pendingMessages.get(pendingKey));
|
|
||||||
}
|
|
||||||
|
|
||||||
pendingMessages.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
storedMessageManager.storeMessages(account, device, remainingMessages);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleQueryDatabase() {
|
private void handleQueryDatabase() {
|
||||||
List<String> messages = storedMessageManager.getOutgoingMessages(account, device);
|
List<String> messages = storedMessages.getMessagesForDevice(account.getId(), device.getId());
|
||||||
|
|
||||||
for (String message : messages) {
|
for (String message : messages) {
|
||||||
handleDeliverOutgoingMessage(message);
|
handleDeliverOutgoingMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketBinary(byte[] bytes, int i, int i2) {
|
||||||
|
logger.info("Received binary message!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketError(Throwable throwable) {
|
||||||
|
logger.info("onWebSocketError", throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void close(CloseStatus closeStatus) {
|
||||||
|
try {
|
||||||
|
if (this.session != null) {
|
||||||
|
if (closeStatus != null) this.session.close(closeStatus);
|
||||||
|
else this.session.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.info("close()", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
package org.whispersystems.textsecuregcm.controllers;
|
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
|
||||||
import com.yammer.dropwizard.auth.AuthenticationException;
|
|
||||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
|
||||||
import org.eclipse.jetty.websocket.WebSocket;
|
|
||||||
import org.eclipse.jetty.websocket.WebSocketServlet;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
public class WebsocketControllerFactory extends WebSocketServlet {
|
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(WebsocketControllerFactory.class);
|
|
||||||
|
|
||||||
private final StoredMessageManager storedMessageManager;
|
|
||||||
private final PubSubManager pubSubManager;
|
|
||||||
private final AccountAuthenticator accountAuthenticator;
|
|
||||||
|
|
||||||
private final LinkedHashMap<BasicCredentials, Optional<Account>> cache =
|
|
||||||
new LinkedHashMap<BasicCredentials, Optional<Account>>() {
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<BasicCredentials, Optional<Account>> eldest) {
|
|
||||||
return size() > 10;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public WebsocketControllerFactory(AccountAuthenticator accountAuthenticator,
|
|
||||||
StoredMessageManager storedMessageManager,
|
|
||||||
PubSubManager pubSubManager)
|
|
||||||
{
|
|
||||||
this.accountAuthenticator = accountAuthenticator;
|
|
||||||
this.storedMessageManager = storedMessageManager;
|
|
||||||
this.pubSubManager = pubSubManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String s) {
|
|
||||||
try {
|
|
||||||
String username = request.getParameter("user");
|
|
||||||
String password = request.getParameter("password");
|
|
||||||
|
|
||||||
if (username == null || password == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
BasicCredentials credentials = new BasicCredentials(username, password);
|
|
||||||
|
|
||||||
Optional<Account> account = cache.remove(credentials);
|
|
||||||
|
|
||||||
if (account == null) {
|
|
||||||
account = accountAuthenticator.authenticate(new BasicCredentials(username, password));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!account.isPresent()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new WebsocketController(storedMessageManager, pubSubManager, account.get());
|
|
||||||
} catch (AuthenticationException e) {
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean checkOrigin(HttpServletRequest request, String origin) {
|
|
||||||
try {
|
|
||||||
String username = request.getParameter("user");
|
|
||||||
String password = request.getParameter("password");
|
|
||||||
|
|
||||||
if (username == null || password == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
BasicCredentials credentials = new BasicCredentials(username, password);
|
|
||||||
Optional<Account> account = accountAuthenticator.authenticate(credentials);
|
|
||||||
|
|
||||||
if (!account.isPresent()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.put(credentials, account);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (AuthenticationException e) {
|
|
||||||
logger.warn("Auth Failure", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -30,4 +30,11 @@ public class ClientContactTokens {
|
|||||||
public List<String> getContacts() {
|
public List<String> getContacts() {
|
||||||
return contacts;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ClientContactTokens() {}
|
||||||
|
|
||||||
|
public ClientContactTokens(List<String> contacts) {
|
||||||
|
this.contacts = contacts;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,23 +41,26 @@ public class EncryptedOutgoingMessage {
|
|||||||
private static final int MAC_KEY_SIZE = 20;
|
private static final int MAC_KEY_SIZE = 20;
|
||||||
private static final int MAC_SIZE = 10;
|
private static final int MAC_SIZE = 10;
|
||||||
|
|
||||||
private final OutgoingMessageSignal outgoingMessage;
|
private final String serialized;
|
||||||
private final String signalingKey;
|
|
||||||
|
|
||||||
public EncryptedOutgoingMessage(OutgoingMessageSignal outgoingMessage,
|
public EncryptedOutgoingMessage(OutgoingMessageSignal outgoingMessage,
|
||||||
String signalingKey)
|
String signalingKey)
|
||||||
|
throws CryptoEncodingException
|
||||||
{
|
{
|
||||||
this.outgoingMessage = outgoingMessage;
|
|
||||||
this.signalingKey = signalingKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String serialize() throws CryptoEncodingException {
|
|
||||||
byte[] plaintext = outgoingMessage.toByteArray();
|
byte[] plaintext = outgoingMessage.toByteArray();
|
||||||
SecretKeySpec cipherKey = getCipherKey (signalingKey);
|
SecretKeySpec cipherKey = getCipherKey (signalingKey);
|
||||||
SecretKeySpec macKey = getMacKey(signalingKey);
|
SecretKeySpec macKey = getMacKey(signalingKey);
|
||||||
byte[] ciphertext = getCiphertext(plaintext, cipherKey, macKey);
|
byte[] ciphertext = getCiphertext(plaintext, cipherKey, macKey);
|
||||||
|
|
||||||
return Base64.encodeBytes(ciphertext);
|
this.serialized = Base64.encodeBytes(ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EncryptedOutgoingMessage(String serialized) {
|
||||||
|
this.serialized = serialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String serialize() {
|
||||||
|
return serialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getCiphertext(byte[] plaintext, SecretKeySpec cipherKey, SecretKeySpec macKey)
|
private byte[] getCiphertext(byte[] plaintext, SecretKeySpec cipherKey, SecretKeySpec macKey)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ package org.whispersystems.textsecuregcm.entities;
|
|||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.xml.bind.annotation.XmlTransient;
|
import javax.xml.bind.annotation.XmlTransient;
|
||||||
@@ -58,8 +59,19 @@ public class PreKey {
|
|||||||
public PreKey() {}
|
public PreKey() {}
|
||||||
|
|
||||||
public PreKey(long id, String number, long deviceId, long keyId,
|
public PreKey(long id, String number, long deviceId, long keyId,
|
||||||
String publicKey, String identityKey,
|
String publicKey, boolean lastResort)
|
||||||
boolean lastResort)
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.number = number;
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
this.keyId = keyId;
|
||||||
|
this.publicKey = publicKey;
|
||||||
|
this.lastResort = lastResort;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public PreKey(long id, String number, long deviceId, long keyId,
|
||||||
|
String publicKey, String identityKey, boolean lastResort)
|
||||||
{
|
{
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.number = number;
|
this.number = number;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.whispersystems.textsecuregcm.entities;
|
package org.whispersystems.textsecuregcm.entities;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import org.hibernate.validator.constraints.NotEmpty;
|
import org.hibernate.validator.constraints.NotEmpty;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
@@ -27,6 +28,7 @@ public class PreKeyList {
|
|||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Valid
|
||||||
private PreKey lastResortKey;
|
private PreKey lastResortKey;
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
@@ -38,7 +40,17 @@ public class PreKeyList {
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public void setKeys(List<PreKey> keys) {
|
||||||
|
this.keys = keys;
|
||||||
|
}
|
||||||
|
|
||||||
public PreKey getLastResortKey() {
|
public PreKey getLastResortKey() {
|
||||||
return lastResortKey;
|
return lastResortKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public void setLastResortKey(PreKey lastResortKey) {
|
||||||
|
this.lastResortKey = lastResortKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.limits;
|
package org.whispersystems.textsecuregcm.limits;
|
||||||
|
|
||||||
import com.yammer.metrics.Metrics;
|
import com.codahale.metrics.Meter;
|
||||||
import com.yammer.metrics.core.Meter;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import net.spy.memcached.MemcachedClient;
|
import net.spy.memcached.MemcachedClient;
|
||||||
|
|
||||||
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
|
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
public class RateLimiter {
|
public class RateLimiter {
|
||||||
|
|
||||||
@@ -35,7 +36,9 @@ public class RateLimiter {
|
|||||||
public RateLimiter(MemcachedClient memcachedClient, String name,
|
public RateLimiter(MemcachedClient memcachedClient, String name,
|
||||||
int bucketSize, double leakRatePerMinute)
|
int bucketSize, double leakRatePerMinute)
|
||||||
{
|
{
|
||||||
this.meter = Metrics.newMeter(RateLimiter.class, name, "exceeded", TimeUnit.MINUTES);
|
MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
|
|
||||||
|
this.meter = metricRegistry.meter(name(getClass(), name, "exceeded"));
|
||||||
this.memcachedClient = memcachedClient;
|
this.memcachedClient = memcachedClient;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.bucketSize = bucketSize;
|
this.bucketSize = bucketSize;
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Gauge;
|
||||||
|
import com.sun.management.OperatingSystemMXBean;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
|
||||||
|
public class CpuUsageGauge implements Gauge<Integer> {
|
||||||
|
@Override
|
||||||
|
public Integer getValue() {
|
||||||
|
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
|
||||||
|
ManagementFactory.getOperatingSystemMXBean();
|
||||||
|
|
||||||
|
return (int) Math.ceil(mbean.getSystemCpuLoad() * 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Gauge;
|
||||||
|
import com.sun.management.OperatingSystemMXBean;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
|
||||||
|
public class FreeMemoryGauge implements Gauge<Long> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long getValue() {
|
||||||
|
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
|
||||||
|
ManagementFactory.getOperatingSystemMXBean();
|
||||||
|
|
||||||
|
return mbean.getFreePhysicalMemorySize();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Counter;
|
||||||
|
import com.codahale.metrics.Gauge;
|
||||||
|
import com.codahale.metrics.Histogram;
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.Metered;
|
||||||
|
import com.codahale.metrics.MetricFilter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.ScheduledReporter;
|
||||||
|
import com.codahale.metrics.Snapshot;
|
||||||
|
import com.codahale.metrics.Timer;
|
||||||
|
import com.fasterxml.jackson.core.JsonEncoding;
|
||||||
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapted from MetricsServlet.
|
||||||
|
*/
|
||||||
|
public class JsonMetricsReporter extends ScheduledReporter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(JsonMetricsReporter.class);
|
||||||
|
private final JsonFactory factory = new JsonFactory();
|
||||||
|
|
||||||
|
private final String table;
|
||||||
|
private final String sunnylabsHost;
|
||||||
|
private final String host;
|
||||||
|
|
||||||
|
public JsonMetricsReporter(MetricRegistry registry, String token, String sunnylabsHost)
|
||||||
|
throws UnknownHostException
|
||||||
|
{
|
||||||
|
super(registry, "jsonmetrics-reporter", MetricFilter.ALL, TimeUnit.SECONDS, TimeUnit.MILLISECONDS);
|
||||||
|
this.table = token;
|
||||||
|
this.sunnylabsHost = sunnylabsHost;
|
||||||
|
this.host = InetAddress.getLocalHost().getHostName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void report(SortedMap<String, Gauge> stringGaugeSortedMap,
|
||||||
|
SortedMap<String, Counter> stringCounterSortedMap,
|
||||||
|
SortedMap<String, Histogram> stringHistogramSortedMap,
|
||||||
|
SortedMap<String, Meter> stringMeterSortedMap,
|
||||||
|
SortedMap<String, Timer> stringTimerSortedMap)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
logger.info("Reporting metrics...");
|
||||||
|
URL url = new URL("https", sunnylabsHost, 443, "/report/metrics?t=" + table + "&h=" + host);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.addRequestProperty("Content-Type", "application/json");
|
||||||
|
|
||||||
|
OutputStream outputStream = connection.getOutputStream();
|
||||||
|
JsonGenerator json = factory.createGenerator(outputStream, JsonEncoding.UTF8);
|
||||||
|
|
||||||
|
json.writeStartObject();
|
||||||
|
|
||||||
|
for (Map.Entry<String, Gauge> gauge : stringGaugeSortedMap.entrySet()) {
|
||||||
|
reportGauge(json, gauge.getKey(), gauge.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String, Counter> counter : stringCounterSortedMap.entrySet()) {
|
||||||
|
reportCounter(json, counter.getKey(), counter.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String, Histogram> histogram : stringHistogramSortedMap.entrySet()) {
|
||||||
|
reportHistogram(json, histogram.getKey(), histogram.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String, Meter> meter : stringMeterSortedMap.entrySet()) {
|
||||||
|
reportMeter(json, meter.getKey(), meter.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<String, Timer> timer : stringTimerSortedMap.entrySet()) {
|
||||||
|
reportTimer(json, timer.getKey(), timer.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
json.writeEndObject();
|
||||||
|
json.close();
|
||||||
|
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
logger.info("Metrics server response: " + connection.getResponseCode());
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("Error sending metrics", e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportGauge(JsonGenerator json, String name, Gauge gauge) throws IOException {
|
||||||
|
Object gaugeValue = evaluateGauge(gauge);
|
||||||
|
|
||||||
|
if (gaugeValue instanceof Number) {
|
||||||
|
json.writeFieldName(sanitize(name));
|
||||||
|
json.writeObject(gaugeValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportCounter(JsonGenerator json, String name, Counter counter) throws IOException {
|
||||||
|
json.writeFieldName(sanitize(name));
|
||||||
|
json.writeNumber(counter.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportHistogram(JsonGenerator json, String name, Histogram histogram) throws IOException {
|
||||||
|
Snapshot snapshot = histogram.getSnapshot();
|
||||||
|
json.writeFieldName(sanitize(name));
|
||||||
|
json.writeStartObject();
|
||||||
|
json.writeNumberField("count", histogram.getCount());
|
||||||
|
writeSnapshot(json, snapshot);
|
||||||
|
json.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportMeter(JsonGenerator json, String name, Meter meter) throws IOException {
|
||||||
|
json.writeFieldName(sanitize(name));
|
||||||
|
json.writeStartObject();
|
||||||
|
writeMetered(json, meter);
|
||||||
|
json.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportTimer(JsonGenerator json, String name, Timer timer) throws IOException {
|
||||||
|
json.writeFieldName(sanitize(name));
|
||||||
|
json.writeStartObject();
|
||||||
|
json.writeFieldName("rate");
|
||||||
|
json.writeStartObject();
|
||||||
|
writeMetered(json, timer);
|
||||||
|
json.writeEndObject();
|
||||||
|
json.writeFieldName("duration");
|
||||||
|
json.writeStartObject();
|
||||||
|
writeSnapshot(json, timer.getSnapshot());
|
||||||
|
json.writeEndObject();
|
||||||
|
json.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object evaluateGauge(Gauge gauge) {
|
||||||
|
try {
|
||||||
|
return gauge.getValue();
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
logger.warn("Error reading gauge", e);
|
||||||
|
return "error reading gauge";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeSnapshot(JsonGenerator json, Snapshot snapshot) throws IOException {
|
||||||
|
json.writeNumberField("max", convertDuration(snapshot.getMax()));
|
||||||
|
json.writeNumberField("mean", convertDuration(snapshot.getMean()));
|
||||||
|
json.writeNumberField("min", convertDuration(snapshot.getMin()));
|
||||||
|
json.writeNumberField("stddev", convertDuration(snapshot.getStdDev()));
|
||||||
|
json.writeNumberField("median", convertDuration(snapshot.getMedian()));
|
||||||
|
json.writeNumberField("p75", convertDuration(snapshot.get75thPercentile()));
|
||||||
|
json.writeNumberField("p95", convertDuration(snapshot.get95thPercentile()));
|
||||||
|
json.writeNumberField("p98", convertDuration(snapshot.get98thPercentile()));
|
||||||
|
json.writeNumberField("p99", convertDuration(snapshot.get99thPercentile()));
|
||||||
|
json.writeNumberField("p999", convertDuration(snapshot.get999thPercentile()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeMetered(JsonGenerator json, Metered meter) throws IOException {
|
||||||
|
json.writeNumberField("count", convertRate(meter.getCount()));
|
||||||
|
json.writeNumberField("mean", convertRate(meter.getMeanRate()));
|
||||||
|
json.writeNumberField("m1", convertRate(meter.getOneMinuteRate()));
|
||||||
|
json.writeNumberField("m5", convertRate(meter.getFiveMinuteRate()));
|
||||||
|
json.writeNumberField("m15", convertRate(meter.getFifteenMinuteRate()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Pattern SIMPLE_NAMES = Pattern.compile("[^a-zA-Z0-9_.\\-~]");
|
||||||
|
|
||||||
|
private String sanitize(String metricName) {
|
||||||
|
return SIMPLE_NAMES.matcher(metricName).replaceAll("_");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
|
||||||
|
import com.codahale.metrics.Gauge;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Pair;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class NetworkGauge implements Gauge<Long> {
|
||||||
|
|
||||||
|
protected Pair<Long, Long> getSentReceived() throws IOException {
|
||||||
|
File proc = new File("/proc/net/dev");
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(proc));
|
||||||
|
String header = reader.readLine();
|
||||||
|
String header2 = reader.readLine();
|
||||||
|
|
||||||
|
long bytesSent = 0;
|
||||||
|
long bytesReceived = 0;
|
||||||
|
|
||||||
|
String interfaceStats;
|
||||||
|
|
||||||
|
while ((interfaceStats = reader.readLine()) != null) {
|
||||||
|
String[] stats = interfaceStats.split("\\s+");
|
||||||
|
|
||||||
|
if (!stats[1].equals("lo:")) {
|
||||||
|
bytesReceived += Long.parseLong(stats[2]);
|
||||||
|
bytesSent += Long.parseLong(stats[10]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Pair<>(bytesSent, bytesReceived);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Pair;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class NetworkReceivedGauge extends NetworkGauge {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(NetworkSentGauge.class);
|
||||||
|
|
||||||
|
private long lastTimestamp;
|
||||||
|
private long lastReceived;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long getValue() {
|
||||||
|
try {
|
||||||
|
long timestamp = System.currentTimeMillis();
|
||||||
|
Pair<Long, Long> sentAndReceived = getSentReceived();
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
if (lastTimestamp != 0) {
|
||||||
|
result = sentAndReceived.second() - lastReceived;
|
||||||
|
lastReceived = sentAndReceived.second();
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTimestamp = timestamp;
|
||||||
|
return result;
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("NetworkReceivedGauge", e);
|
||||||
|
return -1L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.metrics;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Pair;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class NetworkSentGauge extends NetworkGauge {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(NetworkSentGauge.class);
|
||||||
|
|
||||||
|
private long lastTimestamp;
|
||||||
|
private long lastSent;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long getValue() {
|
||||||
|
try {
|
||||||
|
long timestamp = System.currentTimeMillis();
|
||||||
|
Pair<Long, Long> sentAndReceived = getSentReceived();
|
||||||
|
long result = 0;
|
||||||
|
|
||||||
|
if (lastTimestamp != 0) {
|
||||||
|
result = sentAndReceived.first() - lastSent;
|
||||||
|
lastSent = sentAndReceived.first();
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTimestamp = timestamp;
|
||||||
|
return result;
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("NetworkSentGauge", e);
|
||||||
|
return -1L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,8 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.providers;
|
package org.whispersystems.textsecuregcm.providers;
|
||||||
|
|
||||||
import com.yammer.metrics.core.HealthCheck;
|
import com.codahale.metrics.health.HealthCheck;
|
||||||
import com.yammer.metrics.core.HealthCheck.Result;
|
|
||||||
import net.spy.memcached.MemcachedClient;
|
import net.spy.memcached.MemcachedClient;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
@@ -27,7 +26,6 @@ public class MemcacheHealthCheck extends HealthCheck {
|
|||||||
private final MemcachedClient client;
|
private final MemcachedClient client;
|
||||||
|
|
||||||
public MemcacheHealthCheck(MemcachedClient client) {
|
public MemcacheHealthCheck(MemcachedClient client) {
|
||||||
super("memcached");
|
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.providers;
|
package org.whispersystems.textsecuregcm.providers;
|
||||||
|
|
||||||
import com.yammer.metrics.core.HealthCheck;
|
import com.codahale.metrics.health.HealthCheck;
|
||||||
|
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
@@ -26,7 +26,6 @@ public class RedisHealthCheck extends HealthCheck {
|
|||||||
private final JedisPool clientPool;
|
private final JedisPool clientPool;
|
||||||
|
|
||||||
public RedisHealthCheck(JedisPool clientPool) {
|
public RedisHealthCheck(JedisPool clientPool) {
|
||||||
super("redis");
|
|
||||||
this.clientPool = clientPool;
|
this.clientPool = clientPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,18 +16,25 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.push;
|
package org.whispersystems.textsecuregcm.push;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.notnoop.apns.APNS;
|
import com.notnoop.apns.APNS;
|
||||||
import com.notnoop.apns.ApnsService;
|
import com.notnoop.apns.ApnsService;
|
||||||
import com.notnoop.exceptions.NetworkIOException;
|
import com.notnoop.exceptions.NetworkIOException;
|
||||||
import com.yammer.metrics.Metrics;
|
|
||||||
import com.yammer.metrics.core.Meter;
|
|
||||||
import org.bouncycastle.openssl.PEMReader;
|
import org.bouncycastle.openssl.PEMReader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.entities.CryptoEncodingException;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.PubSubMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
|
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -40,21 +47,31 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.security.cert.Certificate;
|
import java.security.cert.Certificate;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
public class APNSender {
|
public class APNSender {
|
||||||
|
|
||||||
private final Meter success = Metrics.newMeter(APNSender.class, "sent", "success", TimeUnit.MINUTES);
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
private final Meter failure = Metrics.newMeter(APNSender.class, "sent", "failure", TimeUnit.MINUTES);
|
private final Meter websocketMeter = metricRegistry.meter(name(getClass(), "websocket"));
|
||||||
private final Logger logger = LoggerFactory.getLogger(APNSender.class);
|
private final Meter pushMeter = metricRegistry.meter(name(getClass(), "push"));
|
||||||
|
private final Meter failureMeter = metricRegistry.meter(name(getClass(), "failure"));
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(APNSender.class);
|
||||||
|
|
||||||
private static final String MESSAGE_BODY = "m";
|
private static final String MESSAGE_BODY = "m";
|
||||||
|
|
||||||
private final Optional<ApnsService> apnService;
|
private final Optional<ApnsService> apnService;
|
||||||
|
private final PubSubManager pubSubManager;
|
||||||
|
private final StoredMessages storedMessages;
|
||||||
|
|
||||||
public APNSender(String apnCertificate, String apnKey)
|
public APNSender(PubSubManager pubSubManager,
|
||||||
|
StoredMessages storedMessages,
|
||||||
|
String apnCertificate, String apnKey)
|
||||||
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
|
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
|
||||||
{
|
{
|
||||||
|
this.pubSubManager = pubSubManager;
|
||||||
|
this.storedMessages = storedMessages;
|
||||||
|
|
||||||
if (!Util.isEmpty(apnCertificate) && !Util.isEmpty(apnKey)) {
|
if (!Util.isEmpty(apnCertificate) && !Util.isEmpty(apnKey)) {
|
||||||
byte[] keyStore = initializeKeyStore(apnCertificate, apnKey);
|
byte[] keyStore = initializeKeyStore(apnCertificate, apnKey);
|
||||||
this.apnService = Optional.of(APNS.newService()
|
this.apnService = Optional.of(APNS.newService()
|
||||||
@@ -65,31 +82,44 @@ public class APNSender {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(String registrationId, EncryptedOutgoingMessage message)
|
public void sendMessage(Account account, Device device,
|
||||||
|
String registrationId, EncryptedOutgoingMessage message)
|
||||||
throws TransientPushFailureException, NotPushRegisteredException
|
throws TransientPushFailureException, NotPushRegisteredException
|
||||||
|
{
|
||||||
|
if (pubSubManager.publish(new WebsocketAddress(account.getId(), device.getId()),
|
||||||
|
new PubSubMessage(PubSubMessage.TYPE_DELIVER, message.serialize())))
|
||||||
|
{
|
||||||
|
websocketMeter.mark();
|
||||||
|
} else {
|
||||||
|
storedMessages.insert(account.getId(), device.getId(), message.serialize());
|
||||||
|
sendPush(registrationId, message.serialize());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendPush(String registrationId, String message)
|
||||||
|
throws TransientPushFailureException
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (!apnService.isPresent()) {
|
if (!apnService.isPresent()) {
|
||||||
failure.mark();
|
failureMeter.mark();
|
||||||
throw new TransientPushFailureException("APN access not configured!");
|
throw new TransientPushFailureException("APN access not configured!");
|
||||||
}
|
}
|
||||||
|
|
||||||
String payload = APNS.newPayload()
|
String payload = APNS.newPayload()
|
||||||
.alertBody("Message!")
|
.alertBody("Message!")
|
||||||
.customField(MESSAGE_BODY, message.serialize())
|
.customField(MESSAGE_BODY, message)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
logger.debug("APN Payload: " + payload);
|
logger.debug("APN Payload: " + payload);
|
||||||
|
|
||||||
apnService.get().push(registrationId, payload);
|
apnService.get().push(registrationId, payload);
|
||||||
success.mark();
|
pushMeter.mark();
|
||||||
} catch (NetworkIOException nioe) {
|
} catch (NetworkIOException nioe) {
|
||||||
logger.warn("Network Error", nioe);
|
logger.warn("Network Error", nioe);
|
||||||
failure.mark();
|
failureMeter.mark();
|
||||||
throw new TransientPushFailureException(nioe);
|
throw new TransientPushFailureException(nioe);
|
||||||
} catch (CryptoEncodingException e) {
|
|
||||||
throw new NotPushRegisteredException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] initializeKeyStore(String pemCertificate, String pemKey)
|
private static byte[] initializeKeyStore(String pemCertificate, String pemKey)
|
||||||
|
|||||||
@@ -16,22 +16,24 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.push;
|
package org.whispersystems.textsecuregcm.push;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import com.google.android.gcm.server.Constants;
|
import com.google.android.gcm.server.Constants;
|
||||||
import com.google.android.gcm.server.Message;
|
import com.google.android.gcm.server.Message;
|
||||||
import com.google.android.gcm.server.Result;
|
import com.google.android.gcm.server.Result;
|
||||||
import com.google.android.gcm.server.Sender;
|
import com.google.android.gcm.server.Sender;
|
||||||
import com.yammer.metrics.Metrics;
|
|
||||||
import com.yammer.metrics.core.Meter;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.CryptoEncodingException;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
public class GCMSender {
|
public class GCMSender {
|
||||||
|
|
||||||
private final Meter success = Metrics.newMeter(GCMSender.class, "sent", "success", TimeUnit.MINUTES);
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(org.whispersystems.textsecuregcm.util.Constants.METRICS_NAME);
|
||||||
private final Meter failure = Metrics.newMeter(GCMSender.class, "sent", "failure", TimeUnit.MINUTES);
|
private final Meter success = metricRegistry.meter(name(getClass(), "sent", "success"));
|
||||||
|
private final Meter failure = metricRegistry.meter(name(getClass(), "sent", "failure"));
|
||||||
|
|
||||||
private final Sender sender;
|
private final Sender sender;
|
||||||
|
|
||||||
@@ -62,8 +64,6 @@ public class GCMSender {
|
|||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new TransientPushFailureException(e);
|
throw new TransientPushFailureException(e);
|
||||||
} catch (CryptoEncodingException e) {
|
|
||||||
throw new NotPushRegisteredException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
|||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.KeyStoreException;
|
import java.security.KeyStoreException;
|
||||||
@@ -37,32 +38,45 @@ public class PushSender {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(PushSender.class);
|
private final Logger logger = LoggerFactory.getLogger(PushSender.class);
|
||||||
|
|
||||||
private final AccountsManager accounts;
|
private final AccountsManager accounts;
|
||||||
private final GCMSender gcmSender;
|
private final GCMSender gcmSender;
|
||||||
private final APNSender apnSender;
|
private final APNSender apnSender;
|
||||||
private final StoredMessageManager storedMessageManager;
|
private final WebsocketSender webSocketSender;
|
||||||
|
|
||||||
public PushSender(GcmConfiguration gcmConfiguration,
|
public PushSender(GcmConfiguration gcmConfiguration,
|
||||||
ApnConfiguration apnConfiguration,
|
ApnConfiguration apnConfiguration,
|
||||||
StoredMessageManager storedMessageManager,
|
StoredMessages storedMessages,
|
||||||
AccountsManager accounts)
|
PubSubManager pubSubManager,
|
||||||
|
AccountsManager accounts)
|
||||||
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
|
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
|
||||||
{
|
{
|
||||||
this.accounts = accounts;
|
this.accounts = accounts;
|
||||||
this.storedMessageManager = storedMessageManager;
|
this.webSocketSender = new WebsocketSender(storedMessages, pubSubManager);
|
||||||
this.gcmSender = new GCMSender(gcmConfiguration.getApiKey());
|
this.gcmSender = new GCMSender(gcmConfiguration.getApiKey());
|
||||||
this.apnSender = new APNSender(apnConfiguration.getCertificate(), apnConfiguration.getKey());
|
this.apnSender = new APNSender(pubSubManager, storedMessages,
|
||||||
|
apnConfiguration.getCertificate(),
|
||||||
|
apnConfiguration.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(Account account, Device device, MessageProtos.OutgoingMessageSignal outgoingMessage)
|
public void sendMessage(Account account, Device device, MessageProtos.OutgoingMessageSignal message)
|
||||||
throws NotPushRegisteredException, TransientPushFailureException
|
throws NotPushRegisteredException, TransientPushFailureException
|
||||||
{
|
{
|
||||||
String signalingKey = device.getSignalingKey();
|
try {
|
||||||
EncryptedOutgoingMessage message = new EncryptedOutgoingMessage(outgoingMessage, signalingKey);
|
String signalingKey = device.getSignalingKey();
|
||||||
|
EncryptedOutgoingMessage encryptedMessage = new EncryptedOutgoingMessage(message, signalingKey);
|
||||||
|
|
||||||
|
sendMessage(account, device, encryptedMessage);
|
||||||
|
} catch (CryptoEncodingException e) {
|
||||||
|
throw new NotPushRegisteredException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(Account account, Device device, EncryptedOutgoingMessage message)
|
||||||
|
throws NotPushRegisteredException, TransientPushFailureException
|
||||||
|
{
|
||||||
if (device.getGcmId() != null) sendGcmMessage(account, device, message);
|
if (device.getGcmId() != null) sendGcmMessage(account, device, message);
|
||||||
else if (device.getApnId() != null) sendApnMessage(account, device, message);
|
else if (device.getApnId() != null) sendApnMessage(account, device, message);
|
||||||
else if (device.getFetchesMessages()) storeFetchedMessage(account, device, message);
|
else if (device.getFetchesMessages()) sendWebSocketMessage(account, device, message);
|
||||||
else throw new NotPushRegisteredException("No delivery possible!");
|
else throw new NotPushRegisteredException("No delivery possible!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +103,7 @@ public class PushSender {
|
|||||||
throws TransientPushFailureException, NotPushRegisteredException
|
throws TransientPushFailureException, NotPushRegisteredException
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
apnSender.sendMessage(device.getApnId(), outgoingMessage);
|
apnSender.sendMessage(account, device, device.getApnId(), outgoingMessage);
|
||||||
} catch (NotPushRegisteredException e) {
|
} catch (NotPushRegisteredException e) {
|
||||||
device.setApnId(null);
|
device.setApnId(null);
|
||||||
accounts.update(account);
|
accounts.update(account);
|
||||||
@@ -97,11 +111,11 @@ public class PushSender {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeFetchedMessage(Account account, Device device, EncryptedOutgoingMessage outgoingMessage)
|
private void sendWebSocketMessage(Account account, Device device, EncryptedOutgoingMessage outgoingMessage)
|
||||||
throws NotPushRegisteredException
|
throws NotPushRegisteredException
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
storedMessageManager.storeMessage(account, device, outgoingMessage);
|
webSocketSender.sendMessage(account, device, outgoingMessage);
|
||||||
} catch (CryptoEncodingException e) {
|
} catch (CryptoEncodingException e) {
|
||||||
throw new NotPushRegisteredException(e);
|
throw new NotPushRegisteredException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2014 Open WhisperSystems
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package org.whispersystems.textsecuregcm.push;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.CryptoEncodingException;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.PubSubMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
|
public class WebsocketSender {
|
||||||
|
|
||||||
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
|
private final Meter onlineMeter = metricRegistry.meter(name(getClass(), "online"));
|
||||||
|
private final Meter offlineMeter = metricRegistry.meter(name(getClass(), "offline"));
|
||||||
|
|
||||||
|
private final StoredMessages storedMessages;
|
||||||
|
private final PubSubManager pubSubManager;
|
||||||
|
|
||||||
|
public WebsocketSender(StoredMessages storedMessages, PubSubManager pubSubManager) {
|
||||||
|
this.storedMessages = storedMessages;
|
||||||
|
this.pubSubManager = pubSubManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(Account account, Device device, EncryptedOutgoingMessage outgoingMessage)
|
||||||
|
throws CryptoEncodingException
|
||||||
|
{
|
||||||
|
sendMessage(account, device, outgoingMessage.serialize());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(Account account, Device device, String serializedMessage) {
|
||||||
|
WebsocketAddress address = new WebsocketAddress(account.getId(), device.getId());
|
||||||
|
PubSubMessage pubSubMessage = new PubSubMessage(PubSubMessage.TYPE_DELIVER, serializedMessage);
|
||||||
|
|
||||||
|
if (pubSubManager.publish(address, pubSubMessage)) {
|
||||||
|
onlineMeter.mark();
|
||||||
|
} else {
|
||||||
|
offlineMeter.mark();
|
||||||
|
storedMessages.insert(account.getId(), device.getId(), serializedMessage);
|
||||||
|
pubSubManager.publish(address, new PubSubMessage(PubSubMessage.TYPE_QUERY_DB, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,11 +16,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.sms;
|
package org.whispersystems.textsecuregcm.sms;
|
||||||
|
|
||||||
import com.yammer.metrics.Metrics;
|
import com.codahale.metrics.Meter;
|
||||||
import com.yammer.metrics.core.Meter;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -28,13 +30,15 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
public class NexmoSmsSender {
|
public class NexmoSmsSender {
|
||||||
|
|
||||||
private final Meter smsMeter = Metrics.newMeter(NexmoSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
private final Meter voxMeter = Metrics.newMeter(NexmoSmsSender.class, "vox", "delivered", TimeUnit.MINUTES);
|
private final Meter smsMeter = metricRegistry.meter(name(getClass(), "sms", "delivered"));
|
||||||
private final Logger logger = LoggerFactory.getLogger(NexmoSmsSender.class);
|
private final Meter voxMeter = metricRegistry.meter(name(getClass(), "vox", "delivered"));
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(NexmoSmsSender.class);
|
||||||
|
|
||||||
private static final String NEXMO_SMS_URL =
|
private static final String NEXMO_SMS_URL =
|
||||||
"https://rest.nexmo.com/sms/json?api_key=%s&api_secret=%s&from=%s&to=%s&text=%s";
|
"https://rest.nexmo.com/sms/json?api_key=%s&api_secret=%s&from=%s&to=%s&text=%s";
|
||||||
|
|||||||
@@ -16,22 +16,25 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.sms;
|
package org.whispersystems.textsecuregcm.sms;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import com.twilio.sdk.TwilioRestClient;
|
import com.twilio.sdk.TwilioRestClient;
|
||||||
import com.twilio.sdk.TwilioRestException;
|
import com.twilio.sdk.TwilioRestException;
|
||||||
import com.twilio.sdk.resource.factory.CallFactory;
|
import com.twilio.sdk.resource.factory.CallFactory;
|
||||||
import com.twilio.sdk.resource.factory.MessageFactory;
|
import com.twilio.sdk.resource.factory.MessageFactory;
|
||||||
import com.yammer.metrics.Metrics;
|
|
||||||
import com.yammer.metrics.core.Meter;
|
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
|
||||||
public class TwilioSmsSender {
|
public class TwilioSmsSender {
|
||||||
|
|
||||||
@@ -40,8 +43,9 @@ public class TwilioSmsSender {
|
|||||||
" <Say voice=\"woman\" language=\"en\">" + SmsSender.VOX_VERIFICATION_TEXT + "%s</Say>\n" +
|
" <Say voice=\"woman\" language=\"en\">" + SmsSender.VOX_VERIFICATION_TEXT + "%s</Say>\n" +
|
||||||
"</Response>";
|
"</Response>";
|
||||||
|
|
||||||
private final Meter smsMeter = Metrics.newMeter(TwilioSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
private final Meter voxMeter = Metrics.newMeter(TwilioSmsSender.class, "vox", "delivered", TimeUnit.MINUTES);
|
private final Meter smsMeter = metricRegistry.meter(name(getClass(), "sms", "delivered"));
|
||||||
|
private final Meter voxMeter = metricRegistry.meter(name(getClass(), "vox", "delivered"));
|
||||||
|
|
||||||
private final String accountId;
|
private final String accountId;
|
||||||
private final String accountToken;
|
private final String accountToken;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class Account implements Serializable {
|
public class Account implements Serializable {
|
||||||
|
|
||||||
public static final int MEMCACHE_VERION = 2;
|
public static final int MEMCACHE_VERION = 3;
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private long id;
|
private long id;
|
||||||
@@ -42,16 +42,14 @@ public class Account implements Serializable {
|
|||||||
@JsonProperty
|
@JsonProperty
|
||||||
private List<Device> devices = new LinkedList<>();
|
private List<Device> devices = new LinkedList<>();
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private String identityKey;
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private Optional<Device> authenticatedDevice;
|
private Optional<Device> authenticatedDevice;
|
||||||
|
|
||||||
public Account() {}
|
public Account() {}
|
||||||
|
|
||||||
public Account(String number, boolean supportsSms) {
|
|
||||||
this.number = number;
|
|
||||||
this.supportsSms = supportsSms;
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public Account(String number, boolean supportsSms, List<Device> devices) {
|
public Account(String number, boolean supportsSms, List<Device> devices) {
|
||||||
this.number = number;
|
this.number = number;
|
||||||
@@ -142,4 +140,12 @@ public class Account implements Serializable {
|
|||||||
public Optional<String> getRelay() {
|
public Optional<String> getRelay() {
|
||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setIdentityKey(String identityKey) {
|
||||||
|
this.identityKey = identityKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentityKey() {
|
||||||
|
return identityKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ public class DirectoryManager {
|
|||||||
pipeline.hset(DIRECTORY_KEY, contact.getToken(), new Gson().toJson(tokenValue).getBytes());
|
pipeline.hset(DIRECTORY_KEY, contact.getToken(), new Gson().toJson(tokenValue).getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PendingClientContact get(BatchOperationHandle handle, byte[] token) {
|
||||||
|
Pipeline pipeline = handle.pipeline;
|
||||||
|
return new PendingClientContact(token, pipeline.hget(DIRECTORY_KEY, token));
|
||||||
|
}
|
||||||
|
|
||||||
public Optional<ClientContact> get(byte[] token) {
|
public Optional<ClientContact> get(byte[] token) {
|
||||||
Jedis jedis = redisPool.getResource();
|
Jedis jedis = redisPool.getResource();
|
||||||
|
|
||||||
@@ -162,4 +167,26 @@ public class DirectoryManager {
|
|||||||
this.supportsSms = supportsSms;
|
this.supportsSms = supportsSms;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class PendingClientContact {
|
||||||
|
private final byte[] token;
|
||||||
|
private final Response<byte[]> response;
|
||||||
|
|
||||||
|
PendingClientContact(byte[] token, Response<byte[]> response) {
|
||||||
|
this.token = token;
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<ClientContact> get() {
|
||||||
|
byte[] result = response.get();
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
return Optional.absent();
|
||||||
|
}
|
||||||
|
|
||||||
|
TokenValue tokenValue = new Gson().fromJson(new String(result), TokenValue.class);
|
||||||
|
return Optional.of(new ClientContact(token, tokenValue.relay, tokenValue.supportsSms));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,12 +50,12 @@ public abstract class Keys {
|
|||||||
@SqlUpdate("DELETE FROM keys WHERE id = :id")
|
@SqlUpdate("DELETE FROM keys WHERE id = :id")
|
||||||
abstract void removeKey(@Bind("id") long id);
|
abstract void removeKey(@Bind("id") long id);
|
||||||
|
|
||||||
@SqlBatch("INSERT INTO keys (number, device_id, key_id, public_key, identity_key, last_resort) VALUES " +
|
@SqlBatch("INSERT INTO keys (number, device_id, key_id, public_key, last_resort) VALUES " +
|
||||||
"(:number, :device_id, :key_id, :public_key, :identity_key, :last_resort)")
|
"(:number, :device_id, :key_id, :public_key, :last_resort)")
|
||||||
abstract void append(@PreKeyBinder List<PreKey> preKeys);
|
abstract void append(@PreKeyBinder List<PreKey> preKeys);
|
||||||
|
|
||||||
@SqlUpdate("INSERT INTO keys (number, device_id, key_id, public_key, identity_key, last_resort) VALUES " +
|
@SqlUpdate("INSERT INTO keys (number, device_id, key_id, public_key, last_resort) VALUES " +
|
||||||
"(:number, :device_id, :key_id, :public_key, :identity_key, :last_resort)")
|
"(:number, :device_id, :key_id, :public_key, :last_resort)")
|
||||||
abstract void append(@PreKeyBinder PreKey preKey);
|
abstract void append(@PreKeyBinder PreKey preKey);
|
||||||
|
|
||||||
@SqlQuery("SELECT * FROM keys WHERE number = :number AND device_id = :device_id ORDER BY key_id ASC FOR UPDATE")
|
@SqlQuery("SELECT * FROM keys WHERE number = :number AND device_id = :device_id ORDER BY key_id ASC FOR UPDATE")
|
||||||
@@ -129,7 +129,6 @@ public abstract class Keys {
|
|||||||
sql.bind("device_id", preKey.getDeviceId());
|
sql.bind("device_id", preKey.getDeviceId());
|
||||||
sql.bind("key_id", preKey.getKeyId());
|
sql.bind("key_id", preKey.getKeyId());
|
||||||
sql.bind("public_key", preKey.getPublicKey());
|
sql.bind("public_key", preKey.getPublicKey());
|
||||||
sql.bind("identity_key", preKey.getIdentityKey());
|
|
||||||
sql.bind("last_resort", preKey.isLastResort() ? 1 : 0);
|
sql.bind("last_resort", preKey.isLastResort() ? 1 : 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -145,7 +144,6 @@ public abstract class Keys {
|
|||||||
{
|
{
|
||||||
return new PreKey(resultSet.getLong("id"), resultSet.getString("number"), resultSet.getLong("device_id"),
|
return new PreKey(resultSet.getLong("id"), resultSet.getString("number"), resultSet.getLong("device_id"),
|
||||||
resultSet.getLong("key_id"), resultSet.getString("public_key"),
|
resultSet.getLong("key_id"), resultSet.getString("public_key"),
|
||||||
resultSet.getString("identity_key"),
|
|
||||||
resultSet.getInt("last_resort") == 1);
|
resultSet.getInt("last_resort") == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ public class PubSubManager {
|
|||||||
public void onSubscribe(String channel, int count) {
|
public void onSubscribe(String channel, int count) {
|
||||||
try {
|
try {
|
||||||
WebsocketAddress address = new WebsocketAddress(channel);
|
WebsocketAddress address = new WebsocketAddress(channel);
|
||||||
|
|
||||||
if (address.getAccountId() == 0 && address.getDeviceId() == 0) {
|
if (address.getAccountId() == 0 && address.getDeviceId() == 0) {
|
||||||
synchronized (PubSubManager.this) {
|
synchronized (PubSubManager.this) {
|
||||||
subscribed = true;
|
subscribed = true;
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (C) 2014 Open WhisperSystems
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package org.whispersystems.textsecuregcm.storage;
|
|
||||||
|
|
||||||
import org.whispersystems.textsecuregcm.entities.CryptoEncodingException;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
|
||||||
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class StoredMessageManager {
|
|
||||||
|
|
||||||
private final StoredMessages storedMessages;
|
|
||||||
private final PubSubManager pubSubManager;
|
|
||||||
|
|
||||||
public StoredMessageManager(StoredMessages storedMessages, PubSubManager pubSubManager) {
|
|
||||||
this.storedMessages = storedMessages;
|
|
||||||
this.pubSubManager = pubSubManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void storeMessage(Account account, Device device, EncryptedOutgoingMessage outgoingMessage)
|
|
||||||
throws CryptoEncodingException
|
|
||||||
{
|
|
||||||
storeMessage(account, device, outgoingMessage.serialize());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void storeMessages(Account account, Device device, List<String> serializedMessages) {
|
|
||||||
for (String serializedMessage : serializedMessages) {
|
|
||||||
storeMessage(account, device, serializedMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void storeMessage(Account account, Device device, String serializedMessage) {
|
|
||||||
if (device.getFetchesMessages()) {
|
|
||||||
WebsocketAddress address = new WebsocketAddress(account.getId(), device.getId());
|
|
||||||
PubSubMessage pubSubMessage = new PubSubMessage(PubSubMessage.TYPE_DELIVER, serializedMessage);
|
|
||||||
|
|
||||||
if (!pubSubManager.publish(address, pubSubMessage)) {
|
|
||||||
storedMessages.insert(account.getId(), device.getId(), serializedMessage);
|
|
||||||
pubSubManager.publish(address, new PubSubMessage(PubSubMessage.TYPE_QUERY_DB, null));
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
storedMessages.insert(account.getId(), device.getId(), serializedMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getOutgoingMessages(Account account, Device device) {
|
|
||||||
return storedMessages.getMessagesForDevice(account.getId(), device.getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,21 +16,71 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.storage;
|
package org.whispersystems.textsecuregcm.storage;
|
||||||
|
|
||||||
import org.skife.jdbi.v2.sqlobject.Bind;
|
import com.codahale.metrics.Histogram;
|
||||||
import org.skife.jdbi.v2.sqlobject.SqlBatch;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
import org.skife.jdbi.v2.sqlobject.SqlQuery;
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
|
|
||||||
|
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface StoredMessages {
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
@SqlUpdate("INSERT INTO messages (account_id, device_id, encrypted_message) VALUES (:account_id, :device_id, :encrypted_message)")
|
public class StoredMessages {
|
||||||
void insert(@Bind("account_id") long accountId, @Bind("device_id") long deviceId, @Bind("encrypted_message") String encryptedOutgoingMessage);
|
|
||||||
|
|
||||||
@SqlBatch("INSERT INTO messages (account_id, device_id, encrypted_message) VALUES (:account_id, :device_id, :encrypted_message)")
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
void insert(@Bind("account_id") long accountId, @Bind("device_id") long deviceId, @Bind("encrypted_message") List<String> encryptedOutgoingMessages);
|
private final Histogram queueSizeHistogram = metricRegistry.histogram(name(getClass(), "queue_size"));
|
||||||
|
|
||||||
@SqlQuery("DELETE FROM messages WHERE account_id = :account_id AND device_id = :device_id RETURNING encrypted_message")
|
private static final String QUEUE_PREFIX = "msgs";
|
||||||
List<String> getMessagesForDevice(@Bind("account_id") long accountId, @Bind("device_id") long deviceId);
|
|
||||||
}
|
private final JedisPool jedisPool;
|
||||||
|
|
||||||
|
public StoredMessages(JedisPool jedisPool) {
|
||||||
|
this.jedisPool = jedisPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(long accountId, long deviceId, String message) {
|
||||||
|
Jedis jedis = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jedis = jedisPool.getResource();
|
||||||
|
|
||||||
|
long queueSize = jedis.lpush(getKey(accountId, deviceId), message);
|
||||||
|
queueSizeHistogram.update(queueSize);
|
||||||
|
|
||||||
|
if (queueSize > 1000) {
|
||||||
|
jedis.ltrim(getKey(accountId, deviceId), 0, 999);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (jedis != null)
|
||||||
|
jedisPool.returnResource(jedis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getMessagesForDevice(long accountId, long deviceId) {
|
||||||
|
List<String> messages = new LinkedList<>();
|
||||||
|
Jedis jedis = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jedis = jedisPool.getResource();
|
||||||
|
String message;
|
||||||
|
|
||||||
|
while ((message = jedis.rpop(QUEUE_PREFIX + accountId + ":" + deviceId)) != null) {
|
||||||
|
messages.add(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
} finally {
|
||||||
|
if (jedis != null)
|
||||||
|
jedisPool.returnResource(jedis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getKey(long accountId, long deviceId) {
|
||||||
|
return QUEUE_PREFIX + ":" + accountId + ":" + deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1241,7 +1241,7 @@ public class Base64
|
|||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public static byte[] decode( String s ) throws java.io.IOException {
|
public static byte[] decode( String s ) throws java.io.IOException {
|
||||||
return decode( s, NO_OPTIONS );
|
return decode( s, DONT_GUNZIP );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.util;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
|
||||||
|
public static final String METRICS_NAME = "textsecure";
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.websocket;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeResponse;
|
||||||
|
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
|
||||||
|
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
||||||
|
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||||
|
import org.whispersystems.textsecuregcm.controllers.WebsocketController;
|
||||||
|
import org.whispersystems.textsecuregcm.push.PushSender;
|
||||||
|
import org.whispersystems.textsecuregcm.push.WebsocketSender;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
|
|
||||||
|
|
||||||
|
public class WebsocketControllerFactory extends WebSocketServlet implements WebSocketCreator {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(WebsocketControllerFactory.class);
|
||||||
|
|
||||||
|
private final PushSender pushSender;
|
||||||
|
private final StoredMessages storedMessages;
|
||||||
|
private final PubSubManager pubSubManager;
|
||||||
|
private final AccountAuthenticator accountAuthenticator;
|
||||||
|
|
||||||
|
public WebsocketControllerFactory(AccountAuthenticator accountAuthenticator,
|
||||||
|
PushSender pushSender,
|
||||||
|
StoredMessages storedMessages,
|
||||||
|
PubSubManager pubSubManager)
|
||||||
|
{
|
||||||
|
this.accountAuthenticator = accountAuthenticator;
|
||||||
|
this.pushSender = pushSender;
|
||||||
|
this.storedMessages = storedMessages;
|
||||||
|
this.pubSubManager = pubSubManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(WebSocketServletFactory factory) {
|
||||||
|
factory.setCreator(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object createWebSocket(UpgradeRequest upgradeRequest, UpgradeResponse upgradeResponse) {
|
||||||
|
return new WebsocketController(accountAuthenticator, pushSender, pubSubManager, storedMessages);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,13 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.workers;
|
package org.whispersystems.textsecuregcm.workers;
|
||||||
|
|
||||||
import com.yammer.dropwizard.cli.ConfiguredCommand;
|
|
||||||
import com.yammer.dropwizard.config.Bootstrap;
|
|
||||||
import com.yammer.dropwizard.db.DatabaseConfiguration;
|
|
||||||
import com.yammer.dropwizard.jdbi.ImmutableListContainerFactory;
|
|
||||||
import com.yammer.dropwizard.jdbi.ImmutableSetContainerFactory;
|
|
||||||
import com.yammer.dropwizard.jdbi.OptionalContainerFactory;
|
|
||||||
import com.yammer.dropwizard.jdbi.args.OptionalArgumentFactory;
|
|
||||||
import net.sourceforge.argparse4j.inf.Namespace;
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
import net.spy.memcached.MemcachedClient;
|
import net.spy.memcached.MemcachedClient;
|
||||||
import org.skife.jdbi.v2.DBI;
|
import org.skife.jdbi.v2.DBI;
|
||||||
@@ -36,6 +29,13 @@ import org.whispersystems.textsecuregcm.storage.Accounts;
|
|||||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||||
|
|
||||||
|
import io.dropwizard.cli.ConfiguredCommand;
|
||||||
|
import io.dropwizard.db.DataSourceFactory;
|
||||||
|
import io.dropwizard.jdbi.ImmutableListContainerFactory;
|
||||||
|
import io.dropwizard.jdbi.ImmutableSetContainerFactory;
|
||||||
|
import io.dropwizard.jdbi.OptionalContainerFactory;
|
||||||
|
import io.dropwizard.jdbi.args.OptionalArgumentFactory;
|
||||||
|
import io.dropwizard.setup.Bootstrap;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
public class DirectoryCommand extends ConfiguredCommand<WhisperServerConfiguration> {
|
public class DirectoryCommand extends ConfiguredCommand<WhisperServerConfiguration> {
|
||||||
@@ -53,8 +53,8 @@ public class DirectoryCommand extends ConfiguredCommand<WhisperServerConfigurati
|
|||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
DatabaseConfiguration dbConfig = config.getDatabaseConfiguration();
|
DataSourceFactory dbConfig = config.getDataSourceFactory();
|
||||||
DBI dbi = new DBI(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPassword());
|
DBI dbi = new DBI(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPassword());
|
||||||
|
|
||||||
dbi.registerArgumentFactory(new OptionalArgumentFactory(dbConfig.getDriverClass()));
|
dbi.registerArgumentFactory(new OptionalArgumentFactory(dbConfig.getDriverClass()));
|
||||||
dbi.registerContainerFactory(new ImmutableListContainerFactory());
|
dbi.registerContainerFactory(new ImmutableListContainerFactory());
|
||||||
|
|||||||
@@ -27,12 +27,14 @@ import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
|||||||
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.DirectoryManager.BatchOperationHandle;
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager.BatchOperationHandle;
|
||||||
import org.whispersystems.textsecuregcm.util.Base64;
|
import org.whispersystems.textsecuregcm.util.Base64;
|
||||||
import org.whispersystems.textsecuregcm.util.Hex;
|
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.whispersystems.textsecuregcm.storage.DirectoryManager.PendingClientContact;
|
||||||
|
|
||||||
public class DirectoryUpdater {
|
public class DirectoryUpdater {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(DirectoryUpdater.class);
|
private final Logger logger = LoggerFactory.getLogger(DirectoryUpdater.class);
|
||||||
@@ -82,54 +84,69 @@ public class DirectoryUpdater {
|
|||||||
|
|
||||||
public void updateFromPeers() {
|
public void updateFromPeers() {
|
||||||
logger.info("Updating peer directories.");
|
logger.info("Updating peer directories.");
|
||||||
List<FederatedClient> clients = federatedClientManager.getClients();
|
|
||||||
|
int contactsAdded = 0;
|
||||||
|
int contactsRemoved = 0;
|
||||||
|
List<FederatedClient> clients = federatedClientManager.getClients();
|
||||||
|
|
||||||
for (FederatedClient client : clients) {
|
for (FederatedClient client : clients) {
|
||||||
logger.info("Updating directory from peer: " + client.getPeerName());
|
logger.info("Updating directory from peer: " + client.getPeerName());
|
||||||
// BatchOperationHandle handle = directory.startBatchOperation();
|
|
||||||
|
|
||||||
try {
|
int userCount = client.getUserCount();
|
||||||
int userCount = client.getUserCount();
|
int retrieved = 0;
|
||||||
int retrieved = 0;
|
|
||||||
|
|
||||||
logger.info("Remote peer user count: " + userCount);
|
logger.info("Remote peer user count: " + userCount);
|
||||||
|
|
||||||
while (retrieved < userCount) {
|
while (retrieved < userCount) {
|
||||||
logger.info("Retrieving remote tokens...");
|
logger.info("Retrieving remote tokens...");
|
||||||
List<ClientContact> clientContacts = client.getUserTokens(retrieved);
|
List<ClientContact> remoteContacts = client.getUserTokens(retrieved);
|
||||||
|
List<PendingClientContact> localContacts = new LinkedList<>();
|
||||||
|
BatchOperationHandle handle = directory.startBatchOperation();
|
||||||
|
|
||||||
if (clientContacts == null) {
|
if (remoteContacts == null) {
|
||||||
logger.info("Remote tokens empty, ending...");
|
logger.info("Remote tokens empty, ending...");
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
logger.info("Retrieved " + clientContacts.size() + " remote tokens...");
|
logger.info("Retrieved " + remoteContacts.size() + " remote tokens...");
|
||||||
}
|
|
||||||
|
|
||||||
for (ClientContact clientContact : clientContacts) {
|
|
||||||
clientContact.setRelay(client.getPeerName());
|
|
||||||
|
|
||||||
Optional<ClientContact> existing = directory.get(clientContact.getToken());
|
|
||||||
|
|
||||||
if (!clientContact.isInactive() && (!existing.isPresent() || client.getPeerName().equals(existing.get().getRelay()))) {
|
|
||||||
// directory.add(handle, clientContact);
|
|
||||||
directory.add(clientContact);
|
|
||||||
} else {
|
|
||||||
if (existing.isPresent() && client.getPeerName().equals(existing.get().getRelay())) {
|
|
||||||
directory.remove(clientContact.getToken());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
retrieved += clientContacts.size();
|
|
||||||
logger.info("Processed: " + retrieved + " remote tokens.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Update from peer complete.");
|
for (ClientContact remoteContact : remoteContacts) {
|
||||||
} finally {
|
localContacts.add(directory.get(handle, remoteContact.getToken()));
|
||||||
// directory.stopBatchOperation(handle);
|
}
|
||||||
|
|
||||||
|
directory.stopBatchOperation(handle);
|
||||||
|
|
||||||
|
handle = directory.startBatchOperation();
|
||||||
|
Iterator<ClientContact> remoteContactIterator = remoteContacts.iterator();
|
||||||
|
Iterator<PendingClientContact> localContactIterator = localContacts.iterator();
|
||||||
|
|
||||||
|
while (remoteContactIterator.hasNext() && localContactIterator.hasNext()) {
|
||||||
|
ClientContact remoteContact = remoteContactIterator.next();
|
||||||
|
Optional<ClientContact> localContact = localContactIterator.next().get();
|
||||||
|
|
||||||
|
remoteContact.setRelay(client.getPeerName());
|
||||||
|
|
||||||
|
if (!remoteContact.isInactive() && (!localContact.isPresent() || client.getPeerName().equals(localContact.get().getRelay()))) {
|
||||||
|
contactsAdded++;
|
||||||
|
directory.add(handle, remoteContact);
|
||||||
|
} else {
|
||||||
|
if (localContact.isPresent() && client.getPeerName().equals(localContact.get().getRelay())) {
|
||||||
|
contactsRemoved++;
|
||||||
|
directory.remove(handle, remoteContact.getToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
directory.stopBatchOperation(handle);
|
||||||
|
|
||||||
|
retrieved += remoteContacts.size();
|
||||||
|
logger.info("Processed: " + retrieved + " remote tokens.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info("Update from peer complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Update from peer directories complete.");
|
logger.info("Update from peer directories complete.");
|
||||||
|
logger.info(String.format("Added %d and removed %d remove contacts.", contactsAdded, contactsRemoved));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,4 +142,33 @@
|
|||||||
</createIndex>
|
</createIndex>
|
||||||
|
|
||||||
</changeSet>
|
</changeSet>
|
||||||
|
|
||||||
|
<changeSet id="3" author="moxie">
|
||||||
|
<sql>CREATE OR REPLACE FUNCTION "custom_json_object_set_key"(
|
||||||
|
"json" json,
|
||||||
|
"key_to_set" TEXT,
|
||||||
|
"value_to_set" anyelement
|
||||||
|
)
|
||||||
|
RETURNS json
|
||||||
|
LANGUAGE sql
|
||||||
|
IMMUTABLE
|
||||||
|
STRICT
|
||||||
|
AS $function$
|
||||||
|
SELECT COALESCE(
|
||||||
|
(SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}')
|
||||||
|
FROM (SELECT *
|
||||||
|
FROM json_each("json")
|
||||||
|
WHERE "key" <> "key_to_set"
|
||||||
|
UNION ALL
|
||||||
|
SELECT "key_to_set", to_json("value_to_set")) AS "fields"),
|
||||||
|
'{}'
|
||||||
|
)::json
|
||||||
|
$function$;</sql>
|
||||||
|
<sql>UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number' AND k.last_resort = 1;</sql>
|
||||||
|
<sql>UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number';</sql>
|
||||||
|
</changeSet>
|
||||||
|
|
||||||
|
<changeSet id="4" author="moxie">
|
||||||
|
<dropColumn tableName="keys" columnName="identity_key"/>
|
||||||
|
</changeSet>
|
||||||
</databaseChangeLog>
|
</databaseChangeLog>
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
|||||||
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.yammer.dropwizard.testing.ResourceTest;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.controllers.AccountController;
|
import org.whispersystems.textsecuregcm.controllers.AccountController;
|
||||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||||
@@ -16,11 +17,12 @@ import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
|||||||
|
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
import static org.fest.assertions.api.Assertions.assertThat;
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
public class AccountControllerTest extends ResourceTest {
|
public class AccountControllerTest {
|
||||||
|
|
||||||
private static final String SENDER = "+14152222222";
|
private static final String SENDER = "+14152222222";
|
||||||
|
|
||||||
@@ -30,26 +32,29 @@ public class AccountControllerTest extends ResourceTest {
|
|||||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
private SmsSender smsSender = mock(SmsSender.class );
|
private SmsSender smsSender = mock(SmsSender.class );
|
||||||
|
|
||||||
@Override
|
@Rule
|
||||||
protected void setUpResources() throws Exception {
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
addProvider(AuthHelper.getAuthenticator());
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new AccountController(pendingAccountsManager,
|
||||||
|
accountsManager,
|
||||||
|
rateLimiters,
|
||||||
|
smsSender))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
||||||
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
||||||
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
||||||
|
|
||||||
when(pendingAccountsManager.getCodeForNumber(SENDER)).thenReturn(Optional.of("1234"));
|
when(pendingAccountsManager.getCodeForNumber(SENDER)).thenReturn(Optional.of("1234"));
|
||||||
|
|
||||||
addResource(new AccountController(pendingAccountsManager,
|
|
||||||
accountsManager,
|
|
||||||
rateLimiters,
|
|
||||||
smsSender));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendCode() throws Exception {
|
public void testSendCode() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/accounts/sms/code/%s", SENDER))
|
resources.client().resource(String.format("/v1/accounts/sms/code/%s", SENDER))
|
||||||
.get(ClientResponse.class);
|
.get(ClientResponse.class);
|
||||||
|
|
||||||
assertThat(response.getStatus()).isEqualTo(200);
|
assertThat(response.getStatus()).isEqualTo(200);
|
||||||
@@ -60,7 +65,7 @@ public class AccountControllerTest extends ResourceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testVerifyCode() throws Exception {
|
public void testVerifyCode() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/accounts/code/%s", "1234"))
|
resources.client().resource(String.format("/v1/accounts/code/%s", "1234"))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||||
.entity(new AccountAttributes("keykeykeykey", false, false, 2222))
|
.entity(new AccountAttributes("keykeykeykey", false, false, 2222))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -68,13 +73,13 @@ public class AccountControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
assertThat(response.getStatus()).isEqualTo(204);
|
assertThat(response.getStatus()).isEqualTo(204);
|
||||||
|
|
||||||
verify(accountsManager).create(isA(Account.class));
|
verify(accountsManager, times(1)).create(isA(Account.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVerifyBadCode() throws Exception {
|
public void testVerifyBadCode() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/accounts/code/%s", "1111"))
|
resources.client().resource(String.format("/v1/accounts/code/%s", "1111"))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||||
.entity(new AccountAttributes("keykeykeykey", false, false, 3333))
|
.entity(new AccountAttributes("keykeykeykey", false, false, 3333))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
|||||||
@@ -17,7 +17,8 @@
|
|||||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.testing.ResourceTest;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.controllers.DeviceController;
|
import org.whispersystems.textsecuregcm.controllers.DeviceController;
|
||||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||||
@@ -33,10 +34,11 @@ import org.whispersystems.textsecuregcm.util.VerificationCode;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
import static org.fest.assertions.api.Assertions.assertThat;
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
public class DeviceControllerTest extends ResourceTest {
|
public class DeviceControllerTest {
|
||||||
@Path("/v1/devices")
|
@Path("/v1/devices")
|
||||||
static class DumbVerificationDeviceController extends DeviceController {
|
static class DumbVerificationDeviceController extends DeviceController {
|
||||||
public DumbVerificationDeviceController(PendingDevicesManager pendingDevices, AccountsManager accounts, RateLimiters rateLimiters) {
|
public DumbVerificationDeviceController(PendingDevicesManager pendingDevices, AccountsManager accounts, RateLimiters rateLimiters) {
|
||||||
@@ -55,10 +57,17 @@ public class DeviceControllerTest extends ResourceTest {
|
|||||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
private Account account = mock(Account.class );
|
private Account account = mock(Account.class );
|
||||||
|
|
||||||
@Override
|
@Rule
|
||||||
protected void setUpResources() throws Exception {
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
addProvider(AuthHelper.getAuthenticator());
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new DumbVerificationDeviceController(pendingDevicesManager,
|
||||||
|
accountsManager,
|
||||||
|
rateLimiters))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
||||||
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
||||||
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
||||||
@@ -69,19 +78,17 @@ public class DeviceControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
when(pendingDevicesManager.getCodeForNumber(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of("5678901"));
|
when(pendingDevicesManager.getCodeForNumber(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of("5678901"));
|
||||||
when(accountsManager.get(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of(account));
|
when(accountsManager.get(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of(account));
|
||||||
|
|
||||||
addResource(new DumbVerificationDeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validDeviceRegisterTest() throws Exception {
|
public void validDeviceRegisterTest() throws Exception {
|
||||||
VerificationCode deviceCode = client().resource("/v1/devices/provisioning_code")
|
VerificationCode deviceCode = resources.client().resource("/v1/devices/provisioning_code")
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.get(VerificationCode.class);
|
.get(VerificationCode.class);
|
||||||
|
|
||||||
assertThat(deviceCode).isEqualTo(new VerificationCode(5678901));
|
assertThat(deviceCode).isEqualTo(new VerificationCode(5678901));
|
||||||
|
|
||||||
DeviceResponse response = client().resource("/v1/devices/5678901")
|
DeviceResponse response = resources.client().resource("/v1/devices/5678901")
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, "password1"))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, "password1"))
|
||||||
.entity(new AccountAttributes("keykeykeykey", false, true, 1234))
|
.entity(new AccountAttributes("keykeykeykey", false, true, 1234))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||||
|
|
||||||
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
import org.whispersystems.textsecuregcm.controllers.DirectoryController;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.ClientContactTokens;
|
||||||
|
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||||
|
import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||||
|
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||||
|
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Base64;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Matchers.anyList;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class DirectoryControllerTest {
|
||||||
|
|
||||||
|
private final RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||||
|
private final RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
|
private final DirectoryManager directoryManager = mock(DirectoryManager.class);
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new DirectoryController(rateLimiters,
|
||||||
|
directoryManager))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
|
when(rateLimiters.getContactsLimiter()).thenReturn(rateLimiter);
|
||||||
|
when(directoryManager.get(anyList())).thenAnswer(new Answer<List<byte[]>>() {
|
||||||
|
@Override
|
||||||
|
public List<byte[]> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||||
|
List<byte[]> query = (List<byte[]>) invocationOnMock.getArguments()[0];
|
||||||
|
List<byte[]> response = new LinkedList<>(query);
|
||||||
|
response.remove(0);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContactIntersection() throws Exception {
|
||||||
|
List<String> tokens = new LinkedList<String>() {{
|
||||||
|
add(Base64.encodeBytes("foo".getBytes()));
|
||||||
|
add(Base64.encodeBytes("bar".getBytes()));
|
||||||
|
add(Base64.encodeBytes("baz".getBytes()));
|
||||||
|
}};
|
||||||
|
|
||||||
|
List<String> expectedResponse = new LinkedList<>(tokens);
|
||||||
|
expectedResponse.remove(0);
|
||||||
|
|
||||||
|
ClientResponse response =
|
||||||
|
resources.client().resource("/v1/directory/tokens/")
|
||||||
|
.entity(new ClientContactTokens(tokens))
|
||||||
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
.header("Authorization",
|
||||||
|
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER,
|
||||||
|
AuthHelper.VALID_PASSWORD))
|
||||||
|
.put(ClientResponse.class);
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(response.getStatus()).isEqualTo(200);
|
||||||
|
assertThat(response.getEntity(ClientContactTokens.class).getContacts()).isEqualTo(expectedResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,8 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.yammer.dropwizard.testing.ResourceTest;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.controllers.FederationController;
|
import org.whispersystems.textsecuregcm.controllers.FederationController;
|
||||||
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
||||||
@@ -23,17 +24,16 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.yammer.dropwizard.testing.JsonHelpers.jsonFixture;
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.jsonFixture;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
public class FederatedControllerTest extends ResourceTest {
|
public class FederatedControllerTest {
|
||||||
|
|
||||||
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
||||||
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
||||||
@@ -46,10 +46,20 @@ public class FederatedControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
private final ObjectMapper mapper = new ObjectMapper();
|
private final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
@Override
|
private final MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
||||||
protected void setUpResources() throws Exception {
|
|
||||||
addProvider(AuthHelper.getAuthenticator());
|
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new FederationController(accountsManager,
|
||||||
|
null, null,
|
||||||
|
messageController))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
||||||
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
||||||
}};
|
}};
|
||||||
@@ -66,15 +76,12 @@ public class FederatedControllerTest extends ResourceTest {
|
|||||||
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
||||||
|
|
||||||
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
||||||
|
|
||||||
MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
|
||||||
addResource(new FederationController(accountsManager, null, null, messageController));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleDeviceCurrent() throws Exception {
|
public void testSingleDeviceCurrent() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/federation/messages/+14152223333/1/%s", SINGLE_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/federation/messages/+14152223333/1/%s", SINGLE_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader("cyanogen", "foofoo"))
|
.header("Authorization", AuthHelper.getAuthHeader("cyanogen", "foofoo"))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
|||||||
@@ -2,10 +2,15 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
|||||||
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.yammer.dropwizard.testing.ResourceTest;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
||||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.PreKeyList;
|
||||||
import org.whispersystems.textsecuregcm.entities.PreKeyStatus;
|
import org.whispersystems.textsecuregcm.entities.PreKeyStatus;
|
||||||
import org.whispersystems.textsecuregcm.entities.UnstructuredPreKeyList;
|
import org.whispersystems.textsecuregcm.entities.UnstructuredPreKeyList;
|
||||||
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
import org.whispersystems.textsecuregcm.limits.RateLimiter;
|
||||||
@@ -16,37 +21,43 @@ import org.whispersystems.textsecuregcm.storage.Device;
|
|||||||
import org.whispersystems.textsecuregcm.storage.Keys;
|
import org.whispersystems.textsecuregcm.storage.Keys;
|
||||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
import static org.fest.assertions.api.Assertions.assertThat;
|
import static org.fest.assertions.api.Assertions.assertThat;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
public class KeyControllerTest extends ResourceTest {
|
public class KeyControllerTest {
|
||||||
|
|
||||||
private final String EXISTS_NUMBER = "+14152222222";
|
private static final String EXISTS_NUMBER = "+14152222222";
|
||||||
private final String NOT_EXISTS_NUMBER = "+14152222220";
|
private static String NOT_EXISTS_NUMBER = "+14152222220";
|
||||||
|
|
||||||
private final int SAMPLE_REGISTRATION_ID = 999;
|
private static int SAMPLE_REGISTRATION_ID = 999;
|
||||||
private final int SAMPLE_REGISTRATION_ID2 = 1002;
|
private static int SAMPLE_REGISTRATION_ID2 = 1002;
|
||||||
|
|
||||||
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, Device.MASTER_ID, 1234, "test1", "test2", false);
|
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, Device.MASTER_ID, 1234, "test1", "test2", false);
|
||||||
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4", false );
|
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4,", false );
|
||||||
private final PreKey SAMPLE_KEY3 = new PreKey(3, EXISTS_NUMBER, 3, 334, "test5", "test6", false);
|
private final PreKey SAMPLE_KEY3 = new PreKey(3, EXISTS_NUMBER, 3, 334, "test5", "test6", false );
|
||||||
private final Keys keys = mock(Keys.class );
|
private final Keys keys = mock(Keys.class );
|
||||||
private final AccountsManager accounts = mock(AccountsManager.class);
|
private final AccountsManager accounts = mock(AccountsManager.class);
|
||||||
|
private final Account existsAccount = mock(Account.class );
|
||||||
|
|
||||||
@Override
|
private RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||||
protected void setUpResources() {
|
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
addProvider(AuthHelper.getAuthenticator());
|
|
||||||
|
|
||||||
RateLimiters rateLimiters = mock(RateLimiters.class);
|
@Rule
|
||||||
RateLimiter rateLimiter = mock(RateLimiter.class );
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new KeysController(rateLimiters, keys, accounts, null))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
Device sampleDevice = mock(Device.class );
|
Device sampleDevice = mock(Device.class );
|
||||||
Device sampleDevice2 = mock(Device.class);
|
Device sampleDevice2 = mock(Device.class);
|
||||||
Device sampleDevice3 = mock(Device.class);
|
Device sampleDevice3 = mock(Device.class);
|
||||||
Account existsAccount = mock(Account.class);
|
|
||||||
|
|
||||||
when(sampleDevice.getRegistrationId()).thenReturn(SAMPLE_REGISTRATION_ID);
|
when(sampleDevice.getRegistrationId()).thenReturn(SAMPLE_REGISTRATION_ID);
|
||||||
when(sampleDevice2.getRegistrationId()).thenReturn(SAMPLE_REGISTRATION_ID2);
|
when(sampleDevice2.getRegistrationId()).thenReturn(SAMPLE_REGISTRATION_ID2);
|
||||||
@@ -59,29 +70,41 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
when(existsAccount.getDevice(2L)).thenReturn(Optional.of(sampleDevice2));
|
when(existsAccount.getDevice(2L)).thenReturn(Optional.of(sampleDevice2));
|
||||||
when(existsAccount.getDevice(3L)).thenReturn(Optional.of(sampleDevice3));
|
when(existsAccount.getDevice(3L)).thenReturn(Optional.of(sampleDevice3));
|
||||||
when(existsAccount.isActive()).thenReturn(true);
|
when(existsAccount.isActive()).thenReturn(true);
|
||||||
|
when(existsAccount.getIdentityKey()).thenReturn("existsidentitykey");
|
||||||
|
|
||||||
when(accounts.get(EXISTS_NUMBER)).thenReturn(Optional.of(existsAccount));
|
when(accounts.get(EXISTS_NUMBER)).thenReturn(Optional.of(existsAccount));
|
||||||
when(accounts.get(NOT_EXISTS_NUMBER)).thenReturn(Optional.<Account>absent());
|
when(accounts.get(NOT_EXISTS_NUMBER)).thenReturn(Optional.<Account>absent());
|
||||||
|
|
||||||
when(rateLimiters.getPreKeysLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getPreKeysLimiter()).thenReturn(rateLimiter);
|
||||||
|
|
||||||
when(keys.get(eq(EXISTS_NUMBER), eq(1L))).thenReturn(Optional.of(new UnstructuredPreKeyList(SAMPLE_KEY)));
|
when(keys.get(eq(EXISTS_NUMBER), eq(1L))).thenAnswer(new Answer<Optional<UnstructuredPreKeyList>>() {
|
||||||
|
@Override
|
||||||
|
public Optional<UnstructuredPreKeyList> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||||
|
return Optional.of(new UnstructuredPreKeyList(cloneKey(SAMPLE_KEY)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
when(keys.get(eq(NOT_EXISTS_NUMBER), eq(1L))).thenReturn(Optional.<UnstructuredPreKeyList>absent());
|
when(keys.get(eq(NOT_EXISTS_NUMBER), eq(1L))).thenReturn(Optional.<UnstructuredPreKeyList>absent());
|
||||||
|
|
||||||
List<PreKey> allKeys = new LinkedList<>();
|
when(keys.get(EXISTS_NUMBER)).thenAnswer(new Answer<Optional<UnstructuredPreKeyList>>() {
|
||||||
allKeys.add(SAMPLE_KEY);
|
@Override
|
||||||
allKeys.add(SAMPLE_KEY2);
|
public Optional<UnstructuredPreKeyList> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||||
allKeys.add(SAMPLE_KEY3);
|
List<PreKey> allKeys = new LinkedList<>();
|
||||||
|
allKeys.add(cloneKey(SAMPLE_KEY));
|
||||||
|
allKeys.add(cloneKey(SAMPLE_KEY2));
|
||||||
|
allKeys.add(cloneKey(SAMPLE_KEY3));
|
||||||
|
|
||||||
|
return Optional.of(new UnstructuredPreKeyList(allKeys));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
when(keys.get(EXISTS_NUMBER)).thenReturn(Optional.of(new UnstructuredPreKeyList(allKeys)));
|
|
||||||
when(keys.getCount(eq(AuthHelper.VALID_NUMBER), eq(1L))).thenReturn(5);
|
when(keys.getCount(eq(AuthHelper.VALID_NUMBER), eq(1L))).thenReturn(5);
|
||||||
|
when(AuthHelper.VALID_ACCOUNT.getIdentityKey()).thenReturn(null);
|
||||||
addResource(new KeysController(rateLimiters, keys, accounts, null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validKeyStatusTest() throws Exception {
|
public void validKeyStatusTest() throws Exception {
|
||||||
PreKeyStatus result = client().resource("/v1/keys")
|
PreKeyStatus result = resources.client().resource("/v1/keys")
|
||||||
.header("Authorization",
|
.header("Authorization",
|
||||||
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.get(PreKeyStatus.class);
|
.get(PreKeyStatus.class);
|
||||||
@@ -93,13 +116,13 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validLegacyRequestTest() throws Exception {
|
public void validLegacyRequestTest() throws Exception {
|
||||||
PreKey result = client().resource(String.format("/v1/keys/%s", EXISTS_NUMBER))
|
PreKey result = resources.client().resource(String.format("/v1/keys/%s", EXISTS_NUMBER))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.get(PreKey.class);
|
.get(PreKey.class);
|
||||||
|
|
||||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
||||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
||||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY.getIdentityKey());
|
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||||
|
|
||||||
assertThat(result.getId() == 0);
|
assertThat(result.getId() == 0);
|
||||||
assertThat(result.getNumber() == null);
|
assertThat(result.getNumber() == null);
|
||||||
@@ -110,7 +133,7 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validMultiRequestTest() throws Exception {
|
public void validMultiRequestTest() throws Exception {
|
||||||
UnstructuredPreKeyList results = client().resource(String.format("/v1/keys/%s/*", EXISTS_NUMBER))
|
UnstructuredPreKeyList results = resources.client().resource(String.format("/v1/keys/%s/*", EXISTS_NUMBER))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.get(UnstructuredPreKeyList.class);
|
.get(UnstructuredPreKeyList.class);
|
||||||
|
|
||||||
@@ -120,7 +143,7 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
||||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
||||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY.getIdentityKey());
|
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||||
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID);
|
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID);
|
||||||
|
|
||||||
assertThat(result.getId() == 0);
|
assertThat(result.getId() == 0);
|
||||||
@@ -129,7 +152,7 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
result = results.getKeys().get(1);
|
result = results.getKeys().get(1);
|
||||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY2.getKeyId());
|
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY2.getKeyId());
|
||||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY2.getPublicKey());
|
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY2.getPublicKey());
|
||||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY2.getIdentityKey());
|
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||||
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID2);
|
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID2);
|
||||||
|
|
||||||
assertThat(result.getId() == 0);
|
assertThat(result.getId() == 0);
|
||||||
@@ -142,27 +165,70 @@ public class KeyControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidRequestTest() throws Exception {
|
public void invalidRequestTest() throws Exception {
|
||||||
ClientResponse response = client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
ClientResponse response = resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.get(ClientResponse.class);
|
.get(ClientResponse.class);
|
||||||
|
|
||||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(404);
|
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void unauthorizedRequestTest() throws Exception {
|
public void unauthorizedRequestTest() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.INVALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.INVALID_PASSWORD))
|
||||||
.get(ClientResponse.class);
|
.get(ClientResponse.class);
|
||||||
|
|
||||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(401);
|
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(401);
|
||||||
|
|
||||||
response =
|
response =
|
||||||
client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||||
.get(ClientResponse.class);
|
.get(ClientResponse.class);
|
||||||
|
|
||||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(401);
|
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(401);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void putKeysTest() throws Exception {
|
||||||
|
final PreKey newKey = new PreKey(0, null, 1L, 31337, "foobar", "foobarbaz", false);
|
||||||
|
final PreKey lastResortKey = new PreKey(0, null, 1L, 0xFFFFFF, "fooz", "foobarbaz", false);
|
||||||
|
|
||||||
|
List<PreKey> preKeys = new LinkedList<PreKey>() {{
|
||||||
|
add(newKey);
|
||||||
|
}};
|
||||||
|
|
||||||
|
PreKeyList preKeyList = new PreKeyList();
|
||||||
|
preKeyList.setKeys(preKeys);
|
||||||
|
preKeyList.setLastResortKey(lastResortKey);
|
||||||
|
|
||||||
|
ClientResponse response =
|
||||||
|
resources.client().resource("/v1/keys")
|
||||||
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
.put(ClientResponse.class, preKeyList);
|
||||||
|
|
||||||
|
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(204);
|
||||||
|
|
||||||
|
ArgumentCaptor<List> listCaptor = ArgumentCaptor.forClass(List.class );
|
||||||
|
ArgumentCaptor<PreKey> lastResortCaptor = ArgumentCaptor.forClass(PreKey.class);
|
||||||
|
verify(keys).store(eq(AuthHelper.VALID_NUMBER), eq(1L), listCaptor.capture(), lastResortCaptor.capture());
|
||||||
|
|
||||||
|
List<PreKey> capturedList = listCaptor.getValue();
|
||||||
|
assertThat(capturedList.size() == 1);
|
||||||
|
assertThat(capturedList.get(0).getIdentityKey().equals("foobarbaz"));
|
||||||
|
assertThat(capturedList.get(0).getKeyId() == 31337);
|
||||||
|
assertThat(capturedList.get(0).getPublicKey().equals("foobar"));
|
||||||
|
|
||||||
|
assertThat(lastResortCaptor.getValue().getPublicKey().equals("fooz"));
|
||||||
|
assertThat(lastResortCaptor.getValue().getIdentityKey().equals("foobarbaz"));
|
||||||
|
|
||||||
|
verify(AuthHelper.VALID_ACCOUNT).setIdentityKey(eq("foobarbaz"));
|
||||||
|
verify(accounts).update(AuthHelper.VALID_ACCOUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PreKey cloneKey(PreKey source) {
|
||||||
|
return new PreKey(source.getId(), source.getNumber(), source.getDeviceId(), source.getKeyId(),
|
||||||
|
source.getPublicKey(), source.getIdentityKey(), source.isLastResort());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,11 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.yammer.dropwizard.testing.ResourceTest;
|
import org.junit.Before;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
||||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.IncomingMessageList;
|
import org.whispersystems.textsecuregcm.entities.IncomingMessageList;
|
||||||
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
||||||
import org.whispersystems.textsecuregcm.entities.MismatchedDevices;
|
import org.whispersystems.textsecuregcm.entities.MismatchedDevices;
|
||||||
@@ -24,35 +25,39 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.yammer.dropwizard.testing.JsonHelpers.asJson;
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
import static com.yammer.dropwizard.testing.JsonHelpers.jsonFixture;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.asJson;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.jsonFixture;
|
||||||
|
|
||||||
public class MessageControllerTest extends ResourceTest {
|
public class MessageControllerTest {
|
||||||
|
|
||||||
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
||||||
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
||||||
|
|
||||||
private PushSender pushSender = mock(PushSender.class );
|
private final PushSender pushSender = mock(PushSender.class );
|
||||||
private FederatedClientManager federatedClientManager = mock(FederatedClientManager.class);
|
private final FederatedClientManager federatedClientManager = mock(FederatedClientManager.class);
|
||||||
private AccountsManager accountsManager = mock(AccountsManager.class );
|
private final AccountsManager accountsManager = mock(AccountsManager.class );
|
||||||
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
private final RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
private final RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||||
|
|
||||||
private final ObjectMapper mapper = new ObjectMapper();
|
private final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
@Override
|
@Rule
|
||||||
protected void setUpResources() throws Exception {
|
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||||
addProvider(AuthHelper.getAuthenticator());
|
.addProvider(AuthHelper.getAuthenticator())
|
||||||
|
.addResource(new MessageController(rateLimiters, pushSender, accountsManager,
|
||||||
|
federatedClientManager))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
||||||
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
||||||
}};
|
}};
|
||||||
@@ -69,15 +74,12 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
||||||
|
|
||||||
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
||||||
|
|
||||||
addResource(new MessageController(rateLimiters, pushSender, accountsManager,
|
|
||||||
federatedClientManager));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleDeviceLegacy() throws Exception {
|
public synchronized void testSingleDeviceLegacy() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource("/v1/messages/")
|
resources.client().resource("/v1/messages/")
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/legacy_message_single_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/legacy_message_single_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -85,13 +87,13 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
assertThat("Good Response", response.getStatus(), is(equalTo(200)));
|
assertThat("Good Response", response.getStatus(), is(equalTo(200)));
|
||||||
|
|
||||||
verify(pushSender).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
verify(pushSender, times(1)).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleDeviceCurrent() throws Exception {
|
public synchronized void testSingleDeviceCurrent() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/messages/%s", SINGLE_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/messages/%s", SINGLE_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -99,13 +101,13 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
|
|
||||||
assertThat("Good Response", response.getStatus(), is(equalTo(204)));
|
assertThat("Good Response", response.getStatus(), is(equalTo(204)));
|
||||||
|
|
||||||
verify(pushSender).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
verify(pushSender, times(1)).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiDeviceMissing() throws Exception {
|
public synchronized void testMultiDeviceMissing() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -121,9 +123,9 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiDeviceExtra() throws Exception {
|
public synchronized void testMultiDeviceExtra() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_extra_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_extra_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -139,9 +141,9 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiDevice() throws Exception {
|
public synchronized void testMultiDevice() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_multi_device.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_multi_device.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
@@ -153,9 +155,9 @@ public class MessageControllerTest extends ResourceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRegistrationIdMismatch() throws Exception {
|
public synchronized void testRegistrationIdMismatch() throws Exception {
|
||||||
ClientResponse response =
|
ClientResponse response =
|
||||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_registration_id.json"), IncomingMessageList.class))
|
.entity(mapper.readValue(jsonFixture("fixtures/current_message_registration_id.json"), IncomingMessageList.class))
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
|||||||
@@ -2,25 +2,28 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
import org.eclipse.jetty.websocket.api.CloseStatus;
|
||||||
import org.eclipse.jetty.websocket.WebSocket;
|
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||||
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
|
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||||
import org.whispersystems.textsecuregcm.controllers.WebsocketController;
|
import org.whispersystems.textsecuregcm.controllers.WebsocketController;
|
||||||
import org.whispersystems.textsecuregcm.controllers.WebsocketControllerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.entities.AcknowledgeWebsocketMessage;
|
import org.whispersystems.textsecuregcm.entities.AcknowledgeWebsocketMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||||
|
import org.whispersystems.textsecuregcm.push.PushSender;
|
||||||
import org.whispersystems.textsecuregcm.storage.Account;
|
import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
import org.whispersystems.textsecuregcm.storage.StoredMessages;
|
||||||
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
||||||
|
import org.whispersystems.textsecuregcm.websocket.WebsocketControllerFactory;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.fest.assertions.api.Assertions.assertThat;
|
import io.dropwizard.auth.basic.BasicCredentials;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@@ -34,13 +37,14 @@ public class WebsocketControllerTest {
|
|||||||
private static final String VALID_PASSWORD = "secure";
|
private static final String VALID_PASSWORD = "secure";
|
||||||
private static final String INVALID_PASSWORD = "insecure";
|
private static final String INVALID_PASSWORD = "insecure";
|
||||||
|
|
||||||
private final StoredMessageManager storedMessageManager = mock(StoredMessageManager.class);
|
private static final StoredMessages storedMessages = mock(StoredMessages.class);
|
||||||
private final AccountAuthenticator accountAuthenticator = mock(AccountAuthenticator.class);
|
private static final AccountAuthenticator accountAuthenticator = mock(AccountAuthenticator.class);
|
||||||
private final PubSubManager pubSubManager = mock(PubSubManager.class );
|
private static final PubSubManager pubSubManager = mock(PubSubManager.class );
|
||||||
private final Account account = mock(Account.class );
|
private static final Account account = mock(Account.class );
|
||||||
private final Device device = mock(Device.class );
|
private static final Device device = mock(Device.class );
|
||||||
private final HttpServletRequest request = mock(HttpServletRequest.class );
|
private static final UpgradeRequest upgradeRequest = mock(UpgradeRequest.class );
|
||||||
private final WebSocket.Connection connection = mock(WebSocket.Connection.class);
|
private static final Session session = mock(Session.class );
|
||||||
|
private static final PushSender pushSender = mock(PushSender.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCredentials() throws Exception {
|
public void testCredentials() throws Exception {
|
||||||
@@ -50,23 +54,35 @@ public class WebsocketControllerTest {
|
|||||||
when(accountAuthenticator.authenticate(eq(new BasicCredentials(INVALID_USER, INVALID_PASSWORD))))
|
when(accountAuthenticator.authenticate(eq(new BasicCredentials(INVALID_USER, INVALID_PASSWORD))))
|
||||||
.thenReturn(Optional.<Account>absent());
|
.thenReturn(Optional.<Account>absent());
|
||||||
|
|
||||||
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator,
|
when(session.getUpgradeRequest()).thenReturn(upgradeRequest);
|
||||||
storedMessageManager,
|
|
||||||
pubSubManager);
|
|
||||||
|
|
||||||
when(request.getParameter(eq("user"))).thenReturn(VALID_USER);
|
WebsocketController controller = new WebsocketController(accountAuthenticator, pushSender, pubSubManager, storedMessages);
|
||||||
when(request.getParameter(eq("password"))).thenReturn(VALID_PASSWORD);
|
|
||||||
|
|
||||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(true);
|
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||||
|
put("login", new String[] {VALID_USER});
|
||||||
|
put("password", new String[] {VALID_PASSWORD});
|
||||||
|
}});
|
||||||
|
|
||||||
when(request.getParameter(eq("user"))).thenReturn(INVALID_USER);
|
controller.onWebSocketConnect(session);
|
||||||
when(request.getParameter(eq("password"))).thenReturn(INVALID_PASSWORD);
|
|
||||||
|
|
||||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(false);
|
verify(session, never()).close();
|
||||||
|
verify(session, never()).close(any(CloseStatus.class));
|
||||||
|
verify(session, never()).close(anyInt(), anyString());
|
||||||
|
|
||||||
|
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||||
|
put("login", new String[] {INVALID_USER});
|
||||||
|
put("password", new String[] {INVALID_PASSWORD});
|
||||||
|
}});
|
||||||
|
|
||||||
|
controller.onWebSocketConnect(session);
|
||||||
|
|
||||||
|
verify(session).close(any(CloseStatus.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpen() throws Exception {
|
public void testOpen() throws Exception {
|
||||||
|
RemoteEndpoint remote = mock(RemoteEndpoint.class);
|
||||||
|
|
||||||
List<String> outgoingMessages = new LinkedList<String>() {{
|
List<String> outgoingMessages = new LinkedList<String>() {{
|
||||||
add("first");
|
add("first");
|
||||||
add("second");
|
add("second");
|
||||||
@@ -76,36 +92,36 @@ public class WebsocketControllerTest {
|
|||||||
when(device.getId()).thenReturn(2L);
|
when(device.getId()).thenReturn(2L);
|
||||||
when(account.getId()).thenReturn(31337L);
|
when(account.getId()).thenReturn(31337L);
|
||||||
when(account.getAuthenticatedDevice()).thenReturn(Optional.of(device));
|
when(account.getAuthenticatedDevice()).thenReturn(Optional.of(device));
|
||||||
|
when(session.getRemote()).thenReturn(remote);
|
||||||
|
when(session.getUpgradeRequest()).thenReturn(upgradeRequest);
|
||||||
|
|
||||||
when(request.getParameter(eq("user"))).thenReturn(VALID_USER);
|
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||||
when(request.getParameter(eq("password"))).thenReturn(VALID_PASSWORD);
|
put("login", new String[] {VALID_USER});
|
||||||
|
put("password", new String[] {VALID_PASSWORD});
|
||||||
|
}});
|
||||||
|
|
||||||
when(accountAuthenticator.authenticate(eq(new BasicCredentials(VALID_USER, VALID_PASSWORD))))
|
when(accountAuthenticator.authenticate(eq(new BasicCredentials(VALID_USER, VALID_PASSWORD))))
|
||||||
.thenReturn(Optional.of(account));
|
.thenReturn(Optional.of(account));
|
||||||
|
|
||||||
when(storedMessageManager.getOutgoingMessages(eq(account), eq(device))).thenReturn(outgoingMessages);
|
when(storedMessages.getMessagesForDevice(account.getId(), device.getId())).thenReturn(outgoingMessages);
|
||||||
|
|
||||||
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator,
|
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator, pushSender, storedMessages, pubSubManager);
|
||||||
storedMessageManager,
|
WebsocketController controller = (WebsocketController) factory.createWebSocket(null, null);
|
||||||
pubSubManager);
|
|
||||||
|
|
||||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(true);
|
controller.onWebSocketConnect(session);
|
||||||
|
|
||||||
WebsocketController socket = (WebsocketController)factory.doWebSocketConnect(request, "foo");
|
verify(pubSubManager).subscribe(eq(new WebsocketAddress(31337L, 2L)), eq((controller)));
|
||||||
socket.onOpen(connection);
|
verify(remote, times(3)).sendStringByFuture(anyString());
|
||||||
|
|
||||||
verify(pubSubManager).subscribe(eq(new WebsocketAddress(31337L, 2L)), eq((socket)));
|
controller.onWebSocketText(mapper.writeValueAsString(new AcknowledgeWebsocketMessage(1)));
|
||||||
verify(connection, times(3)).sendMessage(anyString());
|
controller.onWebSocketClose(1000, "Closed");
|
||||||
|
|
||||||
socket.onMessage(mapper.writeValueAsString(new AcknowledgeWebsocketMessage(1)));
|
|
||||||
socket.onClose(1000, "Closed");
|
|
||||||
|
|
||||||
List<String> pending = new LinkedList<String>() {{
|
List<String> pending = new LinkedList<String>() {{
|
||||||
add("first");
|
add("first");
|
||||||
add("third");
|
add("third");
|
||||||
}};
|
}};
|
||||||
|
|
||||||
verify(storedMessageManager).storeMessages(eq(account), eq(device), eq(pending));
|
verify(pushSender, times(2)).sendMessage(eq(account), eq(device), any(EncryptedOutgoingMessage.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package org.whispersystems.textsecuregcm.tests.entities;
|
package org.whispersystems.textsecuregcm.tests.entities;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
|
|
||||||
import static com.yammer.dropwizard.testing.JsonHelpers.*;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.asJson;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.fromJson;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.jsonFixture;
|
||||||
|
|
||||||
public class ClientContactTest {
|
public class ClientContactTest {
|
||||||
|
|
||||||
@@ -41,4 +44,5 @@ public class ClientContactTest {
|
|||||||
is(contact));
|
is(contact));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import org.whispersystems.textsecuregcm.entities.ClientContact;
|
|||||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
|
|
||||||
import static com.yammer.dropwizard.testing.JsonHelpers.*;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.*;
|
||||||
|
|
||||||
public class PreKeyTest {
|
public class PreKeyTest {
|
||||||
|
|
||||||
|
|||||||
@@ -27,20 +27,20 @@ public class AuthHelper {
|
|||||||
public static final String INVVALID_NUMBER = "+14151111111";
|
public static final String INVVALID_NUMBER = "+14151111111";
|
||||||
public static final String INVALID_PASSWORD = "bar";
|
public static final String INVALID_PASSWORD = "bar";
|
||||||
|
|
||||||
public static MultiBasicAuthProvider<FederatedPeer, Account> getAuthenticator() {
|
public static AccountsManager ACCOUNTS_MANAGER = mock(AccountsManager.class );
|
||||||
AccountsManager accounts = mock(AccountsManager.class );
|
public static Account VALID_ACCOUNT = mock(Account.class );
|
||||||
Account account = mock(Account.class );
|
public static Device VALID_DEVICE = mock(Device.class );
|
||||||
Device device = mock(Device.class );
|
public static AuthenticationCredentials VALID_CREDENTIALS = mock(AuthenticationCredentials.class);
|
||||||
AuthenticationCredentials credentials = mock(AuthenticationCredentials.class);
|
|
||||||
|
|
||||||
when(credentials.verify("foo")).thenReturn(true);
|
public static MultiBasicAuthProvider<FederatedPeer, Account> getAuthenticator() {
|
||||||
when(device.getAuthenticationCredentials()).thenReturn(credentials);
|
when(VALID_CREDENTIALS.verify("foo")).thenReturn(true);
|
||||||
when(device.getId()).thenReturn(1L);
|
when(VALID_DEVICE.getAuthenticationCredentials()).thenReturn(VALID_CREDENTIALS);
|
||||||
when(account.getDevice(anyLong())).thenReturn(Optional.of(device));
|
when(VALID_DEVICE.getId()).thenReturn(1L);
|
||||||
when(account.getNumber()).thenReturn(VALID_NUMBER);
|
when(VALID_ACCOUNT.getDevice(anyLong())).thenReturn(Optional.of(VALID_DEVICE));
|
||||||
when(account.getAuthenticatedDevice()).thenReturn(Optional.of(device));
|
when(VALID_ACCOUNT.getNumber()).thenReturn(VALID_NUMBER);
|
||||||
when(account.getRelay()).thenReturn(Optional.<String>absent());
|
when(VALID_ACCOUNT.getAuthenticatedDevice()).thenReturn(Optional.of(VALID_DEVICE));
|
||||||
when(accounts.get(VALID_NUMBER)).thenReturn(Optional.of(account));
|
when(VALID_ACCOUNT.getRelay()).thenReturn(Optional.<String>absent());
|
||||||
|
when(ACCOUNTS_MANAGER.get(VALID_NUMBER)).thenReturn(Optional.of(VALID_ACCOUNT));
|
||||||
|
|
||||||
List<FederatedPeer> peer = new LinkedList<FederatedPeer>() {{
|
List<FederatedPeer> peer = new LinkedList<FederatedPeer>() {{
|
||||||
add(new FederatedPeer("cyanogen", "https://foo", "foofoo", "bazzzzz"));
|
add(new FederatedPeer("cyanogen", "https://foo", "foofoo", "bazzzzz"));
|
||||||
@@ -51,7 +51,7 @@ public class AuthHelper {
|
|||||||
|
|
||||||
return new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(federationConfiguration),
|
return new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(federationConfiguration),
|
||||||
FederatedPeer.class,
|
FederatedPeer.class,
|
||||||
new AccountAuthenticator(accounts),
|
new AccountAuthenticator(ACCOUNTS_MANAGER),
|
||||||
Account.class, "WhisperServer");
|
Account.class, "WhisperServer");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.whispersystems.textsecuregcm.tests.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static io.dropwizard.testing.FixtureHelpers.fixture;
|
||||||
|
|
||||||
|
public class JsonHelpers {
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public static String asJson(Object object) throws JsonProcessingException {
|
||||||
|
return objectMapper.writeValueAsString(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T fromJson(String value, Class<T> clazz) throws IOException {
|
||||||
|
return objectMapper.readValue(value, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String jsonFixture(String filename) throws IOException {
|
||||||
|
return objectMapper.writeValueAsString(objectMapper.readValue(fixture(filename), JsonNode.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"token" : "BQVVHxMt5zAFXA",
|
"relay" : "whisper",
|
||||||
"relay" : "whisper"
|
"token" : "BQVVHxMt5zAFXA"
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"token" : "BQVVHxMt5zAFXA",
|
|
||||||
"relay" : "whisper",
|
"relay" : "whisper",
|
||||||
"supportsSms" : true
|
"supportsSms" : true,
|
||||||
|
"token" : "BQVVHxMt5zAFXA"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user