Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Manually merged 0102745

  • Loading branch information...
commit 5e646ce995c684d02968d736c18af0647ef2894d 1 parent b6e7e5e
@NewProggie NewProggie authored
View
2  res/values/strings.xml
@@ -34,6 +34,8 @@
<string name="sparkleshare_account">Account</string>
<string name="settings_release_account">Forget account</string>
<string name="settings_hide_files_folders">Show hidden files</string>
+ <string name="sparkleshare_security">Security</string>
+ <string name="settings_accept_all_certificates">Accept all SSL certificates</string>
<string name="app_version">version</string>
<string name="setup_account">Enter link code</string>
<string name="scan_qrcode">Scan QR code</string>
View
6 res/xml/settings.xml
@@ -8,6 +8,12 @@
android:key="@string/settings_hide_files_folders"
android:title="@string/settings_hide_files_folders" />
</PreferenceCategory>
+ <PreferenceCategory
+ android:title="@string/sparkleshare_security">
+ <CheckBoxPreference
+ android:key="@string/settings_accept_all_certificates"
+ android:title="@string/settings_accept_all_certificates" />
+ </PreferenceCategory>
<PreferenceCategory android:title="@string/info">
<Preference
android:key="@string/app_version"
View
22 src/org/sparkleshare/android/BrowsingActivity.java
@@ -11,12 +11,20 @@
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.sparkleshare.android.ui.BaseActivity;
import org.sparkleshare.android.ui.ListEntryItem;
+import org.sparkleshare.android.utils.FakeSocketFactory;
import android.content.Context;
import android.content.ContextWrapper;
@@ -126,13 +134,25 @@ public void onConfigurationChanged(Configuration newConfig) {
protected void onPreExecute() {
}
+ private HttpClient getNewHttpClient() {
+ SharedPreferences sp = SettingsActivity.getSettings(BrowsingActivity.this);
+ boolean acceptAll = sp.getBoolean(getResources().getString(R.string.settings_accept_all_certificates), false);
+
+ SchemeRegistry s = new SchemeRegistry();
+ s.register(new Scheme("http", new PlainSocketFactory(), 80));
+ s.register(new Scheme("https", acceptAll ? new FakeSocketFactory() : SSLSocketFactory.getSocketFactory(), 443));
+
+ HttpParams httpParams = new BasicHttpParams();
+ return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, s), httpParams);
+ }
+
@Override
protected Boolean doInBackground(String... params) {
String server = params[0];
try {
// TODO: Refactor I/O here and in SetupActivity to central place
- HttpClient client = new DefaultHttpClient();
+ HttpClient client = getNewHttpClient();
HttpGet get = new HttpGet(server);
get.setHeader("X-SPARKLE-IDENT", ident);
get.setHeader("X-SPARKLE-AUTH", authCode);
View
26 src/org/sparkleshare/android/FileDetailsActivity.java
@@ -5,7 +5,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -13,11 +12,19 @@
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
import org.sparkleshare.android.ui.BaseActivity;
import org.sparkleshare.android.ui.FormatHelper;
import org.sparkleshare.android.ui.ListEntryItem;
import org.sparkleshare.android.utils.ExternalDirectory;
+import org.sparkleshare.android.utils.FakeSocketFactory;
import org.sparkleshare.android.utils.MimetypeChecker;
import android.app.Notification;
@@ -26,8 +33,7 @@
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
+import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
@@ -125,13 +131,25 @@ protected void onPreExecute() {
notification.contentIntent = intent;
}
+
+ private HttpClient getNewHttpClient() {
+ SharedPreferences sp = SettingsActivity.getSettings(FileDetailsActivity.this);
+ boolean acceptAll = sp.getBoolean(getResources().getString(R.string.settings_accept_all_certificates), false);
+
+ SchemeRegistry s = new SchemeRegistry();
+ s.register(new Scheme("http", new PlainSocketFactory(), 80));
+ s.register(new Scheme("https", acceptAll ? new FakeSocketFactory() : SSLSocketFactory.getSocketFactory(), 443));
+
+ HttpParams httpParams = new BasicHttpParams();
+ return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, s), httpParams);
+ }
@Override
protected Boolean doInBackground(ListEntryItem... params) {
// TODO: Check for connectivity
current = params[0];
try {
- HttpClient client = new DefaultHttpClient();
+ HttpClient client = getNewHttpClient();
HttpGet get = new HttpGet(current.getUrl());
get.setHeader("X-SPARKLE-IDENT", ident);
get.setHeader("X-SPARKLE-AUTH", authCode);
View
22 src/org/sparkleshare/android/SetupActivity.java
@@ -14,11 +14,19 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import org.sparkleshare.android.ui.BaseActivity;
+import org.sparkleshare.android.utils.FakeSocketFactory;
import android.app.Activity;
import android.app.AlertDialog;
@@ -145,9 +153,21 @@ protected void onPreExecute() {
loadingDialog = ProgressDialog.show(context, "", getString(R.string.adding_project));
}
+ private HttpClient getNewHttpClient() {
+ SharedPreferences sp = SettingsActivity.getSettings(SetupActivity.this);
+ boolean acceptAll = sp.getBoolean(getResources().getString(R.string.settings_accept_all_certificates), false);
+
+ SchemeRegistry s = new SchemeRegistry();
+ s.register(new Scheme("http", new PlainSocketFactory(), 80));
+ s.register(new Scheme("https", acceptAll ? new FakeSocketFactory() : SSLSocketFactory.getSocketFactory(), 443));
+
+ HttpParams httpParams = new BasicHttpParams();
+ return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, s), httpParams);
+ }
+
@Override
protected Boolean doInBackground(String... params) {
- HttpClient client = new DefaultHttpClient();
+ HttpClient client = getNewHttpClient();
serverUrl = params[0];
HttpPost post = new HttpPost(serverUrl + AUTH_SUFFIX);
try {
View
81 src/org/sparkleshare/android/utils/FakeSocketFactory.java
@@ -0,0 +1,81 @@
+package org.sparkleshare.android.utils;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.TrustManager;
+
+import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.conn.scheme.LayeredSocketFactory;
+import org.apache.http.conn.scheme.SocketFactory;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+
+public class FakeSocketFactory implements SocketFactory, LayeredSocketFactory {
+
+ private SSLContext sslcontext = null;
+
+ private static SSLContext createEasySSLContext() throws IOException {
+ try {
+ SSLContext context = SSLContext.getInstance("TLS");
+ context.init(null, new TrustManager[] { new FakeTrustManager() }, null);
+ return context;
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ private SSLContext getSSLContext() throws IOException {
+ if (this.sslcontext == null) {
+ this.sslcontext = createEasySSLContext();
+ }
+ return this.sslcontext;
+ }
+
+ @Override
+ public Socket connectSocket(Socket sock, String host, int port,
+ InetAddress localAddress, int localPort, HttpParams params) throws IOException,
+ UnknownHostException, ConnectTimeoutException {
+ int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
+ int soTimeout = HttpConnectionParams.getSoTimeout(params);
+
+ InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
+ SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
+
+ if ((localAddress != null) || (localPort > 0)) {
+ // we need to bind explicitly
+ if (localPort < 0) {
+ localPort = 0; // indicates "any"
+ }
+ InetSocketAddress isa = new InetSocketAddress(localAddress,
+ localPort);
+ sslsock.bind(isa);
+ }
+
+ sslsock.connect(remoteAddress, connTimeout);
+ sslsock.setSoTimeout(soTimeout);
+ return sslsock;
+ }
+
+ @Override
+ public Socket createSocket() throws IOException {
+ return getSSLContext().getSocketFactory().createSocket();
+ }
+
+ @Override
+ public boolean isSecure(Socket arg0) throws IllegalArgumentException {
+ return true;
+ }
+
+ @Override
+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
+ throws IOException, UnknownHostException {
+ return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
+ }
+
+}
View
33 src/org/sparkleshare/android/utils/FakeTrustManager.java
@@ -0,0 +1,33 @@
+package org.sparkleshare.android.utils;
+
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.X509TrustManager;
+
+public class FakeTrustManager implements X509TrustManager {
+
+ private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+ }
+
+ public boolean isClientTrusted(X509Certificate[] chain) {
+ return true;
+ }
+
+ public boolean isServerTrusted(X509Certificate[] chain) {
+ return true;
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return _AcceptedIssuers;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.