首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OSX上使用WPAD检索PAC脚本

在OSX上使用WPAD检索PAC脚本
EN

Stack Overflow用户
提问于 2010-12-08 00:42:29
回答 2查看 1.7K关注 0票数 5

如何在OSX上使用WPAD检索PAC脚本?希望DNS已经为这个约定预先配置了"http://wpad/wpad.dat“,获取"wpad”的内容就足够了吗?

有没有更“正式”的方法来做这件事?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-15 18:38:00

下面是如何获取给定URL的PAC代理:

代码语言:javascript
复制
#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];
}

为简单起见,此代码充满了泄漏(您应该释放通过复制和创建函数获得的所有内容),并且不处理任何潜在的错误。

票数 6
EN

Stack Overflow用户

发布于 2010-12-15 08:17:36

请参阅WPAD draft中有关合规性的第8节。按照您的建议只使用DNS将使您“最小限度地遵循”。

要实现完全兼容,应在使用DNS之前检查主机是否已收到来自DHCP的WPAD配置。您应该能够使用系统配置框架查看主机是否收到来自DHCP服务器的option 252参数。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4379156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档