Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve PAC script using WPAD on OSX

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?

like image 284
JasonGenX Avatar asked Dec 07 '10 16:12

JasonGenX


People also ask

Where do I find PAC files on Mac?

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.

What is WPAD PAC?

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.


1 Answers

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.

like image 175
0xced Avatar answered Sep 30 '22 14:09

0xced