Browse Source

Included GetExternalIPTask in MainActivity to accelerate startup.(not
tested)

lp-tu 10 years ago
parent
commit
bc9660148a

+ 5 - 5
src/de/tudarmstadt/informatik/hostage/HoneyService.java

@@ -39,7 +39,7 @@ public class HoneyService extends Service {
 
 	private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
 	private NotificationCompat.Builder builder;
-	private SharedPreferences pref;
+	private SharedPreferences sessionPref;
 	private Editor editor;
 	private boolean stopped = false;
 
@@ -70,8 +70,8 @@ public class HoneyService extends Service {
 	public void onCreate() {
 		super.onCreate();
 		log = new SQLLogger(getApplicationContext());
-		pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
-		editor = pref.edit();
+		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		editor = sessionPref.edit();
 		createNotification();
 		for (Protocol protocol : getProtocolArray()) {
 			listeners.add(new HoneyListener(this, protocol));
@@ -127,7 +127,7 @@ public class HoneyService extends Service {
 	private BroadcastReceiver netReceiver = new BroadcastReceiver() {
 		@Override
 		public void onReceive(Context context, Intent intent) {
-				String bssid_old = pref.getString(MainActivity.BSSID, "");
+				String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
 				String bssid_new = HelperUtils.getBSSID(context);
 				if(!stopped && (bssid_new == null || !bssid_new.equals(bssid_old))){
 					wifiChangeNotification();
@@ -156,7 +156,7 @@ public class HoneyService extends Service {
 		boolean bssidSeen = false;
 		
 		for(String protocol : getResources().getStringArray(R.array.protocols)){
-			int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
+			int handlerCount = sessionPref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
 			if(handlerCount > 0){
 				activeHandlers = true;
 			}

+ 0 - 39
src/de/tudarmstadt/informatik/hostage/commons/GetExternalIPTask.java

@@ -1,39 +0,0 @@
-package de.tudarmstadt.informatik.hostage.commons;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
-import org.json.JSONObject;
-
-import android.os.AsyncTask;
-/**
- * A AsyncTask that determines and returns the external IP address of the device.
- * Therefore it uses a given URL to get a JSON Object containing the external IP address.
- * @author Lars Pandikow
- *
- */
-public class GetExternalIPTask extends AsyncTask<String, Void, String> {
-	 @Override
-    protected String doInBackground(String... url) {
-   	 String ipAddress = null;
-			try {
-				HttpClient httpclient = new DefaultHttpClient();
-				HttpGet httpget = new HttpGet(url[0]);
-				HttpResponse response;
-
-				response = httpclient.execute(httpget);
-
-				HttpEntity entity = response.getEntity();
-				entity.getContentLength();
-				String str = EntityUtils.toString(entity);
-				JSONObject json_data = new JSONObject(str);
-				ipAddress = json_data.getString("ip");
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-	return ipAddress;
-    }
-}

+ 1 - 36
src/de/tudarmstadt/informatik/hostage/commons/HelperUtils.java

@@ -122,42 +122,7 @@ public final class HelperUtils {
 		return new byte[] { (byte) ((bytes) & 0xff),
 				(byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff),
 				(byte) ((bytes >>> 24) & 0xff) };
-	}
-
-	/**
-	 * Determines and returns the external IP address of the device.
-	 * @param context  Needs a context to get system recourses.
-	 * @return A String representation of the external IP of the device or null if no Internet connectivity exists.
-	 * @see GetExternalIPTask
-	 */
-	public static String getExternalIP(Context context) {
-		String ipAddress = null;
-		GetExternalIPTask task = new GetExternalIPTask();
-		task.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
-		try {
-			ipAddress =  task.get();
-		} catch (InterruptedException e) {
-			e.printStackTrace();
-		} catch (ExecutionException e) {
-			e.printStackTrace();
-		}
-		return ipAddress;
-	}
-	 
-	/**
-	 * Updates the connextion info and saves them in the the SharedPreferences for session data.
-	 * @param context Needs a context to get system recourses.
-	 * @see MainActivity#SESSION_DATA
-	 */
-	public static void updateConnectionInfo(Context context) {	
-		SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
-		Editor editor = pref.edit();
-		editor.putString(MainActivity.SSID, getSSID(context));
-		editor.putString(MainActivity.BSSID, getBSSID(context));
-		editor.putString(MainActivity.INTERNAL_IP, getInternalIP(context));
-		editor.putString(MainActivity.EXTERNAL_IP, getExternalIP(context));
-		editor.commit();
-	}	
+	} 
 
 	/**
 	 * Checks if external storage is available for read and write.

+ 7 - 5
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -5,6 +5,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
 
+import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;
@@ -15,6 +16,7 @@ import de.tudarmstadt.informatik.hostage.logging.Logger;
 import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
+import de.tudarmstadt.informatik.hostage.ui.MainActivity;
 /**
  * Abstract class for a connection handler using a given protocol.
  * @author Mihai Plasoianu 
@@ -55,16 +57,16 @@ public abstract class AbstractHandler implements Runnable {
 		this.protocol = protocol;
 		this.client = client;
 		this.thread = new Thread(this);
-		SharedPreferences pref = PreferenceManager
-				.getDefaultSharedPreferences(service);
+		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
 		Editor editor = pref.edit();
 		TIMEOUT = pref.getInt("timeout", 30) * 1000;
 		attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
 		editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
 		editor.commit();
-		BSSID = HelperUtils.getBSSID(service.getApplicationContext());
-		SSID = HelperUtils.getSSID(service.getApplicationContext());
-		externalIP = HelperUtils.getExternalIP(service.getApplicationContext());
+		SharedPreferences sessionPref = service.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		BSSID = sessionPref.getString(MainActivity.BSSID, null);
+		SSID = sessionPref.getString(MainActivity.SSID, null);
+		externalIP = sessionPref.getString(MainActivity.EXTERNAL_IP, null);
 		setSoTimeout(client);
 		thread.start();
 	}

+ 1 - 0
src/de/tudarmstadt/informatik/hostage/logging/Record.java

@@ -2,6 +2,7 @@ package de.tudarmstadt.informatik.hostage.logging;
 
 import java.io.Serializable;
 import java.net.InetAddress;
+
 /**
  * This class defines the attributes of a record.<br>
  * A Record is a single message exchanged between the application and an attacker.<br>

+ 78 - 38
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -3,6 +3,14 @@ package de.tudarmstadt.informatik.hostage.ui;
 import java.util.ArrayList;
 import java.util.HashMap;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.json.JSONObject;
+
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManager.RunningServiceInfo;
@@ -15,6 +23,7 @@ import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.net.ConnectivityManager;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.support.v4.content.LocalBroadcastManager;
@@ -93,8 +102,8 @@ public class MainActivity extends Activity {
 
 	private HoneyService mService;
 	private boolean serviceBound;
-	private SharedPreferences pref;
-	private Editor editor;
+	private SharedPreferences sessionPref;
+	private Editor sessionEditor;
 	private Logger logger;
 
 	// variables for the swipe animation
@@ -121,9 +130,9 @@ public class MainActivity extends Activity {
 		initViewAnimator();
 		initListView();
 		// Initialize Class variables
-		pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
 		logger = new SQLLogger(this);
-		editor = pref.edit();
+		sessionEditor = sessionPref.edit();
 	}
 
 	@Override
@@ -157,7 +166,7 @@ public class MainActivity extends Activity {
 		if (isServiceRunning()) {
 			bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
 		} else {
-			String bssid_old = pref.getString(MainActivity.BSSID, "");
+			String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
 			String bssid_new = HelperUtils.getBSSID(this);
 			if(bssid_new == null || !bssid_new.equals(bssid_old)){
 				deleteSessionData();
@@ -165,7 +174,7 @@ public class MainActivity extends Activity {
 		}
 		// Update UI
 		updateUI();
-		updateConnectionInfo();
+		updateConnectionInfText();
 	}
 
 	@Override
@@ -366,8 +375,8 @@ public class MainActivity extends Activity {
 	 * Deletes all session related Data.
 	 */
 	private void deleteSessionData(){
-    	editor.clear();
-    	editor.commit();
+    	sessionEditor.clear();
+    	sessionEditor.commit();
 	}
 
 	/**
@@ -422,13 +431,13 @@ public class MainActivity extends Activity {
 	private BroadcastReceiver netReceiver = new BroadcastReceiver() {
 		@Override
 		public void onReceive(Context context, Intent intent) {
-			String bssid_old = pref.getString(BSSID, "");
+			String bssid_old = sessionPref.getString(BSSID, "");
 			String bssid_new = HelperUtils.getBSSID(context);
 			if ((bssid_new == null || !bssid_new.equals(bssid_old)) && serviceBound) {
 				Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
 				unbindService();
 			}	
-			updateConnectionInfo();
+			updateConnectionInfText();
 		}
 	};	
 
@@ -444,9 +453,9 @@ public class MainActivity extends Activity {
 		//Update protocol lights and connection information.
 		for(String protocol : getResources().getStringArray(R.array.protocols)){
 			//Check if protocol is active
-			if(pref.getBoolean(protocol + LISTENER, false)){
+			if(sessionPref.getBoolean(protocol + LISTENER, false)){
 				activeListeners = true;
-				int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0);
+				int handlerCount = sessionPref.getInt(protocol + HANDLER_COUNT, 0);
 				//Check if attacks have been recorded in this session.
 				if(handlerCount > 0){
 					activeHandlers = true;
@@ -567,33 +576,24 @@ public class MainActivity extends Activity {
 	/**
 	 * Gets Information about connection state and updates the GUI.
 	 */
-	private void updateConnectionInfo() {
-		/*
-		final Context context = this;
-		new Thread(new Runnable() {
-			  public void run() {
-		*/ 
-				//Get text fields
+	private void updateConnectionInfText() {
 					TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue);
 					TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue);
 					TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue);
 					TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
-					/*
-					ssidView.setText("Loading...");
-					bssidView.setText("Loading...");
-					internalIPView.setText("Loading...");
+
 					externalIPView.setText("Loading...");
-					*/
 					
 					//Update the connection information
-					HelperUtils.updateConnectionInfo(context);
+					updateConnectionInfo();
+					SetExternalIPTask async = new SetExternalIPTask();
+					async.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
 					
 					//Get connection information
-					String ssid = pref.getString(SSID, "-");
-					String bssid = pref.getString(BSSID, "-");
-					String internalIP = pref.getString(INTERNAL_IP, "-");
-					String externalIP = pref.getString(EXTERNAL_IP, "-");
-
+					String ssid = sessionPref.getString(SSID, null);
+					String bssid = sessionPref.getString(BSSID, null);
+					String internalIP = sessionPref.getString(INTERNAL_IP, null);
+					
 					//Set text fields
 					if (ssid != null)
 						ssidView.setText(ssid);
@@ -609,15 +609,55 @@ public class MainActivity extends Activity {
 						internalIPView.setText(internalIP);
 					else
 						internalIPView.setText("-");
-					
-					if (externalIP != null)
-						externalIPView.setText(externalIP);
-					else
-						externalIPView.setText("-");	
-			/*	  }
-			  }).start();		
-			*/	
 	}
+	
+	/**
+	 * Updates the connection info and saves them in the the SharedPreferences for session data.
+	 * @param context Needs a context to get system recourses.
+	 * @see MainActivity#SESSION_DATA
+	 */
+	private void updateConnectionInfo() {	
+		SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
+		Editor editor = pref.edit();
+		editor.putString(MainActivity.SSID, HelperUtils.getSSID(context));
+		editor.putString(MainActivity.BSSID, HelperUtils.getBSSID(context));
+		editor.putString(MainActivity.INTERNAL_IP, HelperUtils.getInternalIP(context));
+		editor.commit();
+	}	
+	
+	private class SetExternalIPTask extends AsyncTask<String, Void, String>{	
+		 @Override
+		    protected String doInBackground(String... url) {
+		   	 String ipAddress = null;
+					try {
+						HttpClient httpclient = new DefaultHttpClient();
+						HttpGet httpget = new HttpGet(url[0]);
+						HttpResponse response;
+
+						response = httpclient.execute(httpget);
+
+						HttpEntity entity = response.getEntity();
+						entity.getContentLength();
+						String str = EntityUtils.toString(entity);
+						JSONObject json_data = new JSONObject(str);
+						ipAddress = json_data.getString("ip");
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+			return ipAddress;
+		    }
+			 
+			@Override
+			protected void onPostExecute(String result){
+				sessionEditor.putString(MainActivity.EXTERNAL_IP, result);
+				sessionEditor.commit();
+				TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
+				if (result != null)
+					externalIPView.setText(result);
+				else
+					externalIPView.setText("-");	
+			}
+		};
 
 
 	/*############# Help functions for animation ##################*/

+ 3 - 1
src/de/tudarmstadt/informatik/hostage/ui/ViewLog.java

@@ -161,7 +161,8 @@ public class ViewLog extends Activity {
 	 * The local and remote IP of each record will be replaced with the external IP of then device at the time of the upload.
 	 * For the Upload it uses a HttpPost with a HttpsClient, which does not validate any certificates.<br>
 	 * The Upload runs in its own Thread.<br>
-	 * While uploading the method also creates a notification to inform the user about the status of the upload.
+	 * While uploading the method also creates a notification to inform the user about the status of the upload.<br>
+	 * <b>Only uploads Records with a external IP that is not null!
 	 * @param  view View elements which triggers the method call.
 	 * @see de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory
 	 */
@@ -207,6 +208,7 @@ public class ViewLog extends Activity {
 							int retry_counter = 0;
 							while(progressBarStatus < progressMax){
 								Record record = recordList.get(progressBarStatus);
+								// Only upload records with a saved external ip
 								if(record.getExternalIP() != null){
 									retry_counter = 0;
 									if(HelperUtils.uploadSingleRecord(context, record)){