private String getPrimaryNetworkServiceName() { // TODO This would be faster (but harder to test?) if we just launched scutil once // and communicated with it line-by-line using stdin/stdout String output = runScutil("show State:/Network/Global/IPv4"); log.trace(output); Map<String, String> dictionary = Maps.parseDictionary(output.toString(), SCUTIL_LINE, false); String primaryInterface = verifyKey("PrimaryInterface", dictionary, "scutil", output); output = runNetworkSetup("-listnetworkserviceorder"); log.trace(output); dictionary = Maps.parseDictionary(output.toString(), NETWORKSETUP_LISTORDER_LINE, true); String userDefinedName = verifyKey(primaryInterface, dictionary, "networksetup -listnetworksetuporder", output); networkService = userDefinedName; return userDefinedName; }
/** Acquire current network settings using scutil/networksetup */ private MacNetworkSettings getCurrentNetworkSettings() { getPrimaryNetworkServiceName(); String output = runNetworkSetup("-getwebproxy", networkService); log.fine(output); Map<String, String> dictionary = Maps.parseDictionary(output, NETWORKSETUP_LINE, false); String strEnabled = verifyKey("Enabled", dictionary, "networksetup", output); boolean enabled = isTrueOrSomething(strEnabled); String server = verifyKey("Server", dictionary, "networksetup", output); String strPort = verifyKey("Port", dictionary, "networksetup", output); int port1; try { port1 = Integer.parseInt(strPort); } catch (NumberFormatException e) { throw new MacNetworkSetupException("Port didn't look right: " + output, e); } String strAuth = verifyKey("Authenticated Proxy Enabled", dictionary, "networksetup", output); boolean auth = isTrueOrSomething(strAuth); String[] bypassDomains = getCurrentProxyBypassDomains(); MacNetworkSettings networkSettings = new MacNetworkSettings(networkService, enabled, server, port1, auth, bypassDomains); return networkSettings; }