Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFStream crashes after setting SOCKS proxy config

What's wrong with the code below? I use AsyncSocket to connect to a SOCKS proxy and set the proxy settings on onSocketWillConnect delegate method. If I omit the calls to CFReadStreamSetProperty and CFWriteStreamSetProperty the socket connection will follow through smoothly. Else, I get a [Not A Type retain] on a deallocated instance with no traceable stack trace (It might be CFNetwork related?). Does anyone have any idea what gives?

CFDictionaryRef proxyDict = CFNetworkCopySystemProxySettings();
CFMutableDictionaryRef socksConfig = CFDictionaryCreateMutableCopy(NULL, 0, proxyDict);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyHost, CFSTR("192.168.1.148"));
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSProxyPort, (__bridge CFNumberRef)[NSNumber numberWithInt:3129]);
CFDictionarySetValue(socksConfig, kCFStreamPropertySOCKSVersion, kCFStreamSocketSOCKSVersion4);

// set SOCKS for read streams
CFReadStreamRef readStream = [sock getCFReadStream];
if (!CFReadStreamSetProperty(readStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFReadStreamGetError(readStream);
  NSLog(@"[SEVERE] Web Socket Read Stream Error: %ld[%ld]", error.domain, error.error);
}

// set SOCKS for write stream
CFWriteStreamRef writeStream = [sock getCFWriteStream];
if (!CFWriteStreamSetProperty(writeStream, kCFStreamPropertySOCKSProxy, socksConfig)) {
  CFStreamError error = CFWriteStreamGetError(writeStream);
  NSLog(@"[SEVERE] Web Socket Write Stream Error: %ld[%ld]", error.domain, error.error);
}

// Release
CFRelease(socksConfig);
CFRelease(proxyDict);
like image 714
LaN Avatar asked Aug 22 '12 09:08

LaN


1 Answers

From the documentation of CFReadStream:

Properties that can be set configure the behavior of the stream and may be modifiable only at particular times, such as before the stream has been opened. (In fact, you should assume that you can set properties only before opening the stream, unless otherwise noted.)

onSocketWillConnect may be too late to set those properties.

like image 105
Axel K. Avatar answered Oct 05 '22 02:10

Axel K.