NSNetworkSettings & HTTP Proxy Info

On CocoaDev, there’s a page listing an undocumented Foundation class called NSNetworkSettings. Using methods from this class in my AS Studio app on Mac OS X 10.4.3 does return the proper HTTP & HTTPS proxy information calling it like so:

set this_URL to "http://www.google.com/"
set URLWithString to call method "URLWithString:" of class "NSURL" with parameter this_URL
set sharedNetworkSettings to call method "sharedNetworkSettings" of class "NSNetworkSettings"
set proxyPropertiesForURL to call method "proxyPropertiesForURL:" of sharedNetworkSettings with parameter URLWithString

set {HTTP_proxy, HTTP_proxy_port} to {"", 80}
set {HTTPS_proxy, HTTPS_proxy_port} to {"", 80}
tell |kCFStreamPropertyHTTPProxy| of proxyPropertiesForURL
	set HTTP_proxy_enabled to ((its |HTTPEnable|) = 1)
	if HTTP_proxy_enabled then
		set HTTP_proxy to its |HTTPProxy|
		set HTTP_proxy_port to its (|HTTPPort|) as integer
	end if
	set HTTPS_proxy_enabled to ((its |HTTPSEnable|) = 1)
	if HTTPS_proxy_enabled then
		set HTTPS_proxy to its |HTTPSProxy|
		set HTTPS_proxy_port to its (|HTTPSPort|) as integer
	end if
end tell

return {HTTP_proxy_enabled:HTTP_proxy_enabled, HTTP_proxy:HTTP_proxy, HTTP_proxy_port:HTTP_proxy_port, HTTPS_proxy_enabled:HTTPS_proxy_enabled, HTTPS_proxy:HTTPS_proxy, HTTPS_proxy_port:HTTPS_proxy_port}

Can any one else confirm that this works for them? I need compatibility back to 10.2. Is this safe to use? Even if this does return the proper proxy info on 10.2+, is there some better way to get the HTTP & HTTPS proxy info?

Thanks,
Jon

Your code works fine here (OS X 10.4.3).

I don’t know if this exists in older OSs, but there is a file with all this info at:

/Library/Preferences/SystemConfiguration/preferences.plist

(This file is also in OS X 10.3.9)

Hi jj,

Thanks for the confirmation and hint (as always, you’re very helpful). Anyone have confirmation on earlier systems? As to that plist file, it is hard to parse (at least on my system) because the string that is listed for the currentSet value is not actually found in any of the known sets it tracks. The name (encoded) is a few characters different from a set contained in the NetworkSettings default in the same file. Since I couldn’t match it from the name, I didn’t know which set’s proxy info to use. Incidentally, for people who want an easy way to parse a plist file in an AS Studio app, try the following (it should return the contents of a plist file as a record or a list of records):

set the_record to my read_plist(POSIX path of (choose file with prompt "Choose a plist file:"))
log the_record

on read_plist(the_path)
	try
		set a to call method "arrayWithContentsOfFile:" of class "NSArray" with parameter the_path
		return a
	on error
		try
			set d to call method "dictionaryWithContentsOfFile:" of class "NSDictionary" with parameter the_path
			return d
		on error
			return false
		end try
	end try
end read_plist

Jon

I opted to use the SystemConfiguration framework. So, I added this method to my app:

[code]#include <SystemConfiguration/SystemConfiguration.h>

  • (NSDictionary *)getProxyInfo {
    NSMutableDictionary *returnData = [[[NSMutableDictionary alloc] init] autorelease];
    NSDictionary *proxies = (NSDictionary *)SCDynamicStoreCopyProxies(NULL);
    BOOL HTTPEnabled = [[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPEnable] boolValue];
    NSString *HTTPHost = @“”;
    NSNumber *HTTPPort = [NSNumber numberWithInt:80];
    BOOL HTTPSEnabled = [[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPSEnable] boolValue];
    NSString *HTTPSHost = @“”;
    NSNumber *HTTPPort = [NSNumber numberWithInt:443];

    if (HTTPEnabled) {
    HTTPHost = (NSString *)[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPProxy];
    HTTPPort = (NSNumber *)[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPPort];
    }
    if (HTTPSEnabled) {
    HTTPSHost = (NSString *)[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPSProxy];
    HTTPSPort = (NSNumber *)[proxies objectForKey:(NSString *)kSCPropNetProxiesHTTPSPort];
    }

    [returnData setObject:[NSNumber numberWithBool:HTTPEnabled] forKey:@“HTTPEnabled”];
    [returnData setObject:HTTPHost forKey:@“HTTPHost”];
    [returnData setObject:HTTPPort forKey:@“HTTPPort”];
    [returnData setObject:[NSNumber numberWithBool:HTTPSEnabled] forKey:@“HTTPSEnabled”];
    [returnData setObject:HTTPSHost forKey:@“HTTPSHost”];
    [returnData setObject:HTTPSPort forKey:@“HTTPSPort”];
    return returnData;
    }[/code]
    and use it like so:

call method "getProxyInfo"
-->{|HTTPEnabled|:true, |HTTPHost|:"192.168.0.1", |HTTPPort|:80, |HTTPSEnabled|:false, |HTTPSHost|:"", |HTTPSPort|:443}

Of course, more info can be returned (FTP proxies, etc.) – see the API documentation in Xcode.

Jon