1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package proxy provides support for a variety of protocols to proxy network
7
package proxy // import "golang.org/x/net/proxy"
17
// A Dialer is a means to establish a connection.
18
// Custom dialers should also implement ContextDialer.
19
type Dialer interface {
20
// Dial connects to the given address via the proxy.
21
Dial(network, addr string) (c net.Conn, err error)
24
// Auth contains authentication parameters that specific Dialers may require.
29
// FromEnvironment returns the dialer specified by the proxy-related
30
// variables in the environment and makes underlying connections
32
func FromEnvironment() Dialer {
33
return FromEnvironmentUsing(Direct)
36
// FromEnvironmentUsing returns the dialer specify by the proxy-related
37
// variables in the environment and makes underlying connections
38
// using the provided forwarding Dialer (for instance, a *net.Dialer
39
// with desired configuration).
40
func FromEnvironmentUsing(forward Dialer) Dialer {
41
allProxy := allProxyEnv.Get()
42
if len(allProxy) == 0 {
46
proxyURL, err := url.Parse(allProxy)
50
proxy, err := FromURL(proxyURL, forward)
55
noProxy := noProxyEnv.Get()
56
if len(noProxy) == 0 {
60
perHost := NewPerHost(proxy, forward)
61
perHost.AddFromString(noProxy)
65
// proxySchemes is a map from URL schemes to a function that creates a Dialer
66
// from a URL with such a scheme.
67
var proxySchemes map[string]func(*url.URL, Dialer) (Dialer, error)
69
// RegisterDialerType takes a URL scheme and a function to generate Dialers from
70
// a URL with that scheme and a forwarding Dialer. Registered schemes are used
72
func RegisterDialerType(scheme string, f func(*url.URL, Dialer) (Dialer, error)) {
73
if proxySchemes == nil {
74
proxySchemes = make(map[string]func(*url.URL, Dialer) (Dialer, error))
76
proxySchemes[scheme] = f
79
// FromURL returns a Dialer given a URL specification and an underlying
80
// Dialer for it to make network requests.
81
func FromURL(u *url.URL, forward Dialer) (Dialer, error) {
85
auth.User = u.User.Username()
86
if p, ok := u.User.Password(); ok {
92
case "socks5", "socks5h":
98
return SOCKS5("tcp", net.JoinHostPort(addr, port), auth, forward)
101
// If the scheme doesn't match any of the built-in schemes, see if it
102
// was registered by another package.
103
if proxySchemes != nil {
104
if f, ok := proxySchemes[u.Scheme]; ok {
109
return nil, errors.New("proxy: unknown scheme: " + u.Scheme)
113
allProxyEnv = &envOnce{
114
names: []string{"ALL_PROXY", "all_proxy"},
116
noProxyEnv = &envOnce{
117
names: []string{"NO_PROXY", "no_proxy"},
121
// envOnce looks up an environment variable (optionally by multiple
122
// names) once. It mitigates expensive lookups on some platforms
124
// (Borrowed from net/http/transport.go)
131
func (e *envOnce) Get() string {
136
func (e *envOnce) init() {
137
for _, n := range e.names {
145
// reset is used by tests
146
func (e *envOnce) reset() {