Skip to content

Commit

Permalink
Updated readme and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Adele Reed committed Jun 6, 2019
1 parent f365857 commit e9933e0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion GetProxyFunc.go
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
)

//GetProxyFunc is a forwarder for the OS-Exclusive proxyMiddleman_os.go files
// GetProxyFunc is a forwarder for the OS-Exclusive proxyMiddleman_os.go files
func GetProxyFunc() func(*http.Request) (*url.URL, error) {
return proxyMiddleman()
}
44 changes: 38 additions & 6 deletions README.md
Expand Up @@ -2,16 +2,48 @@

Go package to detect the proxy settings on Windows platform.

The settings are read from the registry (`CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`).
The settings are initially attempted to be read from the [`WinHttpGetIEProxyConfigForCurrentUser` DLL call](https://docs.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpgetieproxyconfigforcurrentuser), but falls back to the registry (`CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings`) in the event the DLL call fails.

You can either get the settings via `GetConf` or set the environment variables via `OverrideEnvWithStaticProxy`.
For more information, take a look at the [documentation](https://godoc.org/github.com/mattn/go-ieproxy)

## 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():

If you want to use it system wide, you should use an [`init`](https://golang.org/doc/effective_go.html#init) function:
```go
func init() {
ieproxy.OverrideEnvWithStaticProxy()
http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment
http.DefaultTransport.(*http.Transport).Proxy = ieproxy.GetProxyFunc()
}
```

For more informations, take a look at the [documentation](https://godoc.org/github.com/mattn/go-ieproxy)
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():

```go
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():

```go
func main() {
conf := ieproxy.GetConf()
//Handle proxies how you want to.
}
```
3 changes: 2 additions & 1 deletion ieproxy.go
Expand Up @@ -26,7 +26,8 @@ type StaticProxyConf struct {
type ProxyScriptConf struct {
// Is the proxy active?
Active bool
// PreConfiguredURL of the .pac file
// PreConfiguredURL of the .pac file.
// If this is empty and Active is true, auto-configuration should be assumed.
PreConfiguredURL string
}

Expand Down
2 changes: 1 addition & 1 deletion proxyMiddleman_unix.go
Expand Up @@ -8,6 +8,6 @@ import (
)

func proxyMiddleman() func(req *http.Request) (i *url.URL, e error) {
//Fallthrough to ProxyFromEnvironment on all other OSes.
// Fallthrough to ProxyFromEnvironment on all other OSes.
return http.ProxyFromEnvironment
}
10 changes: 5 additions & 5 deletions proxyMiddleman_windows.go
Expand Up @@ -7,11 +7,11 @@ import (
)

func proxyMiddleman() func(req *http.Request) (i *url.URL, e error) {
//Get the proxy configuration
// Get the proxy configuration
conf := GetConf()

if conf.Script.Active {
//If automatic proxy obtaining is specified
// If automatic proxy obtaining is specified
return func(req *http.Request) (i *url.URL, e error) {
out := &url.URL{Host: conf.Script.FindProxyForURL(req.URL.String())}
if out.Host == "" {
Expand All @@ -20,7 +20,7 @@ func proxyMiddleman() func(req *http.Request) (i *url.URL, e error) {
return out, nil
}
} else if conf.Static.Active {
//If static proxy obtaining is specified
// If static proxy obtaining is specified
prox := httpproxy.Config{
HTTPSProxy: mapFallback("https", "", conf.Static.Protocols),
HTTPProxy: mapFallback("http", "", conf.Static.Protocols),
Expand All @@ -31,12 +31,12 @@ func proxyMiddleman() func(req *http.Request) (i *url.URL, e error) {
return prox.ProxyFunc()(req.URL)
}
} else {
//Final fallthrough case; use the environment variables.
// Final fallthrough case; use the environment variables.
return http.ProxyFromEnvironment
}
}

//Return oKey or fbKey if oKey doesn't exist in the map.
// Return oKey or fbKey if oKey doesn't exist in the map.
func mapFallback(oKey, fbKey string, m map[string]string) string {
if v, ok := m[oKey]; ok {
return v
Expand Down

0 comments on commit e9933e0

Please sign in to comment.