-
Notifications
You must be signed in to change notification settings - Fork 81
/
MTConnectionProbing.m
177 lines (152 loc) · 5.65 KB
/
MTConnectionProbing.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#import "MTConnectionProbing.h"
#if defined(MtProtoKitDynamicFramework)
# import <MTProtoKitDynamic/MTSignal.h>
# import <MTProtoKitDynamic/MTQueue.h>
# import <MTProtoKitDynamic/MTAtomic.h>
# import <MTProtoKitDynamic/MTHttpRequestOperation.h>
# import <MTProtoKitDynamic/MTEncryption.h>
# import <MTProtoKitDynamic/MTRequestMessageService.h>
# import <MTProtoKitDynamic/MTRequest.h>
# import <MTProtoKitDynamic/MTContext.h>
# import <MTProtoKitDynamic/MTApiEnvironment.h>
# import <MTProtoKitDynamic/MTDatacenterAddress.h>
# import <MTProtoKitDynamic/MTDatacenterAddressSet.h>
# import <MTProtoKitDynamic/MTProto.h>
# import <MTProtoKitDynamic/MTSerialization.h>
# import <MTProtoKitDynamic/MTLogging.h>
# import <MTProtoKitDynamic/MTProxyConnectivity.h>
#elif defined(MtProtoKitMacFramework)
# import <MTProtoKitMac/MTSignal.h>
# import <MTProtoKitMac/MTQueue.h>
# import <MTProtoKitMac/MTAtomic.h>
# import <MTProtoKitMac/MTHttpRequestOperation.h>
# import <MTProtoKitMac/MTEncryption.h>
# import <MTProtoKitMac/MTRequestMessageService.h>
# import <MTProtoKitMac/MTRequest.h>
# import <MTProtoKitMac/MTContext.h>
# import <MTProtoKitMac/MTApiEnvironment.h>
# import <MTProtoKitMac/MTDatacenterAddress.h>
# import <MTProtoKitMac/MTDatacenterAddressSet.h>
# import <MTProtoKitMac/MTProto.h>
# import <MTProtoKitMac/MTSerialization.h>
# import <MTProtoKitMac/MTLogging.h>
# import <MTProtoKitMac/MTProxyConnectivity.h>
#else
# import <MTProtoKit/MTSignal.h>
# import <MTProtoKit/MTQueue.h>
# import <MTProtoKit/MTAtomic.h>
# import <MTProtoKit/MTHttpRequestOperation.h>
# import <MTProtoKit/MTEncryption.h>
# import <MTProtoKit/MTRequestMessageService.h>
# import <MTProtoKit/MTRequest.h>
# import <MTProtoKit/MTContext.h>
# import <MTProtoKit/MTApiEnvironment.h>
# import <MTProtoKit/MTDatacenterAddress.h>
# import <MTProtoKit/MTDatacenterAddressSet.h>
# import <MTProtoKit/MTProto.h>
# import <MTProtoKit/MTSerialization.h>
# import <MTProtoKit/MTLogging.h>
# import <MTProtoKit/MTProxyConnectivity.h>
#endif
#import "PingFoundation.h"
@interface MTPingHelper : NSObject <PingFoundationDelegate> {
void (^_success)();
PingFoundation *_ping;
}
@end
@implementation MTPingHelper
+ (void)runLoopThreadFunc {
while (true) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
}
}
+ (NSThread *)runLoopThread {
static NSThread *thread = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
thread = [[NSThread alloc] initWithTarget:self selector:@selector(runLoopThreadFunc) object:nil];
thread.name = @"MTPingHelper";
[thread start];
});
return thread;
}
+ (void)dispatchOnRunLoopThreadImpl:(void (^)())f {
if (f) {
f();
}
}
+ (void)dispatchOnRunLoopThread:(void (^)())block {
[self performSelector:@selector(dispatchOnRunLoopThreadImpl:) onThread:[self runLoopThread] withObject:[block copy] waitUntilDone:false];
}
- (instancetype)initWithSuccess:(void (^)())success {
self = [super init];
if (self != nil) {
_success = [success copy];
NSArray *hosts = @[
@"google.com",
@"8.8.8.8"
];
NSString *host = hosts[(int)(arc4random_uniform((uint32_t)hosts.count))];
_ping = [[PingFoundation alloc] initWithHostName:host];
_ping.delegate = self;
[_ping start];
}
return self;
}
- (void)dealloc {
#if DEBUG
assert(_ping.delegate == nil);
#endif
if (_ping.delegate != nil) {
_ping.delegate = nil;
[_ping stop];
}
}
- (void)stop {
_ping.delegate = nil;
[_ping stop];
}
- (void)pingFoundation:(PingFoundation *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber {
if (_success) {
_success();
}
}
- (void)pingFoundation:(PingFoundation *)pinger didStartWithAddress:(NSData *)__unused address {
[pinger sendPingWithData:nil];
}
@end
@implementation MTConnectionProbing
+ (MTSignal *)pingAddress {
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber) {
MTMetaDisposable *disposable = [[MTMetaDisposable alloc] init];
[MTPingHelper dispatchOnRunLoopThread:^{
MTPingHelper *helper = [[MTPingHelper alloc] initWithSuccess:^{
[subscriber putNext:@true];
[subscriber putCompletion];
}];
[disposable setDisposable:[[MTBlockDisposable alloc] initWithBlock:^{
[MTPingHelper dispatchOnRunLoopThread:^{
[helper stop];
}];
}]];
}];
return disposable;
}];
}
+ (MTSignal *)probeProxyWithContext:(MTContext *)context datacenterId:(NSInteger)datacenterId settings:(MTSocksProxySettings *)settings {
MTSignal *proxyAvailable = [[[MTProxyConnectivity pingProxyWithContext:context datacenterId:datacenterId settings:settings] map:^id(MTProxyConnectivityStatus *status) {
return @(status.reachable);
}] timeout:10.0 onQueue:[MTQueue concurrentDefaultQueue] orSignal:[MTSignal single:@false]];
MTSignal *referenceAvailable = [[self pingAddress] timeout:10.0 onQueue:[MTQueue concurrentDefaultQueue] orSignal:[MTSignal single:@false]];
MTSignal *combined = [[MTSignal combineSignals:@[proxyAvailable, referenceAvailable]] map:^id(NSArray *values) {
NSNumber *proxy = values[0];
NSNumber *ping = values[1];
if (![proxy boolValue] && [ping boolValue]) {
return @true;
} else {
return @false;
}
}];
return combined;
}
@end