ieproxy

package module
Version: v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2020 License: MIT Imports: 5 Imported by: 12

README

ieproxy

Go package to detect the proxy settings on Windows platform.

The settings are initially attempted to be read from the WinHttpGetIEProxyConfigForCurrentUser DLL call, but falls back to the registry (CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings) in the event the DLL call fails.

For more information, take a look at the documentation

Methods

You can either obtain a net/http compatible proxy function using ieproxy.GetProxyFunc(), set environment variables using ieproxy.OverrideEnvWithStaticProxy() (though no automatic configuration is available this way), or obtain the proxy settings via ieproxy.GetConf().

Method Supported configuration options:
ieproxy.GetProxyFunc() Static, Specified script, and fully automatic
ieproxy.OverrideEnvWithStaticProxy() Static
ieproxy.GetConf() Depends on how you use it

Examples

Using GetProxyFunc():
func init() {
	http.DefaultTransport.(*http.Transport).Proxy = ieproxy.GetProxyFunc()
}

GetProxyFunc acts as a middleman between net/http and mattn/go-ieproxy in order to select the correct proxy configuration based off the details supplied in the config.

Using OverrideEnvWithStaticProxy():
func init() {
	ieproxy.OverrideEnvWithStaticProxy()
	http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment
}

OverrideEnvWithStaticProxy overrides the relevant environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY) with the static, manually configured proxy details typically found in the registry.

Using GetConf():
func main() {
	conf := ieproxy.GetConf()
	//Handle proxies how you want to.
}

Documentation

Overview

Package ieproxy is a utility to retrieve the proxy parameters (especially of Internet Explorer on windows)

On windows, it gathers the parameters from the registry (regedit), while it uses env variable on other platforms

Example
package main

import (
	"fmt"
	"net/http"
	"os"
)

func init() {
	OverrideEnvWithStaticProxy()
	http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment
}

func main() {
	fmt.Println("== Proxy configuration ==")
	for _, name := range []string{"http_proxy", "https_proxy", "no_proxy"} {
		fmt.Println(name + ": " + os.Getenv(name))
	}

	fmt.Println("== Proxy test ==")

	req, err := http.NewRequest("GET", "https://golang.org/", nil)
	if err != nil {
		panic(err)
	}
	url, err := http.DefaultTransport.(*http.Transport).Proxy(req)
	if err != nil {
		panic(err)
	}
	if url != nil {
		fmt.Println("PROXY " + url.String())
	} else {
		fmt.Println("DIRECT")
	}
	// Coming output: == Proxy configuration ==
	// http_proxy: ...
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetProxyFunc

func GetProxyFunc() func(*http.Request) (*url.URL, error)

GetProxyFunc is a forwarder for the OS-Exclusive proxyMiddleman_os.go files

func OverrideEnvWithStaticProxy

func OverrideEnvWithStaticProxy()

OverrideEnvWithStaticProxy writes new values to the `http_proxy`, `https_proxy` and `no_proxy` environment variables. The values are taken from the Windows Regedit (should be called in `init()` function - see example)

func StringFromUTF16Ptr

func StringFromUTF16Ptr(s *uint16) string

StringFromUTF16Ptr converts a *uint16 C string to a Go String

Types

type ProxyConf

type ProxyConf struct {
	Static    StaticProxyConf // static configuration
	Automatic ProxyScriptConf // script configuration
}

ProxyConf gathers the configuration for proxy

func GetConf

func GetConf() ProxyConf

GetConf retrieves the proxy configuration from the Windows Regedit

type ProxyScriptConf

type ProxyScriptConf struct {
	// Is the proxy active?
	Active bool
	// PreConfiguredURL of the .pac file.
	// If this is empty and Active is true, auto-configuration should be assumed.
	PreConfiguredURL string
}

ProxyScriptConf contains the configuration for automatic proxy

func (*ProxyScriptConf) FindProxyForURL

func (psc *ProxyScriptConf) FindProxyForURL(URL string) string

FindProxyForURL computes the proxy for a given URL according to the pac file

type StaticProxyConf

type StaticProxyConf struct {
	// Is the proxy active?
	Active bool
	// Proxy address for each scheme (http, https)
	// "" (empty string) is the fallback proxy
	Protocols map[string]string
	// Addresses not to be browsed via the proxy (comma-separated, linux-like)
	NoProxy string
}

StaticProxyConf contains the configuration for static proxy

Directories

Path Synopsis
Package autoload automatically calls OverrideEnvWithStaticProxy, which writes new values to the `http_proxy`, `https_proxy` and `no_proxy` environment variables.
Package autoload automatically calls OverrideEnvWithStaticProxy, which writes new values to the `http_proxy`, `https_proxy` and `no_proxy` environment variables.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL