Add support for manually activating censorship circumvention.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent cbeafac20e
commit 2171cd1d96

@ -10,6 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
extern NSString *const TSRegistrationErrorDomain;
extern NSString *const TSRegistrationErrorUserInfoHTTPStatus;
extern NSString *const kNSNotificationName_RegistrationStateDidChange;
extern NSString *const kNSNotificationName_LocalNumberDidChange;
@class TSNetworkManager;
@class TSStorageManager;

@ -18,11 +18,12 @@ NS_ASSUME_NONNULL_BEGIN
NSString *const TSRegistrationErrorDomain = @"TSRegistrationErrorDomain";
NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus";
NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificationName_RegistrationStateDidChange";
NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange";
@interface TSAccountManager ()
@property (nullable, nonatomic, retain) NSString *phoneNumberAwaitingVerification;
@property (nonatomic, strong, readonly) TSStorageManager *storageManager;
@property (nonatomic, nullable) NSString *phoneNumberAwaitingVerification;
@property (nonatomic, readonly) TSStorageManager *storageManager;
@end
@ -58,6 +59,15 @@ NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificati
return sharedInstance;
}
- (void)setPhoneNumberAwaitingVerification:(NSString *_Nullable)phoneNumberAwaitingVerification
{
_phoneNumberAwaitingVerification = phoneNumberAwaitingVerification;
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_LocalNumberDidChange
object:nil
userInfo:nil];
}
+ (BOOL)isRegistered {
return [TSStorageManager localNumber] ? YES : NO;
}

@ -27,7 +27,7 @@ typedef void (^failureBlock)(NSURLSessionDataTask *task, NSError *error);
static TSNetworkManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
OWSSignalService *signalService = [[OWSSignalService alloc] init];
OWSSignalService *signalService = [OWSSignalService sharedInstance];
sharedMyManager = [[self alloc] initWithSignalService:signalService];
});
return sharedMyManager;

@ -1,17 +1,28 @@
// Created by Michael Kirk on 12/20/16.
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
extern NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange;
@class TSStorageManager;
@class TSAccountManager;
@class AFHTTPSessionManager;
@interface OWSSignalService : NSObject
@property (nonatomic, readonly) BOOL isCensored;
@property (nonatomic, readonly) AFHTTPSessionManager *HTTPSessionManager;
@property (atomic, readonly) BOOL isCensorshipCircumventionActive;
+ (instancetype)sharedInstance;
- (instancetype)init NS_UNAVAILABLE;
- (BOOL)isCensorshipCircumventionManuallyActivated;
- (void)setIsCensorshipCircumventionManuallyActivated:(BOOL)value;
@end
NS_ASSUME_NONNULL_END

@ -1,5 +1,6 @@
// Created by Michael Kirk on 12/20/16.
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import <AFNetworking/AFHTTPSessionManager.h>
@ -11,15 +12,38 @@
NS_ASSUME_NONNULL_BEGIN
NSString *const kNSUserDefaults_isCensorshipCircumventionManuallyActivated =
@"kNSUserDefaults_isCensorshipCircumventionManuallyActivated";
NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
@"kNSNotificationName_IsCensorshipCircumventionActiveDidChange";
@interface OWSSignalService ()
@property (nonatomic, readonly, strong) OWSCensorshipConfiguration *censorshipConfiguration;
@property (nonatomic, readonly) OWSCensorshipConfiguration *censorshipConfiguration;
@property (nonatomic) BOOL hasCensoredPhoneNumber;
@property (atomic) BOOL isCensorshipCircumventionActive;
@end
#pragma mark -
@implementation OWSSignalService
- (instancetype)init
@synthesize isCensorshipCircumventionActive = _isCensorshipCircumventionActive;
+ (instancetype)sharedInstance
{
static OWSSignalService *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] initDefault];
});
return sharedInstance;
}
- (instancetype)initDefault
{
self = [super init];
if (!self) {
@ -28,24 +52,105 @@ NS_ASSUME_NONNULL_BEGIN
_censorshipConfiguration = [OWSCensorshipConfiguration new];
[self observeNotifications];
[self updateIsCensorshipCircumventionActive];
OWSSingletonAssert();
return self;
}
- (BOOL)isCensored
- (void)observeNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(registrationStateDidChange:)
name:kNSNotificationName_RegistrationStateDidChange
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(localNumberDidChange:)
name:kNSNotificationName_LocalNumberDidChange
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)updateHasCensoredPhoneNumber
{
OWSAssert([NSThread isMainThread]);
NSString *localNumber = [TSAccountManager localNumber];
if (localNumber) {
return [self.censorshipConfiguration isCensoredPhoneNumber:localNumber];
self.hasCensoredPhoneNumber = [self.censorshipConfiguration isCensoredPhoneNumber:localNumber];
} else {
DDLogError(@"no known phone number to check for censorship.");
return NO;
DDLogError(@"%@ no known phone number to check for censorship.", self.tag);
self.hasCensoredPhoneNumber = NO;
}
[self updateIsCensorshipCircumventionActive];
}
- (BOOL)isCensorshipCircumventionManuallyActivated
{
OWSAssert([NSThread isMainThread]);
return [[NSUserDefaults.standardUserDefaults
objectForKey:kNSUserDefaults_isCensorshipCircumventionManuallyActivated] boolValue];
}
- (void)setIsCensorshipCircumventionManuallyActivated:(BOOL)value
{
OWSAssert([NSThread isMainThread]);
[NSUserDefaults.standardUserDefaults setObject:@(value)
forKey:kNSUserDefaults_isCensorshipCircumventionManuallyActivated];
[NSUserDefaults.standardUserDefaults synchronize];
[self updateIsCensorshipCircumventionActive];
}
- (void)updateIsCensorshipCircumventionActive
{
OWSAssert([NSThread isMainThread]);
self.isCensorshipCircumventionActive
= (self.isCensorshipCircumventionManuallyActivated || self.hasCensoredPhoneNumber);
}
- (void)setIsCensorshipCircumventionActive:(BOOL)isCensorshipCircumventionActive
{
OWSAssert([NSThread isMainThread]);
@synchronized(self)
{
if (_isCensorshipCircumventionActive == isCensorshipCircumventionActive) {
return;
}
_isCensorshipCircumventionActive = isCensorshipCircumventionActive;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:kNSNotificationName_IsCensorshipCircumventionActiveDidChange
object:nil
userInfo:nil];
}
- (BOOL)isCensorshipCircumventionActive
{
@synchronized(self)
{
return _isCensorshipCircumventionActive;
}
}
- (AFHTTPSessionManager *)HTTPSessionManager
{
if (self.isCensored) {
if (self.isCensorshipCircumventionActive) {
DDLogInfo(@"%@ using reflector HTTPSessionManager", self.tag);
return self.reflectorHTTPSessionManager;
} else {
@ -125,6 +230,18 @@ NS_ASSUME_NONNULL_BEGIN
return securityPolicy;
}
#pragma mark - Events
- (void)registrationStateDidChange:(NSNotification *)notification
{
[self updateHasCensoredPhoneNumber];
}
- (void)localNumberDidChange:(NSNotification *)notification
{
[self updateHasCensoredPhoneNumber];
}
#pragma mark - Logging
+ (NSString *)tag

@ -101,7 +101,7 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
OWSAssert([NSThread isMainThread]);
_signalService = [OWSSignalService new];
_signalService = [OWSSignalService sharedInstance];
_status = kSocketStatusClosed;
_fetchingTaskIdentifier = UIBackgroundTaskInvalid;
@ -140,6 +140,10 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
selector:@selector(registrationStateDidChange:)
name:kNSNotificationName_RegistrationStateDidChange
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(isCensorshipCircumventionActiveDidChange:)
name:kNSNotificationName_IsCensorshipCircumventionActiveDidChange
object:nil];
}
+ (instancetype)sharedManager {
@ -156,11 +160,7 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
- (void)ensureWebsocketIsOpen
{
OWSAssert([NSThread isMainThread]);
if (self.signalService.isCensored) {
DDLogWarn(@"%@ Skipping opening of websocket due to censorship circumvention.", self.tag);
return;
}
OWSAssert(!self.signalService.isCensorshipCircumventionActive);
// Try to reuse the existing socket (if any) if it is in a valid state.
if (self.websocket) {
@ -526,6 +526,11 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
return NO;
}
if (self.signalService.isCensorshipCircumventionActive) {
DDLogWarn(@"%@ Skipping opening of websocket due to censorship circumvention.", self.tag);
return NO;
}
if (self.appIsActive) {
// If app is active, keep web socket alive.
return YES;
@ -707,6 +712,13 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification";
[self applyDesiredSocketState];
}
- (void)isCensorshipCircumventionActiveDidChange:(NSNotification *)notification
{
OWSAssert([NSThread isMainThread]);
[self applyDesiredSocketState];
}
#pragma mark - Logging
+ (NSString *)tag

Loading…
Cancel
Save