VLCHTTPUploaderController.m 12 KB

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