Retrieve PAC script using WPAD on OSX

后端 未结 2 1581
梦谈多话
梦谈多话 2021-02-06 15:49

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 co

2条回答
  •  一向
    一向 (楼主)
    2021-02-06 16:21

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

    #import 
    #import 
    #import 
    
    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.

提交回复
热议问题