MmsConnection refactor
- Use Apache HttpClient v4.x, only library that seems to like HTTP proxies - Remove custom redirect logic in favor of library's Fixes #1904 // FREEBIEpull/1/head
parent
f1d230ce6e
commit
89fb80fcc5
@ -0,0 +1,93 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2014 Open 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.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.HttpGetHC4;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import ws.com.google.android.mms.pdu.PduParser;
|
||||||
|
import ws.com.google.android.mms.pdu.RetrieveConf;
|
||||||
|
|
||||||
|
public class IncomingMmsConnection extends MmsConnection {
|
||||||
|
private static final String TAG = IncomingMmsConnection.class.getSimpleName();
|
||||||
|
|
||||||
|
public IncomingMmsConnection(Context context, Apn apn) {
|
||||||
|
super(context, apn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected HttpUriRequest constructRequest(boolean useProxy) throws IOException {
|
||||||
|
HttpGetHC4 request = new HttpGetHC4(apn.getMmsc());
|
||||||
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
||||||
|
if (useProxy) {
|
||||||
|
HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort());
|
||||||
|
request.setConfig(RequestConfig.custom().setProxy(proxy).build());
|
||||||
|
}
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isConnectionPossible(Context context, String apn) {
|
||||||
|
try {
|
||||||
|
getApn(context, apn);
|
||||||
|
return true;
|
||||||
|
} catch (ApnUnavailableException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RetrieveConf retrieve(boolean usingMmsRadio, boolean useProxyIfAvailable)
|
||||||
|
throws IOException, ApnUnavailableException
|
||||||
|
{
|
||||||
|
byte[] pdu = null;
|
||||||
|
|
||||||
|
final boolean useProxy = useProxyIfAvailable && apn.hasProxy();
|
||||||
|
final String targetHost = useProxy
|
||||||
|
? apn.getProxy()
|
||||||
|
: Uri.parse(apn.getMmsc()).getHost();
|
||||||
|
try {
|
||||||
|
if (checkRouteToHost(context, targetHost, usingMmsRadio)) {
|
||||||
|
Log.w(TAG, "got successful route to host " + targetHost);
|
||||||
|
pdu = makeRequest(useProxy);
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
Log.w(TAG, ioe);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pdu == null) {
|
||||||
|
throw new IOException("Connection manager could not obtain route to host.");
|
||||||
|
}
|
||||||
|
|
||||||
|
RetrieveConf retrieved = (RetrieveConf)new PduParser(pdu).parse();
|
||||||
|
|
||||||
|
if (retrieved == null) {
|
||||||
|
Log.w(TAG, "Couldn't parse PDU, byte response: " + Arrays.toString(pdu));
|
||||||
|
Log.w(TAG, "Couldn't parse PDU, ASCII: " + new String(pdu));
|
||||||
|
throw new IOException("Bad retrieved PDU");
|
||||||
|
}
|
||||||
|
|
||||||
|
return retrieved;
|
||||||
|
}
|
||||||
|
}
|
@ -1,254 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteException;
|
|
||||||
import android.net.ConnectivityManager;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.database.ApnDatabase;
|
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
||||||
import org.thoughtcrime.securesms.util.TelephonyUtil;
|
|
||||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
||||||
import org.whispersystems.textsecure.util.Conversions;
|
|
||||||
import org.whispersystems.textsecure.util.Util;
|
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Proxy;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class MmsCommunication {
|
|
||||||
private static final String TAG = "MmsCommunication";
|
|
||||||
|
|
||||||
public static final int MAX_REDIRECTS = 10;
|
|
||||||
|
|
||||||
protected static MmsConnectionParameters getLocallyConfiguredMmsConnectionParameters(Context context)
|
|
||||||
throws ApnUnavailableException
|
|
||||||
{
|
|
||||||
if (TextSecurePreferences.isUseLocalApnsEnabled(context)) {
|
|
||||||
String mmsc = TextSecurePreferences.getMmscUrl(context);
|
|
||||||
|
|
||||||
if (mmsc == null)
|
|
||||||
throw new ApnUnavailableException("Malformed locally configured MMSC.");
|
|
||||||
|
|
||||||
if (!mmsc.startsWith("http"))
|
|
||||||
mmsc = "http://" + mmsc;
|
|
||||||
|
|
||||||
String proxy = TextSecurePreferences.getMmscProxy(context);
|
|
||||||
String port = TextSecurePreferences.getMmscProxyPort(context);
|
|
||||||
|
|
||||||
return new MmsConnectionParameters(mmsc, proxy, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new ApnUnavailableException("No locally configured parameters available");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static MmsConnectionParameters getLocalMmsConnectionParameters(Context context)
|
|
||||||
throws ApnUnavailableException
|
|
||||||
{
|
|
||||||
if (TextSecurePreferences.isUseLocalApnsEnabled(context)) {
|
|
||||||
return getLocallyConfiguredMmsConnectionParameters(context);
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
MmsConnectionParameters params = ApnDatabase.getInstance(context)
|
|
||||||
.getMmsConnectionParameters(TelephonyUtil.getMccMnc(context),
|
|
||||||
TelephonyUtil.getApn(context));
|
|
||||||
|
|
||||||
if (params == null) {
|
|
||||||
throw new ApnUnavailableException("No parameters available from ApnDefaults.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return params;
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
throw new ApnUnavailableException("ApnDatabase threw an IOException", ioe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static MmsConnectionParameters getMmsConnectionParameters(Context context, String apn)
|
|
||||||
throws ApnUnavailableException
|
|
||||||
{
|
|
||||||
Log.w(TAG, "Getting MMSC params for apn " + apn);
|
|
||||||
Cursor cursor = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
cursor = DatabaseFactory.getMmsDatabase(context).getCarrierMmsInformation(apn);
|
|
||||||
|
|
||||||
if (cursor == null || !cursor.moveToFirst()) {
|
|
||||||
Log.w(TAG, "Android didn't have a result, querying local parameters.");
|
|
||||||
return getLocalMmsConnectionParameters(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
String mmsc = cursor.getString(cursor.getColumnIndexOrThrow("mmsc"));
|
|
||||||
String proxy = cursor.getString(cursor.getColumnIndexOrThrow("mmsproxy"));
|
|
||||||
String port = cursor.getString(cursor.getColumnIndexOrThrow("mmsport"));
|
|
||||||
|
|
||||||
if (!Util.isEmpty(mmsc)) {
|
|
||||||
Log.w(TAG, "Using Android-provided MMSC parameters.");
|
|
||||||
return new MmsConnectionParameters(mmsc, proxy, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (cursor.moveToNext());
|
|
||||||
|
|
||||||
Log.w(TAG, "Android provided results were empty, querying local parameters.");
|
|
||||||
return getLocalMmsConnectionParameters(context);
|
|
||||||
} catch (SQLiteException sqe) {
|
|
||||||
Log.w(TAG, sqe);
|
|
||||||
} catch (SecurityException se) {
|
|
||||||
Log.w(TAG, "Android won't let us query the APN database.");
|
|
||||||
return getLocalMmsConnectionParameters(context);
|
|
||||||
} catch (IllegalArgumentException iae) {
|
|
||||||
Log.w(TAG, iae);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null)
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
return getLocalMmsConnectionParameters(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
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!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.w(TAG, "Checking route to address: " + host + " , " + inetAddress.getHostAddress());
|
|
||||||
|
|
||||||
byte[] ipAddressBytes = inetAddress.getAddress();
|
|
||||||
|
|
||||||
if (ipAddressBytes != null && ipAddressBytes.length == 4) {
|
|
||||||
int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
|
|
||||||
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
|
|
||||||
return manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static HttpURLConnection constructHttpClient(String urlString, String proxy, int port)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
HttpURLConnection urlConnection;
|
|
||||||
URL url = new URL(urlString);
|
|
||||||
|
|
||||||
if (proxy != null) {
|
|
||||||
Log.w(TAG, String.format("Constructing http client using a proxy: (%s:%d)", proxy, port));
|
|
||||||
Proxy proxyRoute = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port));
|
|
||||||
urlConnection = (HttpURLConnection) url.openConnection(proxyRoute);
|
|
||||||
} else {
|
|
||||||
Log.w(TAG, "Constructing http client without proxy");
|
|
||||||
urlConnection = (HttpURLConnection) url.openConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
urlConnection.setInstanceFollowRedirects(false);
|
|
||||||
urlConnection.setConnectTimeout(20*1000);
|
|
||||||
urlConnection.setReadTimeout(20*1000);
|
|
||||||
urlConnection.setUseCaches(false);
|
|
||||||
urlConnection.setRequestProperty("User-Agent", "Android-Mms/2.0");
|
|
||||||
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
|
|
||||||
return urlConnection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class MmsConnectionParameters {
|
|
||||||
|
|
||||||
public class Apn {
|
|
||||||
private final String mmsc;
|
|
||||||
private final String proxy;
|
|
||||||
private final String port;
|
|
||||||
|
|
||||||
public Apn(String mmsc, String proxy, String port) {
|
|
||||||
this.mmsc = mmsc;
|
|
||||||
this.proxy = proxy;
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasProxy() {
|
|
||||||
return !Util.isEmpty(proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMmsc() {
|
|
||||||
return mmsc;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getProxy() {
|
|
||||||
if (!hasProxy())
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return proxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPort() {
|
|
||||||
if (Util.isEmpty(port))
|
|
||||||
return 80;
|
|
||||||
|
|
||||||
return Integer.parseInt(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return MmsConnectionParameters.class.getSimpleName() +
|
|
||||||
"{ mmsc: \"" + mmsc + "\"" +
|
|
||||||
", proxy: " + (proxy == null ? "none" : '"' + proxy + '"') +
|
|
||||||
", port: " + (port == null ? "none" : port) + " }";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Apn> apn = new ArrayList<Apn>();
|
|
||||||
|
|
||||||
public MmsConnectionParameters(String mmsc, String proxy, String port) {
|
|
||||||
apn.add(new Apn(mmsc, proxy, port));
|
|
||||||
}
|
|
||||||
|
|
||||||
public MmsConnectionParameters add(String mmsc, String proxy, String port) {
|
|
||||||
apn.add(new Apn(mmsc, proxy, port));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Apn> get() {
|
|
||||||
return apn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,189 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
import org.thoughtcrime.securesms.database.ApnDatabase;
|
||||||
|
import org.thoughtcrime.securesms.util.TelephonyUtil;
|
||||||
|
import org.whispersystems.textsecure.util.Conversions;
|
||||||
|
import org.whispersystems.textsecure.util.Util;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
|
public abstract class MmsConnection {
|
||||||
|
private static final String TAG = "MmsCommunication";
|
||||||
|
|
||||||
|
protected final Context context;
|
||||||
|
protected final Apn apn;
|
||||||
|
|
||||||
|
protected MmsConnection(Context context, Apn apn) {
|
||||||
|
this.context = context;
|
||||||
|
this.apn = apn;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static Apn getLocalApn(Context context) throws ApnUnavailableException {
|
||||||
|
try {
|
||||||
|
Apn params = ApnDatabase.getInstance(context)
|
||||||
|
.getMmsConnectionParameters(TelephonyUtil.getMccMnc(context),
|
||||||
|
TelephonyUtil.getApn(context));
|
||||||
|
|
||||||
|
if (params == null) {
|
||||||
|
throw new ApnUnavailableException("No parameters available from ApnDefaults.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
throw new ApnUnavailableException("ApnDatabase threw an IOException", ioe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Apn getApn(Context context, String apnName) throws ApnUnavailableException {
|
||||||
|
Log.w(TAG, "Getting MMSC params for apn " + apnName);
|
||||||
|
return getLocalApn(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
byte[] ipAddressBytes = inetAddress.getAddress();
|
||||||
|
if (ipAddressBytes == null || ipAddressBytes.length != 4) {
|
||||||
|
Log.w(TAG, "returning vacuous success since android.net package doesn't support IPv6");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
|
||||||
|
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
|
||||||
|
boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
|
||||||
|
Log.w(TAG, "requestRouteToHost result: " + routeToHostObtained);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CloseableHttpClient constructHttpClient()
|
||||||
|
throws IOException {
|
||||||
|
RequestConfig config = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(20 * 1000)
|
||||||
|
.setConnectionRequestTimeout(20 * 1000)
|
||||||
|
.setSocketTimeout(20 * 1000)
|
||||||
|
.setMaxRedirects(20)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return HttpClients.custom()
|
||||||
|
.setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
|
||||||
|
.setRedirectStrategy(new LaxRedirectStrategy())
|
||||||
|
.setUserAgent("Android-Mms/2.0")
|
||||||
|
.setConnectionManager(new BasicHttpClientConnectionManager())
|
||||||
|
.setDefaultRequestConfig(config)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected byte[] makeRequest(boolean useProxy) throws IOException {
|
||||||
|
Log.w(TAG, "connecting to " + apn.getMmsc() + (useProxy ? " using proxy" : ""));
|
||||||
|
|
||||||
|
HttpUriRequest request;
|
||||||
|
CloseableHttpClient client = null;
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
try {
|
||||||
|
request = constructRequest(useProxy);
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract HttpUriRequest constructRequest(boolean useProxy) throws IOException;
|
||||||
|
|
||||||
|
public static class Apn {
|
||||||
|
private final String mmsc;
|
||||||
|
private final String proxy;
|
||||||
|
private final String port;
|
||||||
|
|
||||||
|
public Apn(String mmsc, String proxy, String port) {
|
||||||
|
this.mmsc = mmsc;
|
||||||
|
this.proxy = proxy;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Apn.class.getSimpleName() +
|
||||||
|
"{ mmsc: \"" + mmsc + "\"" +
|
||||||
|
", proxy: " + (proxy == null ? "none" : '"' + proxy + '"') +
|
||||||
|
", port: " + (port == null ? "none" : port) + " }";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,130 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import ws.com.google.android.mms.pdu.PduParser;
|
|
||||||
import ws.com.google.android.mms.pdu.RetrieveConf;
|
|
||||||
|
|
||||||
public class MmsDownloadHelper extends MmsCommunication {
|
|
||||||
private static final String TAG = MmsDownloadHelper.class.getSimpleName();
|
|
||||||
|
|
||||||
private static byte[] makeRequest(String url, String proxy, int proxyPort)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
HttpURLConnection client = null;
|
|
||||||
|
|
||||||
int redirects = MAX_REDIRECTS;
|
|
||||||
final Set<String> previousUrls = new HashSet<String>();
|
|
||||||
String currentUrl = url;
|
|
||||||
while (redirects-- > 0) {
|
|
||||||
if (previousUrls.contains(currentUrl)) {
|
|
||||||
throw new IOException("redirect loop detected");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
client = constructHttpClient(currentUrl, proxy, proxyPort);
|
|
||||||
|
|
||||||
client.setDoInput(true);
|
|
||||||
client.setRequestMethod("GET");
|
|
||||||
client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
|
||||||
|
|
||||||
Log.w(TAG, "connecting to " + currentUrl);
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
int responseCode = client.getResponseCode();
|
|
||||||
Log.w(TAG, "* response code: " + responseCode + "/" + client.getResponseMessage());
|
|
||||||
|
|
||||||
if (responseCode == 301 || responseCode == 302) {
|
|
||||||
final String redirectUrl = client.getHeaderField("Location");
|
|
||||||
Log.w(TAG, "* Location: " + redirectUrl);
|
|
||||||
if (TextUtils.isEmpty(redirectUrl)) {
|
|
||||||
throw new IOException("Got redirect response code, but Location header was empty or missing");
|
|
||||||
}
|
|
||||||
previousUrls.add(currentUrl);
|
|
||||||
currentUrl = redirectUrl;
|
|
||||||
} else if (responseCode == 200) {
|
|
||||||
final InputStream is = client.getInputStream();
|
|
||||||
return parseResponse(is);
|
|
||||||
} else {
|
|
||||||
throw new IOException("unhandled response code");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (client != null) client.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new IOException("max redirects hit");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isMmsConnectionParametersAvailable(Context context, String apn) {
|
|
||||||
try {
|
|
||||||
getMmsConnectionParameters(context, apn);
|
|
||||||
return true;
|
|
||||||
} catch (ApnUnavailableException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static RetrieveConf retrieveMms(Context context, String url, String apn,
|
|
||||||
boolean usingMmsRadio, boolean proxyIfPossible)
|
|
||||||
throws IOException, ApnUnavailableException
|
|
||||||
{
|
|
||||||
MmsConnectionParameters connectionParameters = getMmsConnectionParameters(context, apn);
|
|
||||||
byte[] pdu = null;
|
|
||||||
|
|
||||||
for (MmsConnectionParameters.Apn param : connectionParameters.get()) {
|
|
||||||
try {
|
|
||||||
if (proxyIfPossible && param.hasProxy()) {
|
|
||||||
if (checkRouteToHost(context, param.getProxy(), usingMmsRadio)) {
|
|
||||||
pdu = makeRequest(url, param.getProxy(), param.getPort());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (checkRouteToHost(context, Uri.parse(url).getHost(), usingMmsRadio)) {
|
|
||||||
pdu = makeRequest(url, null, -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pdu != null) break;
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.w(TAG, ioe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pdu == null) {
|
|
||||||
throw new IOException("Connection manager could not obtain route to host.");
|
|
||||||
}
|
|
||||||
|
|
||||||
RetrieveConf retrieved = (RetrieveConf)new PduParser(pdu).parse();
|
|
||||||
|
|
||||||
if (retrieved == null) {
|
|
||||||
Log.w(TAG, "Couldn't parse PDU, raw server response: " + Arrays.toString(pdu));
|
|
||||||
throw new IOException("Bad retrieved PDU");
|
|
||||||
}
|
|
||||||
|
|
||||||
return retrieved;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,163 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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;
|
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import ws.com.google.android.mms.pdu.PduParser;
|
|
||||||
import ws.com.google.android.mms.pdu.SendConf;
|
|
||||||
|
|
||||||
public class MmsSendHelper extends MmsCommunication {
|
|
||||||
private final static String TAG = MmsSendHelper.class.getSimpleName();
|
|
||||||
|
|
||||||
private static byte[] makePost(String url, String proxy, int proxyPort, byte[] mms)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
if (mms == null) return null;
|
|
||||||
|
|
||||||
HttpURLConnection client = null;
|
|
||||||
|
|
||||||
int redirects = MAX_REDIRECTS;
|
|
||||||
final Set<String> previousUrls = new HashSet<String>();
|
|
||||||
String currentUrl = url;
|
|
||||||
while (redirects-- > 0) {
|
|
||||||
if (previousUrls.contains(currentUrl)) {
|
|
||||||
throw new IOException("redirect loop detected");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
client = constructHttpClient(currentUrl, proxy, proxyPort);
|
|
||||||
client.setFixedLengthStreamingMode(mms.length);
|
|
||||||
client.setDoInput(true);
|
|
||||||
client.setDoOutput(true);
|
|
||||||
client.setRequestMethod("POST");
|
|
||||||
client.setRequestProperty("Content-Type", "application/vnd.wap.mms-message");
|
|
||||||
client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
|
||||||
client.setRequestProperty("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
|
|
||||||
|
|
||||||
Log.w(TAG, "connecting to " + currentUrl);
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
Log.w(TAG, "* writing mms payload, " + mms.length + " bytes");
|
|
||||||
OutputStream out = client.getOutputStream();
|
|
||||||
out.write(mms);
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
Log.w(TAG, "* payload sent");
|
|
||||||
|
|
||||||
int responseCode = client.getResponseCode();
|
|
||||||
Log.w(TAG, "* response code: " + responseCode + "/" + client.getResponseMessage());
|
|
||||||
|
|
||||||
if (responseCode == 301 || responseCode == 302) {
|
|
||||||
final String redirectUrl = client.getHeaderField("Location");
|
|
||||||
Log.w(TAG, "* Location: " + redirectUrl);
|
|
||||||
if (TextUtils.isEmpty(redirectUrl)) {
|
|
||||||
throw new IOException("Got redirect response code, but Location header was empty or missing");
|
|
||||||
}
|
|
||||||
previousUrls.add(currentUrl);
|
|
||||||
currentUrl = redirectUrl;
|
|
||||||
} else if (responseCode == 200) {
|
|
||||||
final InputStream is = client.getInputStream();
|
|
||||||
return parseResponse(is);
|
|
||||||
} else {
|
|
||||||
throw new IOException("unhandled response code");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (client != null) client.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new IOException("max redirects hit");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendNotificationReceived(Context context, byte[] mms, String apn,
|
|
||||||
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
sendBytes(context, mms, apn, usingMmsRadio, useProxyIfAvailable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SendConf sendMms(Context context, byte[] mms, String apn,
|
|
||||||
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
byte[] response = sendBytes(context, mms, apn, usingMmsRadio, useProxyIfAvailable);
|
|
||||||
return (SendConf) new PduParser(response).parse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] sendBytes(Context context, byte[] mms, String apn,
|
|
||||||
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
Log.w(TAG, "Sending MMS of length: " + mms.length + "." + (usingMmsRadio ? " using mms radio" : ""));
|
|
||||||
try {
|
|
||||||
MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn);
|
|
||||||
|
|
||||||
for (MmsConnectionParameters.Apn param : parameters.get()) {
|
|
||||||
try {
|
|
||||||
if (useProxyIfAvailable && param.hasProxy()) {
|
|
||||||
if (checkRouteToHost(context, param.getProxy(), usingMmsRadio)) {
|
|
||||||
byte[] response = makePost(param.getMmsc(), param.getProxy(), param.getPort(), mms);
|
|
||||||
if (response != null) return response;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (checkRouteToHost(context, Uri.parse(param.getMmsc()).getHost(), usingMmsRadio)) {
|
|
||||||
byte[] response = makePost(param.getMmsc(), null, -1, mms);
|
|
||||||
if (response != null) return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
Log.w(TAG, ioe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IOException("Connection manager could not obtain route to host.");
|
|
||||||
} catch (ApnUnavailableException aue) {
|
|
||||||
Log.w(TAG, aue);
|
|
||||||
throw new IOException("Failed to get MMSC information...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasNecessaryApnDetails(Context context) {
|
|
||||||
try {
|
|
||||||
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(MmsRadio.TYPE_MOBILE_MMS);
|
|
||||||
if (networkInfo == null) {
|
|
||||||
Log.w(TAG, "MMS network info was null, unsupported by this device");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String apn = networkInfo.getExtraInfo();
|
|
||||||
|
|
||||||
MmsCommunication.getMmsConnectionParameters(context, apn);
|
|
||||||
return true;
|
|
||||||
} catch (ApnUnavailableException e) {
|
|
||||||
Log.w("MmsSendHelper", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,111 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2014 Open 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;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.HttpPostHC4;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
import org.apache.http.entity.ByteArrayEntityHC4;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import ws.com.google.android.mms.pdu.PduParser;
|
||||||
|
import ws.com.google.android.mms.pdu.SendConf;
|
||||||
|
|
||||||
|
public class OutgoingMmsConnection extends MmsConnection {
|
||||||
|
private final static String TAG = OutgoingMmsConnection.class.getSimpleName();
|
||||||
|
|
||||||
|
private final byte[] mms;
|
||||||
|
|
||||||
|
public OutgoingMmsConnection(Context context, String apnName, byte[] mms) throws ApnUnavailableException {
|
||||||
|
super(context, getApn(context, apnName));
|
||||||
|
this.mms = mms;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected HttpUriRequest constructRequest(boolean useProxy)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
HttpPostHC4 request = new HttpPostHC4(apn.getMmsc());
|
||||||
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
||||||
|
request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
|
||||||
|
request.addHeader("Content-Type", "application/vnd.wap.mms-message");
|
||||||
|
request.setEntity(new ByteArrayEntityHC4(mms));
|
||||||
|
if (useProxy) {
|
||||||
|
HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort());
|
||||||
|
request.setConfig(RequestConfig.custom().setProxy(proxy).build());
|
||||||
|
}
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendNotificationReceived(boolean usingMmsRadio, boolean useProxyIfAvailable)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
sendBytes(usingMmsRadio, useProxyIfAvailable);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SendConf send(boolean useMmsRadio, boolean useProxyIfAvailable) throws IOException {
|
||||||
|
byte[] response = sendBytes(useMmsRadio, useProxyIfAvailable);
|
||||||
|
return (SendConf) new PduParser(response).parse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] sendBytes(boolean useMmsRadio, boolean useProxyIfAvailable) throws IOException {
|
||||||
|
final boolean useProxy = useProxyIfAvailable && apn.hasProxy();
|
||||||
|
final String targetHost = useProxy
|
||||||
|
? apn.getProxy()
|
||||||
|
: Uri.parse(apn.getMmsc()).getHost();
|
||||||
|
|
||||||
|
Log.w(TAG, "Sending MMS of length: " + mms.length
|
||||||
|
+ (useMmsRadio ? ", using mms radio" : "")
|
||||||
|
+ (useProxy ? ", using proxy" : ""));
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (checkRouteToHost(context, targetHost, useMmsRadio)) {
|
||||||
|
Log.w(TAG, "got successful route to host " + targetHost);
|
||||||
|
byte[] response = makeRequest(useProxy);
|
||||||
|
if (response != null) return response;
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
Log.w(TAG, ioe);
|
||||||
|
}
|
||||||
|
throw new IOException("Connection manager could not obtain route to host.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isConnectionPossible(Context context) {
|
||||||
|
try {
|
||||||
|
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(MmsRadio.TYPE_MOBILE_MMS);
|
||||||
|
if (networkInfo == null) {
|
||||||
|
Log.w(TAG, "MMS network info was null, unsupported by this device");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
getApn(context, networkInfo.getExtraInfo());
|
||||||
|
return true;
|
||||||
|
} catch (ApnUnavailableException e) {
|
||||||
|
Log.w(TAG, e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue