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?
pac file (or a URL to load one from), you can configure OS X to use it in System Preferences -> Network preference pane -> select the active network service (interface) in the sidebar -> Advanced -> Proxies tab -> Automatic proxy configuration. Save this answer. Show activity on this post.
The Web Proxy Autodiscovery Protocol (WPAD) is a method used by web browsers to locate the URL of a PAC file automatically, without manual configuration. WPAD can be used with Content Gateway, but is not an option with hybrid web filtering.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With