︙ | | |
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
-
+
-
+
-
-
|
Class of_tls_socket_class = Nil;
static of_run_loop_mode_t connectRunLoopMode = @"of_tcp_socket_connect_mode";
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;
@interface OFTCPSocket_AsyncConnectContext: OFObject <OFStreamDelegate>
@interface OFTCPSocket_AsyncConnectContext: OFObject <OFTCPSocketDelegate>
{
OFTCPSocket *_socket;
OFString *_host;
uint16_t _port;
OFString *_SOCKS5Host;
uint16_t _SOCKS5Port;
id _target;
id <OFTCPSocketDelegate> _delegate;
SEL _selector;
id _context;
#ifdef OF_HAVE_BLOCKS
of_tcp_socket_async_connect_block_t _block;
#endif
id _exception;
OFData *_socketAddresses;
size_t _socketAddressesIndex;
enum {
|
︙ | | |
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
|
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
|
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
|
}
- (instancetype)initWithSocket: (OFTCPSocket *)sock
host: (OFString *)host
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
target: (id)target
delegate: (id <OFTCPSocketDelegate>)delegate;
selector: (SEL)selector
context: (id)context;
#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
host: (OFString *)host
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
block: (of_tcp_socket_async_connect_block_t)block;
#endif
- (void)didConnect;
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception;
- (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception;
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)sendSOCKS5Request;
@end
@interface OFTCPSocket_ConnectContext: OFObject
@interface OFTCPSocket_ConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
bool _done;
id _exception;
}
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception;
@end
@implementation OFTCPSocket_AsyncConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
host: (OFString *)host
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
target: (id)target
delegate: (id <OFTCPSocketDelegate>)delegate
selector: (SEL)selector
context: (id)context
{
self = [super init];
@try {
_socket = [sock retain];
_host = [host copy];
_port = port;
_SOCKS5Host = [SOCKS5Host copy];
_SOCKS5Port = SOCKS5Port;
_target = [target retain];
_delegate = [delegate retain];
_selector = selector;
_context = [context retain];
[_socket setDelegate: self];
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
+
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
|
return self;
}
#endif
- (void)dealloc
{
#ifdef OF_HAVE_BLOCKS
if (_block != NULL)
#endif
if ([_socket delegate] == self)
[_socket setDelegate: nil];
if ([_socket delegate] == self)
[_socket setDelegate: _delegate];
[_socket release];
[_host release];
[_SOCKS5Host release];
[_target release];
[_delegate release];
[_context release];
#ifdef OF_HAVE_BLOCKS
[_block release];
#endif
[_exception release];
[_socketAddresses release];
[_request release];
[super dealloc];
}
- (void)didConnect
{
[_socket setDelegate: nil];
if (_exception == nil)
[_socket setBlocking: true];
#ifdef OF_HAVE_BLOCKS
if (_block != NULL)
_block(_socket, _exception);
else {
#endif
void (*func)(id, SEL, OFTCPSocket *, id, id) =
(void (*)(id, SEL, OFTCPSocket *, id, id))
[_target methodForSelector: _selector];
[_socket setDelegate: _delegate];
if (_exception == nil) {
if ([_delegate respondsToSelector:
@selector(socket:didConnectToHost:port:)])
[_delegate socket: _socket
didConnectToHost: _host
port: _port];
} else {
if ([_delegate respondsToSelector:
@selector(stream:didFailWithException:)])
[_delegate stream: _socket
didFailWithException: _exception];
}
func(_target, _selector, _socket, _context, _exception);
#ifdef OF_HAVE_BLOCKS
}
#endif
}
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception
{
if (exception != nil) {
if (_socketAddressesIndex >= [_socketAddresses count]) {
_exception = [exception retain];
[self didConnect];
} else {
|
︙ | | |
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
-
+
-
-
+
-
|
}
[_socket setBlocking: false];
if (![_socket of_connectSocketToAddress: &address
errNo: &errNo]) {
if (errNo == EINPROGRESS) {
SEL selector = @selector(socketDidConnect:context:
SEL selector = @selector(socketDidConnect:exception:);
exception:);
[OFRunLoop of_addAsyncConnectForTCPSocket: _socket
mode: runLoopMode
target: self
selector: selector
selector: selector];
context: nil];
return;
} else {
[_socket of_closeSocket];
if (_socketAddressesIndex >= [_socketAddresses count]) {
_exception = [[OFConnectionFailedException
alloc] initWithHost: _host
|
︙ | | |
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
-
-
+
+
|
[_socket asyncReadIntoBuffer: _buffer
exactLength: 1
runLoopMode: runLoopMode];
return false;
case 4: /* IPv6 */
_SOCKS5State = SOCKS5_STATE_READ_ADDRESS;
[_socket asyncReadIntoBuffer: _buffer
exactLength: 16 + 2
runLoopMode: runLoopMode];
exactLength: 16 + 2
runLoopMode: runLoopMode];
return false;
default:
_exception = [[OFConnectionFailedException alloc]
initWithHost: _host
port: _port
socket: self
errNo: EPROTONOSUPPORT];
|
︙ | | |
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
-
+
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
|
didFailWithException: (id)exception
{
_exception = [exception retain];
[self didConnect];
}
@end
@implementation OFTCPSocket_ConnectContext
@implementation OFTCPSocket_ConnectDelegate
- (void)dealloc
{
[_exception release];
[super dealloc];
}
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception
- (void)socket: (OF_KINDOF(OFTCPSocket *))sock
didConnectToHost: (OFString *)host
port: (uint16_t)port
{
_done = true;
}
- (void)stream: (OF_KINDOF(OFStream *))stream
didFailWithException: (id)exception
{
_exception = [exception retain];
_done = true;
_exception = [exception retain];
}
@end
@implementation OFTCPSocket
@synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port;
@dynamic delegate;
+ (void)setSOCKS5Host: (OFString *)host
{
id old = defaultSOCKS5Host;
defaultSOCKS5Host = [host copy];
[old release];
}
|
︙ | | |
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
|
+
-
-
+
+
+
-
+
-
-
-
-
+
-
-
+
+
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
+
|
return errNo;
}
- (void)connectToHost: (OFString *)host
port: (uint16_t)port
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate = [_delegate retain];
OFTCPSocket_ConnectContext *context =
[[[OFTCPSocket_ConnectContext alloc] init] autorelease];
OFTCPSocket_ConnectDelegate *connectDelegate =
[[[OFTCPSocket_ConnectDelegate alloc] init] autorelease];
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
[self setDelegate: connectDelegate];
[self asyncConnectToHost: host
port: port
runLoopMode: connectRunLoopMode
runLoopMode: connectRunLoopMode];
target: context
selector: @selector(socketDidConnect:context:exception:)
context: nil];
while (!context->_done)
while (!connectDelegate->_done)
[runLoop runMode: connectRunLoopMode
beforeDate: nil];
/* Cleanup */
[runLoop runMode: connectRunLoopMode
beforeDate: [OFDate date]];
if (context->_exception != nil)
@throw context->_exception;
if (connectDelegate->_exception != nil)
@throw connectDelegate->_exception;
[self setDelegate: delegate];
objc_autoreleasePoolPop(pool);
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
target: (id)target
selector: (SEL)selector
context: (id)context
{
[self asyncConnectToHost: host
port: port
runLoopMode: of_run_loop_mode_default
runLoopMode: of_run_loop_mode_default];
target: target
selector: selector
context: context];
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
[[[[OFTCPSocket_AsyncConnectContext alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
target: target
selector: selector
context: context] autorelease]
delegate: _delegate] autorelease]
startWithRunLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
|
︙ | | |
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
-
-
-
-
-
-
+
+
+
+
+
+
|
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
block: (of_tcp_socket_async_connect_block_t)block
{
void *pool = objc_autoreleasePoolPush();
[[[[OFTCPSocket_AsyncConnectContext alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
block: block] autorelease]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
block: block] autorelease]
startWithRunLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
#endif
- (uint16_t)bindToHost: (OFString *)host
|
︙ | | |
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
|
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
|
-
+
-
-
-
+
-
-
-
-
-
-
-
-
+
-
|
OF_SOCKET_ADDRESS_FAMILY_UNKNOWN;
break;
}
return client;
}
- (void)asyncAcceptWithTarget: (id)target
- (void)asyncAccept
selector: (SEL)selector
context: (id)context
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default];
target: target
selector: selector
context: context];
}
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context
{
[OFRunLoop of_addAsyncAcceptForTCPSocket: self
mode: runLoopMode
target: target
selector: selector
delegate: _delegate];
context: context];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
block: block];
|
︙ | | |