在 OSX 上使用 WPAD 检索 PAC 脚本

发布于 2024-10-06 11:00:47 字数 115 浏览 3 评论 0原文

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

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

How do I retrieve the PAC script using WPAD on OSX? is it enough to fetch the contents of "http://wpad/wpad.dat" in hopes that the DNS has "wpad" pre-configured for this convention?

is there a more "formal" method of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-10-13 11:00:47

以下是如何获取给定 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 函数获得的所有内容)并且不处理任何潜在的错误。

Here is how to get PAC proxies for a given URL:

#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];
}

For the sake of simplicity, this code is full of leaks (you should release everything you got through Copy and Create functions) and does not handle any potential error.

甲如呢乙后呢 2024-10-13 11:00:47

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

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

编辑:实际上,您可以直接从 系统配置框架。您似乎对 kSCPropNetProxiesProxyAutoConfigEnable 感兴趣,如果将其设置为 1,则 WPAD URL 应位于 kSCPropNetProxiesProxyAutoConfigURLString 中。

See section 8 of the WPAD draft on compliance. Using only DNS as you suggest would make you "minimally compliant".

To be fully compliant, you should check to see if the host has received WPAD configuration from DHCP prior to using DNS. You should be able to use the System Configuration framework to see if the host received an option 252 parameter from the DHCP server.

EDIT: Actually, you can get the WPAD URL directly from the system configuration framework. Looks like you'd be interested in kSCPropNetProxiesProxyAutoConfigEnable, and if that's set to 1, the WPAD URL should be in kSCPropNetProxiesProxyAutoConfigURLString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文