Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OSX - how can I grab proxy configuration using Cocoa or even pure C functions?

I have noticed that my app doesn't use the proxy settings available for the machine (I'm using the Charles proxy to test proxy configuration). One piece of the app that makes calls using NSURLConnection correctly uses and makes requests using the proxy, the other piece of the app (that is a MonoMac app, running on Mono, obviously) does not.

It keeps making requests as if there is no proxy configured. Is there a function or object I can use to grab the same proxy configuration NSURLConnection is using?

like image 562
Maurício Linhares Avatar asked Nov 07 '12 18:11

Maurício Linhares


People also ask

How do I find my network proxy settings Mac?

On your Mac, choose Apple menu > System Preferences, then click Network . In the list, select the network service you use—for example, Ethernet or Wi-Fi. Click Advanced, then click Proxies.

How do I change my proxy settings on my Mac?

In the Safari app on your Mac, choose Safari > Settings, then click Advanced. Click Change Settings (next to Proxies) to open Network settings. Change the proxy settings using the information your network administrator provided. Click OK.

How do I install a PAC file on a Mac?

Select the proxy you want to configure, then enter the settings on the right. Configure proxy server settings automatically: Select Auto Proxy Discovery. Use a proxy auto-configuration (PAC) file: Select Automatic Proxy Configuration, then enter the address of the PAC file in the URL field.


2 Answers

The Proxies Dictionary in SystemConfiguration framework would contain this information

like image 43
Colin Swelin Avatar answered Nov 03 '22 02:11

Colin Swelin


The function that gives you all the proxy information is SCDynamicStoreCopyProxies(), it can be called as in the example below (once you're done you also have to CFRelease all of these objects, since they're all from CF and not directly Cocoa objects):

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>

int main(int argc, const char * argv[])
{

  @autoreleasepool {

    CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);

    CFIndex count = CFDictionaryGetCount(proxies);

    NSLog(@"Number of keys is %ld", count);

    NSDictionary * proxyConfiguration = (NSDictionary*) proxies;

    for ( id key in proxyConfiguration.keyEnumerator ) {
      NSLog(@"Pair is %@ -> %@", key, [proxyConfiguration valueForKey: key]);
    }

  }
    return 0;
}

And the output will be something like:

2012-11-07 16:33:57.844 network-test[6501:403] Number of keys is 12
2012-11-07 16:33:57.847 network-test[6501:403] Pair is HTTPEnable -> 1
2012-11-07 16:33:57.848 network-test[6501:403] Pair is HTTPSProxy -> 127.0.0.1
2012-11-07 16:33:57.848 network-test[6501:403] Pair is ExceptionsList -> (
    "www.google.com"
)
2012-11-07 16:33:57.849 network-test[6501:403] Pair is HTTPSPort -> 8888
2012-11-07 16:33:57.850 network-test[6501:403] Pair is __SCOPED__ -> {
    en1 =     {
        ExceptionsList =         (
            "www.google.com"
        );
        FTPPassive = 1;
        HTTPEnable = 1;
        HTTPPort = 8888;
        HTTPProxy = "127.0.0.1";
        HTTPSEnable = 1;
        HTTPSPort = 8888;
        HTTPSProxy = "127.0.0.1";
        SOCKSEnable = 1;
        SOCKSPort = 8889;
        SOCKSProxy = "127.0.0.1";
    };
}
2012-11-07 16:33:57.850 network-test[6501:403] Pair is HTTPProxy -> 127.0.0.1
2012-11-07 16:33:57.851 network-test[6501:403] Pair is SOCKSPort -> 8889
2012-11-07 16:33:57.852 network-test[6501:403] Pair is SOCKSProxy -> 127.0.0.1
2012-11-07 16:33:57.852 network-test[6501:403] Pair is HTTPSEnable -> 1
2012-11-07 16:33:57.853 network-test[6501:403] Pair is SOCKSEnable -> 1
2012-11-07 16:33:57.853 network-test[6501:403] Pair is HTTPPort -> 8888
2012-11-07 16:33:57.854 network-test[6501:403] Pair is FTPPassive -> 1
like image 172
Maurício Linhares Avatar answered Nov 03 '22 00:11

Maurício Linhares