FLEXNetworkMITMViewController.m 16 KB

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