︙ | | |
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
-
+
|
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#import "OFTCPSocket.h"
#import "OFTCPSocket+Private.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFDate.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFTimer.h"
|
︙ | | |
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
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
-
-
-
-
-
-
-
+
-
+
|
#import "OFOutOfRangeException.h"
#import "OFSetOptionFailedException.h"
#import "socket.h"
#import "socket_helpers.h"
#import "resolver.h"
/* References for static linking */
void
_references_to_categories_of_OFTCPSocket(void)
{
_OFTCPSocket_SOCKS5_reference = 1;
}
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_ConnectContext: OFObject
@interface OFTCPSocket_AsyncConnectContext: OFObject
{
OFTCPSocket *_socket;
OFString *_host;
uint16_t _port;
OFString *_SOCKS5Host;
uint16_t _SOCKS5Port;
id _target;
|
︙ | | |
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
- (bool)socket: (OFTCPSocket *)sock
didReadSOCKS5AddressLength: (unsigned char *)addressLength
length: (size_t)length
context: (id)context
exception: (id)exception;
@end
@interface OFTCPSocket_ConnectContext: OFObject
{
@public
bool _connected;
id _exception;
}
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception;
@end
@implementation OFTCPSocket_ConnectContext
@implementation OFTCPSocket_AsyncConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
host: (OFString *)host
port: (uint16_t)port
SOCKS5Host: (OFString *)SOCKS5Host
SOCKS5Port: (uint16_t)SOCKS5Port
target: (id)target
selector: (SEL)selector
|
︙ | | |
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
target: self
selector: @selector(socket:didReadSOCKS5Address:
length:context:exception:)
context: nil];
return false;
}
@end
@implementation OFTCPSocket_ConnectContext
- (void)dealloc
{
[_exception release];
[super dealloc];
}
- (void)socketDidConnect: (OFTCPSocket *)sock
context: (id)context
exception: (id)exception
{
if (exception != nil)
_exception = [exception retain];
_connected = true;
}
@end
@implementation OFTCPSocket
@synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port;
+ (void)setSOCKS5Host: (OFString *)host
{
id old = defaultSOCKS5Host;
|
︙ | | |
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
+
|
return errNo;
}
- (void)connectToHost: (OFString *)host
port: (uint16_t)port
{
OFString *destinationHost = host;
uint16_t destinationPort = port;
of_resolver_result_t **results, **iter;
int errNo = 0;
if (_socket != INVALID_SOCKET)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
void *pool = objc_autoreleasePoolPush();
OFTCPSocket_ConnectContext *context =
[[[OFTCPSocket_ConnectContext alloc] init] autorelease];
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
if (_SOCKS5Host != nil) {
/* Connect to the SOCKS5 proxy instead */
host = _SOCKS5Host;
port = _SOCKS5Port;
}
[self asyncConnectToHost: host
results = of_resolve_host(host, port, SOCK_STREAM);
port: port
for (iter = results; *iter != NULL; iter++) {
of_resolver_result_t *result = *iter;
of_socket_address_t address;
runLoopMode: connectRunLoopMode
switch (result->family) {
case AF_INET:
address.family = OF_SOCKET_ADDRESS_FAMILY_IPV4;
break;
#ifdef AF_INET6
case AF_INET6:
address.family = OF_SOCKET_ADDRESS_FAMILY_IPV6;
break;
#endif
default:
errNo = EAFNOSUPPORT;
continue;
target: context
}
selector: @selector(socketDidConnect:context:exception:)
context: nil];
if (result->addressLength > sizeof(address)) {
errNo = EOVERFLOW;
continue;
}
while (!context->_connected)
address.length = result->addressLength;
memcpy(&address.sockaddr.sockaddr, result->address,
result->addressLength);
[runLoop runMode: connectRunLoopMode
if (![self of_createSocketForAddress: &address
errNo: &errNo])
continue;
beforeDate: nil];
_blocking = true;
if (![self of_connectSocketToAddress: &address
errNo: &errNo]) {
[self of_closeSocket];
continue;
/* Cleanup */
}
break;
}
[runLoop runMode: connectRunLoopMode
beforeDate: [OFDate date]];
if (context->_exception != nil)
of_resolver_free(results);
@throw context->_exception;
if (_socket == INVALID_SOCKET)
@throw [OFConnectionFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
if (_SOCKS5Host != nil)
[self OF_SOCKS5ConnectToHost: destinationHost
port: destinationPort];
objc_autoreleasePoolPop(pool);
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
target: (id)target
selector: (SEL)selector
context: (id)context
|
︙ | | |
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
-
+
|
runLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
[[[[OFTCPSocket_ConnectContext alloc]
[[[[OFTCPSocket_AsyncConnectContext alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
target: target
selector: selector
|
︙ | | |
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
|
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
-
+
|
- (void)asyncConnectToHost: (OFString *)host
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_ConnectContext alloc]
[[[[OFTCPSocket_AsyncConnectContext alloc]
initWithSocket: self
host: host
port: port
SOCKS5Host: _SOCKS5Host
SOCKS5Port: _SOCKS5Port
block: block] autorelease]
startWithRunLoopMode: runLoopMode];
|
︙ | | |