2014-09-16 23:21:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.ConnectivityManager;
|
2015-05-01 19:03:05 +00:00
|
|
|
import android.net.NetworkInfo;
|
2014-12-29 22:01:02 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2014-09-16 23:21:41 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2015-03-06 21:57:00 +00:00
|
|
|
import org.apache.http.Header;
|
2014-11-25 08:48:09 +00:00
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
|
import org.apache.http.client.CredentialsProvider;
|
2014-09-16 23:21:41 +00:00
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
import org.apache.http.impl.NoConnectionReuseStrategyHC4;
|
2014-11-25 08:48:09 +00:00
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
2014-09-16 23:21:41 +00:00
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.impl.client.LaxRedirectStrategy;
|
|
|
|
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
2015-03-06 21:57:00 +00:00
|
|
|
import org.apache.http.message.BasicHeader;
|
2014-09-16 23:21:41 +00:00
|
|
|
import org.thoughtcrime.securesms.database.ApnDatabase;
|
|
|
|
import org.thoughtcrime.securesms.util.TelephonyUtil;
|
2014-11-12 17:14:59 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Conversions;
|
2015-01-18 03:10:46 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-02-17 04:31:44 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2014-09-16 23:21:41 +00:00
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2015-05-01 19:03:05 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2014-09-16 23:21:41 +00:00
|
|
|
import java.net.InetAddress;
|
2014-11-25 08:48:09 +00:00
|
|
|
import java.net.URL;
|
2015-03-06 21:57:00 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-09-16 23:21:41 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public abstract class LegacyMmsConnection {
|
2015-01-18 03:10:46 +00:00
|
|
|
|
|
|
|
public static final String USER_AGENT = "Android-Mms/2.0";
|
|
|
|
|
2015-05-01 19:03:05 +00:00
|
|
|
private static final String TAG = LegacyMmsConnection.class.getSimpleName();
|
2014-09-16 23:21:41 +00:00
|
|
|
|
|
|
|
protected final Context context;
|
|
|
|
protected final Apn apn;
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
protected LegacyMmsConnection(Context context) throws ApnUnavailableException {
|
2014-09-16 23:21:41 +00:00
|
|
|
this.context = context;
|
2014-12-29 22:01:02 +00:00
|
|
|
this.apn = getApn(context);
|
2014-09-16 23:21:41 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
public static Apn getApn(Context context) throws ApnUnavailableException {
|
2015-02-17 04:31:44 +00:00
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
try {
|
2015-02-17 04:31:44 +00:00
|
|
|
Optional<Apn> params = ApnDatabase.getInstance(context)
|
|
|
|
.getMmsConnectionParameters(TelephonyUtil.getMccMnc(context),
|
|
|
|
TelephonyUtil.getApn(context));
|
2014-09-16 23:21:41 +00:00
|
|
|
|
2015-02-17 04:31:44 +00:00
|
|
|
if (!params.isPresent()) {
|
2014-09-16 23:21:41 +00:00
|
|
|
throw new ApnUnavailableException("No parameters available from ApnDefaults.");
|
|
|
|
}
|
|
|
|
|
2015-02-17 04:31:44 +00:00
|
|
|
return params.get();
|
2014-09-16 23:21:41 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
throw new ApnUnavailableException("ApnDatabase threw an IOException", ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
protected boolean isCdmaDevice() {
|
|
|
|
return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:03:05 +00:00
|
|
|
@SuppressWarnings("TryWithIdenticalCatches")
|
2014-09-16 23:21:41 +00:00
|
|
|
protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
InetAddress inetAddress = InetAddress.getByName(host);
|
|
|
|
if (!usingMmsRadio) {
|
|
|
|
if (inetAddress.isSiteLocalAddress()) {
|
|
|
|
throw new IOException("RFC1918 address in non-MMS radio situation!");
|
|
|
|
}
|
|
|
|
Log.w(TAG, "returning vacuous success since MMS radio is not in use");
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-16 07:28:52 +00:00
|
|
|
|
|
|
|
if (inetAddress == null) {
|
|
|
|
throw new IOException("Unable to lookup host: InetAddress.getByName() returned null.");
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
byte[] ipAddressBytes = inetAddress.getAddress();
|
2015-05-01 19:03:05 +00:00
|
|
|
if (ipAddressBytes == null) {
|
|
|
|
Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway.");
|
2014-09-16 23:21:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
|
|
|
|
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
2015-05-01 19:03:05 +00:00
|
|
|
try {
|
|
|
|
final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress", Integer.TYPE, InetAddress.class);
|
|
|
|
final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager, MmsRadio.TYPE_MOBILE_MMS, inetAddress);
|
|
|
|
Log.w(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained);
|
|
|
|
return routeToHostObtained;
|
|
|
|
} catch (NoSuchMethodException nsme) {
|
|
|
|
Log.w(TAG, nsme);
|
|
|
|
} catch (IllegalAccessException iae) {
|
|
|
|
Log.w(TAG, iae);
|
|
|
|
} catch (InvocationTargetException ite) {
|
|
|
|
Log.w(TAG, ite);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
|
|
|
|
final boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
|
|
|
|
Log.w(TAG, "requestRouteToHost(" + ipAddress + ") -> " + routeToHostObtained);
|
2014-09-16 23:21:41 +00:00
|
|
|
return routeToHostObtained;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static byte[] parseResponse(InputStream is) throws IOException {
|
|
|
|
InputStream in = new BufferedInputStream(is);
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
Util.copy(in, baos);
|
|
|
|
|
|
|
|
Log.w(TAG, "Received full server response, " + baos.size() + " bytes");
|
|
|
|
|
|
|
|
return baos.toByteArray();
|
|
|
|
}
|
|
|
|
|
2015-01-18 03:10:46 +00:00
|
|
|
protected CloseableHttpClient constructHttpClient() throws IOException {
|
2014-09-16 23:21:41 +00:00
|
|
|
RequestConfig config = RequestConfig.custom()
|
|
|
|
.setConnectTimeout(20 * 1000)
|
|
|
|
.setConnectionRequestTimeout(20 * 1000)
|
|
|
|
.setSocketTimeout(20 * 1000)
|
|
|
|
.setMaxRedirects(20)
|
|
|
|
.build();
|
|
|
|
|
2015-01-18 03:10:46 +00:00
|
|
|
URL mmsc = new URL(apn.getMmsc());
|
2014-11-25 08:48:09 +00:00
|
|
|
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
|
|
|
|
|
if (apn.hasAuthentication()) {
|
|
|
|
credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
|
|
|
|
new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
return HttpClients.custom()
|
|
|
|
.setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
|
|
|
|
.setRedirectStrategy(new LaxRedirectStrategy())
|
2015-01-18 03:10:46 +00:00
|
|
|
.setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
|
2014-09-16 23:21:41 +00:00
|
|
|
.setConnectionManager(new BasicHttpClientConnectionManager())
|
|
|
|
.setDefaultRequestConfig(config)
|
2014-11-25 08:48:09 +00:00
|
|
|
.setDefaultCredentialsProvider(credsProvider)
|
2014-09-16 23:21:41 +00:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
protected byte[] execute(HttpUriRequest request) throws IOException {
|
|
|
|
Log.w(TAG, "connecting to " + apn.getMmsc());
|
2014-09-16 23:21:41 +00:00
|
|
|
|
|
|
|
CloseableHttpClient client = null;
|
|
|
|
CloseableHttpResponse response = null;
|
|
|
|
try {
|
|
|
|
client = constructHttpClient();
|
|
|
|
response = client.execute(request);
|
|
|
|
|
|
|
|
Log.w(TAG, "* response code: " + response.getStatusLine());
|
|
|
|
|
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
|
return parseResponse(response.getEntity().getContent());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (response != null) response.close();
|
|
|
|
if (client != null) client.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IOException("unhandled response code");
|
|
|
|
}
|
|
|
|
|
2015-03-06 21:57:00 +00:00
|
|
|
protected List<Header> getBaseHeaders() {
|
|
|
|
final String number = TelephonyUtil.getManager(context).getLine1Number();
|
|
|
|
return new LinkedList<Header>() {{
|
|
|
|
add(new BasicHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"));
|
|
|
|
add(new BasicHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml"));
|
|
|
|
add(new BasicHeader("Content-Type", "application/vnd.wap.mms-message"));
|
|
|
|
add(new BasicHeader("x-carrier-magic", "http://magic.google.com"));
|
|
|
|
if (!TextUtils.isEmpty(number)) {
|
|
|
|
add(new BasicHeader("x-up-calling-line-id", number));
|
|
|
|
add(new BasicHeader("X-MDN", number));
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
public static class Apn {
|
2015-02-17 04:31:44 +00:00
|
|
|
|
|
|
|
public static Apn EMPTY = new Apn("", "", "", "", "");
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
private final String mmsc;
|
|
|
|
private final String proxy;
|
|
|
|
private final String port;
|
2014-11-25 08:48:09 +00:00
|
|
|
private final String username;
|
|
|
|
private final String password;
|
|
|
|
|
|
|
|
public Apn(String mmsc, String proxy, String port, String username, String password) {
|
|
|
|
this.mmsc = mmsc;
|
|
|
|
this.proxy = proxy;
|
|
|
|
this.port = port;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2014-09-16 23:21:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 04:31:44 +00:00
|
|
|
public Apn(Apn customApn, Apn defaultApn,
|
|
|
|
boolean useCustomMmsc,
|
|
|
|
boolean useCustomProxy,
|
|
|
|
boolean useCustomProxyPort,
|
|
|
|
boolean useCustomUsername,
|
|
|
|
boolean useCustomPassword)
|
|
|
|
{
|
|
|
|
this.mmsc = useCustomMmsc ? customApn.mmsc : defaultApn.mmsc;
|
|
|
|
this.proxy = useCustomProxy ? customApn.proxy : defaultApn.proxy;
|
|
|
|
this.port = useCustomProxyPort ? customApn.port : defaultApn.port;
|
|
|
|
this.username = useCustomUsername ? customApn.username : defaultApn.username;
|
|
|
|
this.password = useCustomPassword ? customApn.password : defaultApn.password;
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
public boolean hasProxy() {
|
|
|
|
return !TextUtils.isEmpty(proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMmsc() {
|
|
|
|
return mmsc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getProxy() {
|
|
|
|
return hasProxy() ? proxy : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
return TextUtils.isEmpty(port) ? 80 : Integer.parseInt(port);
|
|
|
|
}
|
|
|
|
|
2014-11-25 08:48:09 +00:00
|
|
|
public boolean hasAuthentication() {
|
2015-04-02 17:57:32 +00:00
|
|
|
return !TextUtils.isEmpty(username);
|
2014-11-25 08:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return Apn.class.getSimpleName() +
|
|
|
|
"{ mmsc: \"" + mmsc + "\"" +
|
|
|
|
", proxy: " + (proxy == null ? "none" : '"' + proxy + '"') +
|
2014-11-25 08:48:09 +00:00
|
|
|
", port: " + (port == null ? "(none)" : port) +
|
|
|
|
", user: " + (username == null ? "none" : '"' + username + '"') +
|
|
|
|
", pass: " + (password == null ? "none" : '"' + password + '"') + " }";
|
2014-09-16 23:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|