Cocoa NSStream可以与socks一起使用,但不能同时使用socks5

时间:2010-04-07 20:30:41

标签: cocoa ssl socks nsstream

在连接(到FTP,首先没有SSL)时,我运行:

NSArray *objects = [NSArray arrayWithObjects:@"proxy.ip", [NSNumber numberWithInt:1080], NSStreamSOCKSProxyVersion5, @"user", @"pass", nil];
NSArray *keys = [NSArray arrayWithObjects:NSStreamSOCKSProxyHostKey, NSStreamSOCKSProxyPortKey, NSStreamSOCKSProxyVersionKey, NSStreamSOCKSProxyUserKey, NSStreamSOCKSProxyPasswordKey, nil];
NSDictionary *proxyDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream setProperty:proxyDictionary forKey:NSStreamSOCKSProxyConfigurationKey];
[iStream open];

同样适用于iStream。这允许我通过socks5代理成功连接。 如果我继续没有setProperty:proxyDictionary ...(socks5禁用)我会告诉服务器切换到SSL,然后成功将这些设置应用到输入/输出流,从而给我一个SSL连接:

NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
[settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
// to allow selfsigned certificates:
[settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
[iStream retain];   
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
CFReadStreamSetProperty((CFReadStreamRef)iStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
//[iStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey]; <-- needs to be default or unset, then it works.

对于oStream也一样。如果我禁用socks5,所有这些都可以正常工作。如果我将其打开(第一个snippit中的第7行),则在应用SSL设置时会失去联系。

如果我不得不猜测,我认为在应用(ssl)“设置”时会失去一些属性?

请帮助:)

  • 埃文

1 个答案:

答案 0 :(得分:0)

解决方案在Problem with NSStream SSL Connection,我不应该设置安全级别。我把上面的帖子编辑成了一个有效的解决方案。