FLEXNetworkMITMViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. //
  2. // FLEXNetworkMITMViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/8/15.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXColor.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXNetworkMITMViewController.h"
  11. #import "FLEXNetworkTransaction.h"
  12. #import "FLEXNetworkRecorder.h"
  13. #import "FLEXNetworkObserver.h"
  14. #import "FLEXNetworkTransactionCell.h"
  15. #import "FLEXNetworkTransactionDetailController.h"
  16. #import "FLEXNetworkSettingsController.h"
  17. #import "FLEXGlobalsViewController.h"
  18. #import "UIBarButtonItem+FLEX.h"
  19. #import "FLEXResources.h"
  20. @interface FLEXNetworkMITMViewController ()
  21. /// Backing model
  22. @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *networkTransactions;
  23. @property (nonatomic) long long bytesReceived;
  24. @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *filteredNetworkTransactions;
  25. @property (nonatomic) long long filteredBytesReceived;
  26. @property (nonatomic) BOOL rowInsertInProgress;
  27. @property (nonatomic) BOOL isPresentingSearch;
  28. @property (nonatomic) BOOL pendingReload;
  29. @end
  30. @implementation FLEXNetworkMITMViewController
  31. #pragma mark - Lifecycle
  32. - (id)init {
  33. return [self initWithStyle:UITableViewStylePlain];
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.showsSearchBar = YES;
  38. self.showSearchBarInitially = NO;
  39. [self addToolbarItems:@[
  40. [UIBarButtonItem
  41. flex_itemWithImage:FLEXResources.gearIcon
  42. target:self
  43. action:@selector(settingsButtonTapped:)
  44. ],
  45. [[UIBarButtonItem
  46. flex_systemItem:UIBarButtonSystemItemTrash
  47. target:self
  48. action:@selector(trashButtonTapped:)
  49. ] flex_withTintColor:UIColor.redColor]
  50. ]];
  51. [self.tableView
  52. registerClass:[FLEXNetworkTransactionCell class]
  53. forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier
  54. ];
  55. #if !TARGET_OS_TV
  56. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  57. #else
  58. [self addlongPressGestureRecognizer];
  59. #endif
  60. self.tableView.rowHeight = FLEXNetworkTransactionCell.preferredCellHeight;
  61. [self registerForNotifications];
  62. [self updateTransactions];
  63. }
  64. - (void)viewWillAppear:(BOOL)animated {
  65. [super viewWillAppear:animated];
  66. // Reload the table if we received updates while not on-screen
  67. if (self.pendingReload) {
  68. [self.tableView reloadData];
  69. self.pendingReload = NO;
  70. }
  71. }
  72. - (void)dealloc {
  73. [NSNotificationCenter.defaultCenter removeObserver:self];
  74. }
  75. - (void)registerForNotifications {
  76. NSDictionary *notifications = @{
  77. kFLEXNetworkRecorderNewTransactionNotification:
  78. NSStringFromSelector(@selector(handleNewTransactionRecordedNotification:)),
  79. kFLEXNetworkRecorderTransactionUpdatedNotification:
  80. NSStringFromSelector(@selector(handleTransactionUpdatedNotification:)),
  81. kFLEXNetworkRecorderTransactionsClearedNotification:
  82. NSStringFromSelector(@selector(handleTransactionsClearedNotification:)),
  83. kFLEXNetworkObserverEnabledStateChangedNotification:
  84. NSStringFromSelector(@selector(handleNetworkObserverEnabledStateChangedNotification:)),
  85. };
  86. for (NSString *name in notifications.allKeys) {
  87. [NSNotificationCenter.defaultCenter addObserver:self
  88. selector:NSSelectorFromString(notifications[name]) name:name object:nil
  89. ];
  90. }
  91. }
  92. #pragma mark - Private
  93. #pragma mark Button Actions
  94. - (void)settingsButtonTapped:(UIBarButtonItem *)sender {
  95. UIViewController *settings = [FLEXNetworkSettingsController new];
  96. settings.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(
  97. Done, self, @selector(settingsViewControllerDoneTapped:)
  98. );
  99. settings.title = @"Network Debugging Settings";
  100. // This is not a FLEXNavigationController because it is not intended as a new tab
  101. UIViewController *nav = [[UINavigationController alloc] initWithRootViewController:settings];
  102. [self presentViewController:nav animated:YES completion:nil];
  103. }
  104. - (void)trashButtonTapped:(UIBarButtonItem *)sender {
  105. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  106. make.title(@"Clear All Recorded Requests?");
  107. make.message(@"This cannot be undone.");
  108. make.button(@"Cancel").cancelStyle();
  109. make.button(@"Clear All").destructiveStyle().handler(^(NSArray *strings) {
  110. [FLEXNetworkRecorder.defaultRecorder clearRecordedActivity];
  111. });
  112. } showFrom:self source:sender];
  113. }
  114. - (void)settingsViewControllerDoneTapped:(id)sender {
  115. [self dismissViewControllerAnimated:YES completion:nil];
  116. }
  117. #pragma mark Transactions
  118. - (void)updateTransactions {
  119. self.networkTransactions = [FLEXNetworkRecorder.defaultRecorder networkTransactions];
  120. }
  121. - (void)setNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
  122. if (![_networkTransactions isEqual:networkTransactions]) {
  123. _networkTransactions = networkTransactions;
  124. [self updateBytesReceived];
  125. [self updateFilteredBytesReceived];
  126. }
  127. }
  128. - (void)updateBytesReceived {
  129. long long bytesReceived = 0;
  130. for (FLEXNetworkTransaction *transaction in self.networkTransactions) {
  131. bytesReceived += transaction.receivedDataLength;
  132. }
  133. self.bytesReceived = bytesReceived;
  134. [self updateFirstSectionHeader];
  135. }
  136. - (void)setFilteredNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
  137. if (![_filteredNetworkTransactions isEqual:networkTransactions]) {
  138. _filteredNetworkTransactions = networkTransactions;
  139. [self updateFilteredBytesReceived];
  140. }
  141. }
  142. - (void)updateFilteredBytesReceived {
  143. long long filteredBytesReceived = 0;
  144. for (FLEXNetworkTransaction *transaction in self.filteredNetworkTransactions) {
  145. filteredBytesReceived += transaction.receivedDataLength;
  146. }
  147. self.filteredBytesReceived = filteredBytesReceived;
  148. [self updateFirstSectionHeader];
  149. }
  150. #pragma mark Header
  151. - (void)updateFirstSectionHeader {
  152. UIView *view = [self.tableView headerViewForSection:0];
  153. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  154. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  155. headerView.textLabel.text = [self headerText];
  156. [headerView setNeedsLayout];
  157. }
  158. }
  159. - (NSString *)headerText {
  160. long long bytesReceived = 0;
  161. NSInteger totalRequests = 0;
  162. if (self.searchController.isActive) {
  163. bytesReceived = self.filteredBytesReceived;
  164. totalRequests = self.filteredNetworkTransactions.count;
  165. } else {
  166. bytesReceived = self.bytesReceived;
  167. totalRequests = self.networkTransactions.count;
  168. }
  169. NSString *byteCountText = [NSByteCountFormatter
  170. stringFromByteCount:bytesReceived countStyle:NSByteCountFormatterCountStyleBinary
  171. ];
  172. NSString *requestsText = totalRequests == 1 ? @"Request" : @"Requests";
  173. return [NSString stringWithFormat:@"%@ %@ (%@ received)",
  174. @(totalRequests), requestsText, byteCountText
  175. ];
  176. }
  177. #pragma mark - FLEXGlobalsEntry
  178. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  179. return @"📡 Network History";
  180. }
  181. + (FLEXGlobalsEntryRowAction)globalsEntryRowAction:(FLEXGlobalsRow)row {
  182. return ^(UITableViewController *host) {
  183. if (FLEXNetworkObserver.isEnabled) {
  184. [host.navigationController pushViewController:[
  185. self globalsEntryViewController:row
  186. ] animated:YES];
  187. } else {
  188. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  189. make.title(@"Network Monitor Disabled");
  190. make.message(@"You must enable network monitoring to proceed.");
  191. make.button(@"Turn On").handler(^(NSArray<NSString *> *strings) {
  192. FLEXNetworkObserver.enabled = YES;
  193. [host.navigationController pushViewController:[
  194. self globalsEntryViewController:row
  195. ] animated:YES];
  196. }).cancelStyle();
  197. make.button(@"Dismiss");
  198. } showFrom:host];
  199. }
  200. };
  201. }
  202. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  203. UIViewController *controller = [self new];
  204. controller.title = [self globalsEntryTitle:row];
  205. return controller;
  206. }
  207. #pragma mark - Notification Handlers
  208. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification {
  209. [self tryUpdateTransactions];
  210. }
  211. - (void)tryUpdateTransactions {
  212. // Don't do any view updating if we aren't in the view hierarchy
  213. if (!self.viewIfLoaded.window) {
  214. [self updateTransactions];
  215. self.pendingReload = YES;
  216. return;
  217. }
  218. // Let the previous row insert animation finish before starting a new one to avoid stomping.
  219. // We'll try calling the method again when the insertion completes,
  220. // and we properly no-op if there haven't been changes.
  221. if (self.rowInsertInProgress) {
  222. return;
  223. }
  224. if (self.searchController.isActive) {
  225. [self updateTransactions];
  226. [self updateSearchResults:self.searchText];
  227. return;
  228. }
  229. NSInteger existingRowCount = self.networkTransactions.count;
  230. [self updateTransactions];
  231. NSInteger newRowCount = self.networkTransactions.count;
  232. NSInteger addedRowCount = newRowCount - existingRowCount;
  233. if (addedRowCount != 0 && !self.isPresentingSearch) {
  234. // Insert animation if we're at the top.
  235. if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
  236. [CATransaction begin];
  237. self.rowInsertInProgress = YES;
  238. [CATransaction setCompletionBlock:^{
  239. self.rowInsertInProgress = NO;
  240. [self tryUpdateTransactions];
  241. }];
  242. NSMutableArray<NSIndexPath *> *indexPathsToReload = [NSMutableArray new];
  243. for (NSInteger row = 0; row < addedRowCount; row++) {
  244. [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
  245. }
  246. [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  247. [CATransaction commit];
  248. } else {
  249. // Maintain the user's position if they've scrolled down.
  250. CGSize existingContentSize = self.tableView.contentSize;
  251. [self.tableView reloadData];
  252. CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
  253. self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
  254. }
  255. }
  256. }
  257. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification {
  258. [self updateBytesReceived];
  259. [self updateFilteredBytesReceived];
  260. FLEXNetworkTransaction *transaction = notification.userInfo[kFLEXNetworkRecorderUserInfoTransactionKey];
  261. // Update both the main table view and search table view if needed.
  262. for (FLEXNetworkTransactionCell *cell in [self.tableView visibleCells]) {
  263. if ([cell.transaction isEqual:transaction]) {
  264. // Using -[UITableView reloadRowsAtIndexPaths:withRowAnimation:] is overkill here and kicks off a lot of
  265. // work that can make the table view somewhat unresponsive when lots of updates are streaming in.
  266. // We just need to tell the cell that it needs to re-layout.
  267. [cell setNeedsLayout];
  268. break;
  269. }
  270. }
  271. [self updateFirstSectionHeader];
  272. }
  273. - (void)handleTransactionsClearedNotification:(NSNotification *)notification {
  274. [self updateTransactions];
  275. [self.tableView reloadData];
  276. }
  277. - (void)handleNetworkObserverEnabledStateChangedNotification:(NSNotification *)notification {
  278. // Update the header, which displays a warning when network debugging is disabled
  279. [self updateFirstSectionHeader];
  280. }
  281. #pragma mark - Table view data source
  282. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  283. return self.searchController.isActive ? self.filteredNetworkTransactions.count : self.networkTransactions.count;
  284. }
  285. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  286. return [self headerText];
  287. }
  288. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  289. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  290. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  291. headerView.textLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
  292. }
  293. }
  294. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  295. FLEXNetworkTransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  296. cell.transaction = [self transactionAtIndexPath:indexPath];
  297. // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
  298. NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
  299. if ((totalRows - indexPath.row) % 2 == 0) {
  300. cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
  301. } else {
  302. cell.backgroundColor = FLEXColor.primaryBackgroundColor;
  303. }
  304. return cell;
  305. }
  306. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  307. FLEXNetworkTransactionDetailController *detailViewController = [FLEXNetworkTransactionDetailController new];
  308. detailViewController.transaction = [self transactionAtIndexPath:indexPath];
  309. [self.navigationController pushViewController:detailViewController animated:YES];
  310. }
  311. #pragma mark - Menu Actions
  312. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  313. return YES;
  314. }
  315. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  316. return action == @selector(copy:);
  317. }
  318. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  319. if (action == @selector(copy:)) {
  320. NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
  321. #if !TARGET_OS_TV
  322. UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
  323. #endif
  324. }
  325. }
  326. - (void)addlongPressGestureRecognizer {
  327. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  328. longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
  329. [self.tableView addGestureRecognizer:longPress];
  330. UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  331. rightTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeRightArrow]];
  332. [self.tableView addGestureRecognizer:rightTap];
  333. }
  334. - (void)longPress:(UILongPressGestureRecognizer*)gesture {
  335. if ( gesture.state == UIGestureRecognizerStateEnded) {
  336. UITableViewCell *cell = [gesture.view valueForKey:@"_focusedCell"];
  337. [self showActionForCell:cell];
  338. }
  339. }
  340. - (void)showActionForCell:(UITableViewCell *)cell {
  341. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  342. NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
  343. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  344. make.title(@"");
  345. make.button([NSString stringWithFormat:@"Blacklist '%@'", request.URL.host]).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  346. NSMutableArray *blacklist = FLEXNetworkRecorder.defaultRecorder.hostBlacklist;
  347. [blacklist addObject:request.URL.host];
  348. [FLEXNetworkRecorder.defaultRecorder clearBlacklistedTransactions];
  349. [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
  350. [self tryUpdateTransactions];
  351. });
  352. make.button(@"Cancel").cancelStyle();
  353. } showFrom:self];
  354. }
  355. #if FLEX_AT_LEAST_IOS13_SDK
  356. #if !TARGET_OS_TV
  357. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  358. NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
  359. return [UIContextMenuConfiguration
  360. configurationWithIdentifier:nil
  361. previewProvider:nil
  362. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  363. UIAction *copy = [UIAction
  364. actionWithTitle:@"Copy"
  365. image:nil
  366. identifier:nil
  367. handler:^(__kindof UIAction *action) {
  368. UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
  369. }
  370. ];
  371. UIAction *blacklist = [UIAction
  372. actionWithTitle:[NSString stringWithFormat:@"Blacklist '%@'", request.URL.host]
  373. image:nil
  374. identifier:nil
  375. handler:^(__kindof UIAction *action) {
  376. NSMutableArray *blacklist = FLEXNetworkRecorder.defaultRecorder.hostBlacklist;
  377. [blacklist addObject:request.URL.host];
  378. [FLEXNetworkRecorder.defaultRecorder clearBlacklistedTransactions];
  379. [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
  380. [self tryUpdateTransactions];
  381. }
  382. ];
  383. return [UIMenu
  384. menuWithTitle:@"" image:nil identifier:nil
  385. options:UIMenuOptionsDisplayInline
  386. children:@[copy, blacklist]
  387. ];
  388. }
  389. ];
  390. }
  391. #endif
  392. #endif
  393. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath {
  394. return self.searchController.isActive ? self.filteredNetworkTransactions[indexPath.row] : self.networkTransactions[indexPath.row];
  395. }
  396. #pragma mark - Search Bar
  397. - (void)updateSearchResults:(NSString *)searchString {
  398. if (!searchString.length) {
  399. self.filteredNetworkTransactions = self.networkTransactions;
  400. [self.tableView reloadData];
  401. } else {
  402. [self onBackgroundQueue:^NSArray *{
  403. return [self.networkTransactions flex_filtered:^BOOL(FLEXNetworkTransaction *entry, NSUInteger idx) {
  404. return [entry.request.URL.absoluteString localizedCaseInsensitiveContainsString:searchString];
  405. }];
  406. } thenOnMainQueue:^(NSArray *filteredNetworkTransactions) {
  407. if ([self.searchText isEqual:searchString]) {
  408. self.filteredNetworkTransactions = filteredNetworkTransactions;
  409. [self.tableView reloadData];
  410. }
  411. }];
  412. }
  413. }
  414. #pragma mark UISearchControllerDelegate
  415. - (void)willPresentSearchController:(UISearchController *)searchController {
  416. self.isPresentingSearch = YES;
  417. }
  418. - (void)didPresentSearchController:(UISearchController *)searchController {
  419. self.isPresentingSearch = NO;
  420. }
  421. - (void)willDismissSearchController:(UISearchController *)searchController {
  422. [self.tableView reloadData];
  423. }
  424. @end