From c03a3355df9282000a676ec2cd7c9b4a79819958 Mon Sep 17 00:00:00 2001 From: Brad Kennedy Date: Fri, 14 Sep 2018 16:35:38 -0400 Subject: [PATCH] Added internal certificate which allows us to use HTTPS to our local server on campus --- .idea/misc.xml | 2 +- .../pspenler/maptracker/Configuration.java | 2 +- .../pspenler/maptracker/MainActivity.java | 22 ++++++++++++++----- .../pspenler/maptracker/MapActivity.java | 11 +++++----- .../pspenler/maptracker/WebThread.java | 6 +++-- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index c0f68ed..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/ca/uoguelph/pspenler/maptracker/Configuration.java b/app/src/main/java/ca/uoguelph/pspenler/maptracker/Configuration.java index 3397932..b537d17 100644 --- a/app/src/main/java/ca/uoguelph/pspenler/maptracker/Configuration.java +++ b/app/src/main/java/ca/uoguelph/pspenler/maptracker/Configuration.java @@ -55,7 +55,7 @@ public void initConfig(String name, String confFile, String server, String label //Checks that the results server is valid if (server.equals("")) { error = "Results server address cannot be empty"; - } else if (!MainActivity.hasInternetAccess(server)) { + } else if (!MainActivity.hasInternetAccess(server, true)) { error = "Inputted results server not available"; } diff --git a/app/src/main/java/ca/uoguelph/pspenler/maptracker/MainActivity.java b/app/src/main/java/ca/uoguelph/pspenler/maptracker/MainActivity.java index 6d5aee8..95a514b 100644 --- a/app/src/main/java/ca/uoguelph/pspenler/maptracker/MainActivity.java +++ b/app/src/main/java/ca/uoguelph/pspenler/maptracker/MainActivity.java @@ -32,6 +32,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.KeyStore; @@ -85,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode); onRequestPermissionsResult(requestCode, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, grantResults); + sslContext = getSingleSSLContext(); } public void launchConfiguration(View view) { @@ -159,14 +161,21 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String permissi } } - public static boolean hasInternetAccess(final String url) { + public static boolean hasInternetAccess(final String url, final boolean internal) { hasInternet = false; Thread thread = new Thread(new Runnable() { @Override public void run() { try { - HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection(); + Log.d("DEBUG", url); + HttpURLConnection urlc; + if (internal) { + urlc = createTLSConnInternal(url); + } else { + urlc = (HttpsURLConnection) new URL(url).openConnection(); + } + urlc.setRequestProperty("User-Agent", "Android"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); @@ -308,7 +317,7 @@ protected void onPostExecute(Integer resultCode) { } else if (resultCode == 409) { uploadDialog.setMessage("This experiment name already exists. Please change the name in the configuration"); } else if (resultCode == 404 || resultCode == 0) { - if (!hasInternetAccess("http://clients3.google.com/generate_204")) { + if (!hasInternetAccess("http://clients3.google.com/generate_204", false)) { uploadDialog.setMessage("No internet connection. Please check network settings"); } else if (configuration.getName().contains("/")) { uploadDialog.setMessage("The experiment name is invalid. Please remove any '/' characters from the experiment name"); @@ -335,7 +344,7 @@ private SSLContext getSingleSSLContext() { try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = getResources().openRawResource( - getResources().getIdentifier("ca", + getResources().getIdentifier("server", "raw", getPackageName())); Certificate ca; try { @@ -362,12 +371,13 @@ private SSLContext getSingleSSLContext() { private static SSLContext sslContext; - private HttpsURLConnection createTLSConnInternal(String urls) throws IOException { + static HttpsURLConnection createTLSConnInternal(String urls) throws IOException { if (sslContext == null) { - sslContext = getSingleSSLContext(); + throw new RuntimeException("SSL Was not initialized"); } try { + Log.d("createTLSConnInternal", urls); URL url = new URL(urls); HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); diff --git a/app/src/main/java/ca/uoguelph/pspenler/maptracker/MapActivity.java b/app/src/main/java/ca/uoguelph/pspenler/maptracker/MapActivity.java index 023ec07..c58c396 100644 --- a/app/src/main/java/ca/uoguelph/pspenler/maptracker/MapActivity.java +++ b/app/src/main/java/ca/uoguelph/pspenler/maptracker/MapActivity.java @@ -23,6 +23,8 @@ import java.net.HttpURLConnection; import java.net.URL; +import javax.net.ssl.HttpsURLConnection; + interface AsyncResponse { void processFinish(String output); } @@ -71,7 +73,7 @@ public void onClick(View v) { private void initMapView() { String type = config.getImagePath().substring(0, Math.min(config.getImagePath().length(), 4)); - if (type.equals("http") && (mapPath == null || mapPath.equals(""))) { + if ((type.equals("http") || type.equals("http")) && (mapPath == null || mapPath.equals(""))) { progressDialog = new Dialog(this, R.style.Theme_AppCompat_Dialog_Alert); progressDialog.setTitle("Uploading data"); progressDialog.setContentView(R.layout.dialog_upload); @@ -153,12 +155,11 @@ class DownloadMapImage extends AsyncTask { protected String doInBackground(String... strings) { int count; try { - URL url = new URL(strings[0]); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpsURLConnection connection = MainActivity.createTLSConnInternal(strings[0]); connection.connect(); // download the file - InputStream input = new BufferedInputStream(url.openStream(), 8192); + InputStream input = new BufferedInputStream(connection.getInputStream(), 8192); filepath = strings[1]; // Output stream @@ -184,7 +185,7 @@ protected String doInBackground(String... strings) { connection.disconnect(); } catch (Exception e) { - Log.e("Error: ", e.getMessage()); + Log.e("Error: ", e.getMessage(), e); return "Could not load map, check configuration file"; } diff --git a/app/src/main/java/ca/uoguelph/pspenler/maptracker/WebThread.java b/app/src/main/java/ca/uoguelph/pspenler/maptracker/WebThread.java index 7d8580b..4f3ff1a 100644 --- a/app/src/main/java/ca/uoguelph/pspenler/maptracker/WebThread.java +++ b/app/src/main/java/ca/uoguelph/pspenler/maptracker/WebThread.java @@ -20,6 +20,8 @@ import java.net.URL; import java.util.ArrayList; +import javax.net.ssl.HttpsURLConnection; + public class WebThread extends Thread { private Handler handler; @@ -48,8 +50,8 @@ public void run() { Uri uri = Uri.parse(configUri); br = new BufferedReader(new FileReader(new File(uri.getPath()))); } else if (type.equals("http")) { - URL url = new URL(configUri); - br = new BufferedReader(new InputStreamReader(url.openStream())); + HttpsURLConnection conn = MainActivity.createTLSConnInternal(configUri); + br = new BufferedReader(new InputStreamReader(conn.getInputStream())); } else { throw new Exception("Config file must be web or local address");