Browse Source

Updating of connection information in MainActivity now in a own Thread,
deleted because of error.
UploadLog outsourced to HelperUtils

lp-tu 10 years ago
parent
commit
34ec9ce11d

+ 30 - 0
src/de/tudarmstadt/informatik/hostage/commons/HelperUtils.java

@@ -5,19 +5,24 @@ import java.net.UnknownHostException;
 import java.security.KeyStore;
 import java.util.concurrent.ExecutionException;
 
+import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
 import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
 import org.apache.http.conn.ClientConnectionManager;
 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.entity.StringEntity;
 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.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.HTTP;
+
+import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
 import de.tudarmstadt.informatik.hostage.ui.MainActivity;
 import android.content.Context;
@@ -28,6 +33,7 @@ import android.net.NetworkInfo;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.os.Environment;
+import android.preference.PreferenceManager;
 import android.text.TextUtils;
 
 /**
@@ -198,6 +204,30 @@ public final class HelperUtils {
 		}
 	}
 	
+	/**
+	 * Uploads a single Record to a server, specified in the settings.
+	 * @param record The Record to upload.
+	 * @return True if the upload was successful, else false.
+	 */
+	public static boolean uploadSingleRecord(Context context, Record record){
+	  	// Create a https client. Uses MySSLSocketFactory to accept all certificates
+		HttpClient httpclient = HelperUtils.createHttpClient();
+		HttpPost httppost;
+		try {
+			// Create HttpPost
+			httppost = new HttpPost(PreferenceManager.getDefaultSharedPreferences(context).getString("pref_upload", "https://ssi.cased.de"));
+			// Create JSON String of Record
+			StringEntity se = new StringEntity(record.toString(0x01));
+			httppost.setEntity(se);
+			// Execute HttpPost
+			HttpResponse response = httpclient.execute(httppost);
+		} catch (Exception e) {
+			e.printStackTrace();
+			return false;
+		}		
+		return true;
+	}
+	
 	/**
 	 * Concatenates several byte arrays.
 	 * @param bytes The byte arrays.

+ 8 - 3
src/de/tudarmstadt/informatik/hostage/ui/MainActivity.java

@@ -568,18 +568,22 @@ 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() { 
+			  public void run() {
+		*/ 
 				//Get text fields
 					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);
@@ -610,8 +614,9 @@ public class MainActivity extends Activity {
 						externalIPView.setText(externalIP);
 					else
 						externalIPView.setText("-");	
-				  }
-			  }).start();			
+			/*	  }
+			  }).start();		
+			*/	
 	}
 
 

+ 15 - 32
src/de/tudarmstadt/informatik/hostage/ui/ViewLog.java

@@ -61,8 +61,6 @@ import android.widget.Toast;
  */
 public class ViewLog extends Activity {
 	
-	public static final int JSON = 0x01;
-	
 	private final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm", Locale.US);
 
 	private Logger logger;
@@ -196,53 +194,36 @@ public class ViewLog extends Activity {
 		builder.setOnlyAlertOnce(false);
 		mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 		mNotifyManager.notify(2, builder.build());
+		final Context context = this;
 		// Create a new Thread for upload
 		new Thread(new Runnable() {
 			  public void run() {	
-				  	// Create a https client. Uses MySSLSocketFactory to accept all certificates
-					HttpClient httpclient = HelperUtils.createHttpClient();
 					// get RecordList
 					ArrayList<Record> recordList = logger.getRecordOfEachAttack(lastUploadedAttackId);
 					final int progressMax = recordList.size();		
 					Log.i("SQLLogger", "Logs to upload: " + progressMax);	
-							HttpPost httppost;
+							
 							int progressBarStatus = 0;
 							int retry_counter = 0;
 							while(progressBarStatus < progressMax){
 								Record record = recordList.get(progressBarStatus);
 								if(record.getExternalIP() != null){
-									Log.i("SQLLogger", "Uploading log: " + progressBarStatus);
-									try {
-									// Create HttpPost
-									httppost = new HttpPost(pref.getString("pref_upload", "https://ssi.cased.de"));
-									// Create JSON String of Record
-									StringEntity se = new StringEntity(record.toString(JSON));
-									httppost.setEntity(se);
-									// Execute HttpPost
-									HttpResponse response = httpclient.execute(httppost);
-									Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getReasonPhrase());	
-									Log.i("SQLLogger", "Statuscode of log "  + progressBarStatus + ": " + " " + response.getStatusLine().getStatusCode());	
 									retry_counter = 0;
-									// Update Notification progress bar
-									progressBarStatus++;								
-									builder.setProgress(progressMax, progressBarStatus, false);
-				                     // Update the progress bar
-									mNotifyManager.notify(2, builder.build());
-									} catch (Exception e) {
-
+									if(HelperUtils.uploadSingleRecord(context, record)){
+										// Update Notification progress bar
+										progressBarStatus++;								
+										builder.setProgress(progressMax, progressBarStatus, false);
+					                     // Update the progress bar
+										mNotifyManager.notify(2, builder.build());
+									}else{
 										retry_counter++;
 										if(retry_counter == 3){
 											retry_counter = 0;
 											progressBarStatus++;
-											Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed. Continue with next log");
-										} else {
-											Log.i("SQLLogger", "Upload of log " + progressBarStatus + " failed...retry");
-										}
-
-										e.printStackTrace();
-									}
-								}								
-							}
+										}	
+									}						
+								}
+							}	
 							
 					if(progressBarStatus == progressMax){
 						// When the loop is finished, updates the notification
@@ -256,6 +237,8 @@ public class ViewLog extends Activity {
 		editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackId - 1);
 		editor.commit();				
 	}
+	
+
 
 	/**
 	 * Starts a ViewLogTable Activity.