CFStream shuts down after setting SOCKS proxy config

What happened to the code below? I use AsyncSocket to connect to SOCKS proxies and set proxy settings in the onSocketWillConnect delegation onSocketWillConnect . If I omit calls to CFReadStreamSetProperty and CFWriteStreamSetProperty , the socket connection will run smoothly. Otherwise, I get [Do not save type] on the freed instance without tracked stack trace (maybe this is due to CFNetwork?). Does anyone know 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); 
+7
source share
1 answer

From the CFReadStream documentation:

The properties that can be configured adjust the behavior of the stream and can only be changed at certain times, for example, before the stream was opened. (In fact, you should assume that you can only set properties before the stream opens, unless otherwise specified.)

onSocketWillConnect may be too late to set these properties.

+1
source

All Articles