Giter Site home page Giter Site logo

go-ieproxy's Introduction

ieproxy

Go package to detect the proxy settings on Windows platform, and MacOS.

On Windows, 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.

On MacOS, the settings are read from CFNetworkCopySystemProxySettings method of CFNetwork.

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.
}

go-ieproxy's People

Contributors

adreed-msft avatar caarlos0 avatar denandz avatar drakkan avatar gapra-msft avatar johnrusk avatar kedarb avatar laurazard avatar mattn avatar maxcleme avatar mmulthaupt avatar oliverpool avatar sethvargo avatar yylyyl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-ieproxy's Issues

build fails on darwin

Describe the bug
Building things that use this library seems to yield an error on darwin, both amd64 and arm64.

Example output:

❯ GOOS=darwin GOARCH=arm64 go build .

github.com/mattn/go-ieproxy

../../Go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:36:9: undefined: getConf
../../Go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:41:9: undefined: reloadConf
../../Go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:48:2: undefined: overrideEnvWithStaticProxy
../../Go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:53:13: psc.findProxyForURL undefined (type *Proxy

New Release

Hi,

This is more of a question: would it be possible to release a new tag that includes #26 ?

Thanks!

Windows Automatic Pac Lookup fails on Windows 7 due to invalid flags

In pac_windows.go:70 we find the following line:

dwFlags:                fWINHTTP_AUTOPROXY_CONFIG_URL + fWINHTTP_AUTOPROXY_NO_CACHE_CLIENT + fWINHTTP_AUTOPROXY_NO_CACHE_SVC,

The following two flags are causing windows to return a ERROR_INVALID_PARAMETER when I execute the proxy lookup:

  • fWINHTTP_AUTOPROXY_NO_CACHE_CLIENT
  • fWINHTTP_AUTOPROXY_NO_CACHE_SVC

Removing these flags allows me to resolve the proxy successfully. I am running this on Windows 7.

Helper functions

Hello,

Nice work :)
How do you plug FindProxyForURL in a workflow?
I'm wondering if you use something like this:

func Handler(req *http.Request) (*url.URL, error) {
	conf := GetConf()
	if conf.Static.Active {
		return http.ProxyFromEnvironment(req)
	}
	if conf.Automatic.Active {
		proxy := conf.Automatic.FindProxyForURL(req.URL.String())
		if proxy != "" {
			return url.Parse(proxy)
		}
	}
	return nil, nil
}

// Client is a proxy enabled http.Client
var Client = &http.Client{
	Transport: &http.Transport{
		Proxy: Handler,
	},
}

// Get issues a GET to the specified URL using the proxy.
func Get(url string) (*http.Response, error) {
	return Client.Get(url)
}

If yes, would it be something interesting to add to the library?

Thank you

Cross-compilation error to Darwin on Ubuntu

I am new to go lang. I tried to build v0.0.3 for Darwin on Ubuntu but got the following errors. Cross-compilation for Windows is OK. Not sure what causes the problem.

> GOOS=darwin GOARCH=amd64 go build .
# github.com/mattn/go-ieproxy
./ieproxy.go:36:9: undefined: getConf
./ieproxy.go:41:9: undefined: reloadConf
./ieproxy.go:48:2: undefined: overrideEnvWithStaticProxy
./ieproxy.go:53:12: psc.findProxyForURL undefined (type *ProxyScriptConf has no field or method findProxyForURL, but does have FindProxyForURL)

My environment is

> go version
go version go1.17.6 linux/amd64
> uname -rv
5.4.0-1063-gcp #67~18.04.1-Ubuntu SMP Tue Jan 18 11:59:15 UTC 2022

Can not upgrade from v0.0.1 too v0.0.2 or v.0.03

$ go get github.com/mattn/[email protected]
go: downloading github.com/mattn/go-ieproxy v0.0.2
# github.com/mattn/go-ieproxy
../../../../../.gvm/pkgsets/go1.16.13/global/pkg/mod/github.com/mattn/[email protected]/ieproxy_darwin.go:37:2: could not determine kind of name for C.CFNumberGetValue
../../../../../.gvm/pkgsets/go1.16.13/global/pkg/mod/github.com/mattn/[email protected]/ieproxy_darwin.go:35:29: could not determine kind of name for C.CFNumberRef
../../../../../.gvm/pkgsets/go1.16.13/global/pkg/mod/github.com/mattn/[email protected]/ieproxy_darwin.go:28:26: could not determine kind of name for C.calloc
../../../../../.gvm/pkgsets/go1.16.13/global/pkg/mod/github.com/mattn/[email protected]/ieproxy_darwin.go:29:8: could not determine kind of name for C.free
../../../../../.gvm/pkgsets/go1.16.13/global/pkg/mod/github.com/mattn/[email protected]/ieproxy_darwin.go:37:28: could not determine kind of name for C.kCFNumberIntType

Possible non-return of proxy lookup functions on Windows

Hi Mattn, we recently worked around an issue that we found. Sometimes, about 1 time in a million, looking up a proxy would lock up and never return, on Windows 10.

We put a workaround into AzCopy here: https://github.com/Azure/azure-storage-azcopy/blob/863222d2e2a2bc038f5c35c4faf825ee145c210e/common/ProxyLookupCache.go#L89 . In that code c.lookupMethod == ieproxy.GetProxyFunc(). The workaround calls go-ieproxy in a separate goroutine, so that it can impose a strict timeout with a select statement.

Currently, that workaround is combined with some caching code. It doesn't seem like the caching code would make sense in go-ieproxy. But maybe the workaround would be of interest to you, on its own. (Although, if used without caching, it would have a little more overhead because it uses the separate goroutine for every call).

I'm not sure of the best way to proceed - e.g. does it make sense to put any of this code into go-ieproxy? So at this stage I'm logging this issue rather than creating a PR.

What do you think?

cc @adreed-msft , @zezha-msft

Wrong order of priority of Automatic over Static config on Windows.

In proxyMiddleman() in proxyMiddleman_windows.go, the automatic config is used if it is enabled, even if static config is enabled as well. However, all common Windows software appears to prioritize static config over automatic config when it is enabled still considers static config if automatic config does not deliver any proxy, most notably the major browsers including Internet Explorer itself.

Panic when fetching proxy config on macOS 12.3.1

On an M1 MacBook Pro, macOS 12.3.1:

Whether using ieproxy.GetConf() or the provided ieproxy.GetProxyFunc(), I can reliably get it to panic. Disabling wifi and re-enabling it fixes it for a moment, but enabling/disabling proxies in the network settings panel and rerunning the code gets it to a state where it consistently panics on the same line.

Stacktrace:

goroutine 4 [syscall]:
runtime.cgocall(0x1049958c8, 0x14000052c28)
        /opt/homebrew/Cellar/go/1.17.6/libexec/src/runtime/cgocall.go:156 +0x50 fp=0x14000052bf0 sp=0x14000052bb0 pc=0x10472eea0
github.com/mattn/go-ieproxy._Cfunc_CFNumberGetValue(0x0, 0x9, 0x14000018d88)
        _cgo_gotypes.go:191 +0x44 fp=0x14000052c20 sp=0x14000052bf0 pc=0x104984bb4
github.com/mattn/go-ieproxy.cfNumberGetGoInt.func1(0x0, 0x14000018d88)
        [...removed]/github.com/mattn/go-ieproxy/ieproxy_darwin.go:43 +0x78 fp=0x14000052c50 sp=0x14000052c20 pc=0x104985248
github.com/mattn/go-ieproxy.cfNumberGetGoInt(0x0)
        [...removed]/github.com/mattn/go-ieproxy/ieproxy_darwin.go:43 +0x40 fp=0x14000052c80 sp=0x14000052c50 pc=0x1049851a0
github.com/mattn/go-ieproxy.writeConf()
        [...removed]/github.com/mattn/go-ieproxy/ieproxy_darwin.go:76 +0x27c fp=0x14000052d80 sp=0x14000052c80 pc=0x10498569c
sync.(*Once).doSlow(0x104c92f30, 0x104a7b5d8)
        /opt/homebrew/Cellar/go/1.17.6/libexec/src/sync/once.go:68 +0x10c fp=0x14000052dd0 sp=0x14000052d80 pc=0x10479de9c
sync.(*Once).Do(...)
        /opt/homebrew/Cellar/go/1.17.6/libexec/src/sync/once.go:59
github.com/mattn/go-ieproxy.getConf(...)
        [...removed]/github.com/mattn/go-ieproxy/ieproxy_darwin.go:23
github.com/mattn/go-ieproxy.GetConf(...)
        [...removed]/github.com/mattn/go-ieproxy/ieproxy.go:36

findProxyForURL with cgo on windows

Found a similar problem as #43 on windows

GOOS=windows go build
# github.com/mattn/go-ieproxy
../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_windows.go:22:6: getConf redeclared in this block
	../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_unix.go:6:6: other declaration of getConf
../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_windows.go:28:6: reloadConf redeclared in this block
	../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_unix.go:10:6: other declaration of reloadConf
../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_windows.go:133:6: overrideEnvWithStaticProxy redeclared in this block
	../../../../../pkg/mod/github.com/mattn/[email protected]/ieproxy_unix.go:14:6: other declaration of overrideEnvWithStaticProxy
../../../../../pkg/mod/github.com/mattn/[email protected]/pac_windows.go:9:29: ProxyScriptConf.findProxyForURL redeclared in this block
	../../../../../pkg/mod/github.com/mattn/[email protected]/pac_unix.go:6:29: other declaration of findProxyForURL

Support proxy.pac

Hi,

I would also like this library to support proxy.pac files (at least for windows).

To do this, I thought of using the WinHttpGetProxyForUrl function of the Winhttp.dll library.

However I don't have any experience with dll calling and I can't get it to work...
Do you have any idea?

func main() {
	var winHttp = syscall.NewLazyDLL("Winhttp.dll")
	var open = winHttp.NewProc("WinHttpOpen")
	var close = winHttp.NewProc("WinHttpCloseHandle")
	var getProxyForUrl = winHttp.NewProc("WinHttpGetProxyForUrl")

	handle, _, err := open.Call(0, 0, 0, 0, 0)
	defer close.Call(handle)

	fmt.Println(err)

	ret, _, err := getProxyForUrl.Call(
		handle,
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("https://www.google.com"))),
		0,
		0,
	)

	fmt.Println(ret) // prints 0
	fmt.Println(err) // prints The parameter is incorrect.

}

Can't cross compile in v.0.03

Hello guys,

I work for MinIO company. And lately I've been working to understand this issue below coming from go-ieproxy:

# github.com/mattn/go-ieproxy
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:36:9: undefined: getConf
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:41:9: undefined: reloadConf
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:48:2: undefined: overrideEnvWithStaticProxy
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:53:12: psc.findProxyForURL undefined (type *ProxyScriptConf has no field or method findProxyForURL, but does have FindProxyForURL)

I decided to isolate this from our code to understand better, and this is what I did:

  1. Created a tmp directory under my home
  2. cloned this repository
  3. created these files below and performed cross compilation:

test.go

package testing

import (
	"fmt"
	"github.com/mattn/go-ieproxy"
)

func main() {
	ieproxy.GetProxyFunc()
    fmt.Println("hello world")
}

go.mod

module example.com/testing

go 1.17

require github.com/mattn/go-ieproxy v0.0.3

require (
	golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d // indirect
	golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe // indirect
	golang.org/x/text v0.3.7 // indirect
)

go.sum

github.com/mattn/go-ieproxy v0.0.3 h1:YkaHmK1CzE5C4O7A3hv3TCbfNDPSCf0RKZFX+VhBeYk=
github.com/mattn/go-ieproxy v0.0.3/go.mod h1:6ZpRmhBaYuBX1U2za+9rC9iCGLsSp2tftelZne7CPko=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d h1:62NvYBuaanGXR2ZOfwDFkhhl6X1DUgf8qg3GuQvxZsE=
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe h1:W8vbETX/n8S6EmY0Pu4Ix7VvpsJUESTwl0oCK8MJOgk=
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
cniackz:/Users/cniackz/tmp # 

Cross compilation

cniackz:/Users/cniackz/tmp # CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 GO111MODULE=on go build
cniackz:/Users/cniackz/tmp # CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 GO111MODULE=on go build
# github.com/mattn/go-ieproxy
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:36:9: undefined: getConf
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:41:9: undefined: reloadConf
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:48:2: undefined: overrideEnvWithStaticProxy
../go/pkg/mod/github.com/mattn/[email protected]/ieproxy.go:53:12: psc.findProxyForURL undefined (type *ProxyScriptConf has no field or method findProxyForURL, but does have FindProxyForURL)

Notice how above is failing the same way, the question I have is:
Why getConf is undefined?, If I can see it is defined for darwin under ieproxy_darwin.go

// GetConf retrieves the proxy configuration from the Windows Regedit
func getConf() ProxyConf {
	once.Do(writeConf)
	return darwinProxyConf
}

Can you help me to understand better what could be?.

Thank you in advance for your help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.