FLEXNetworkMITMViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // FLEXNetworkMITMViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/8/15.
  6. // Copyright (c) 2015 Flipboard. 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 "FLEXNetworkTransactionTableViewCell.h"
  15. #import "FLEXNetworkTransactionDetailTableViewController.h"
  16. #import "FLEXNetworkSettingsTableViewController.h"
  17. #import "UIBarButtonItem+FLEX.h"
  18. #import "FLEXResources.h"
  19. @interface FLEXNetworkMITMViewController ()
  20. /// Backing model
  21. @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *networkTransactions;
  22. @property (nonatomic) long long bytesReceived;
  23. @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *filteredNetworkTransactions;
  24. @property (nonatomic) long long filteredBytesReceived;
  25. @property (nonatomic) BOOL rowInsertInProgress;
  26. @property (nonatomic) BOOL isPresentingSearch;
  27. @property (nonatomic) BOOL pendingReload;
  28. @end
  29. @implementation FLEXNetworkMITMViewController
  30. #pragma mark - Lifecycle
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.showsSearchBar = YES;
  34. [self addToolbarItems:@[[UIBarButtonItem
  35. itemWithImage:FLEXResources.gearIcon
  36. target:self
  37. action:@selector(settingsButtonTapped:)
  38. ]]];
  39. [self.tableView
  40. registerClass:[FLEXNetworkTransactionTableViewCell class]
  41. forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier
  42. ];
  43. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  44. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  45. [self registerForNotifications];
  46. [self updateTransactions];
  47. }
  48. - (void)viewWillAppear:(BOOL)animated {
  49. [super viewWillAppear:animated];
  50. // Reload the table if we received updates while not on-screen
  51. if (self.pendingReload) {
  52. [self.tableView reloadData];
  53. self.pendingReload = NO;
  54. }
  55. }
  56. - (void)dealloc {
  57. [NSNotificationCenter.defaultCenter removeObserver:self];
  58. }
  59. - (void)registerForNotifications {
  60. NSDictionary *notifications = @{
  61. kFLEXNetworkRecorderNewTransactionNotification:
  62. NSStringFromSelector(@selector(handleNewTransactionRecordedNotification:)),
  63. kFLEXNetworkRecorderTransactionUpdatedNotification:
  64. NSStringFromSelector(@selector(handleTransactionUpdatedNotification:)),
  65. kFLEXNetworkRecorderTransactionsClearedNotification:
  66. NSStringFromSelector(@selector(handleTransactionsClearedNotification:)),
  67. kFLEXNetworkObserverEnabledStateChangedNotification:
  68. NSStringFromSelector(@selector(handleNetworkObserverEnabledStateChangedNotification:)),
  69. };
  70. for (NSString *name in notifications.allKeys) {
  71. [NSNotificationCenter.defaultCenter addObserver:self
  72. selector:NSSelectorFromString(notifications[name]) name:name object:nil
  73. ];
  74. }
  75. }
  76. #pragma mark - Private
  77. #pragma mark Button Actions
  78. - (void)settingsButtonTapped:(id)sender {
  79. UIViewController *settings = [FLEXNetworkSettingsTableViewController new];
  80. settings.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(
  81. Done, self, @selector(settingsViewControllerDoneTapped:)
  82. );
  83. settings.title = @"Network Debugging Settings";
  84. // This is not a FLEXNavigationController because it is not intended as a new tab
  85. UIViewController *nav = [[UINavigationController alloc] initWithRootViewController:settings];
  86. [self presentViewController:nav animated:YES completion:nil];
  87. }
  88. - (void)settingsViewControllerDoneTapped:(id)sender {
  89. [self dismissViewControllerAnimated:YES completion:nil];
  90. }
  91. #pragma mark Transactions
  92. - (void)updateTransactions {
  93. self.networkTransactions = [[FLEXNetworkRecorder defaultRecorder] networkTransactions];
  94. }
  95. - (void)setNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
  96. if (![_networkTransactions isEqual:networkTransactions]) {
  97. _networkTransactions = networkTransactions;
  98. [self updateBytesReceived];
  99. [self updateFilteredBytesReceived];
  100. }
  101. }
  102. - (void)updateBytesReceived {
  103. long long bytesReceived = 0;
  104. for (FLEXNetworkTransaction *transaction in self.networkTransactions) {
  105. bytesReceived += transaction.receivedDataLength;
  106. }
  107. self.bytesReceived = bytesReceived;
  108. [self updateFirstSectionHeader];
  109. }
  110. - (void)setFilteredNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
  111. if (![_filteredNetworkTransactions isEqual:networkTransactions]) {
  112. _filteredNetworkTransactions = networkTransactions;
  113. [self updateFilteredBytesReceived];
  114. }
  115. }
  116. - (void)updateFilteredBytesReceived {
  117. long long filteredBytesReceived = 0;
  118. for (FLEXNetworkTransaction *transaction in self.filteredNetworkTransactions) {
  119. filteredBytesReceived += transaction.receivedDataLength;
  120. }
  121. self.filteredBytesReceived = filteredBytesReceived;
  122. [self updateFirstSectionHeader];
  123. }
  124. #pragma mark Header
  125. - (void)updateFirstSectionHeader {
  126. UIView *view = [self.tableView headerViewForSection:0];
  127. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  128. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  129. headerView.textLabel.text = [self headerText];
  130. [headerView setNeedsLayout];
  131. }
  132. }
  133. - (NSString *)headerText {
  134. NSString *headerText = nil;
  135. if (FLEXNetworkObserver.isEnabled) {
  136. long long bytesReceived = 0;
  137. NSInteger totalRequests = 0;
  138. if (self.searchController.isActive) {
  139. bytesReceived = self.filteredBytesReceived;
  140. totalRequests = self.filteredNetworkTransactions.count;
  141. } else {
  142. bytesReceived = self.bytesReceived;
  143. totalRequests = self.networkTransactions.count;
  144. }
  145. NSString *byteCountText = [NSByteCountFormatter stringFromByteCount:bytesReceived countStyle:NSByteCountFormatterCountStyleBinary];
  146. NSString *requestsText = totalRequests == 1 ? @"Request" : @"Requests";
  147. headerText = [NSString stringWithFormat:@"%ld %@ (%@ received)", (long)totalRequests, requestsText, byteCountText];
  148. } else {
  149. headerText = @"⚠️ Debugging Disabled (Enable in Settings)";
  150. }
  151. return headerText;
  152. }
  153. #pragma mark - FLEXGlobalsEntry
  154. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  155. return @"📡 Network History";
  156. }
  157. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  158. return [self new];
  159. }
  160. #pragma mark - Notification Handlers
  161. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification {
  162. [self tryUpdateTransactions];
  163. }
  164. - (void)tryUpdateTransactions {
  165. // Don't do any updating if we aren't in the view hierarchy
  166. if (!self.viewIfLoaded.window) {
  167. self.pendingReload = YES;
  168. return;
  169. }
  170. // Let the previous row insert animation finish before starting a new one to avoid stomping.
  171. // We'll try calling the method again when the insertion completes, and we properly no-op if there haven't been changes.
  172. if (self.rowInsertInProgress) {
  173. return;
  174. }
  175. if (self.searchController.isActive) {
  176. [self updateTransactions];
  177. [self updateSearchResults:nil];
  178. return;
  179. }
  180. NSInteger existingRowCount = self.networkTransactions.count;
  181. [self updateTransactions];
  182. NSInteger newRowCount = self.networkTransactions.count;
  183. NSInteger addedRowCount = newRowCount - existingRowCount;
  184. if (addedRowCount != 0 && !self.isPresentingSearch) {
  185. // Insert animation if we're at the top.
  186. if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
  187. [CATransaction begin];
  188. self.rowInsertInProgress = YES;
  189. [CATransaction setCompletionBlock:^{
  190. self.rowInsertInProgress = NO;
  191. [self tryUpdateTransactions];
  192. }];
  193. NSMutableArray<NSIndexPath *> *indexPathsToReload = [NSMutableArray array];
  194. for (NSInteger row = 0; row < addedRowCount; row++) {
  195. [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
  196. }
  197. [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  198. [CATransaction commit];
  199. } else {
  200. // Maintain the user's position if they've scrolled down.
  201. CGSize existingContentSize = self.tableView.contentSize;
  202. [self.tableView reloadData];
  203. CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
  204. self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
  205. }
  206. }
  207. }
  208. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification {
  209. [self updateBytesReceived];
  210. [self updateFilteredBytesReceived];
  211. FLEXNetworkTransaction *transaction = notification.userInfo[kFLEXNetworkRecorderUserInfoTransactionKey];
  212. // Update both the main table view and search table view if needed.
  213. for (FLEXNetworkTransactionTableViewCell *cell in [self.tableView visibleCells]) {
  214. if ([cell.transaction isEqual:transaction]) {
  215. // Using -[UITableView reloadRowsAtIndexPaths:withRowAnimation:] is overkill here and kicks off a lot of
  216. // work that can make the table view somewhat unresponsive when lots of updates are streaming in.
  217. // We just need to tell the cell that it needs to re-layout.
  218. [cell setNeedsLayout];
  219. break;
  220. }
  221. }
  222. [self updateFirstSectionHeader];
  223. }
  224. - (void)handleTransactionsClearedNotification:(NSNotification *)notification {
  225. [self updateTransactions];
  226. [self.tableView reloadData];
  227. }
  228. - (void)handleNetworkObserverEnabledStateChangedNotification:(NSNotification *)notification {
  229. // Update the header, which displays a warning when network debugging is disabled
  230. [self updateFirstSectionHeader];
  231. }
  232. #pragma mark - Table view data source
  233. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  234. return self.searchController.isActive ? self.filteredNetworkTransactions.count : self.networkTransactions.count;
  235. }
  236. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  237. return [self headerText];
  238. }
  239. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  240. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  241. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  242. headerView.textLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
  243. }
  244. }
  245. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  246. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  247. cell.transaction = [self transactionAtIndexPath:indexPath];
  248. // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
  249. NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
  250. if ((totalRows - indexPath.row) % 2 == 0) {
  251. cell.backgroundColor = [FLEXColor secondaryBackgroundColor];
  252. } else {
  253. cell.backgroundColor = [FLEXColor primaryBackgroundColor];
  254. }
  255. return cell;
  256. }
  257. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  258. FLEXNetworkTransactionDetailTableViewController *detailViewController = [FLEXNetworkTransactionDetailTableViewController new];
  259. detailViewController.transaction = [self transactionAtIndexPath:indexPath];
  260. [self.navigationController pushViewController:detailViewController animated:YES];
  261. }
  262. #pragma mark - Menu Actions
  263. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  264. return YES;
  265. }
  266. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  267. return action == @selector(copy:);
  268. }
  269. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  270. if (action == @selector(copy:)) {
  271. NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
  272. UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
  273. }
  274. }
  275. #if FLEX_AT_LEAST_IOS13_SDK
  276. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  277. return [UIContextMenuConfiguration
  278. configurationWithIdentifier:nil
  279. previewProvider:nil
  280. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  281. UIAction *copy = [UIAction
  282. actionWithTitle:@"Copy"
  283. image:nil
  284. identifier:nil
  285. handler:^(__kindof UIAction *action) {
  286. NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
  287. UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
  288. }
  289. ];
  290. return [UIMenu
  291. menuWithTitle:@"" image:nil identifier:nil
  292. options:UIMenuOptionsDisplayInline
  293. children:@[copy]
  294. ];
  295. }
  296. ];
  297. }
  298. #endif
  299. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath {
  300. return self.searchController.isActive ? self.filteredNetworkTransactions[indexPath.row] : self.networkTransactions[indexPath.row];
  301. }
  302. #pragma mark - Search Bar
  303. - (void)updateSearchResults:(NSString *)searchString {
  304. if (!searchString) {
  305. self.filteredNetworkTransactions = self.networkTransactions;
  306. [self.tableView reloadData];
  307. } else {
  308. [self onBackgroundQueue:^NSArray *{
  309. return [self.networkTransactions flex_filtered:^BOOL(FLEXNetworkTransaction *entry, NSUInteger idx) {
  310. return [entry.request.URL.absoluteString localizedCaseInsensitiveContainsString:searchString];
  311. }];
  312. } thenOnMainQueue:^(NSArray *filteredNetworkTransactions) {
  313. if ([self.searchText isEqual:searchString]) {
  314. self.filteredNetworkTransactions = filteredNetworkTransactions;
  315. [self.tableView reloadData];
  316. }
  317. }];
  318. }
  319. }
  320. #pragma mark UISearchControllerDelegate
  321. - (void)willPresentSearchController:(UISearchController *)searchController {
  322. self.isPresentingSearch = YES;
  323. }
  324. - (void)didPresentSearchController:(UISearchController *)searchController {
  325. self.isPresentingSearch = NO;
  326. }
  327. - (void)willDismissSearchController:(UISearchController *)searchController {
  328. [self.tableView reloadData];
  329. }
  330. @end