123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- //
- // FLEXNetworkMITMViewController.m
- // Flipboard
- //
- // Created by Ryan Olson on 2/8/15.
- // Copyright (c) 2020 FLEX Team. All rights reserved.
- //
- #import "FLEXColor.h"
- #import "FLEXUtility.h"
- #import "FLEXNetworkMITMViewController.h"
- #import "FLEXNetworkTransaction.h"
- #import "FLEXNetworkRecorder.h"
- #import "FLEXNetworkObserver.h"
- #import "FLEXNetworkTransactionCell.h"
- #import "FLEXNetworkTransactionDetailController.h"
- #import "FLEXNetworkSettingsController.h"
- #import "FLEXGlobalsViewController.h"
- #import "UIBarButtonItem+FLEX.h"
- #import "FLEXResources.h"
- @interface FLEXNetworkMITMViewController ()
- /// Backing model
- @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *networkTransactions;
- @property (nonatomic) long long bytesReceived;
- @property (nonatomic, copy) NSArray<FLEXNetworkTransaction *> *filteredNetworkTransactions;
- @property (nonatomic) long long filteredBytesReceived;
- @property (nonatomic) BOOL rowInsertInProgress;
- @property (nonatomic) BOOL isPresentingSearch;
- @property (nonatomic) BOOL pendingReload;
- @end
- @implementation FLEXNetworkMITMViewController
- #pragma mark - Lifecycle
- - (id)init {
- return [self initWithStyle:UITableViewStylePlain];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.showsSearchBar = YES;
- self.showSearchBarInitially = NO;
-
- [self addToolbarItems:@[
- [UIBarButtonItem
- flex_itemWithImage:FLEXResources.gearIcon
- target:self
- action:@selector(settingsButtonTapped:)
- ],
- [[UIBarButtonItem
- flex_systemItem:UIBarButtonSystemItemTrash
- target:self
- action:@selector(trashButtonTapped:)
- ] flex_withTintColor:UIColor.redColor]
- ]];
- [self.tableView
- registerClass:[FLEXNetworkTransactionCell class]
- forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier
- ];
- #if !TARGET_OS_TV
- self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- #else
- [self addlongPressGestureRecognizer];
- #endif
- self.tableView.rowHeight = FLEXNetworkTransactionCell.preferredCellHeight;
- [self registerForNotifications];
- [self updateTransactions];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- // Reload the table if we received updates while not on-screen
- if (self.pendingReload) {
- [self.tableView reloadData];
- self.pendingReload = NO;
- }
- }
- - (void)dealloc {
- [NSNotificationCenter.defaultCenter removeObserver:self];
- }
- - (void)registerForNotifications {
- NSDictionary *notifications = @{
- kFLEXNetworkRecorderNewTransactionNotification:
- NSStringFromSelector(@selector(handleNewTransactionRecordedNotification:)),
- kFLEXNetworkRecorderTransactionUpdatedNotification:
- NSStringFromSelector(@selector(handleTransactionUpdatedNotification:)),
- kFLEXNetworkRecorderTransactionsClearedNotification:
- NSStringFromSelector(@selector(handleTransactionsClearedNotification:)),
- kFLEXNetworkObserverEnabledStateChangedNotification:
- NSStringFromSelector(@selector(handleNetworkObserverEnabledStateChangedNotification:)),
- };
-
- for (NSString *name in notifications.allKeys) {
- [NSNotificationCenter.defaultCenter addObserver:self
- selector:NSSelectorFromString(notifications[name]) name:name object:nil
- ];
- }
- }
- #pragma mark - Private
- #pragma mark Button Actions
- - (void)settingsButtonTapped:(UIBarButtonItem *)sender {
- UIViewController *settings = [FLEXNetworkSettingsController new];
- settings.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(
- Done, self, @selector(settingsViewControllerDoneTapped:)
- );
- settings.title = @"Network Debugging Settings";
-
- // This is not a FLEXNavigationController because it is not intended as a new tab
- UIViewController *nav = [[UINavigationController alloc] initWithRootViewController:settings];
- [self presentViewController:nav animated:YES completion:nil];
- }
- - (void)trashButtonTapped:(UIBarButtonItem *)sender {
- [FLEXAlert makeSheet:^(FLEXAlert *make) {
- make.title(@"Clear All Recorded Requests?");
- make.message(@"This cannot be undone.");
-
- make.button(@"Cancel").cancelStyle();
- make.button(@"Clear All").destructiveStyle().handler(^(NSArray *strings) {
- [FLEXNetworkRecorder.defaultRecorder clearRecordedActivity];
- });
- } showFrom:self source:sender];
- }
- - (void)settingsViewControllerDoneTapped:(id)sender {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark Transactions
- - (void)updateTransactions {
- self.networkTransactions = [FLEXNetworkRecorder.defaultRecorder networkTransactions];
- }
- - (void)setNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
- if (![_networkTransactions isEqual:networkTransactions]) {
- _networkTransactions = networkTransactions;
- [self updateBytesReceived];
- [self updateFilteredBytesReceived];
- }
- }
- - (void)updateBytesReceived {
- long long bytesReceived = 0;
- for (FLEXNetworkTransaction *transaction in self.networkTransactions) {
- bytesReceived += transaction.receivedDataLength;
- }
- self.bytesReceived = bytesReceived;
- [self updateFirstSectionHeader];
- }
- - (void)setFilteredNetworkTransactions:(NSArray<FLEXNetworkTransaction *> *)networkTransactions {
- if (![_filteredNetworkTransactions isEqual:networkTransactions]) {
- _filteredNetworkTransactions = networkTransactions;
- [self updateFilteredBytesReceived];
- }
- }
- - (void)updateFilteredBytesReceived {
- long long filteredBytesReceived = 0;
- for (FLEXNetworkTransaction *transaction in self.filteredNetworkTransactions) {
- filteredBytesReceived += transaction.receivedDataLength;
- }
- self.filteredBytesReceived = filteredBytesReceived;
- [self updateFirstSectionHeader];
- }
- #pragma mark Header
- - (void)updateFirstSectionHeader {
- UIView *view = [self.tableView headerViewForSection:0];
- if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
- UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
- headerView.textLabel.text = [self headerText];
- [headerView setNeedsLayout];
- }
- }
- - (NSString *)headerText {
- long long bytesReceived = 0;
- NSInteger totalRequests = 0;
- if (self.searchController.isActive) {
- bytesReceived = self.filteredBytesReceived;
- totalRequests = self.filteredNetworkTransactions.count;
- } else {
- bytesReceived = self.bytesReceived;
- totalRequests = self.networkTransactions.count;
- }
-
- NSString *byteCountText = [NSByteCountFormatter
- stringFromByteCount:bytesReceived countStyle:NSByteCountFormatterCountStyleBinary
- ];
- NSString *requestsText = totalRequests == 1 ? @"Request" : @"Requests";
- return [NSString stringWithFormat:@"%@ %@ (%@ received)",
- @(totalRequests), requestsText, byteCountText
- ];
- }
- #pragma mark - FLEXGlobalsEntry
- + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
- return @"📡 Network History";
- }
- + (FLEXGlobalsEntryRowAction)globalsEntryRowAction:(FLEXGlobalsRow)row {
- return ^(UITableViewController *host) {
- if (FLEXNetworkObserver.isEnabled) {
- [host.navigationController pushViewController:[
- self globalsEntryViewController:row
- ] animated:YES];
- } else {
- [FLEXAlert makeAlert:^(FLEXAlert *make) {
- make.title(@"Network Monitor Disabled");
- make.message(@"You must enable network monitoring to proceed.");
-
- make.button(@"Turn On").handler(^(NSArray<NSString *> *strings) {
- FLEXNetworkObserver.enabled = YES;
- [host.navigationController pushViewController:[
- self globalsEntryViewController:row
- ] animated:YES];
- }).cancelStyle();
- make.button(@"Dismiss");
- } showFrom:host];
- }
- };
- }
- + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
- UIViewController *controller = [self new];
- controller.title = [self globalsEntryTitle:row];
- return controller;
- }
- #pragma mark - Notification Handlers
- - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification {
- [self tryUpdateTransactions];
- }
- - (void)tryUpdateTransactions {
- // Don't do any view updating if we aren't in the view hierarchy
- if (!self.viewIfLoaded.window) {
- [self updateTransactions];
- self.pendingReload = YES;
- return;
- }
-
- // Let the previous row insert animation finish before starting a new one to avoid stomping.
- // We'll try calling the method again when the insertion completes,
- // and we properly no-op if there haven't been changes.
- if (self.rowInsertInProgress) {
- return;
- }
-
- if (self.searchController.isActive) {
- [self updateTransactions];
- [self updateSearchResults:self.searchText];
- return;
- }
- NSInteger existingRowCount = self.networkTransactions.count;
- [self updateTransactions];
- NSInteger newRowCount = self.networkTransactions.count;
- NSInteger addedRowCount = newRowCount - existingRowCount;
- if (addedRowCount != 0 && !self.isPresentingSearch) {
- // Insert animation if we're at the top.
- if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
- [CATransaction begin];
-
- self.rowInsertInProgress = YES;
- [CATransaction setCompletionBlock:^{
- self.rowInsertInProgress = NO;
- [self tryUpdateTransactions];
- }];
- NSMutableArray<NSIndexPath *> *indexPathsToReload = [NSMutableArray new];
- for (NSInteger row = 0; row < addedRowCount; row++) {
- [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
- }
- [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
- [CATransaction commit];
- } else {
- // Maintain the user's position if they've scrolled down.
- CGSize existingContentSize = self.tableView.contentSize;
- [self.tableView reloadData];
- CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
- self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
- }
- }
- }
- - (void)handleTransactionUpdatedNotification:(NSNotification *)notification {
- [self updateBytesReceived];
- [self updateFilteredBytesReceived];
- FLEXNetworkTransaction *transaction = notification.userInfo[kFLEXNetworkRecorderUserInfoTransactionKey];
- // Update both the main table view and search table view if needed.
- for (FLEXNetworkTransactionCell *cell in [self.tableView visibleCells]) {
- if ([cell.transaction isEqual:transaction]) {
- // Using -[UITableView reloadRowsAtIndexPaths:withRowAnimation:] is overkill here and kicks off a lot of
- // work that can make the table view somewhat unresponsive when lots of updates are streaming in.
- // We just need to tell the cell that it needs to re-layout.
- [cell setNeedsLayout];
- break;
- }
- }
- [self updateFirstSectionHeader];
- }
- - (void)handleTransactionsClearedNotification:(NSNotification *)notification {
- [self updateTransactions];
- [self.tableView reloadData];
- }
- - (void)handleNetworkObserverEnabledStateChangedNotification:(NSNotification *)notification {
- // Update the header, which displays a warning when network debugging is disabled
- [self updateFirstSectionHeader];
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.searchController.isActive ? self.filteredNetworkTransactions.count : self.networkTransactions.count;
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- return [self headerText];
- }
- - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
- if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
- UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
- headerView.textLabel.font = [UIFont systemFontOfSize:14.0 weight:UIFontWeightSemibold];
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- FLEXNetworkTransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
- cell.transaction = [self transactionAtIndexPath:indexPath];
- // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
- NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
- if ((totalRows - indexPath.row) % 2 == 0) {
- cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
- } else {
- cell.backgroundColor = FLEXColor.primaryBackgroundColor;
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- FLEXNetworkTransactionDetailController *detailViewController = [FLEXNetworkTransactionDetailController new];
- detailViewController.transaction = [self transactionAtIndexPath:indexPath];
- [self.navigationController pushViewController:detailViewController animated:YES];
- }
- #pragma mark - Menu Actions
- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
- return YES;
- }
- - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
- return action == @selector(copy:);
- }
- - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
- if (action == @selector(copy:)) {
- NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
- #if !TARGET_OS_TV
- UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
- #endif
- }
- }
- - (void)addlongPressGestureRecognizer {
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
- longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
- [self.tableView addGestureRecognizer:longPress];
- UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
- rightTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeRightArrow]];
- [self.tableView addGestureRecognizer:rightTap];
- }
- - (void)longPress:(UILongPressGestureRecognizer*)gesture {
- if ( gesture.state == UIGestureRecognizerStateEnded) {
-
- UITableViewCell *cell = [gesture.view valueForKey:@"_focusedCell"];
- [self showActionForCell:cell];
- }
- }
- - (void)showActionForCell:(UITableViewCell *)cell {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
- NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
- [FLEXAlert makeAlert:^(FLEXAlert *make) {
- make.title(@"");
- make.button([NSString stringWithFormat:@"Blacklist '%@'", request.URL.host]).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
- NSMutableArray *blacklist = FLEXNetworkRecorder.defaultRecorder.hostBlacklist;
- [blacklist addObject:request.URL.host];
- [FLEXNetworkRecorder.defaultRecorder clearBlacklistedTransactions];
- [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
- [self tryUpdateTransactions];
- });
- make.button(@"Cancel").cancelStyle();
- } showFrom:self];
- }
- #if FLEX_AT_LEAST_IOS13_SDK
- #if !TARGET_OS_TV
- - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
- NSURLRequest *request = [self transactionAtIndexPath:indexPath].request;
- return [UIContextMenuConfiguration
- configurationWithIdentifier:nil
- previewProvider:nil
- actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
- UIAction *copy = [UIAction
- actionWithTitle:@"Copy"
- image:nil
- identifier:nil
- handler:^(__kindof UIAction *action) {
- UIPasteboard.generalPasteboard.string = request.URL.absoluteString ?: @"";
- }
- ];
- UIAction *blacklist = [UIAction
- actionWithTitle:[NSString stringWithFormat:@"Blacklist '%@'", request.URL.host]
- image:nil
- identifier:nil
- handler:^(__kindof UIAction *action) {
- NSMutableArray *blacklist = FLEXNetworkRecorder.defaultRecorder.hostBlacklist;
- [blacklist addObject:request.URL.host];
- [FLEXNetworkRecorder.defaultRecorder clearBlacklistedTransactions];
- [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
- [self tryUpdateTransactions];
- }
- ];
- return [UIMenu
- menuWithTitle:@"" image:nil identifier:nil
- options:UIMenuOptionsDisplayInline
- children:@[copy, blacklist]
- ];
- }
- ];
- }
- #endif
- #endif
- - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath {
- return self.searchController.isActive ? self.filteredNetworkTransactions[indexPath.row] : self.networkTransactions[indexPath.row];
- }
- #pragma mark - Search Bar
- - (void)updateSearchResults:(NSString *)searchString {
- if (!searchString.length) {
- self.filteredNetworkTransactions = self.networkTransactions;
- [self.tableView reloadData];
- } else {
- [self onBackgroundQueue:^NSArray *{
- return [self.networkTransactions flex_filtered:^BOOL(FLEXNetworkTransaction *entry, NSUInteger idx) {
- return [entry.request.URL.absoluteString localizedCaseInsensitiveContainsString:searchString];
- }];
- } thenOnMainQueue:^(NSArray *filteredNetworkTransactions) {
- if ([self.searchText isEqual:searchString]) {
- self.filteredNetworkTransactions = filteredNetworkTransactions;
- [self.tableView reloadData];
- }
- }];
- }
- }
- #pragma mark UISearchControllerDelegate
- - (void)willPresentSearchController:(UISearchController *)searchController {
- self.isPresentingSearch = YES;
- }
- - (void)didPresentSearchController:(UISearchController *)searchController {
- self.isPresentingSearch = NO;
- }
- - (void)willDismissSearchController:(UISearchController *)searchController {
- [self.tableView reloadData];
- }
- @end
|