-
Notifications
You must be signed in to change notification settings - Fork 81
/
MTProxyConnectivity.m
172 lines (153 loc) · 7.17 KB
/
MTProxyConnectivity.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
#import "MTProxyConnectivity.h"
#if defined(MtProtoKitDynamicFramework)
# import <MTProtoKitDynamic/MTSignal.h>
# import <MTProtoKitDynamic/MTQueue.h>
# import <MTProtoKitDynamic/MTContext.h>
# import <MTProtoKitDynamic/MTApiEnvironment.h>
# import <MTProtoKitDynamic/MTDatacenterAddressSet.h>
# import <MTProtoKitDynamic/MTDatacenterAddress.h>
# import <MTProtoKitDynamic/MTTcpConnection.h>
# import <MTProtoKitDynamic/MTTransportScheme.h>
#elif defined(MtProtoKitMacFramework)
# import <MTProtoKitMac/MTSignal.h>
# import <MTProtoKitMac/MTQueue.h>
# import <MTProtoKitMac/MTContext.h>
# import <MTProtoKitMac/MTApiEnvironment.h>
# import <MTProtoKitMac/MTDatacenterAddressSet.h>
# import <MTProtoKitMac/MTDatacenterAddress.h>
# import <MTProtoKitMac/MTTcpConnection.h>
# import <MTProtoKitMac/MTTransportScheme.h>
#else
# import <MTProtoKit/MTSignal.h>
# import <MTProtoKit/MTQueue.h>
# import <MTProtoKit/MTContext.h>
# import <MTProtoKit/MTApiEnvironment.h>
# import <MTProtoKit/MTDatacenterAddressSet.h>
# import <MTProtoKit/MTDatacenterAddress.h>
# import <MTProtoKit/MTTcpConnection.h>
# import <MTProtoKit/MTTransportScheme.h>
#endif
#import "MTDiscoverConnectionSignals.h"
@implementation MTProxyConnectivityStatus
- (instancetype)initWithReachable:(bool)reachable roundTripTime:(NSTimeInterval)roundTripTime {
self = [super init];
if (self != nil) {
_reachable = reachable;
_roundTripTime = roundTripTime;
}
return self;
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[MTProxyConnectivityStatus class]]) {
return false;
}
MTProxyConnectivityStatus *other = object;
if (_reachable != other.reachable) {
return false;
}
if (_roundTripTime != other.roundTripTime) {
return false;
}
return true;
}
@end
@implementation MTProxyConnectivity
+ (bool)isResponseValid:(NSData *)data payloadData:(MTPayloadData)payloadData
{
if (data.length >= 84)
{
uint8_t zero[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
uint8_t resPq[] = { 0x63, 0x24, 0x16, 0x05 };
if (memcmp((uint8_t * const)data.bytes, zero, 8) == 0 && memcmp(((uint8_t * const)data.bytes) + 20, resPq, 4) == 0 && memcmp(((uint8_t * const)data.bytes) + 24, payloadData.nonce, 16) == 0)
{
return true;
}
}
return false;
}
+ (MTSignal *)pingWithAddress:(MTDatacenterAddress *)address datacenterId:(NSUInteger)datacenterId settings:(MTSocksProxySettings *)settings context:(MTContext *)context {
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber) {
MTQueue *queue = [[MTQueue alloc] init];
MTMetaDisposable *disposable = [[MTMetaDisposable alloc] init];
[queue dispatchOnQueue:^{
[subscriber putNext:[NSNull null]];
MTPayloadData payloadData;
NSData *data = [MTDiscoverConnectionSignals payloadData:&payloadData context:context address:address];
MTContext *proxyContext = [[MTContext alloc] initWithSerialization:context.serialization apiEnvironment:[[context apiEnvironment] withUpdatedSocksProxySettings:settings] isTestingEnvironment:context.isTestingEnvironment useTempAuthKeys:context.useTempAuthKeys];
MTTcpConnection *connection = [[MTTcpConnection alloc] initWithContext:proxyContext datacenterId:datacenterId scheme:[[MTTransportScheme alloc] initWithTransportClass:[MTTcpConnection class] address:address media:false] interface:nil usageCalculationInfo:nil];
__weak MTTcpConnection *weakConnection = connection;
__block NSTimeInterval startTime = CFAbsoluteTimeGetCurrent();
connection.connectionOpened = ^ {
__strong MTTcpConnection *strongConnection = weakConnection;
if (strongConnection != nil) {
startTime = CFAbsoluteTimeGetCurrent();
[strongConnection sendDatas:@[data] completion:nil requestQuickAck:false expectDataInResponse:true];
}
};
__block bool received = false;
connection.connectionReceivedData = ^(NSData *data) {
received = true;
if ([self isResponseValid:data payloadData:payloadData]) {
NSTimeInterval roundTripTime = CFAbsoluteTimeGetCurrent() - startTime;
[subscriber putNext:[[MTProxyConnectivityStatus alloc] initWithReachable:true roundTripTime:roundTripTime]];
} else {
[subscriber putNext:[[MTProxyConnectivityStatus alloc] initWithReachable:false roundTripTime:0.0]];
}
[subscriber putCompletion];
};
connection.connectionClosed = ^
{
if (!received) {
[subscriber putNext:[[MTProxyConnectivityStatus alloc] initWithReachable:false roundTripTime:0.0]];
[subscriber putCompletion];
}
};
[connection start];
[disposable setDisposable:[[MTBlockDisposable alloc] initWithBlock:^{
[queue dispatchOnQueue:^{
[connection stop];
__unused id desc = [proxyContext description];
}];
}]];
}];
return disposable;
}];
}
+ (MTSignal *)pingProxyWithContext:(MTContext *)context datacenterId:(NSInteger)datacenterId settings:(MTSocksProxySettings *)settings {
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber) {
MTDatacenterAddressSet *addressSet = [context addressSetForDatacenterWithId:datacenterId];
NSMutableArray *signals = [[NSMutableArray alloc] init];
for (MTDatacenterAddress *address in addressSet.addressList) {
if (!address.isIpv6) {
[signals addObject:[self pingWithAddress:address datacenterId:datacenterId settings:settings context:context]];
}
if (address.isIpv6) {
[signals addObject:[self pingWithAddress:address datacenterId:datacenterId settings:settings context:context]];
}
}
if (signals.count == 0) {
[subscriber putNext:[[MTProxyConnectivityStatus alloc] initWithReachable:false roundTripTime:0.0]];
[subscriber putCompletion];
return nil;
}
return [[MTSignal combineSignals:signals] startWithNext:^(NSArray *results) {
bool allStatusesAreValid = true;
for (MTProxyConnectivityStatus *status in results) {
if ([status isKindOfClass:[MTProxyConnectivityStatus class]]) {
if (status.reachable) {
[subscriber putNext:status];
[subscriber putCompletion];
return;
}
} else {
allStatusesAreValid = false;
}
}
if (allStatusesAreValid) {
[subscriber putNext:[[MTProxyConnectivityStatus alloc] initWithReachable:false roundTripTime:0.0]];
[subscriber putCompletion];
}
}];
}];
}
@end