From: James Bunton Date: Sat, 7 Feb 2009 13:26:18 +0000 (+1100) Subject: Rewrote proxyconf completely in Objective C X-Git-Url: https://code.delx.au/osx-proxyconf/commitdiff_plain/0d8f59d3f2a491918cb4221ed49b45f760cac97f Rewrote proxyconf completely in Objective C --- diff --git a/.hgignore b/.hgignore index 11c66dc..bcdbe3b 100644 --- a/.hgignore +++ b/.hgignore @@ -1 +1 @@ -sysconfig +proxyconf diff --git a/Makefile b/Makefile index 48bdc11..6df6746 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ CFLAGS := -Wall -W -std=c99 -arch i386 -arch ppc $(CFLAGS) LDFLAGS := $(LDFLAGS) FRAMEWORKS := -framework CoreFoundation -framework SystemConfiguration -framework Foundation -TARGETS := sysconfig +TARGETS := proxyconf all: $(TARGETS) %: %.m + export MACOSX_DEPLOYMENT_TARGET=10.4; \ $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) $< clean: diff --git a/README b/README index 65e96c0..26e3437 100644 --- a/README +++ b/README @@ -35,7 +35,7 @@ $ cd proxyconf $ make Now add the following line to your ~/.bashrc (modify as appropriate): -eval $(~/Downloads/proxyconf/proxyconf.sh) +eval $(~/Downloads/proxyconf/proxyconf) ----------- diff --git a/proxyconf.m b/proxyconf.m new file mode 100644 index 0000000..a161886 --- /dev/null +++ b/proxyconf.m @@ -0,0 +1,95 @@ +#include + +#import +#import + +void +DXPrint(NSString* format, ...) +{ + va_list args; + va_start(args, format); + NSString* string = [[NSString alloc] initWithFormat:format arguments:args]; + va_end(args); + fputs([string UTF8String], stdout); + [string release]; +} + + + + +NSDictionary* proxies; +BOOL proxyFound; + +void +printUnset(NSString* env) +{ + DXPrint(@"unset %@\n", env); +} + +void +printProxy(NSDictionary* proxies, NSString* proto) +{ + NSString* key = nil; + NSString* env = [[proto lowercaseString] stringByAppendingString:@"_proxy"]; + + key = [proto stringByAppendingString:@"Enable"]; + NSNumber* enabled = [proxies objectForKey:key]; + if(enabled == nil || [enabled intValue] != 1) { + printUnset(env); + return; + } + + key = [proto stringByAppendingString:@"Proxy"]; + id host = [proxies objectForKey:key]; + if(host == nil) { + printUnset(env); + return; + } + + key = [proto stringByAppendingString:@"Port"]; + id port = [proxies objectForKey:key]; + if(port == nil) { + printUnset(env); + return; + } + + NSString* uriPrefix = nil; + if(![proto isEqual:@"SOCKS"]) { + uriPrefix = @"http://"; + } else { + uriPrefix = @"socks://"; + } + + DXPrint(@"export %@='%@%@:%@'\n", env, uriPrefix, host, port); + proxyFound = YES; +} + +void +printExceptions(NSDictionary* proxies) +{ + id exceptions = [proxies objectForKey:@"ExceptionsList"]; + if(exceptions == nil || ![exceptions isKindOfClass:[NSArray class]] || !proxyFound) { + printUnset(@"no_proxy"); + return; + } + + DXPrint(@"export no_proxy='%@'\n", [exceptions componentsJoinedByString:@","]); +} + +int +main() +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + proxies = (NSDictionary*)SCDynamicStoreCopyProxies(nil); + proxyFound = NO; + printProxy(proxies, @"HTTP"); + printProxy(proxies, @"HTTPS"); + printProxy(proxies, @"FTP"); + printProxy(proxies, @"SOCKS"); + printExceptions(proxies); + + [pool release]; + return 0; +} + diff --git a/proxyconf.sh b/proxyconf.sh deleted file mode 100755 index a6b7d23..0000000 --- a/proxyconf.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -PROXY_FOUND=0 -function printEnvironment() { - local proxy_type="$1" - local environment_variable="$2" - local uri_prefix="${3:-http}" - local host port - - if [ "$(sysconfig -q "${proxy_type}Enable")" = "1" ]; then - host="$(sysconfig -q "${proxy_type}Proxy")" - port="$(sysconfig -q "${proxy_type}Port")" - echo "export ${environment_variable}='${uri_prefix}://${host}:${port}'" - - PROXY_FOUND=1 - else - echo "unset ${environment_variable}" - fi - -} - - -PATH="$(dirname "$0"):${PATH}" - -printEnvironment "HTTP" "http_proxy" -printEnvironment "HTTPS" "https_proxy" -printEnvironment "FTP" "ftp_proxy" -printEnvironment "SOCKS" "socks_proxy" "socks" - -no_proxy="$(sysconfig -q ExceptionsList)" -if [ -n "${no_proxy}" -a $PROXY_FOUND -eq 1 ]; then - echo "export no_proxy=\"${no_proxy}\"" -else - echo "unset no_proxy" -fi diff --git a/sysconfig.m b/sysconfig.m deleted file mode 100644 index c28ce59..0000000 --- a/sysconfig.m +++ /dev/null @@ -1,87 +0,0 @@ -#import -#import - -void -NSPrintF(NSString* file, NSString* format, ...) -{ - va_list args; - va_start(args, format); - NSString* string = [[NSString alloc] initWithFormat:format arguments:args]; - va_end(args); - [string writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil]; - [string release]; -} - -void -NSPrint(NSString* format, ...) -{ - va_list args; - va_start(args, format); - NSString* string = [[NSString alloc] initWithFormat:format arguments:args]; - va_end(args); - [string writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil]; - [string release]; -} - - -bool -printSystemConfiguration(bool quiet, NSString* keyName, NSDictionary* proxies) -{ - id value = [proxies objectForKey:keyName]; - if(nil != value) { - NSArray* items; - if([value isKindOfClass:[NSArray class]]) { - items = value; - } else { - items = [NSArray arrayWithObjects:value, nil]; - } - - NSPrint([items componentsJoinedByString:@","]); - NSPrint(@"\n"); - - } else { - if(!quiet) { - NSPrintF(@"/dev/stderr", @"Value does not exist\n"); - } - return FALSE; // Signal a fail condition - } - return TRUE; -} - -void -printUsage(void) -{ - NSPrintF(@"/dev/stderr", @"sysconfig [-q] SystemConfigurationKeyName\n"); - exit(1); -} - -int -main(int argc, const char* argv[]) -{ - NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - NSDictionary* proxies = (NSDictionary*)SCDynamicStoreCopyProxies(nil); - bool quiet = FALSE; - NSString* keyName; - - if(2 == argc) { - keyName = [NSString stringWithCString:argv[1]]; - } else if(3 == argc) { - if(![[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) { - printUsage(); - } else { - quiet = TRUE; - } - - keyName = [NSString stringWithCString:argv[2]]; - } else { - printUsage(); - } - - if(!printSystemConfiguration(quiet, keyName, proxies)) { - return 2; - } - - [pool release]; - return 0; -} -