NCNetworkingSync.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // NCNetworkingSync.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "NCNetworkingSync.h"
  9. #import "CCUtility.h"
  10. #import "CCCertificate.h"
  11. @implementation NCNetworkingSync
  12. + (NCNetworkingSync *)sharedManager {
  13. static NCNetworkingSync *sharedManager;
  14. @synchronized(self)
  15. {
  16. if (!sharedManager) {
  17. sharedManager = [NCNetworkingSync new];
  18. }
  19. return sharedManager;
  20. }
  21. }
  22. #pragma --------------------------------------------------------------------------------------------
  23. #pragma mark ============================
  24. #pragma --------------------------------------------------------------------------------------------
  25. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  26. {
  27. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  28. __block NSError *returnError = nil;
  29. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  30. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  31. [communication setUserAgent:[CCUtility getUserAgent]];
  32. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  33. // Progress
  34. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  35. dispatch_semaphore_signal(semaphore);
  36. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  37. returnError = error;
  38. dispatch_semaphore_signal(semaphore);
  39. } failureBeforeRequest:^(NSError *error) {
  40. returnError = error;
  41. dispatch_semaphore_signal(semaphore);
  42. }];
  43. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  44. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  45. return returnError;
  46. }
  47. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  48. {
  49. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  50. __block NSError *returnError = nil;
  51. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  52. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  53. [communication setUserAgent:[CCUtility getUserAgent]];
  54. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  55. dispatch_semaphore_signal(semaphore);
  56. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  57. returnError = error;
  58. dispatch_semaphore_signal(semaphore);
  59. }];
  60. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  61. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  62. return returnError;
  63. }
  64. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  65. {
  66. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  67. __block NSError *returnError = nil;
  68. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  69. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  70. [communication setUserAgent:[CCUtility getUserAgent]];
  71. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  72. dispatch_semaphore_signal(semaphore);
  73. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  74. returnError = error;
  75. dispatch_semaphore_signal(semaphore);
  76. }];
  77. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  78. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  79. return returnError;
  80. }
  81. #pragma --------------------------------------------------------------------------------------------
  82. #pragma mark ===== End-to-End Encryption =====
  83. #pragma --------------------------------------------------------------------------------------------
  84. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  85. {
  86. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  87. __block NSError *returnError = nil;
  88. __block NSString *returnToken = nil;
  89. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  90. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  91. [communication setUserAgent:[CCUtility getUserAgent]];
  92. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  93. returnToken = token;
  94. dispatch_semaphore_signal(semaphore);
  95. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  96. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  97. dispatch_semaphore_signal(semaphore);
  98. }];
  99. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  100. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  101. *token = returnToken;
  102. return returnError;
  103. }
  104. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  105. {
  106. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  107. __block NSError *returnError= nil;
  108. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  109. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  110. [communication setUserAgent:[CCUtility getUserAgent]];
  111. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  112. dispatch_semaphore_signal(semaphore);
  113. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  114. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  115. dispatch_semaphore_signal(semaphore);
  116. }];
  117. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  118. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  119. return returnError;
  120. }
  121. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  122. {
  123. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  124. __block NSError *returnError= nil;
  125. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  126. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  127. [communication setUserAgent:[CCUtility getUserAgent]];
  128. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  129. dispatch_semaphore_signal(semaphore);
  130. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  131. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  132. dispatch_semaphore_signal(semaphore);
  133. }];
  134. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  135. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  136. return returnError;
  137. }
  138. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  139. {
  140. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  141. __block NSError *returnError= nil;
  142. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  143. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  144. [communication setUserAgent:[CCUtility getUserAgent]];
  145. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  146. dispatch_semaphore_signal(semaphore);
  147. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  148. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Remove folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  149. dispatch_semaphore_signal(semaphore);
  150. }];
  151. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  152. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  153. return returnError;
  154. }
  155. @end