Browse Source

sync bugfixes

Mihai Plasoianu 8 years ago
parent
commit
271de9ac78

+ 0 - 5
src/de/tudarmstadt/informatik/hostage/logging/AttackRecord.java

@@ -193,9 +193,4 @@ public class AttackRecord implements Parcelable, Serializable {
 						getProtocol(), getRemoteIP(), getRemotePort(), getExternalIP(), getLocalPort());
 	}
 
-	public String toJSON() {
-		return String.format("{\"bssid\":\"%s\",\"ssid\":\"%s\",\"latitude\":%d,\"longitude\":%d,\"timestamp\":%d,\"attacks\":%d,\"portscans\":%d}", "bssid",
-				"ssid", 23, 32, 123456, 22, 2);
-	}
-
 }

+ 58 - 39
src/de/tudarmstadt/informatik/hostage/logging/NetworkRecord.java

@@ -8,8 +8,8 @@ import android.os.Parcelable;
 /**
  * Holds all necessary information about a single network.
  */
-public class NetworkRecord implements Parcelable, Serializable{	
-	
+public class NetworkRecord implements Parcelable, Serializable {
+
 	private static final long serialVersionUID = -1586629159904177836L;
 
 	private String bssid;
@@ -18,122 +18,141 @@ public class NetworkRecord implements Parcelable, Serializable{
 	private double latitude;
 	private double longitude;
 	private float accuracy;
-	
-    public static final Parcelable.Creator<NetworkRecord> CREATOR = new Parcelable.Creator<NetworkRecord>() {
-    	@Override
-        public NetworkRecord createFromParcel(Parcel source) {
-                    return new NetworkRecord(source);
-            }
-
-            @Override
-            public NetworkRecord[] newArray(int size) {
-            	return new NetworkRecord[size];
-            }
-    };
 
+	public static final Parcelable.Creator<NetworkRecord> CREATOR = new Parcelable.Creator<NetworkRecord>() {
+		@Override
+		public NetworkRecord createFromParcel(Parcel source) {
+			return new NetworkRecord(source);
+		}
 
-    public NetworkRecord() {
+		@Override
+		public NetworkRecord[] newArray(int size) {
+			return new NetworkRecord[size];
+		}
+	};
 
-    }
+	public NetworkRecord() {
 
+	}
 
-    public NetworkRecord(Parcel source) {
-            this.bssid = source.readString();
-            this.ssid = source.readString();  
-            this.timestampLocation = source.readLong();
-            this.latitude = source.readDouble();
-            this.longitude = source.readDouble();
-            this.accuracy = source.readFloat();
-    }
+	public NetworkRecord(Parcel source) {
+		this.bssid = source.readString();
+		this.ssid = source.readString();
+		this.timestampLocation = source.readLong();
+		this.latitude = source.readDouble();
+		this.longitude = source.readDouble();
+		this.accuracy = source.readFloat();
+	}
 
-	
 	@Override
 	public int describeContents() {
 		return 0;
 	}
-	
+
 	@Override
 	public void writeToParcel(Parcel dest, int flags) {
-        dest.writeString(bssid);
-        dest.writeString(ssid);
-        dest.writeLong(timestampLocation);
-        dest.writeDouble(latitude);
-        dest.writeDouble(longitude);
-        dest.writeFloat(accuracy);
+		dest.writeString(bssid);
+		dest.writeString(ssid);
+		dest.writeLong(timestampLocation);
+		dest.writeDouble(latitude);
+		dest.writeDouble(longitude);
+		dest.writeFloat(accuracy);
 
 	}
-	
+
 	/**
 	 * @return the bssid
 	 */
 	public String getBssid() {
 		return bssid;
 	}
+
 	/**
-	 * @param bssid the bssid to set
+	 * @param bssid
+	 *            the bssid to set
 	 */
 	public void setBssid(String bssid) {
 		this.bssid = bssid;
 	}
+
 	/**
 	 * @return the ssid
 	 */
 	public String getSsid() {
 		return ssid;
 	}
+
 	/**
-	 * @param ssid the ssid to set
+	 * @param ssid
+	 *            the ssid to set
 	 */
 	public void setSsid(String ssid) {
 		this.ssid = ssid;
 	}
+
 	/**
 	 * @return the timestampLocation
 	 */
 	public long getTimestampLocation() {
 		return timestampLocation;
 	}
+
 	/**
-	 * @param timestampLocation the timestampLocation to set
+	 * @param timestampLocation
+	 *            the timestampLocation to set
 	 */
 	public void setTimestampLocation(long timestampLocation) {
 		this.timestampLocation = timestampLocation;
 	}
+
 	/**
 	 * @return the latitude
 	 */
 	public double getLatitude() {
 		return latitude;
 	}
+
 	/**
-	 * @param latitude the latitude to set
+	 * @param latitude
+	 *            the latitude to set
 	 */
 	public void setLatitude(double latitude) {
 		this.latitude = latitude;
 	}
+
 	/**
 	 * @return the longitude
 	 */
 	public double getLongitude() {
 		return longitude;
 	}
+
 	/**
-	 * @param longitude the longitude to set
+	 * @param longitude
+	 *            the longitude to set
 	 */
 	public void setLongitude(double longitude) {
 		this.longitude = longitude;
 	}
+
 	/**
 	 * @return the accuracy
 	 */
 	public float getAccuracy() {
 		return accuracy;
 	}
+
 	/**
-	 * @param accuracy the accuracy to set
+	 * @param accuracy
+	 *            the accuracy to set
 	 */
 	public void setAccuracy(float accuracy) {
 		this.accuracy = accuracy;
 	}
 
+	public String toJSON() {
+		return String.format("{\"bssid\":\"%s\",\"ssid\":%s,\"latitude\":%s,\"longitude\":%s,\"timestamp\":%s,\"attacks\":%d,\"portscans\":%d}", bssid, ssid,
+				latitude, longitude, timestampLocation, -1, -1);
+	}
+
 }

+ 3 - 1
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -662,7 +662,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	 * @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
 	 * @see  {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
 	 */
-	public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {;
+	public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
 		for (NetworkRecord record : networkInformation) {
 			updateNetworkInformation(record);
 		}
@@ -771,6 +771,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		return syncInfo;
 	}	
 	
+	
+	
 	/**
 	 * Updates the sync_info table with the information contained in the parameter.
 	 * @param networkInformation ArrayList of {@link SyncInfoRecord SyncInfoRecords}

+ 27 - 10
src/de/tudarmstadt/informatik/hostage/sync/tracing/TracingSyncService.java

@@ -25,6 +25,8 @@ 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 org.json.JSONException;
+import org.json.JSONObject;
 
 import android.app.IntentService;
 import android.content.Intent;
@@ -34,7 +36,8 @@ import android.os.Bundle;
 import android.os.ResultReceiver;
 import android.preference.PreferenceManager;
 import android.util.Log;
-import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
+import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
+import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
 import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 
@@ -106,13 +109,11 @@ public class TracingSyncService extends IntentService {
 		// String serverAddress = pref.getString("pref_upload",
 		// "https://ssi.cased.de");
 		String serverAddress = "http://87.230.23.240/hostage/push.php";
-		ArrayList<AttackRecord> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId);
+		ArrayList<NetworkRecord> recordList = dbh.getNetworkInformation();
+
 		int size = recordList.size();
 		int offset = 1;
-		for (AttackRecord record : recordList) {
-			editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
-			editor.commit();
-
+		for (NetworkRecord record : recordList) {
 			boolean success = uploadSingleRecord(record, serverAddress);
 			Log.i("Tracing upload", "Upload of record: " + offset + "/" + size + ((success) ? " successful." : " failed."));
 			if (receiver != null) {
@@ -136,7 +137,7 @@ public class TracingSyncService extends IntentService {
 	 * @serverAddress Address of the target server
 	 * @return True if the upload was successful, else false.
 	 */
-	private boolean uploadSingleRecord(AttackRecord record, String serverAddress) {
+	private boolean uploadSingleRecord(NetworkRecord record, String serverAddress) {
 		// Create a https client. Uses MySSLSocketFactory to accept all
 		// certificates
 		HttpPost httppost;
@@ -147,11 +148,13 @@ public class TracingSyncService extends IntentService {
 			// Create JSON String of Record
 			// TODO StringEntity se = new
 			// StringEntity(record.toString(TraCINgFormatter.getInstance()));
-			StringEntity se = new StringEntity("record=" + record.toJSON()); // FIXME
+			String s = record.toJSON();
+			StringEntity se = new StringEntity("record=" + record.toJSON());
 			httppost.addHeader("content-type", "application/x-www-form-urlencoded");
 			httppost.setEntity(se);
 			// Execute HttpPost
 			HttpResponse response = httpClient.execute(httppost);
+			getRemoteData(record.getBssid(), record.getTimestampLocation());
 			Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
 		} catch (Exception e) {
 			e.printStackTrace();
@@ -168,7 +171,7 @@ public class TracingSyncService extends IntentService {
 		OutputStreamWriter request = null;
 		URL url = null;
 		String response = null;
-		String parameters = "bssid=" + bssid + "timestamp=" + timestamp;
+		String parameters = "bssid=" + bssid;
 		try {
 			url = new URL("http://87.230.23.240/hostage/pull.php");
 			connection = (HttpURLConnection) url.openConnection();
@@ -189,11 +192,25 @@ public class TracingSyncService extends IntentService {
 				sb.append(line);
 			}
 			response = sb.toString();
+			JSONObject jsonObj = new JSONObject(response);
+				NetworkRecord net = new NetworkRecord();
+				net.setBssid(jsonObj.getString("bssid"));
+				net.setSsid(jsonObj.getString("ssid"));
+				net.setLatitude(jsonObj.getDouble("latitude"));
+				net.setLongitude(jsonObj.getDouble("longitude"));
+				net.setTimestampLocation(jsonObj.getLong("timestamp"));
+				SyncInfoRecord sync = new SyncInfoRecord();
+				sync.setBSSID(jsonObj.getString("bssid"));
+				sync.setDeviceID("-1");
+				sync.setNumber_of_attacks(jsonObj.getLong("attacks"));
+				sync.setNumber_of_portscans(jsonObj.getLong("portscans"));
+				dbh.updateNetworkInformation(net);
 			isr.close();
 			reader.close();
-			Log.d("TEST", response);
 		} catch (IOException e) {
 			Log.i("NetworkTest", "Network Error: " + e);
+		} catch (JSONException e) {
+			e.printStackTrace();
 		}
 	}
 

+ 86 - 70
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

@@ -4,12 +4,10 @@ import java.util.ArrayList;
 
 import android.annotation.SuppressLint;
 import android.app.Activity;
-import android.app.AlertDialog;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
@@ -27,6 +25,7 @@ import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
 import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
 import de.tudarmstadt.informatik.hostage.model.Profile;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
 import de.tudarmstadt.informatik.hostage.ui.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
@@ -263,74 +262,91 @@ public class HomeFragment extends Fragment {
 		if (switchChangeListener == null) {
 			switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
 				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-					//startActivity(new Intent(getActivity(), TracingSyncActivity.class));
-					if (!HelperUtils.isWifiConnected(getActivity())) {
-						new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.wifi_not_connected_msg)
-								.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
-									public void onClick(DialogInterface dialog, int which) {
-
-									}
-								}).setIcon(android.R.drawable.ic_dialog_info).show();
-
-						setStateNotActive();
-						setStateNotConnected();
-					} else {
-						if (isChecked) {
-							boolean protocolActivated = false;
-							if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
-								for (String protocol : getResources().getStringArray(R.array.protocols)) {
-									if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
-										MainActivity.getInstance().getHostageService().startListener(protocol);
-										protocolActivated = true;
-									}
-								}
-							} else {
-								ProfileManager profileManager = ProfileManager.getInstance();
-
-								if (profileManager.isRandomActive()) {
-									profileManager.randomizeProtocols(profileManager.getRandomProfile());
-								}
-
-								for (String protocol : profileManager.getCurrentActivatedProfile().getActiveProtocols()) {
-									if (protocol.equals("GHOST"))
-										continue;
-
-									if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
-										MainActivity.getInstance().getHostageService().startListener(protocol);
-										protocolActivated = true;
-									}
-								}
-
-								Profile currentProfile = profileManager.getCurrentActivatedProfile();
-
-								if (currentProfile.mGhostActive) {
-									for (int port : currentProfile.getGhostPorts()) {
-										MainActivity.getInstance().getHostageService().startListener("GHOST", port);
-										protocolActivated = true;
-									}
-								}
-							}
-
-							if (protocolActivated) {
-								setStateActive();
-							} else {
-								new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
-										.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
-											public void onClick(DialogInterface dialog, int which) {
-
-											}
-										}).setIcon(android.R.drawable.ic_dialog_info).show();
-
-								setStateNotActive();
-							}
-						} else {
-							if (MainActivity.getInstance().getHostageService() != null) {
-								MainActivity.getInstance().getHostageService().stopListeners();
-								MainActivity.getInstance().stopAndUnbind();
-							}
-							setStateNotActive();
-						}
-					}
+					startActivity(new Intent(getActivity(), TracingSyncActivity.class));
+					// if (!HelperUtils.isWifiConnected(getActivity())) {
+					// new
+					// AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.wifi_not_connected_msg)
+					// .setPositiveButton(android.R.string.ok, new
+					// DialogInterface.OnClickListener() {
+					// public void onClick(DialogInterface dialog, int which) {
+					//
+					// }
+					// }).setIcon(android.R.drawable.ic_dialog_info).show();
+					//
+					// setStateNotActive();
+					// setStateNotConnected();
+					// } else {
+					// if (isChecked) {
+					// boolean protocolActivated = false;
+					// if
+					// (ProfileManager.getInstance().getCurrentActivatedProfile()
+					// == null) {
+					// for (String protocol :
+					// getResources().getStringArray(R.array.protocols)) {
+					// if
+					// (!MainActivity.getInstance().getHostageService().isRunning(protocol))
+					// {
+					// MainActivity.getInstance().getHostageService().startListener(protocol);
+					// protocolActivated = true;
+					// }
+					// }
+					// } else {
+					// ProfileManager profileManager =
+					// ProfileManager.getInstance();
+					//
+					// if (profileManager.isRandomActive()) {
+					// profileManager.randomizeProtocols(profileManager.getRandomProfile());
+					// }
+					//
+					// for (String protocol :
+					// profileManager.getCurrentActivatedProfile().getActiveProtocols())
+					// {
+					// if (protocol.equals("GHOST"))
+					// continue;
+					//
+					// if
+					// (!MainActivity.getInstance().getHostageService().isRunning(protocol))
+					// {
+					// MainActivity.getInstance().getHostageService().startListener(protocol);
+					// protocolActivated = true;
+					// }
+					// }
+					//
+					// Profile currentProfile =
+					// profileManager.getCurrentActivatedProfile();
+					//
+					// if (currentProfile.mGhostActive) {
+					// for (int port : currentProfile.getGhostPorts()) {
+					// MainActivity.getInstance().getHostageService().startListener("GHOST",
+					// port);
+					// protocolActivated = true;
+					// }
+					// }
+					// }
+					//
+					// if (protocolActivated) {
+					// setStateActive();
+					// } else {
+					// new
+					// AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
+					// .setPositiveButton(android.R.string.ok, new
+					// DialogInterface.OnClickListener() {
+					// public void onClick(DialogInterface dialog, int which) {
+					//
+					// }
+					// }).setIcon(android.R.drawable.ic_dialog_info).show();
+					//
+					// setStateNotActive();
+					// }
+					// } else {
+					// if (MainActivity.getInstance().getHostageService() !=
+					// null) {
+					// MainActivity.getInstance().getHostageService().stopListeners();
+					// MainActivity.getInstance().stopAndUnbind();
+					// }
+					// setStateNotActive();
+					// }
+					// }
 				}
 			};
 		}