VLCHTTPUploaderController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*****************************************************************************
  2. * VLCHTTPUploaderController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Jean-Baptiste Kempf <jb # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. * Jean-Romain Prévost <jr # 3on.fr>
  12. * Carola Nitz <caro # videolan.org>
  13. * Ron Soffer <rsoffer1 # gmail.com>
  14. *
  15. * Refer to the COPYING file of the official project for license.
  16. *****************************************************************************/
  17. #import "VLCHTTPUploaderController.h"
  18. #import "VLCHTTPConnection.h"
  19. #import "VLCActivityManager.h"
  20. #import "HTTPServer.h"
  21. #import "Reachability.h"
  22. #import <ifaddrs.h>
  23. #import <arpa/inet.h>
  24. #if TARGET_OS_IOS
  25. #import "VLC-Swift.h"
  26. #import "VLCMediaFileDiscoverer.h"
  27. #endif
  28. @interface VLCHTTPUploaderController()
  29. {
  30. NSString *_nameOfUsedNetworkInterface;
  31. HTTPServer *_httpServer;
  32. UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
  33. Reachability *_reachability;
  34. }
  35. @end
  36. @implementation VLCHTTPUploaderController
  37. + (instancetype)sharedInstance
  38. {
  39. static VLCHTTPUploaderController *sharedInstance = nil;
  40. static dispatch_once_t pred;
  41. dispatch_once(&pred, ^{
  42. sharedInstance = [VLCHTTPUploaderController new];
  43. });
  44. return sharedInstance;
  45. }
  46. - (id)init
  47. {
  48. if (self = [super init]) {
  49. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  50. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  51. name:UIApplicationDidBecomeActiveNotification object:nil];
  52. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  53. name:UIApplicationDidEnterBackgroundNotification object:nil];
  54. [center addObserver:self selector:@selector(netReachabilityChanged) name:kReachabilityChangedNotification object:nil];
  55. BOOL isHTTPServerOn = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus];
  56. [self netReachabilityChanged];
  57. [self changeHTTPServerState:isHTTPServerOn];
  58. }
  59. return self;
  60. }
  61. - (void)applicationDidBecomeActive: (NSNotification *)notification
  62. {
  63. if (!_httpServer.isRunning)
  64. [self changeHTTPServerState:[[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus]];
  65. if (_backgroundTaskIdentifier && _backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
  66. [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
  67. _backgroundTaskIdentifier = 0;
  68. }
  69. }
  70. - (void)applicationDidEnterBackground: (NSNotification *)notification
  71. {
  72. if (_httpServer.isRunning) {
  73. if (!_backgroundTaskIdentifier || _backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
  74. dispatch_block_t expirationHandler = ^{
  75. [self changeHTTPServerState:NO];
  76. [[UIApplication sharedApplication] endBackgroundTask:self->_backgroundTaskIdentifier];
  77. self->_backgroundTaskIdentifier = 0;
  78. };
  79. if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]) {
  80. _backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"VLCUploader" expirationHandler:expirationHandler];
  81. } else {
  82. _backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];
  83. }
  84. }
  85. }
  86. }
  87. - (NSString *)httpStatus
  88. {
  89. if (_httpServer.isRunning) {
  90. if (_httpServer.listeningPort != 80) {
  91. return [NSString stringWithFormat:@"http://%@:%i\nhttp://%@:%i",
  92. [self currentIPAddress],
  93. _httpServer.listeningPort,
  94. [self hostname],
  95. _httpServer.listeningPort];
  96. } else {
  97. return [NSString stringWithFormat:@"http://%@\nhttp://%@",
  98. [self currentIPAddress],
  99. [self hostname]];
  100. }
  101. } else {
  102. return NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
  103. }
  104. }
  105. - (BOOL)isServerRunning
  106. {
  107. return _httpServer.isRunning;
  108. }
  109. - (void)netReachabilityChanged
  110. {
  111. // find an interface to listen on
  112. struct ifaddrs *listOfInterfaces = NULL;
  113. struct ifaddrs *anInterface = NULL;
  114. BOOL serverWasRunning = self.isServerRunning;
  115. [self changeHTTPServerState:NO];
  116. _nameOfUsedNetworkInterface = nil;
  117. int ret = getifaddrs(&listOfInterfaces);
  118. if (ret == 0) {
  119. anInterface = listOfInterfaces;
  120. while (anInterface != NULL) {
  121. if (anInterface->ifa_addr->sa_family == AF_INET) {
  122. APLog(@"Found interface %s, address %@", anInterface->ifa_name, @(inet_ntoa(((struct sockaddr_in *)anInterface->ifa_addr)->sin_addr)));
  123. /* check for primary interface first */
  124. if (strncmp (anInterface->ifa_name,"en0",strlen("en0")) == 0) {
  125. unsigned int flags = anInterface->ifa_flags;
  126. if( (flags & 0x1) && (flags & 0x40) && !(flags & 0x8) ) {
  127. _nameOfUsedNetworkInterface = [NSString stringWithUTF8String:anInterface->ifa_name];
  128. break;
  129. }
  130. }
  131. /* oh well, let's move on to the secondary interface */
  132. if (strncmp (anInterface->ifa_name,"en1",strlen("en1")) == 0) {
  133. unsigned int flags = anInterface->ifa_flags;
  134. if( (flags & 0x1) && (flags & 0x40) && !(flags & 0x8) ) {
  135. _nameOfUsedNetworkInterface = [NSString stringWithUTF8String:anInterface->ifa_name];
  136. break;
  137. }
  138. }
  139. if (strncmp (anInterface->ifa_name,"bridge100",strlen("bridge100")) == 0) {
  140. unsigned int flags = anInterface->ifa_flags;
  141. if( (flags & 0x1) && (flags & 0x40) && !(flags & 0x8) ) {
  142. _nameOfUsedNetworkInterface = [NSString stringWithUTF8String:anInterface->ifa_name];
  143. break;
  144. }
  145. }
  146. }
  147. anInterface = anInterface->ifa_next;
  148. }
  149. }
  150. freeifaddrs(listOfInterfaces);
  151. if (_nameOfUsedNetworkInterface == nil) {
  152. _isReachable = NO;
  153. [self changeHTTPServerState:NO];
  154. return;
  155. }
  156. _isReachable = YES;
  157. if (serverWasRunning) {
  158. [self changeHTTPServerState:YES];
  159. }
  160. }
  161. - (BOOL)changeHTTPServerState:(BOOL)state
  162. {
  163. if (!state) {
  164. [_httpServer stop];
  165. return true;
  166. }
  167. if (_nameOfUsedNetworkInterface == nil) {
  168. APLog(@"No interface to listen on, server not started");
  169. _isReachable = NO;
  170. return NO;
  171. }
  172. #if TARGET_OS_IOS
  173. // clean cache before accepting new stuff
  174. [self cleanCache];
  175. #endif
  176. // Initialize our http server
  177. _httpServer = [[HTTPServer alloc] init];
  178. [_httpServer setInterface:_nameOfUsedNetworkInterface];
  179. [_httpServer setIPv4Enabled:YES];
  180. [_httpServer setIPv6Enabled:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingWiFiSharingIPv6] boolValue]];
  181. // Tell the server to broadcast its presence via Bonjour.
  182. // This allows browsers such as Safari to automatically discover our service.
  183. [_httpServer setType:@"_http._tcp."];
  184. // Serve files from the standard Sites folder
  185. NSString *docRoot = [[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] stringByDeletingLastPathComponent];
  186. APLog(@"Setting document root: %@", docRoot);
  187. [_httpServer setDocumentRoot:docRoot];
  188. [_httpServer setPort:80];
  189. [_httpServer setConnectionClass:[VLCHTTPConnection class]];
  190. NSError *error = nil;
  191. if (![_httpServer start:&error]) {
  192. if (error.code == EACCES) {
  193. APLog(@"Port forbidden by OS, trying another one");
  194. [_httpServer setPort:8888];
  195. if(![_httpServer start:&error])
  196. return true;
  197. }
  198. /* Address already in Use, take a random one */
  199. if (error.code == EADDRINUSE) {
  200. APLog(@"Port already in use, trying another one");
  201. [_httpServer setPort:0];
  202. if(![_httpServer start:&error])
  203. return true;
  204. }
  205. if (error) {
  206. APLog(@"Error starting HTTP Server: %@", error.localizedDescription);
  207. [_httpServer stop];
  208. }
  209. return false;
  210. }
  211. return true;
  212. }
  213. - (NSString *)currentIPAddress
  214. {
  215. NSString *address = @"";
  216. struct ifaddrs *interfaces = NULL;
  217. struct ifaddrs *temp_addr = NULL;
  218. int success = getifaddrs(&interfaces);
  219. if (success != 0) {
  220. freeifaddrs(interfaces);
  221. return address;
  222. }
  223. temp_addr = interfaces;
  224. while (temp_addr != NULL) {
  225. if (temp_addr->ifa_addr->sa_family == AF_INET) {
  226. if([@(temp_addr->ifa_name) isEqualToString:_nameOfUsedNetworkInterface])
  227. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  228. }
  229. temp_addr = temp_addr->ifa_next;
  230. }
  231. freeifaddrs(interfaces);
  232. return address;
  233. }
  234. - (NSString *)hostname
  235. {
  236. char baseHostName[256];
  237. int success = gethostname(baseHostName, 255);
  238. if (success != 0)
  239. return nil;
  240. baseHostName[255] = '\0';
  241. #if !TARGET_IPHONE_SIMULATOR
  242. return [NSString stringWithFormat:@"%s.local", baseHostName];
  243. #else
  244. return [NSString stringWithFormat:@"%s", baseHostName];
  245. #endif
  246. }
  247. - (NSString *)hostnamePort
  248. {
  249. return [NSString stringWithFormat:@"%i", _httpServer.listeningPort];
  250. }
  251. - (void)moveFileFrom:(NSString *)filepath
  252. {
  253. /* update media library when file upload was completed */
  254. VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
  255. [activityManager networkActivityStopped];
  256. [activityManager activateIdleTimer];
  257. /* on tvOS, the media remains in the cache folder and will disappear from there
  258. * while on iOS we have persistent storage, so move it there */
  259. #if TARGET_OS_IOS
  260. NSString *fileName = [filepath lastPathComponent];
  261. NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  262. NSString *uploadPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
  263. firstObject] stringByAppendingPathComponent:@"Upload"];
  264. NSString *finalFilePath = [libraryPath
  265. stringByAppendingString:[filepath
  266. stringByReplacingOccurrencesOfString:uploadPath
  267. withString:@""]];
  268. NSFileManager *fileManager = [NSFileManager defaultManager];
  269. // Re-create the folder structure of the user
  270. if (![fileManager createDirectoryAtPath:[finalFilePath stringByDeletingLastPathComponent]
  271. withIntermediateDirectories:YES attributes:nil error:nil])
  272. APLog(@"Could not create directory at path: %@", finalFilePath);
  273. if ([fileManager fileExistsAtPath:finalFilePath]) {
  274. /* we don't want to over-write existing files, so add an integer to the file name */
  275. NSString *potentialFullPath;
  276. NSString *currentPath = [finalFilePath stringByDeletingLastPathComponent];
  277. NSString *fileExtension = [fileName pathExtension];
  278. NSString *rawFileName = [fileName stringByDeletingPathExtension];
  279. for (NSUInteger x = 1; x < 100; x++) {
  280. potentialFullPath = [currentPath stringByAppendingString:[NSString
  281. stringWithFormat:@"/%@-%lu.%@",
  282. rawFileName,
  283. (unsigned long)x,
  284. fileExtension]];
  285. if (![[NSFileManager defaultManager] fileExistsAtPath:potentialFullPath]) {
  286. finalFilePath = potentialFullPath;
  287. break;
  288. }
  289. }
  290. }
  291. NSError *error;
  292. [fileManager moveItemAtPath:filepath toPath:finalFilePath error:&error];
  293. if (error) {
  294. APLog(@"Moving received media %@ to library folder failed (%li), deleting", fileName, (long)error.code);
  295. [fileManager removeItemAtPath:filepath error:nil];
  296. }
  297. [[VLCMediaFileDiscoverer sharedInstance] performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
  298. // FIXME: Replace notifications by cleaner observers
  299. [[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.VLCNewFileAddedNotification
  300. object:self];
  301. #endif
  302. }
  303. - (void)cleanCache
  304. {
  305. if ([[VLCActivityManager defaultManager] haveNetworkActivity])
  306. return;
  307. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  308. NSString* uploadDirPath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  309. NSFileManager *fileManager = [NSFileManager defaultManager];
  310. if ([fileManager fileExistsAtPath:uploadDirPath])
  311. [fileManager removeItemAtPath:uploadDirPath error:nil];
  312. }
  313. @end