From 45a295412ea3d0ea8abca1c6d1a8db4564afa7f6 Mon Sep 17 00:00:00 2001 From: Kyle Larose Date: Wed, 24 Aug 2022 14:02:52 -0400 Subject: [PATCH] pass http proxy on java command line Often if we need a proxy to access the resources required by the java app, the java app itself may need a proxy to access anything it interacts with (e.g. APIs). Sometimes the env vars such as HTTP_PROXY are insufficient to enable the proxy. Instead, it must be passed in on the java command line. Hook this up. At the same time, allow the launching application to force the proxies to be used, even if the system would otherwise indicate not to. This allows the launching application to use a different default proxy than the system's configuration would. --- launcher/jnlp/launcher.go | 50 +++++++++++++++++++++++++++++++++++++++ settings/settings.go | 5 ++++ 2 files changed, 55 insertions(+) diff --git a/launcher/jnlp/launcher.go b/launcher/jnlp/launcher.go index 42ea128..aeac80c 100644 --- a/launcher/jnlp/launcher.go +++ b/launcher/jnlp/launcher.go @@ -272,6 +272,7 @@ func (launcher *Launcher) command() (*exec.Cmd, error) { if splash := launcher.getSplashScreen(); splash != "" { javaArgs = append(javaArgs, fmt.Sprintf("-splash:%s", splash)) } + javaArgs = append(javaArgs, launcher.getProxyArgs()...) if jnlp.AppDescription != nil { javaArgs = append(javaArgs, jnlp.AppDescription.MainClass) for _, appArg := range jnlp.AppDescription.Arguments { @@ -290,6 +291,55 @@ func (launcher *Launcher) command() (*exec.Cmd, error) { return cmd, nil } +func (launcher *Launcher) getProxyArgs() []string { + args := []string{} + if !settings.UseHttpProxyEnvironmentVariable() { + return args + } + + args = append(args, launcher.getHttpProxyArgs()...) + args = append(args, launcher.getHttpsProxyArgs()...) + return args +} + +func (launcher *Launcher) getHttpProxyArgs() []string { + args := []string{} + httpProxy := os.Getenv("HTTP_PROXY") + uri, err := url.Parse(httpProxy) + if err != nil { + return args + } + + if uri.Hostname() != "" { + args = append(args, fmt.Sprintf("-Dhttp.proxyHost=%s", uri.Hostname())) + } + + if uri.Port() != "" { + args = append(args, fmt.Sprintf("-Dhttp.proxyPort=%s", uri.Port())) + } + + return args +} + +func (launcher *Launcher) getHttpsProxyArgs() []string { + args := []string{} + httpsProxy := os.Getenv("HTTPS_PROXY") + uri, err := url.Parse(httpsProxy) + if err != nil { + return args + } + + if uri.Hostname() != "" { + args = append(args, fmt.Sprintf("-Dhttps.proxyHost=%s", uri.Hostname())) + } + + if uri.Port() != "" { + args = append(args, fmt.Sprintf("-Dhttps.proxyPort=%s", uri.Port())) + } + + return args +} + func (launcher *Launcher) exec() error { cmd, err := launcher.command() if err != nil { diff --git a/settings/settings.go b/settings/settings.go index 12fd6bd..7419abc 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -244,6 +244,11 @@ func UseHttpProxyEnvironmentVariable() bool { return useHttpProxyEnvironmentVariable } +// SetUseHttpProxyEnvironmentVariable allows overriding the default determined by the OS +func SetUseHttpProxyEnvironmentVariable(use bool) { + useHttpProxyEnvironmentVariable = use +} + func init() { javaExecutable = getJavaExecutable() jarSignerExecutable = getJARSignerExecutable()