VLCHTTPUploaderController.m 12 KB

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