【问题标题】:Retrieve PAC script using WPAD on OSX在 OSX 上使用 WPAD 检索 PAC 脚本
【发布时间】:2010-12-15 18:37:59
【问题描述】:

如何在 OSX 上使用 WPAD 检索 PAC 脚本?是否足以获取“http://wpad/wpad.dat”的内容以希望 DNS 已为此约定预配置“wpad”?

有没有更“正式”的方法来做到这一点?

【问题讨论】:

    标签: cocoa macos network-programming


    【解决方案1】:

    以下是获取给定 URL 的 PAC 代理的方法:

    #import <Foundation/Foundation.h>
    #import <CoreServices/CoreServices.h>
    #import <SystemConfiguration/SystemConfiguration.h>
    
    CFArrayRef CopyPACProxiesForURL(CFURLRef targetURL, CFErrorRef *error)
    {
        CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
        if (!proxies)
            return NULL;
    
        CFNumberRef pacEnabled;
        if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigEnable)))
        {
            int enabled;
            if (CFNumberGetValue(pacEnabled, kCFNumberIntType, &enabled) && enabled)
            {
                CFStringRef pacLocation = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesProxyAutoConfigURLString);
                CFURLRef pacUrl = CFURLCreateWithString(kCFAllocatorDefault, pacLocation, NULL);
                CFDataRef pacData;
                SInt32 errorCode;
                if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, pacUrl, &pacData, NULL, NULL, &errorCode))
                    return NULL;
    
                CFStringRef pacScript = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, pacData, kCFStringEncodingISOLatin1);
                if (!pacScript)
                    return NULL;
    
                CFArrayRef pacProxies = CFNetworkCopyProxiesForAutoConfigurationScript(pacScript, targetURL, error);
                return pacProxies;
            }
        }
    
        return NULL;
    }
    
    int main(int argc, const char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        CFURLRef targetURL = (CFURLRef)[NSURL URLWithString : @"http://stackoverflow.com/questions/4379156/retrieve-pac-script-using-wpad-on-osx/"];
        CFErrorRef error = NULL;
        CFArrayRef proxies = CopyPACProxiesForURL(targetURL, &error);
        if (proxies)
        {
            for (CFIndex i = 0; i < CFArrayGetCount(proxies); i++)
            {
                CFDictionaryRef proxy = CFArrayGetValueAtIndex(proxies, i);
                NSLog(@"%d\n%@", i, [(id)proxy description]);
            }
            CFRelease(proxies);
        }
    
        [pool drain];
    }
    

    为简单起见,这段代码充满了漏洞(您应该释放通过CopyCreate 函数获得的所有内容)并且不处理任何潜在错误。

    【讨论】:

    • 在启用kSCPropNetProxiesProxyAutoDiscoveryEnable 时,即当 Mac OS 应该使用 DHCP 或 DNS 找出 wpad.dat/PAC 的位置时,有没有办法获取 wpad.dat 文件的位置?
    【解决方案2】:

    请参阅WPAD draft 中关于合规性的第 8 节。按照您的建议仅使用 DNS 将使您“最低限度地合规”。

    为了完全符合要求,您应该在使用 DNS 之前检查主机是否已从 DHCP 接收到 WPAD 配置。您应该能够使用系统配置框架来查看主机是否从 DHCP 服务器接收到选项 252 参数。

    编辑:实际上,您可以直接从system configuration framework 获取 WPAD URL。看起来您会对kSCPropNetProxiesProxyAutoConfigEnable 感兴趣,如果将其设置为1,则WPAD URL 应位于kSCPropNetProxiesProxyAutoConfigURLString

    【讨论】:

      猜你喜欢
      • 2012-02-12
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多