mirror of
https://github.com/signalapp/Signal-Server
synced 2026-02-27 18:13:16 +00:00
Add configuration by country for sending from alpha IDs
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class TwilioAlphaIdConfiguration {
|
||||
@NotEmpty
|
||||
private String prefix;
|
||||
|
||||
@NotEmpty
|
||||
private String value;
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,9 @@ package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@@ -53,6 +53,10 @@ public class TwilioConfiguration {
|
||||
@Valid
|
||||
private RetryConfiguration retry = new RetryConfiguration();
|
||||
|
||||
@NotNull
|
||||
@Valid
|
||||
private List<TwilioAlphaIdConfiguration> alphaId;
|
||||
|
||||
public String getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
@@ -115,4 +119,13 @@ public class TwilioConfiguration {
|
||||
public void setRetry(RetryConfiguration retry) {
|
||||
this.retry = retry;
|
||||
}
|
||||
|
||||
public List<TwilioAlphaIdConfiguration> getAlphaId() {
|
||||
return alphaId;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setAlphaId(List<TwilioAlphaIdConfiguration> alphaId) {
|
||||
this.alphaId = alphaId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioAlphaIdConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||
import org.whispersystems.textsecuregcm.http.FaultTolerantHttpClient;
|
||||
import org.whispersystems.textsecuregcm.http.FormDataBodyPublisher;
|
||||
@@ -41,6 +42,7 @@ import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
@@ -60,12 +62,13 @@ public class TwilioSmsSender {
|
||||
private final Meter voxMeter = metricRegistry.meter(name(getClass(), "vox", "delivered"));
|
||||
private final Meter priceMeter = metricRegistry.meter(name(getClass(), "price"));
|
||||
|
||||
private final String accountId;
|
||||
private final String accountToken;
|
||||
private final ArrayList<String> numbers;
|
||||
private final String messagingServicesId;
|
||||
private final String localDomain;
|
||||
private final Random random;
|
||||
private final String accountId;
|
||||
private final String accountToken;
|
||||
private final ArrayList<String> numbers;
|
||||
private final String messagingServicesId;
|
||||
private final String localDomain;
|
||||
private final List<TwilioAlphaIdConfiguration> alphaId;
|
||||
private final Random random;
|
||||
|
||||
private final FaultTolerantHttpClient httpClient;
|
||||
private final URI smsUri;
|
||||
@@ -80,6 +83,7 @@ public class TwilioSmsSender {
|
||||
this.numbers = new ArrayList<>(twilioConfiguration.getNumbers());
|
||||
this.localDomain = twilioConfiguration.getLocalDomain();
|
||||
this.messagingServicesId = twilioConfiguration.getMessagingServicesId();
|
||||
this.alphaId = twilioConfiguration.getAlphaId();
|
||||
this.random = new Random(System.currentTimeMillis());
|
||||
this.smsUri = URI.create(baseUri + "/2010-04-01/Accounts/" + accountId + "/Messages.json");
|
||||
this.voxUri = URI.create(baseUri + "/2010-04-01/Accounts/" + accountId + "/Calls.json" );
|
||||
@@ -101,13 +105,7 @@ public class TwilioSmsSender {
|
||||
public CompletableFuture<Boolean> deliverArbitrarySms(String destination, String message) {
|
||||
Map<String, String> requestParameters = new HashMap<>();
|
||||
requestParameters.put("To", destination);
|
||||
|
||||
if (Util.isEmpty(messagingServicesId)) {
|
||||
requestParameters.put("From", getRandom(random, numbers));
|
||||
} else {
|
||||
requestParameters.put("MessagingServiceSid", messagingServicesId);
|
||||
}
|
||||
|
||||
setOriginationRequestParameter(destination, requestParameters);
|
||||
requestParameters.put("Body", message);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
@@ -127,12 +125,7 @@ public class TwilioSmsSender {
|
||||
public CompletableFuture<Boolean> deliverSmsVerification(String destination, Optional<String> clientType, String verificationCode) {
|
||||
Map<String, String> requestParameters = new HashMap<>();
|
||||
requestParameters.put("To", destination);
|
||||
|
||||
if (Util.isEmpty(messagingServicesId)) {
|
||||
requestParameters.put("From", getRandom(random, numbers));
|
||||
} else {
|
||||
requestParameters.put("MessagingServiceSid", messagingServicesId);
|
||||
}
|
||||
setOriginationRequestParameter(destination, requestParameters);
|
||||
|
||||
if ("ios".equals(clientType.orElse(null))) {
|
||||
requestParameters.put("Body", String.format(SmsSender.SMS_IOS_VERIFICATION_TEXT, verificationCode, verificationCode));
|
||||
@@ -182,6 +175,17 @@ public class TwilioSmsSender {
|
||||
.handle(this::processResponse);
|
||||
}
|
||||
|
||||
private void setOriginationRequestParameter(String destination, Map<String, String> requestParameters) {
|
||||
final Optional<TwilioAlphaIdConfiguration> alphaIdConfiguration = getAlphaIdConfigurationForNumber(destination);
|
||||
if (alphaIdConfiguration.isPresent()) {
|
||||
requestParameters.put("From", alphaIdConfiguration.get().getValue());
|
||||
} else if (Util.isEmpty(messagingServicesId)) {
|
||||
requestParameters.put("From", getRandom(random, numbers));
|
||||
} else {
|
||||
requestParameters.put("MessagingServiceSid", messagingServicesId);
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandom(Random random, ArrayList<String> elements) {
|
||||
return elements.get(random.nextInt(elements.size()));
|
||||
}
|
||||
@@ -220,6 +224,21 @@ public class TwilioSmsSender {
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<TwilioAlphaIdConfiguration> getAlphaIdConfigurationForNumber(String number) {
|
||||
if (alphaId == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final String countryCode = Util.getCountryCode(number);
|
||||
for (TwilioAlphaIdConfiguration twilioAlphaIdConfiguration : alphaId) {
|
||||
if (twilioAlphaIdConfiguration.getPrefix().equalsIgnoreCase(countryCode)) {
|
||||
return Optional.of(twilioAlphaIdConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static class TwilioResponse {
|
||||
|
||||
private TwilioSuccessResponse successResponse;
|
||||
|
||||
Reference in New Issue
Block a user