
package info.guardianproject.netcipher;

import android.net.Uri;
import android.text.TextUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URI;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

public class NetCipher {

    private NetCipher() {
        // this is a utility class with only static methods
    }

    public final static Proxy ORBOT_HTTP_PROXY = new Proxy(Proxy.Type.HTTP,
            new InetSocketAddress("127.0.0.1", 8118));

    private static Proxy proxy;

    public static void setProxy(String host, int port) {
        if (host != null && port > 0) {
            InetSocketAddress isa = new InetSocketAddress(host, port);
            proxy = new Proxy(Proxy.Type.HTTP, isa);
        } else {
            proxy = null;
        }
    }

    public static void setProxy(Proxy proxy) {
        NetCipher.proxy = proxy;
    }

    public static Proxy getProxy() {
        return proxy;
    }

    public static HttpURLConnection getHttpURLConnection(String urlString)
            throws MalformedURLException, IOException {
        return getHttpURLConnection(new URL(urlString));
    }

    public static HttpURLConnection getHttpURLConnection(URL url) throws IOException {
        if (proxy != null) {
            return (HttpURLConnection) url.openConnection(proxy);
        } else {
            return (HttpURLConnection) url.openConnection();
        }
    }

    public static HttpsURLConnection getHttpsURLConnection(String urlString)
            throws MalformedURLException, IOException, KeyManagementException {
        urlString.replaceFirst("^[Hh][Tt][Tt][Pp]:", "https:");
        return getHttpsURLConnection(new URL(urlString), false);
    }

    public static HttpsURLConnection getHttpsURLConnection(Uri uri)
            throws MalformedURLException, IOException, KeyManagementException {
        return getHttpsURLConnection(uri.toString());
    }

    public static HttpsURLConnection getHttpsURLConnection(URI uri)
            throws MalformedURLException, IOException, KeyManagementException {
        if (TextUtils.equals(uri.getScheme(), "https"))
            return getHttpsURLConnection(uri.toURL(), false);
        else
            // otherwise force scheme to https
            return getHttpsURLConnection(uri.toString());
    }

    public static HttpsURLConnection getHttpsURLConnection(URL url)
            throws IOException, KeyManagementException {
        return getHttpsURLConnection(url, false);
    }

    /**
     * Get a {@link HttpsURLConnection} from a {@link URL} using a more
     * compatible, but less strong, suite of ciphers.
     *
     * @param url
     * @param compatible
     * @return
     * @throws IOException
     * @throws KeyManagementException
     */
    public static HttpsURLConnection getCompatibleHttpsURLConnection(URL url)
            throws IOException, KeyManagementException {
        return getHttpsURLConnection(url, true);
    }

    /**
     * Get a {@link HttpsURLConnection} from a {@link URL}, and specify whether
     * it should use a more compatible, but less strong, suite of ciphers.
     *
     * @param url
     * @param compatible
     * @return
     * @throws IOException
     * @throws KeyManagementException
     */
    public static HttpsURLConnection getHttpsURLConnection(URL url, boolean compatible)
            throws IOException, KeyManagementException {
        SSLContext sslcontext;
        try {
            sslcontext = SSLContext.getInstance("TLSv1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        sslcontext.init(null, null, null); // null means use default
        SSLSocketFactory tlsOnly = new TlsOnlySocketFactory(sslcontext.getSocketFactory(),
                compatible);
        HttpsURLConnection.setDefaultSSLSocketFactory(tlsOnly);
        if (proxy != null) {
            return (HttpsURLConnection) url.openConnection(proxy);
        } else {
            return (HttpsURLConnection) url.openConnection();
        }
    }
}

@@ -1770,77 +1770,18 @@
 ing)
-%0A            throws MalformedURLException, IOException, KeyManagement
+ throws IO
 Exce
@@ -1995,77 +1995,18 @@
 uri)
-%0A            throws MalformedURLException, IOException, KeyManagement
+ throws IO
 Exce
@@ -2145,77 +2145,18 @@
 uri)
-%0A            throws MalformedURLException, IOException, KeyManagement
+ throws IO
 Exce
@@ -2477,54 +2477,18 @@
 url)
-%0A            throws IOException, KeyManagement
+ throws IO
 Exce
@@ -3281,33 +3281,35 @@
   * @throws 
-KeyManage
+IllegalArgu
 mentExceptio
@@ -3301,32 +3301,71 @@
 rgumentException
+ if the proxy or TLS setup is incorrect
 %0A     */%0A    pub
@@ -3479,32 +3479,8 @@
 tion
-, KeyManagementException
  %7B%0A 
@@ -3593,183 +3593,276 @@
     
-%7D catch (NoSuchAlgorithmException e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A        sslcontext.init(null, null, null); // null means use default
+    sslcontext.init(null, null, null); // null means use default%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            throw new IllegalArgumentException(e);%0A        %7D catch (KeyManagementException e) %7B%0A            throw new IllegalArgumentException(e);%0A        %7D
 %0A   

handle KeyManagementException as an IllegalArgumentException