TracingSyncService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package de.tudarmstadt.informatik.hostage.sync.tracing;
  2. import java.security.KeyStore;
  3. import java.util.ArrayList;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.HttpVersion;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.conn.ClientConnectionManager;
  9. import org.apache.http.conn.scheme.PlainSocketFactory;
  10. import org.apache.http.conn.scheme.Scheme;
  11. import org.apache.http.conn.scheme.SchemeRegistry;
  12. import org.apache.http.conn.ssl.SSLSocketFactory;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  16. import org.apache.http.params.BasicHttpParams;
  17. import org.apache.http.params.HttpParams;
  18. import org.apache.http.params.HttpProtocolParams;
  19. import org.apache.http.protocol.HTTP;
  20. import android.app.IntentService;
  21. import android.content.Intent;
  22. import android.content.SharedPreferences;
  23. import android.content.SharedPreferences.Editor;
  24. import android.os.Bundle;
  25. import android.os.ResultReceiver;
  26. import android.preference.PreferenceManager;
  27. import android.util.Log;
  28. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  29. import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
  30. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  31. public class TracingSyncService extends IntentService{
  32. public static final String REMOTE_DEVICE = "de.tudarmstadt.informatik.hostage.REMOTE_DEVICE";
  33. public static final String ACTION_START_SYNC = "de.tudarmstadt.informatik.hostage.ACTION_START_SYNC";
  34. public static final String EXTRA_RECEIVER = "de.tudarmstadt.informatik.hostage.EXTRA_HANDLER";
  35. public static final String UPLOAD_SIZE = "de.tudarmstadt.informatik.hostage.UPLOAD_SIZE";
  36. public static final String UPLOAD_PROGRESS = "de.tudarmstadt.informatik.hostage.UPLOAD_PROGRESS";
  37. public static final int RECORD_UPLOADED = 0x00;
  38. public static final int SYNC_COMPLETE = 0x01;
  39. private HttpClient httpClient;
  40. private ResultReceiver receiver;
  41. HostageDBOpenHelper dbh;
  42. SharedPreferences pref;
  43. Editor editor;
  44. public TracingSyncService() {
  45. super(TracingSyncService.class.getName());
  46. }
  47. @Override
  48. public void onCreate() {
  49. super.onCreate();
  50. pref = PreferenceManager.getDefaultSharedPreferences(this);
  51. editor = pref.edit();
  52. dbh = new HostageDBOpenHelper(this);
  53. }
  54. /**
  55. * The IntentService calls this method from the default worker thread with
  56. * the intent that started the service. When this method returns, IntentService
  57. * stops the service, as appropriate.
  58. */
  59. @Override
  60. protected void onHandleIntent(Intent intent) {
  61. if (intent != null) {
  62. final String action = intent.getAction();
  63. if (ACTION_START_SYNC.equals(action)) {
  64. receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
  65. uploadNewRecords();
  66. getRemoteData();
  67. //TODO add: dbh.clearSyncInfos();
  68. if(receiver != null){
  69. receiver.send(SYNC_COMPLETE, null);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * Uploads all new Records to a server, specified in the settings.
  76. */
  77. private void uploadNewRecords() {
  78. int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  79. String serverAddress = pref.getString("pref_upload", "https://ssi.cased.de");
  80. ArrayList<AttackRecord> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId);
  81. int size = recordList.size();
  82. int offset = 1;
  83. for(AttackRecord record: recordList){
  84. editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
  85. editor.commit();
  86. boolean success = uploadSingleRecord(record, serverAddress);
  87. Log.i("Tracing upload", "Upload of record: " + offset +"/" + size + ((success) ? " successful.": " failed."));
  88. if(receiver != null){
  89. Bundle data = new Bundle();
  90. data.putInt(UPLOAD_SIZE, size);
  91. data.putInt(UPLOAD_PROGRESS, offset);
  92. receiver.send(RECORD_UPLOADED, data);
  93. }
  94. offset++;
  95. }
  96. }
  97. /**
  98. * Uploads a single Record to a server, specified in the settings.
  99. *
  100. * @param record The Record to upload.
  101. * @serverAddress Address of the target server
  102. * @return True if the upload was successful, else false.
  103. */
  104. private boolean uploadSingleRecord(AttackRecord record, String serverAddress) {
  105. // Create a https client. Uses MySSLSocketFactory to accept all certificates
  106. HttpPost httppost;
  107. try {
  108. httpClient = createHttpClient();
  109. // Create HttpPost
  110. httppost = new HttpPost(serverAddress);
  111. // Create JSON String of Record
  112. //TODO FIX ME
  113. //StringEntity se = new StringEntity( record.toString(TraCINgFormatter.getInstance()) );
  114. StringEntity se = new StringEntity(record.toString());
  115. httppost.setEntity(se);
  116. // Execute HttpPost
  117. HttpResponse response = httpClient.execute(httppost);
  118. Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Uploads a single Record to a server, specified in the settings.
  127. *
  128. * @param record
  129. * The Record to upload.
  130. * @return True if the upload was successful, else false.
  131. */
  132. private void getRemoteData() {
  133. //TODO GET DATA FROM SERVER
  134. //TODO SAVE DATA IN DATABASE
  135. }
  136. /**
  137. * Creates a HttpClient with an own SSL Socket.
  138. *
  139. * @return HttpsClient who accepts accepts all certificates.
  140. * @see MySSLSocketFactory
  141. */
  142. private HttpClient createHttpClient() {
  143. try {
  144. KeyStore trustStore = KeyStore.getInstance(KeyStore
  145. .getDefaultType());
  146. trustStore.load(null, null);
  147. SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
  148. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  149. HttpParams params = new BasicHttpParams();
  150. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  151. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  152. SchemeRegistry registry = new SchemeRegistry();
  153. registry.register(new Scheme("http", PlainSocketFactory
  154. .getSocketFactory(), 80));
  155. registry.register(new Scheme("https", sf, 443));
  156. ClientConnectionManager ccm = new ThreadSafeClientConnManager(
  157. params, registry);
  158. return new DefaultHttpClient(ccm, params);
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. return new DefaultHttpClient();
  162. }
  163. }
  164. }