From c96e2bb8b42f5bcc32e5975ab1f216df44cdd21f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 19 Jun 2018 12:09:44 -0400 Subject: [PATCH] Outage detection. --- .../HomeView/HomeViewController.m | 24 ++++++++++++++++++- .../translations/en.lproj/Localizable.strings | 3 +++ .../src/Network/OutageDetection.swift | 23 +++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index edb93f5cd..b19e3f680 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -78,6 +78,7 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations @property (nonatomic) NSLayoutConstraint *hideDeregisteredViewConstraint; @property (nonatomic) NSLayoutConstraint *hideArchiveReminderViewConstraint; @property (nonatomic) NSLayoutConstraint *hideMissingContactsPermissionViewConstraint; +@property (nonatomic) NSLayoutConstraint *outageViewConstraint; @property (nonatomic) TSThread *lastThread; @@ -166,6 +167,10 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations selector:@selector(deregistrationStateDidChange:) name:DeregistrationStateDidChangeNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(outageStateDidChange:) + name:OutageDetection.outageStateDidChange + object:nil]; } - (void)dealloc @@ -198,6 +203,13 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations [self updateReminderViews]; } +- (void)outageStateDidChange:(id)notification +{ + OWSAssertIsOnMainThread(); + + [self updateReminderViews]; +} + #pragma mark - View Life Cycle - (void)loadView @@ -233,6 +245,13 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations self.hideDeregisteredViewConstraint = [deregisteredView autoSetDimension:ALDimensionHeight toSize:0]; self.hideDeregisteredViewConstraint.priority = UILayoutPriorityRequired; + ReminderView *outageView = [ReminderView + nagWithText:NSLocalizedString(@"OUTAGE_WARNING", @"Label warning the user that the Signal service may be down.") + tapAction:nil]; + [reminderStackView addArrangedSubview:outageView]; + self.outageViewConstraint = [outageView autoSetDimension:ALDimensionHeight toSize:0]; + self.outageViewConstraint.priority = UILayoutPriorityRequired; + ReminderView *archiveReminderView = [ReminderView explanationWithText:NSLocalizedString(@"INBOX_VIEW_ARCHIVE_MODE_REMINDER", @"Label reminding the user that they are in archive mode.")]; @@ -291,16 +310,19 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations BOOL shouldHideArchiveReminderView = self.homeViewMode != HomeViewMode_Archive; BOOL shouldHideMissingContactsPermissionView = !self.shouldShowMissingContactsPermissionView; BOOL shouldHideDeregisteredView = !TSAccountManager.sharedInstance.isDeregistered; + BOOL shouldHideOutageView = !OutageDetection.sharedManager.hasOutage; if (self.hideArchiveReminderViewConstraint.active == shouldHideArchiveReminderView && self.hideMissingContactsPermissionViewConstraint.active == shouldHideMissingContactsPermissionView - && self.hideDeregisteredViewConstraint.active == shouldHideDeregisteredView) { + && self.hideDeregisteredViewConstraint.active == shouldHideDeregisteredView + && self.outageViewConstraint.active == shouldHideOutageView) { return; } self.hideArchiveReminderViewConstraint.active = shouldHideArchiveReminderView; self.hideMissingContactsPermissionViewConstraint.active = shouldHideMissingContactsPermissionView; self.hideDeregisteredViewConstraint.active = shouldHideDeregisteredView; + self.outageViewConstraint.active = shouldHideOutageView; [self.view setNeedsLayout]; [self.view layoutSubviews]; diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index 707b768cf..72790f8f3 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -1405,6 +1405,9 @@ /* Info Message when {{other user}} updates message expiration to {{time amount}}, see the *_TIME_AMOUNT strings for context. */ "OTHER_UPDATED_DISAPPEARING_MESSAGES_CONFIGURATION" = "%@ set disappearing message time to %@."; +/* Label warning the user that the Signal service may be down. */ +"OUTAGE_WARNING" = "Signal is experiencing technical difficulties. We are working hard to restore service as quickly as possible."; + /* No comment provided by engineer. */ "OUTGOING_CALL" = "Outgoing call"; diff --git a/SignalServiceKit/src/Network/OutageDetection.swift b/SignalServiceKit/src/Network/OutageDetection.swift index e645c6b43..81b978d47 100644 --- a/SignalServiceKit/src/Network/OutageDetection.swift +++ b/SignalServiceKit/src/Network/OutageDetection.swift @@ -10,10 +10,15 @@ public class OutageDetection: NSObject { @objc(sharedManager) public static let shared = OutageDetection() + @objc public static let outageStateDidChange = Notification.Name("OutageStateDidChange") + // These properties should only be accessed on the main thread. - private var hasOutage = false { + @objc + public var hasOutage = false { didSet { SwiftAssertIsOnMainThread(#function) + + NotificationCenter.default.postNotificationNameAsync(OutageDetection.outageStateDidChange, object: nil) } } private var mayHaveOutage = false { @@ -23,7 +28,14 @@ public class OutageDetection: NSObject { ensureCheckTimer() } } +<<<<<<< HEAD + +||||||| merged common ancestors + +======= + // We want to be conversative and only +>>>>>>> Outage detection. private func checkForOutageSync() -> Bool { let host = CFHostCreateWithName(nil, "uptime.signal.org" as CFString).takeRetainedValue() CFHostStartInfoResolution(host, .addresses, nil) @@ -42,9 +54,14 @@ public class OutageDetection: NSObject { if getnameinfo(address.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(address.length), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 { let addressString = String(cString: hostname) - if addressString != "127.0.0.1" { - Logger.verbose("\(logTag) addressString: \(addressString)") + let kHealthyAddress = "127.0.0.1" + let kOutageAddress = "127.0.0.2" + if addressString == kHealthyAddress { + // Do nothing. + } else if addressString == kOutageAddress { isOutageDetected = true + } else { + owsFail("\(logTag) unexpected address: \(addressString)") } } }