Nucleus
Performance & native

Native proxyNew

Read the operating system's proxy configuration, PAC scripts included, and install it as the JVM-wide ProxySelector.

View as Markdown

native-proxy reads the proxy configuration of the operating system on Windows, macOS and Linux and exposes it as a ProxySelector. It follows Chromium's proxy stack, so a URL resolves to the proxy Edge, Chrome or Safari would use, machine-wide and managed settings included.

native-proxymacOSWindowsLinuxGradle plugin: Not neededWindow runtime: Not needed
OSConfigurationPAC and WPAD
WindowsWinHTTP and the Internet Settings registryEvaluated through WinHTTP
macOSSCDynamicStorePAC evaluated through CFNetwork
LinuxGSettings (org.gnome.system.proxy), then KDE kioslaverc, then http_proxy / no_proxyNot evaluated

Add the dependency

build.gradle.kts
dependencies {
    implementation("dev.nucleusframework:nucleus.native-proxy:3.0.0")
}

Route the JVM through the OS proxy

Call install() once from main():

import dev.nucleusframework.nativeproxy.NativeProxy

NativeProxy.install()

Every JDK connection that consults ProxySelector.getDefault(), such as HttpURLConnection, java.net.http.HttpClient or OkHttp's default, then uses the OS proxy. The selector that was default before becomes the fallback, so http.proxyHost and the other JDK proxy properties still apply to URIs the OS has no opinion on. install() also watches the configuration, so the selector follows a change in the OS settings.

install() returns false and leaves the JVM default alone when the platform has no backend. uninstall() restores the previous selector.

Resolve a URL

val proxies: List<ProxyServer> = NativeProxy.proxiesFor(URI("https://intranet.corp"))
val javaProxies: List<Proxy> = NativeProxy.javaProxiesFor(URI("https://intranet.corp"))

proxiesFor returns the proxies to try, in order; an empty list means a direct connection. javaProxiesFor returns JDK proxies and is never empty: a direct connection is Proxy.NO_PROXY. For a client you configure yourself, NativeProxy.selector answers from the OS configuration alone, with no fallback.

A URL resolves in Chromium's order:

  1. The bypass list: a match goes direct.
  2. The PAC script, from an explicit URL or WPAD. Results are cached per origin and dropped on every configuration change.
  3. The static rules, also used when the PAC script cannot be evaluated.

Watch for changes

NativeProxy.addChangeListener { settings ->
    println("Proxy changed: ${settings.rules}")
}

The listener runs on a background thread. Windows and macOS are notified by the OS, with a 2 s coalescing delay; Linux polls the desktop configuration. removeChangeListener stops the watcher once no listener is left and the selector is not installed.

Read the settings

settings() returns the cached SystemProxySettings; refresh() reads it again.

PropertyMeaning
autoDetectWPAD is on ("Automatically detect settings")
pacUrlThe explicit PAC script URL, or null
rulesProxyRules: one list for every scheme, or per scheme (http=…;https=…;socks=…)
bypassRulesProxyBypassRules: hosts reached without a proxy
isDirectNo WPAD, no PAC and no rules

ProxyRules.parse and ProxyBypassRules.parse accept the Chromium syntax, for a configuration that does not come from the OS. Bypass rules take glob host patterns, a leading . for a whole domain, :port and scheme:// restrictions, CIDR blocks, <local> for simple host names and <-loopback>. Localhost and link-local addresses bypass the proxy unless <-loopback> says otherwise.

Linux evaluates neither PAC scripts nor WPAD, which would need a JavaScript engine: only static rules and bypass lists apply. On macOS, WPAD without an explicit PAC URL is reported in autoDetect but not evaluated; when DHCP discovery succeeds, macOS writes the PAC URL into the settings itself.

Logging

The module logs at FINE under the dev.nucleusframework.nativeproxy logger. Set that logger to FINE, or set allowNucleusRuntimeLogging = true (dev.nucleusframework.core.runtime.tools) from main().

API reference

SymbolNotes
NativeProxy.isSupportedWhether the platform has a native backend
install(): Boolean / uninstall()Install or remove the JVM-wide selector
selector: ProxySelectorAnswers from the OS alone, with no fallback
proxiesFor(uri): List<ProxyServer>Empty for a direct connection
javaProxiesFor(uri): List<Proxy>Proxy.NO_PROXY for a direct connection
settings() / refresh()SystemProxySettings, cached / read again
addChangeListener / removeChangeListener(SystemProxySettings) -> Unit, on a background thread
ProxyServerprotocol (HTTP, HTTPS, SOCKS4, SOCKS5), host, port, toJavaProxy()

What's next

  • Native SSL: trust the OS certificates on the same connections.
  • Native HTTP: HTTP clients wired to native trust.
  • Modules: the full list of Nucleus modules.
Was this page helpful?
Edit on GitHub

Last updated on