I added SOCKS ability to CocoaAsyncSocket. I first tried to do this on GCDAsyncSocket, but failed.
For CocoaAsyncSocket, I simply set SOCKS properties on the read/write stream in this method (In GCDAsyncSocket, only iOS used read/write sockets using CFSocket) This was pasted in the method - (BOOL)createStreamsToHost:(NSString )hostname onPort:(UInt16)port error:(NSError *)errPtr
NSLog(@"Setting SOCKS proxy: %@:%d", theProxyHost, theProxyPort);
CFDictionaryRef proxyDict = CFNetworkCopySystemProxySettings();
CFMutableDictionaryRef socksConfig = CFDictionaryCreateMutableCopy(NULL, 0, proxyDict);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyHost, (CFStringRef)theProxyHost);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyPort, (__bridge CFNumberRef)[NSNumber numberWithInteger:theProxyPort]);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSVersion, kCFStreamSocketSOCKSVersion4);
CFReadStreamSetProperty((CFReadStreamRef)theReadStream, kCFStreamPropertySOCKSProxy, socksConfig);
CFWriteStreamSetProperty((CFWriteStreamRef)theWriteStream, kCFStreamPropertySOCKSProxy, socksConfig);
CFRelease(proxyDict);
It works well, as it routes all traffic through the Proxy. But either I don't understand how proxies work, or I did something wrong, because:
if(![socket connectToHost:@"somedomain.com" onPort:9567 error:&error]) {
NSLog(@"Could not connect: %@", [error localizedDescription]);
}
This always returns true, regardless if the server responds. Because the Proxy responds. CocoaAsyncSocket's delegate method
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
is called with host and port of the proxy server, not the server that I called connectToHost:onPort:error on.
What am I missing here?