Skip to content

Commit

Permalink
Added internal certificate which allows us to use HTTPS to our local …
Browse files Browse the repository at this point in the history
…server on campus
  • Loading branch information
co60ca committed Sep 14, 2018
1 parent 37e221b commit c03a335
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

interface AsyncResponse {
void processFinish(String output);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -153,12 +155,11 @@ class DownloadMapImage extends AsyncTask<String, Integer, String> {
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
Expand All @@ -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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit c03a335

Please sign in to comment.