mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-25 00:38:00 +01:00
Upgrade to dropwizard 0.7.
This commit is contained in:
@@ -17,8 +17,6 @@
|
||||
package org.whispersystems.textsecuregcm;
|
||||
|
||||
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.FederationConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.GcmConfiguration;
|
||||
@@ -35,6 +33,9 @@ import org.whispersystems.textsecuregcm.configuration.WebsocketConfiguration;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import io.dropwizard.Configuration;
|
||||
import io.dropwizard.db.DataSourceFactory;
|
||||
|
||||
public class WhisperServerConfiguration extends Configuration {
|
||||
|
||||
@NotNull
|
||||
@@ -74,7 +75,7 @@ public class WhisperServerConfiguration extends Configuration {
|
||||
@Valid
|
||||
@NotNull
|
||||
@JsonProperty
|
||||
private DatabaseConfiguration database = new DatabaseConfiguration();
|
||||
private DataSourceFactory database = new DataSourceFactory();
|
||||
|
||||
@Valid
|
||||
@NotNull
|
||||
@@ -87,7 +88,7 @@ public class WhisperServerConfiguration extends Configuration {
|
||||
|
||||
@Valid
|
||||
@JsonProperty
|
||||
private MetricsConfiguration metrics = new MetricsConfiguration();
|
||||
private MetricsConfiguration viz = new MetricsConfiguration();
|
||||
|
||||
@Valid
|
||||
@JsonProperty
|
||||
@@ -125,7 +126,7 @@ public class WhisperServerConfiguration extends Configuration {
|
||||
return redis;
|
||||
}
|
||||
|
||||
public DatabaseConfiguration getDatabaseConfiguration() {
|
||||
public DataSourceFactory getDataSourceFactory() {
|
||||
return database;
|
||||
}
|
||||
|
||||
@@ -142,6 +143,6 @@ public class WhisperServerConfiguration extends Configuration {
|
||||
}
|
||||
|
||||
public MetricsConfiguration getMetricsConfiguration() {
|
||||
return metrics;
|
||||
return viz;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,18 +16,12 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm;
|
||||
|
||||
import com.codahale.metrics.SharedMetricRegistries;
|
||||
import com.codahale.metrics.graphite.GraphiteReporter;
|
||||
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.Metrics;
|
||||
import com.yammer.metrics.reporting.GraphiteReporter;
|
||||
import net.spy.memcached.MemcachedClient;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.eclipse.jetty.servlets.CrossOriginFilter;
|
||||
import org.skife.jdbi.v2.DBI;
|
||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||
import org.whispersystems.textsecuregcm.auth.FederatedPeerAuthenticator;
|
||||
@@ -71,16 +65,28 @@ import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
||||
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.workers.DirectoryCommand;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletRegistration;
|
||||
import java.security.Security;
|
||||
import java.util.EnumSet;
|
||||
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;
|
||||
|
||||
public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
||||
public class WhisperServerService extends Application<WhisperServerConfiguration> {
|
||||
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
@@ -88,24 +94,28 @@ public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
||||
|
||||
@Override
|
||||
public void initialize(Bootstrap<WhisperServerConfiguration> bootstrap) {
|
||||
bootstrap.setName("whisper-server");
|
||||
bootstrap.addCommand(new DirectoryCommand());
|
||||
bootstrap.addBundle(new MigrationsBundle<WhisperServerConfiguration>() {
|
||||
@Override
|
||||
public DatabaseConfiguration getDatabaseConfiguration(WhisperServerConfiguration configuration) {
|
||||
return configuration.getDatabaseConfiguration();
|
||||
public DataSourceFactory getDataSourceFactory(WhisperServerConfiguration configuration) {
|
||||
return configuration.getDataSourceFactory();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "whisper-server";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(WhisperServerConfiguration config, Environment environment)
|
||||
throws Exception
|
||||
{
|
||||
config.getHttpConfiguration().setConnectorType(HttpConfiguration.ConnectorType.NONBLOCKING);
|
||||
|
||||
SharedMetricRegistries.add(Constants.METRICS_NAME, environment.metrics());
|
||||
|
||||
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);
|
||||
PendingAccounts pendingAccounts = jdbi.onDemand(PendingAccounts.class);
|
||||
@@ -140,44 +150,59 @@ public class WhisperServerService extends Service<WhisperServerConfiguration> {
|
||||
KeysController keysController = new KeysController(rateLimiters, keys, accountsManager, federatedClientManager);
|
||||
MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
||||
|
||||
environment.addProvider(new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(config.getFederationConfiguration()),
|
||||
FederatedPeer.class,
|
||||
deviceAuthenticator,
|
||||
Device.class, "WhisperServer"));
|
||||
environment.jersey().register(new MultiBasicAuthProvider<>(new FederatedPeerAuthenticator(config.getFederationConfiguration()),
|
||||
FederatedPeer.class,
|
||||
deviceAuthenticator,
|
||||
Device.class, "WhisperServer"));
|
||||
|
||||
environment.addResource(new AccountController(pendingAccountsManager, accountsManager, rateLimiters, smsSender));
|
||||
environment.addResource(new DeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
||||
environment.addResource(new DirectoryController(rateLimiters, directory));
|
||||
environment.addResource(new FederationController(accountsManager, attachmentController, keysController, messageController));
|
||||
environment.addResource(attachmentController);
|
||||
environment.addResource(keysController);
|
||||
environment.addResource(messageController);
|
||||
environment.jersey().register(new AccountController(pendingAccountsManager, accountsManager, rateLimiters, smsSender));
|
||||
environment.jersey().register(new DeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
||||
environment.jersey().register(new DirectoryController(rateLimiters, directory));
|
||||
environment.jersey().register(new FederationController(accountsManager, attachmentController, keysController, messageController));
|
||||
environment.jersey().register(attachmentController);
|
||||
environment.jersey().register(keysController);
|
||||
environment.jersey().register(messageController);
|
||||
|
||||
if (config.getWebsocketConfiguration().isEnabled()) {
|
||||
environment.addServlet(new WebsocketControllerFactory(deviceAuthenticator, storedMessageManager, pubSubManager),
|
||||
"/v1/websocket/");
|
||||
environment.addFilter(new CORSHeaderFilter(), "/*");
|
||||
WebsocketControllerFactory servlet = new WebsocketControllerFactory(deviceAuthenticator,
|
||||
storedMessageManager,
|
||||
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.addHealthCheck(new MemcacheHealthCheck(memcachedClient));
|
||||
environment.healthChecks().register("redis", new RedisHealthCheck(redisClient));
|
||||
environment.healthChecks().register("memcache", new MemcacheHealthCheck(memcachedClient));
|
||||
|
||||
environment.addProvider(new IOExceptionMapper());
|
||||
environment.addProvider(new RateLimitExceededExceptionMapper());
|
||||
environment.jersey().register(new IOExceptionMapper());
|
||||
environment.jersey().register(new RateLimitExceededExceptionMapper());
|
||||
|
||||
Metrics.newGauge(CpuUsageGauge.class, "cpu", new CpuUsageGauge());
|
||||
Metrics.newGauge(FreeMemoryGauge.class, "free_memory", new FreeMemoryGauge());
|
||||
Metrics.newGauge(NetworkSentGauge.class, "bytes_sent", new NetworkSentGauge());
|
||||
Metrics.newGauge(NetworkReceivedGauge.class, "bytes_received", new NetworkReceivedGauge());
|
||||
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()) {
|
||||
GraphiteReporter.enable(15, TimeUnit.SECONDS,
|
||||
config.getGraphiteConfiguration().getHost(),
|
||||
config.getGraphiteConfiguration().getPort());
|
||||
GraphiteReporterFactory graphiteReporterFactory = new GraphiteReporterFactory();
|
||||
graphiteReporterFactory.setHost(config.getGraphiteConfiguration().getHost());
|
||||
graphiteReporterFactory.setPort(config.getGraphiteConfiguration().getPort());
|
||||
|
||||
GraphiteReporter graphiteReporter = (GraphiteReporter) graphiteReporterFactory.build(environment.metrics());
|
||||
graphiteReporter.start(15, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
if (config.getMetricsConfiguration().isEnabled()) {
|
||||
new JsonMetricsReporter("textsecure", Metrics.defaultRegistry(),
|
||||
new JsonMetricsReporter(environment.metrics(),
|
||||
config.getMetricsConfiguration().getToken(),
|
||||
config.getMetricsConfiguration().getHost())
|
||||
.start(60, TimeUnit.SECONDS);
|
||||
|
||||
@@ -16,29 +16,27 @@
|
||||
*/
|
||||
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.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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
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> {
|
||||
|
||||
private final Meter authenticationFailedMeter = Metrics.newMeter(AccountAuthenticator.class,
|
||||
"authentication", "failed",
|
||||
TimeUnit.MINUTES);
|
||||
|
||||
private final Meter authenticationSucceededMeter = Metrics.newMeter(AccountAuthenticator.class,
|
||||
"authentication", "succeeded",
|
||||
TimeUnit.MINUTES);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
private final Meter authenticationFailedMeter = metricRegistry.meter(name(getClass(), "authentication", "failed" ));
|
||||
private final Meter authenticationSucceededMeter = metricRegistry.meter(name(getClass(), "authentication", "succeeded"));
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AccountAuthenticator.class);
|
||||
|
||||
|
||||
@@ -25,13 +25,13 @@ import java.io.IOException;
|
||||
public class AuthorizationHeader {
|
||||
|
||||
private final String number;
|
||||
private final long accountId;
|
||||
private final long accountId;
|
||||
private final String password;
|
||||
|
||||
private AuthorizationHeader(String number, long accountId, String password) {
|
||||
this.number = number;
|
||||
this.number = number;
|
||||
this.accountId = accountId;
|
||||
this.password = password;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public static AuthorizationHeader fromUserAndPassword(String user, String password) throws InvalidAuthorizationHeaderException {
|
||||
|
||||
@@ -16,30 +16,35 @@
|
||||
*/
|
||||
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.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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.configuration.FederationConfiguration;
|
||||
import org.whispersystems.textsecuregcm.federation.FederatedPeer;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
|
||||
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> {
|
||||
|
||||
private final Meter authenticationFailedMeter = Metrics.newMeter(FederatedPeerAuthenticator.class,
|
||||
"authentication", "failed",
|
||||
TimeUnit.MINUTES);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
|
||||
private final Meter authenticationSucceededMeter = Metrics.newMeter(FederatedPeerAuthenticator.class,
|
||||
"authentication", "succeeded",
|
||||
TimeUnit.MINUTES);
|
||||
private final Meter authenticationFailedMeter = metricRegistry.meter(name(getClass(),
|
||||
"authentication",
|
||||
"failed"));
|
||||
|
||||
private final Meter authenticationSucceededMeter = metricRegistry.meter(name(getClass(),
|
||||
"authentication",
|
||||
"succeeded"));
|
||||
|
||||
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.spi.inject.Injectable;
|
||||
import com.sun.jersey.spi.inject.InjectableProvider;
|
||||
import com.yammer.dropwizard.auth.Auth;
|
||||
import com.yammer.dropwizard.auth.Authenticator;
|
||||
import com.yammer.dropwizard.auth.basic.BasicAuthProvider;
|
||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
import io.dropwizard.auth.Authenticator;
|
||||
import io.dropwizard.auth.basic.BasicAuthProvider;
|
||||
import io.dropwizard.auth.basic.BasicCredentials;
|
||||
|
||||
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<T2> provider2;
|
||||
|
||||
@@ -44,8 +41,8 @@ public class MultiBasicAuthProvider<T1,T2> implements InjectableProvider<Auth, P
|
||||
Class<?> clazz2,
|
||||
String realm)
|
||||
{
|
||||
this.provider1 = new BasicAuthProvider<T1>(authenticator1, realm);
|
||||
this.provider2 = new BasicAuthProvider<T2>(authenticator2, realm);
|
||||
this.provider1 = new BasicAuthProvider<>(authenticator1, realm);
|
||||
this.provider2 = new BasicAuthProvider<>(authenticator2, realm);
|
||||
this.clazz1 = clazz1;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,9 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.auth.AuthenticationCredentials;
|
||||
@@ -55,6 +54,8 @@ import java.io.IOException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/accounts")
|
||||
public class AccountController {
|
||||
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.amazonaws.HttpMethod;
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.AttachmentDescriptor;
|
||||
@@ -44,6 +43,8 @@ import java.net.URL;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
|
||||
@Path("/v1/attachments")
|
||||
public class AttachmentController {
|
||||
|
||||
@@ -16,10 +16,9 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
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.LoggerFactory;
|
||||
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.limits.RateLimiters;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.Device;
|
||||
import org.whispersystems.textsecuregcm.storage.PendingDevicesManager;
|
||||
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||
|
||||
@@ -48,6 +47,8 @@ import javax.ws.rs.core.Response;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/devices")
|
||||
public class DeviceController {
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
*/
|
||||
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.yammer.dropwizard.auth.Auth;
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.annotation.Timed;
|
||||
import com.yammer.metrics.core.Histogram;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.ClientContact;
|
||||
@@ -30,6 +30,7 @@ import org.whispersystems.textsecuregcm.limits.RateLimiters;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.DirectoryManager;
|
||||
import org.whispersystems.textsecuregcm.util.Base64;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.Consumes;
|
||||
@@ -45,11 +46,15 @@ import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/directory")
|
||||
public class DirectoryController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(DirectoryController.class);
|
||||
private final Histogram contactsHistogram = Metrics.newHistogram(DirectoryController.class, "contacts");
|
||||
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 DirectoryManager directory;
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.AccountCount;
|
||||
@@ -45,6 +44,8 @@ import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/federation")
|
||||
public class FederationController {
|
||||
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||
@@ -47,6 +46,8 @@ import javax.ws.rs.core.Response;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/keys")
|
||||
public class KeysController {
|
||||
|
||||
|
||||
@@ -16,10 +16,9 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import com.codahale.metrics.annotation.Timed;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.yammer.dropwizard.auth.Auth;
|
||||
import com.yammer.metrics.annotation.Timed;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.IncomingMessage;
|
||||
@@ -56,6 +55,8 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import io.dropwizard.auth.Auth;
|
||||
|
||||
@Path("/v1/messages")
|
||||
public class MessageController {
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class MismatchedDevicesException extends Exception {
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import org.whispersystems.textsecuregcm.federation.NoSuchPeerException;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,7 +25,7 @@ public class NoSuchUserException extends Exception {
|
||||
|
||||
public NoSuchUserException(String user) {
|
||||
super(user);
|
||||
missing = new LinkedList<String>();
|
||||
missing = new LinkedList<>();
|
||||
missing.add(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
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.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||
import org.whispersystems.textsecuregcm.entities.AcknowledgeWebsocketMessage;
|
||||
import org.whispersystems.textsecuregcm.entities.IncomingWebsocketMessage;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
@@ -22,111 +27,94 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
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 ObjectMapper mapper = new ObjectMapper();
|
||||
private static final Map<Long, String> pendingMessages = new HashMap<>();
|
||||
|
||||
private final AccountAuthenticator accountAuthenticator;
|
||||
private final StoredMessageManager storedMessageManager;
|
||||
private final PubSubManager pubSubManager;
|
||||
|
||||
private final Account account;
|
||||
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,
|
||||
PubSubManager pubSubManager,
|
||||
Account account)
|
||||
public WebsocketController(AccountAuthenticator accountAuthenticator,
|
||||
StoredMessageManager storedMessageManager,
|
||||
PubSubManager pubSubManager)
|
||||
{
|
||||
this.accountAuthenticator = accountAuthenticator;
|
||||
this.storedMessageManager = storedMessageManager;
|
||||
this.pubSubManager = pubSubManager;
|
||||
this.account = account;
|
||||
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
|
||||
public void onClose(int i, String s) {
|
||||
handleClose();
|
||||
public void onWebSocketConnect(Session session) {
|
||||
try {
|
||||
UpgradeRequest request = session.getUpgradeRequest();
|
||||
Map<String, String[]> parameters = request.getParameterMap();
|
||||
String[] usernames = parameters.get("login" );
|
||||
String[] passwords = parameters.get("password");
|
||||
|
||||
if (usernames == null || usernames.length == 0 ||
|
||||
passwords == null || passwords.length == 0)
|
||||
{
|
||||
session.close(new CloseStatus(401, "Unauthorized"));
|
||||
return;
|
||||
}
|
||||
|
||||
BasicCredentials credentials = new BasicCredentials(usernames[0], passwords[0]);
|
||||
Optional<Account> account = accountAuthenticator.authenticate(credentials);
|
||||
|
||||
if (!account.isPresent()) {
|
||||
session.close(new CloseStatus(401, "Unauthorized"));
|
||||
return;
|
||||
}
|
||||
|
||||
this.account = account.get();
|
||||
this.device = account.get().getAuthenticatedDevice().get();
|
||||
this.session = session;
|
||||
|
||||
this.session.setIdleTimeout(10 * 60 * 1000);
|
||||
this.pubSubManager.subscribe(new WebsocketAddress(this.account.getId(),
|
||||
this.device.getId()), this);
|
||||
|
||||
handleQueryDatabase();
|
||||
} catch (AuthenticationException e) {
|
||||
try { session.close(500, "Server Error");} catch (IOException e1) {}
|
||||
} catch (IOException ioe) {
|
||||
logger.info("Abrupt session close.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String body) {
|
||||
public void onWebSocketText(String body) {
|
||||
try {
|
||||
IncomingWebsocketMessage incomingMessage = mapper.readValue(body, IncomingWebsocketMessage.class);
|
||||
|
||||
switch (incomingMessage.getType()) {
|
||||
case IncomingWebsocketMessage.TYPE_ACKNOWLEDGE_MESSAGE: handleMessageAck(body); break;
|
||||
case IncomingWebsocketMessage.TYPE_PING_MESSAGE: handlePing(); break;
|
||||
default: handleClose(); break;
|
||||
case IncomingWebsocketMessage.TYPE_ACKNOWLEDGE_MESSAGE:
|
||||
handleMessageAck(body);
|
||||
break;
|
||||
default:
|
||||
close(new CloseStatus(410, "Unknown Type"));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.debug("Parse", e);
|
||||
handleClose();
|
||||
close(new CloseStatus(410, "Badly Formatted"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPubSubMessage(PubSubMessage outgoingMessage) {
|
||||
switch (outgoingMessage.getType()) {
|
||||
case PubSubMessage.TYPE_DELIVER: handleDeliverOutgoingMessage(outgoingMessage.getContents()); break;
|
||||
case PubSubMessage.TYPE_QUERY_DB: handleQueryDatabase(); break;
|
||||
default:
|
||||
logger.warn("Unknown pubsub message: " + outgoingMessage.getType());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDeliverOutgoingMessage(String message) {
|
||||
try {
|
||||
long messageSequence;
|
||||
|
||||
synchronized (pendingMessages) {
|
||||
messageSequence = pendingMessageSequence++;
|
||||
pendingMessages.put(messageSequence, message);
|
||||
}
|
||||
|
||||
connection.sendMessage(mapper.writeValueAsString(new WebsocketMessage(messageSequence, message)));
|
||||
} catch (IOException e) {
|
||||
logger.debug("Response failed", e);
|
||||
handleClose();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessageAck(String message) {
|
||||
try {
|
||||
AcknowledgeWebsocketMessage ack = mapper.readValue(message, AcknowledgeWebsocketMessage.class);
|
||||
|
||||
synchronized (pendingMessages) {
|
||||
pendingMessages.remove(ack.getId());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.warn("Mapping", e);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
public void onWebSocketClose(int i, String s) {
|
||||
pubSubManager.unsubscribe(new WebsocketAddress(account.getId(), device.getId()), this);
|
||||
connection.close();
|
||||
|
||||
List<String> remainingMessages = new LinkedList<>();
|
||||
|
||||
@@ -144,6 +132,50 @@ public class WebsocketController implements WebSocket.OnTextMessage, PubSubListe
|
||||
storedMessageManager.storeMessages(account, device, remainingMessages);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPubSubMessage(PubSubMessage outgoingMessage) {
|
||||
switch (outgoingMessage.getType()) {
|
||||
case PubSubMessage.TYPE_DELIVER:
|
||||
handleDeliverOutgoingMessage(outgoingMessage.getContents());
|
||||
break;
|
||||
case PubSubMessage.TYPE_QUERY_DB:
|
||||
handleQueryDatabase();
|
||||
break;
|
||||
default:
|
||||
logger.warn("Unknown pubsub message: " + outgoingMessage.getType());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDeliverOutgoingMessage(String message) {
|
||||
try {
|
||||
long messageSequence;
|
||||
|
||||
synchronized (pendingMessages) {
|
||||
messageSequence = pendingMessageSequence++;
|
||||
pendingMessages.put(messageSequence, message);
|
||||
}
|
||||
|
||||
WebsocketMessage websocketMessage = new WebsocketMessage(messageSequence, message);
|
||||
session.getRemote().sendStringByFuture(mapper.writeValueAsString(websocketMessage));
|
||||
} catch (IOException e) {
|
||||
logger.debug("Response failed", e);
|
||||
close(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessageAck(String message) {
|
||||
try {
|
||||
AcknowledgeWebsocketMessage ack = mapper.readValue(message, AcknowledgeWebsocketMessage.class);
|
||||
|
||||
synchronized (pendingMessages) {
|
||||
pendingMessages.remove(ack.getId());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.warn("Mapping", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleQueryDatabase() {
|
||||
List<String> messages = storedMessageManager.getOutgoingMessages(account, device);
|
||||
|
||||
@@ -152,4 +184,25 @@ public class WebsocketController implements WebSocket.OnTextMessage, PubSubListe
|
||||
}
|
||||
}
|
||||
|
||||
@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,23 +1,18 @@
|
||||
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.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.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 {
|
||||
public class WebsocketControllerFactory extends WebSocketServlet implements WebSocketCreator {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(WebsocketControllerFactory.class);
|
||||
|
||||
@@ -25,14 +20,6 @@ public class WebsocketControllerFactory extends WebSocketServlet {
|
||||
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)
|
||||
@@ -43,56 +30,12 @@ public class WebsocketControllerFactory extends WebSocketServlet {
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
public void configure(WebSocketServletFactory factory) {
|
||||
factory.setCreator(this);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
public Object createWebSocket(UpgradeRequest upgradeRequest, UpgradeResponse upgradeResponse) {
|
||||
return new WebsocketController(accountAuthenticator, storedMessageManager, pubSubManager);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,14 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.limits;
|
||||
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.core.Meter;
|
||||
import com.codahale.metrics.Meter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.SharedMetricRegistries;
|
||||
import net.spy.memcached.MemcachedClient;
|
||||
|
||||
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 {
|
||||
|
||||
@@ -35,7 +36,9 @@ public class RateLimiter {
|
||||
public RateLimiter(MemcachedClient memcachedClient, String name,
|
||||
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.name = name;
|
||||
this.bucketSize = bucketSize;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package org.whispersystems.textsecuregcm.metrics;
|
||||
|
||||
import com.codahale.metrics.Gauge;
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
import com.yammer.metrics.core.Gauge;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class CpuUsageGauge extends Gauge<Integer> {
|
||||
public class CpuUsageGauge implements Gauge<Integer> {
|
||||
@Override
|
||||
public Integer value() {
|
||||
public Integer getValue() {
|
||||
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
|
||||
ManagementFactory.getOperatingSystemMXBean();
|
||||
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
package org.whispersystems.textsecuregcm.metrics;
|
||||
|
||||
import com.codahale.metrics.Gauge;
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
import com.yammer.metrics.core.Gauge;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class FreeMemoryGauge extends Gauge<Long> {
|
||||
public class FreeMemoryGauge implements Gauge<Long> {
|
||||
|
||||
@Override
|
||||
public Long value() {
|
||||
public Long getValue() {
|
||||
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
|
||||
ManagementFactory.getOperatingSystemMXBean();
|
||||
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
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.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 com.yammer.metrics.core.Clock;
|
||||
import com.yammer.metrics.core.Counter;
|
||||
import com.yammer.metrics.core.Gauge;
|
||||
import com.yammer.metrics.core.Histogram;
|
||||
import com.yammer.metrics.core.Metered;
|
||||
import com.yammer.metrics.core.Metric;
|
||||
import com.yammer.metrics.core.MetricName;
|
||||
import com.yammer.metrics.core.MetricProcessor;
|
||||
import com.yammer.metrics.core.MetricsRegistry;
|
||||
import com.yammer.metrics.core.Sampling;
|
||||
import com.yammer.metrics.core.Summarizable;
|
||||
import com.yammer.metrics.core.Timer;
|
||||
import com.yammer.metrics.core.VirtualMachineMetrics;
|
||||
import com.yammer.metrics.reporting.AbstractPollingReporter;
|
||||
import com.yammer.metrics.stats.Snapshot;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -33,292 +27,133 @@ import java.util.regex.Pattern;
|
||||
/**
|
||||
* Adapted from MetricsServlet.
|
||||
*/
|
||||
public class JsonMetricsReporter extends AbstractPollingReporter implements MetricProcessor<JsonMetricsReporter.Context> {
|
||||
private final Clock clock = Clock.defaultClock();
|
||||
private final VirtualMachineMetrics vm = VirtualMachineMetrics.getInstance();
|
||||
private final String service;
|
||||
private final MetricsRegistry registry;
|
||||
public class JsonMetricsReporter extends ScheduledReporter {
|
||||
private final JsonFactory factory = new JsonFactory();
|
||||
|
||||
private final String table;
|
||||
private final String sunnylabsHost;
|
||||
private final String host;
|
||||
|
||||
private final boolean includeVMMetrics;
|
||||
|
||||
public JsonMetricsReporter(String service, MetricsRegistry registry, String token, String sunnylabsHost) throws UnknownHostException {
|
||||
this(service, registry, token, sunnylabsHost, true);
|
||||
}
|
||||
|
||||
public JsonMetricsReporter(String service, MetricsRegistry registry, String token, String sunnylabsHost, boolean includeVMMetrics) throws UnknownHostException {
|
||||
super(registry, "jsonmetrics-reporter");
|
||||
this.service = service;
|
||||
this.registry = registry;
|
||||
this.table = token;
|
||||
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();
|
||||
this.includeVMMetrics = includeVMMetrics;
|
||||
this.host = InetAddress.getLocalHost().getHostName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void report(SortedMap<String, Gauge> stringGaugeSortedMap,
|
||||
SortedMap<String, Counter> stringCounterSortedMap,
|
||||
SortedMap<String, Histogram> stringHistogramSortedMap,
|
||||
SortedMap<String, Meter> stringMeterSortedMap,
|
||||
SortedMap<String, Timer> stringTimerSortedMap)
|
||||
{
|
||||
try {
|
||||
URL http = new URL("https", sunnylabsHost, 443, "/report/metrics?t=" + table + "&h=" + host);
|
||||
System.out.println("Reporting started to: " + http);
|
||||
HttpURLConnection urlc = (HttpURLConnection) http.openConnection();
|
||||
urlc.setDoOutput(true);
|
||||
urlc.addRequestProperty("Content-Type", "application/json");
|
||||
OutputStream outputStream = urlc.getOutputStream();
|
||||
writeJson(outputStream);
|
||||
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();
|
||||
System.out.println("Reporting complete: " + urlc.getResponseCode());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static final class Context {
|
||||
final boolean showFullSamples;
|
||||
final JsonGenerator json;
|
||||
|
||||
Context(JsonGenerator json, boolean showFullSamples) {
|
||||
this.json = json;
|
||||
this.showFullSamples = showFullSamples;
|
||||
}
|
||||
}
|
||||
|
||||
public void writeJson(OutputStream out) throws IOException {
|
||||
final JsonGenerator json = factory.createGenerator(out, JsonEncoding.UTF8);
|
||||
json.writeStartObject();
|
||||
if (includeVMMetrics) {
|
||||
writeVmMetrics(json);
|
||||
}
|
||||
writeRegularMetrics(json, false);
|
||||
json.writeEndObject();
|
||||
json.close();
|
||||
}
|
||||
|
||||
private void writeVmMetrics(JsonGenerator json) throws IOException {
|
||||
json.writeFieldName(service);
|
||||
json.writeStartObject();
|
||||
json.writeFieldName("jvm");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeFieldName("vm");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeStringField("name", vm.name());
|
||||
json.writeStringField("version", vm.version());
|
||||
}
|
||||
json.writeEndObject();
|
||||
|
||||
json.writeFieldName("memory");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeNumberField("totalInit", vm.totalInit());
|
||||
json.writeNumberField("totalUsed", vm.totalUsed());
|
||||
json.writeNumberField("totalMax", vm.totalMax());
|
||||
json.writeNumberField("totalCommitted", vm.totalCommitted());
|
||||
|
||||
json.writeNumberField("heapInit", vm.heapInit());
|
||||
json.writeNumberField("heapUsed", vm.heapUsed());
|
||||
json.writeNumberField("heapMax", vm.heapMax());
|
||||
json.writeNumberField("heapCommitted", vm.heapCommitted());
|
||||
|
||||
json.writeNumberField("heap_usage", vm.heapUsage());
|
||||
json.writeNumberField("non_heap_usage", vm.nonHeapUsage());
|
||||
json.writeFieldName("memory_pool_usages");
|
||||
json.writeStartObject();
|
||||
{
|
||||
for (Map.Entry<String, Double> pool : vm.memoryPoolUsage().entrySet()) {
|
||||
json.writeNumberField(pool.getKey(), pool.getValue());
|
||||
}
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndObject();
|
||||
|
||||
final Map<String, VirtualMachineMetrics.BufferPoolStats> bufferPoolStats = vm.getBufferPoolStats();
|
||||
if (!bufferPoolStats.isEmpty()) {
|
||||
json.writeFieldName("buffers");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeFieldName("direct");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeNumberField("count", bufferPoolStats.get("direct").getCount());
|
||||
json.writeNumberField("memoryUsed", bufferPoolStats.get("direct").getMemoryUsed());
|
||||
json.writeNumberField("totalCapacity", bufferPoolStats.get("direct").getTotalCapacity());
|
||||
}
|
||||
json.writeEndObject();
|
||||
|
||||
json.writeFieldName("mapped");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeNumberField("count", bufferPoolStats.get("mapped").getCount());
|
||||
json.writeNumberField("memoryUsed", bufferPoolStats.get("mapped").getMemoryUsed());
|
||||
json.writeNumberField("totalCapacity", bufferPoolStats.get("mapped").getTotalCapacity());
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
|
||||
json.writeNumberField("daemon_thread_count", vm.daemonThreadCount());
|
||||
json.writeNumberField("thread_count", vm.threadCount());
|
||||
json.writeNumberField("current_time", clock.time());
|
||||
json.writeNumberField("uptime", vm.uptime());
|
||||
json.writeNumberField("fd_usage", vm.fileDescriptorUsage());
|
||||
|
||||
json.writeFieldName("thread-states");
|
||||
json.writeStartObject();
|
||||
{
|
||||
for (Map.Entry<Thread.State, Double> entry : vm.threadStatePercentages()
|
||||
.entrySet()) {
|
||||
json.writeNumberField(entry.getKey().toString().toLowerCase(),
|
||||
entry.getValue());
|
||||
}
|
||||
}
|
||||
json.writeEndObject();
|
||||
|
||||
json.writeFieldName("garbage-collectors");
|
||||
json.writeStartObject();
|
||||
{
|
||||
for (Map.Entry<String, VirtualMachineMetrics.GarbageCollectorStats> entry : vm.garbageCollectors()
|
||||
.entrySet()) {
|
||||
json.writeFieldName(entry.getKey());
|
||||
json.writeStartObject();
|
||||
{
|
||||
final VirtualMachineMetrics.GarbageCollectorStats gc = entry.getValue();
|
||||
json.writeNumberField("runs", gc.getRuns());
|
||||
json.writeNumberField("time", gc.getTime(TimeUnit.MILLISECONDS));
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
json.writeEndObject();
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
public void writeRegularMetrics(JsonGenerator json, boolean showFullSamples) throws IOException {
|
||||
for (Map.Entry<String, SortedMap<MetricName, Metric>> entry : registry.groupedMetrics().entrySet()) {
|
||||
for (Map.Entry<MetricName, Metric> subEntry : entry.getValue().entrySet()) {
|
||||
json.writeFieldName(sanitize(subEntry.getKey()));
|
||||
try {
|
||||
subEntry.getValue()
|
||||
.processWith(this,
|
||||
subEntry.getKey(),
|
||||
new Context(json, showFullSamples));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processHistogram(MetricName name, Histogram histogram, Context context) throws Exception {
|
||||
final JsonGenerator json = context.json;
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeNumberField("count", histogram.count());
|
||||
writeSummarizable(histogram, json);
|
||||
writeSampling(histogram, json);
|
||||
if (context.showFullSamples) {
|
||||
json.writeObjectField("values", histogram.getSnapshot().getValues());
|
||||
}
|
||||
histogram.clear();
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCounter(MetricName name, Counter counter, Context context) throws Exception {
|
||||
final JsonGenerator json = context.json;
|
||||
json.writeNumber(counter.count());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processGauge(MetricName name, Gauge<?> gauge, Context context) throws Exception {
|
||||
final JsonGenerator json = context.json;
|
||||
public void reportGauge(JsonGenerator json, String name, Gauge gauge) throws IOException {
|
||||
json.writeFieldName(sanitize(name));
|
||||
json.writeObject(evaluateGauge(gauge));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processMeter(MetricName name, Metered meter, Context context) throws Exception {
|
||||
final JsonGenerator json = context.json;
|
||||
public void reportCounter(JsonGenerator json, String name, Counter counter) throws IOException {
|
||||
json.writeFieldName(sanitize(name));
|
||||
json.writeNumber(counter.getCount());
|
||||
}
|
||||
|
||||
public void reportHistogram(JsonGenerator json, String name, Histogram histogram) throws IOException {
|
||||
Snapshot snapshot = histogram.getSnapshot();
|
||||
json.writeFieldName(sanitize(name));
|
||||
json.writeStartObject();
|
||||
{
|
||||
writeMeteredFields(meter, json);
|
||||
}
|
||||
json.writeNumberField("count", histogram.getCount());
|
||||
writeSnapshot(json, snapshot);
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processTimer(MetricName name, Timer timer, Context context) throws Exception {
|
||||
final JsonGenerator json = context.json;
|
||||
public void reportMeter(JsonGenerator json, String name, Meter meter) throws IOException {
|
||||
json.writeFieldName(sanitize(name));
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeFieldName("duration");
|
||||
json.writeStartObject();
|
||||
{
|
||||
json.writeStringField("unit", timer.durationUnit().toString().toLowerCase());
|
||||
writeSummarizable(timer, json);
|
||||
writeSampling(timer, json);
|
||||
if (context.showFullSamples) {
|
||||
json.writeObjectField("values", timer.getSnapshot().getValues());
|
||||
}
|
||||
}
|
||||
json.writeEndObject();
|
||||
json.writeNumberField("count", meter.getCount());
|
||||
json.writeNumberField("mean", meter.getMeanRate());
|
||||
json.writeNumberField("m1", meter.getOneMinuteRate());
|
||||
json.writeNumberField("m5", meter.getFiveMinuteRate());
|
||||
json.writeNumberField("m15", meter.getFifteenMinuteRate());
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
json.writeFieldName("rate");
|
||||
json.writeStartObject();
|
||||
{
|
||||
writeMeteredFields(timer, json);
|
||||
}
|
||||
json.writeEndObject();
|
||||
}
|
||||
public void reportTimer(JsonGenerator json, String name, Timer timer) throws IOException {
|
||||
json.writeFieldName(sanitize(name));
|
||||
json.writeStartObject();
|
||||
json.writeNumberField("count", timer.getCount());
|
||||
writeSnapshot(json, timer.getSnapshot());
|
||||
json.writeEndObject();
|
||||
}
|
||||
|
||||
private static Object evaluateGauge(Gauge<?> gauge) {
|
||||
try {
|
||||
return gauge.value();
|
||||
return gauge.getValue();
|
||||
} catch (RuntimeException e) {
|
||||
return "error reading gauge: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeSummarizable(Summarizable metric, JsonGenerator json) throws IOException {
|
||||
json.writeNumberField("min", metric.min());
|
||||
json.writeNumberField("max", metric.max());
|
||||
json.writeNumberField("mean", metric.mean());
|
||||
}
|
||||
|
||||
private static void writeSampling(Sampling metric, JsonGenerator json) throws IOException {
|
||||
final Snapshot snapshot = metric.getSnapshot();
|
||||
private static void writeSnapshot(JsonGenerator json, Snapshot snapshot) throws IOException {
|
||||
json.writeNumberField("max", snapshot.getMax());
|
||||
json.writeNumberField("mean", snapshot.getMean());
|
||||
json.writeNumberField("min", snapshot.getMin());
|
||||
json.writeNumberField("stddev", snapshot.getStdDev());
|
||||
json.writeNumberField("median", snapshot.getMedian());
|
||||
json.writeNumberField("p75", snapshot.get75thPercentile());
|
||||
json.writeNumberField("p95", snapshot.get95thPercentile());
|
||||
json.writeNumberField("p98", snapshot.get98thPercentile());
|
||||
json.writeNumberField("p99", snapshot.get99thPercentile());
|
||||
json.writeNumberField("p999", snapshot.get999thPercentile());
|
||||
}
|
||||
|
||||
private static void writeMeteredFields(Metered metered, JsonGenerator json) throws IOException {
|
||||
json.writeNumberField("count", metered.count());
|
||||
json.writeNumberField("mean", metered.meanRate());
|
||||
json.writeNumberField("m1", metered.oneMinuteRate());
|
||||
json.writeNumberField("m5", metered.fiveMinuteRate());
|
||||
json.writeNumberField("m15", metered.fifteenMinuteRate());
|
||||
}
|
||||
|
||||
private static final Pattern SIMPLE_NAMES = Pattern.compile("[^a-zA-Z0-9_.\\-~]");
|
||||
|
||||
private String sanitize(MetricName metricName) {
|
||||
return SIMPLE_NAMES.matcher(metricName.getGroup() + "." + metricName.getName()).replaceAll("_");
|
||||
private String sanitize(String metricName) {
|
||||
return SIMPLE_NAMES.matcher(metricName).replaceAll("_");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
package org.whispersystems.textsecuregcm.metrics;
|
||||
|
||||
|
||||
import com.yammer.metrics.core.Gauge;
|
||||
import com.codahale.metrics.Gauge;
|
||||
import org.whispersystems.textsecuregcm.util.Pair;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class NetworkGauge extends Gauge<Long> {
|
||||
public abstract class NetworkGauge implements Gauge<Long> {
|
||||
|
||||
protected Pair<Long, Long> getSentReceived() throws IOException {
|
||||
File proc = new File("/proc/net/dev");
|
||||
|
||||
@@ -14,7 +14,7 @@ public class NetworkReceivedGauge extends NetworkGauge {
|
||||
private long lastReceived;
|
||||
|
||||
@Override
|
||||
public Long value() {
|
||||
public Long getValue() {
|
||||
try {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
Pair<Long, Long> sentAndReceived = getSentReceived();
|
||||
|
||||
@@ -14,7 +14,7 @@ public class NetworkSentGauge extends NetworkGauge {
|
||||
private long lastSent;
|
||||
|
||||
@Override
|
||||
public Long value() {
|
||||
public Long getValue() {
|
||||
try {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
Pair<Long, Long> sentAndReceived = getSentReceived();
|
||||
|
||||
@@ -16,8 +16,7 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.providers;
|
||||
|
||||
import com.yammer.metrics.core.HealthCheck;
|
||||
import com.yammer.metrics.core.HealthCheck.Result;
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
import net.spy.memcached.MemcachedClient;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
@@ -27,7 +26,6 @@ public class MemcacheHealthCheck extends HealthCheck {
|
||||
private final MemcachedClient client;
|
||||
|
||||
public MemcacheHealthCheck(MemcachedClient client) {
|
||||
super("memcached");
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
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.JedisPool;
|
||||
@@ -26,7 +26,6 @@ public class RedisHealthCheck extends HealthCheck {
|
||||
private final JedisPool clientPool;
|
||||
|
||||
public RedisHealthCheck(JedisPool clientPool) {
|
||||
super("redis");
|
||||
this.clientPool = clientPool;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,17 +16,19 @@
|
||||
*/
|
||||
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.notnoop.apns.APNS;
|
||||
import com.notnoop.apns.ApnsService;
|
||||
import com.notnoop.exceptions.NetworkIOException;
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.core.Meter;
|
||||
import org.bouncycastle.openssl.PEMReader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.entities.CryptoEncodingException;
|
||||
import org.whispersystems.textsecuregcm.entities.EncryptedOutgoingMessage;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -40,13 +42,15 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
|
||||
public class APNSender {
|
||||
|
||||
private final Meter success = Metrics.newMeter(APNSender.class, "sent", "success", TimeUnit.MINUTES);
|
||||
private final Meter failure = Metrics.newMeter(APNSender.class, "sent", "failure", TimeUnit.MINUTES);
|
||||
private final Logger logger = LoggerFactory.getLogger(APNSender.class);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
private final Meter success = metricRegistry.meter(name(getClass(), "sent", "success"));
|
||||
private final Meter failure = metricRegistry.meter(name(getClass(), "sent", "failure"));
|
||||
private final Logger logger = LoggerFactory.getLogger(APNSender.class);
|
||||
|
||||
private static final String MESSAGE_BODY = "m";
|
||||
|
||||
|
||||
@@ -16,22 +16,25 @@
|
||||
*/
|
||||
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.Message;
|
||||
import com.google.android.gcm.server.Result;
|
||||
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 java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
|
||||
public class GCMSender {
|
||||
|
||||
private final Meter success = Metrics.newMeter(GCMSender.class, "sent", "success", TimeUnit.MINUTES);
|
||||
private final Meter failure = Metrics.newMeter(GCMSender.class, "sent", "failure", TimeUnit.MINUTES);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(org.whispersystems.textsecuregcm.util.Constants.METRICS_NAME);
|
||||
private final Meter success = metricRegistry.meter(name(getClass(), "sent", "success"));
|
||||
private final Meter failure = metricRegistry.meter(name(getClass(), "sent", "failure"));
|
||||
|
||||
private final Sender sender;
|
||||
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.sms;
|
||||
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.core.Meter;
|
||||
import com.codahale.metrics.Meter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.SharedMetricRegistries;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@@ -28,13 +30,15 @@ import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
|
||||
public class NexmoSmsSender {
|
||||
|
||||
private final Meter smsMeter = Metrics.newMeter(NexmoSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
|
||||
private final Meter voxMeter = Metrics.newMeter(NexmoSmsSender.class, "vox", "delivered", TimeUnit.MINUTES);
|
||||
private final Logger logger = LoggerFactory.getLogger(NexmoSmsSender.class);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
private final Meter smsMeter = metricRegistry.meter(name(getClass(), "sms", "delivered"));
|
||||
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 =
|
||||
"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;
|
||||
|
||||
import com.codahale.metrics.Meter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.SharedMetricRegistries;
|
||||
import com.twilio.sdk.TwilioRestClient;
|
||||
import com.twilio.sdk.TwilioRestException;
|
||||
import com.twilio.sdk.resource.factory.CallFactory;
|
||||
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.message.BasicNameValuePair;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||
import org.whispersystems.textsecuregcm.util.Constants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.codahale.metrics.MetricRegistry.name;
|
||||
|
||||
public class TwilioSmsSender {
|
||||
|
||||
@@ -40,8 +43,9 @@ public class TwilioSmsSender {
|
||||
" <Say voice=\"woman\" language=\"en\">" + SmsSender.VOX_VERIFICATION_TEXT + "%s</Say>\n" +
|
||||
"</Response>";
|
||||
|
||||
private final Meter smsMeter = Metrics.newMeter(TwilioSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
|
||||
private final Meter voxMeter = Metrics.newMeter(TwilioSmsSender.class, "vox", "delivered", TimeUnit.MINUTES);
|
||||
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||
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 accountToken;
|
||||
|
||||
@@ -134,6 +134,7 @@ public class PubSubManager {
|
||||
public void onSubscribe(String channel, int count) {
|
||||
try {
|
||||
WebsocketAddress address = new WebsocketAddress(channel);
|
||||
|
||||
if (address.getAccountId() == 0 && address.getDeviceId() == 0) {
|
||||
synchronized (PubSubManager.this) {
|
||||
subscribed = true;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String METRICS_NAME = "textsecure";
|
||||
|
||||
}
|
||||
@@ -16,13 +16,6 @@
|
||||
*/
|
||||
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.spy.memcached.MemcachedClient;
|
||||
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.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;
|
||||
|
||||
public class DirectoryCommand extends ConfiguredCommand<WhisperServerConfiguration> {
|
||||
@@ -53,8 +53,8 @@ public class DirectoryCommand extends ConfiguredCommand<WhisperServerConfigurati
|
||||
throws Exception
|
||||
{
|
||||
try {
|
||||
DatabaseConfiguration dbConfig = config.getDatabaseConfiguration();
|
||||
DBI dbi = new DBI(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPassword());
|
||||
DataSourceFactory dbConfig = config.getDataSourceFactory();
|
||||
DBI dbi = new DBI(dbConfig.getUrl(), dbConfig.getUser(), dbConfig.getPassword());
|
||||
|
||||
dbi.registerArgumentFactory(new OptionalArgumentFactory(dbConfig.getDriverClass()));
|
||||
dbi.registerContainerFactory(new ImmutableListContainerFactory());
|
||||
|
||||
Reference in New Issue
Block a user