mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-26 15:13:18 +01:00
Squashed History
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Copyright (C) 2013 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.sms;
|
||||
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.core.Meter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.whispersystems.textsecuregcm.sms.SenderFactory.VoxSender;
|
||||
import org.whispersystems.textsecuregcm.sms.SenderFactory.SmsSender;
|
||||
|
||||
public class NexmoSmsSender implements SmsSender, VoxSender {
|
||||
|
||||
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 static final String NEXMO_SMS_URL =
|
||||
"https://rest.nexmo.com/sms/json?api_key=%s&api_secret=%s&from=%s&to=%s&text=%s";
|
||||
|
||||
private static final String NEXMO_VOX_URL =
|
||||
"https://rest.nexmo.com/tts/json?api_key=%s&api_secret=%s&to=%s&text=%s";
|
||||
|
||||
private final String apiKey;
|
||||
private final String apiSecret;
|
||||
private final String number;
|
||||
|
||||
public NexmoSmsSender(NexmoConfiguration config) {
|
||||
this.apiKey = config.getApiKey();
|
||||
this.apiSecret = config.getApiSecret();
|
||||
this.number = config.getNumber();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliverSmsVerification(String destination, String verificationCode) throws IOException {
|
||||
URL url = new URL(String.format(NEXMO_SMS_URL, apiKey, apiSecret, number, destination,
|
||||
URLEncoder.encode(SmsSender.VERIFICATION_TEXT + verificationCode, "UTF-8")));
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
while (reader.readLine() != null) {}
|
||||
reader.close();
|
||||
smsMeter.mark();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliverVoxVerification(String destination, String message) throws IOException {
|
||||
URL url = new URL(String.format(NEXMO_VOX_URL, apiKey, apiSecret, destination,
|
||||
URLEncoder.encode(VoxSender.VERIFICATION_TEXT + message, "UTF-8")));
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
logger.debug(line);
|
||||
}
|
||||
reader.close();
|
||||
voxMeter.mark();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (C) 2013 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.sms;
|
||||
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import org.whispersystems.textsecuregcm.configuration.NexmoConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SenderFactory {
|
||||
|
||||
private final TwilioSmsSender twilioSender;
|
||||
private final Optional<NexmoSmsSender> nexmoSender;
|
||||
|
||||
public SenderFactory(TwilioConfiguration twilioConfig, NexmoConfiguration nexmoConfig) {
|
||||
this.twilioSender = new TwilioSmsSender(twilioConfig);
|
||||
|
||||
if (nexmoConfig != null) {
|
||||
this.nexmoSender = Optional.of(new NexmoSmsSender(nexmoConfig));
|
||||
} else {
|
||||
this.nexmoSender = Optional.absent();
|
||||
}
|
||||
}
|
||||
|
||||
public SmsSender getSmsSender(String number) {
|
||||
if (nexmoSender.isPresent() && !isTwilioDestination(number)) {
|
||||
return nexmoSender.get();
|
||||
} else {
|
||||
return twilioSender;
|
||||
}
|
||||
}
|
||||
|
||||
public VoxSender getVoxSender(String number) {
|
||||
if (nexmoSender.isPresent()) {
|
||||
return nexmoSender.get();
|
||||
}
|
||||
|
||||
throw new AssertionError("FIX ME!");
|
||||
}
|
||||
|
||||
private boolean isTwilioDestination(String number) {
|
||||
return number.length() == 12 && number.startsWith("+1");
|
||||
}
|
||||
|
||||
public interface SmsSender {
|
||||
public static final String VERIFICATION_TEXT = "Your TextSecure verification code: ";
|
||||
public void deliverSmsVerification(String destination, String verificationCode) throws IOException;
|
||||
}
|
||||
|
||||
public interface VoxSender {
|
||||
public static final String VERIFICATION_TEXT = "Your TextSecure verification code is: ";
|
||||
public void deliverVoxVerification(String destination, String verificationCode) throws IOException;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Copyright (C) 2013 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.sms;
|
||||
|
||||
import com.sun.jersey.core.util.Base64;
|
||||
import com.yammer.metrics.Metrics;
|
||||
import com.yammer.metrics.core.Meter;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TwilioSmsSender implements SenderFactory.SmsSender {
|
||||
|
||||
private final Meter smsMeter = Metrics.newMeter(TwilioSmsSender.class, "sms", "delivered", TimeUnit.MINUTES);
|
||||
|
||||
private static final String TWILIO_URL = "https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages";
|
||||
|
||||
private final String accountId;
|
||||
private final String accountToken;
|
||||
private final String number;
|
||||
|
||||
public TwilioSmsSender(TwilioConfiguration config) {
|
||||
this.accountId = config.getAccountId();
|
||||
this.accountToken = config.getAccountToken();
|
||||
this.number = config.getNumber();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliverSmsVerification(String destination, String verificationCode) throws IOException {
|
||||
URL url = new URL(String.format(TWILIO_URL, accountId));
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestProperty("Authorization", getTwilioAuthorizationHeader());
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
Map<String, String> formData = new HashMap<>();
|
||||
formData.put("From", number);
|
||||
formData.put("To", destination);
|
||||
formData.put("Body", VERIFICATION_TEXT + verificationCode);
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
|
||||
writer.write(Util.encodeFormParams(formData));
|
||||
writer.flush();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
while (reader.readLine() != null) {}
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
||||
smsMeter.mark();
|
||||
}
|
||||
|
||||
private String getTwilioAuthorizationHeader() {
|
||||
String encoded = new String(Base64.encode(String.format("%s:%s", accountId, accountToken)));
|
||||
return "Basic " + encoded.replace("\n", "");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user