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
16 // A Dialer is a means to establish a connection.
17 type Dialer
interface {
18 // Dial connects to the given address via the proxy.
19 Dial(network
, addr
string) (c net
.Conn
, err error
)
22 // Auth contains authentication parameters that specific Dialers may require.
27 // DefaultDialer returns the dialer specified by the proxy related variables in
29 func FromEnvironment() Dialer
{
30 allProxy
:= os
.Getenv("all_proxy")
31 if len(allProxy
) == 0 {
35 proxyURL
, err
:= url
.Parse(allProxy
)
39 proxy
, err
:= FromURL(proxyURL
, Direct
)
44 noProxy
:= os
.Getenv("no_proxy")
45 if len(noProxy
) == 0 {
49 perHost
:= NewPerHost(proxy
, Direct
)
50 perHost
.AddFromString(noProxy
)
54 // proxySchemes is a map from URL schemes to a function that creates a Dialer
55 // from a URL with such a scheme.
56 var proxySchemes
map[string]func(*url
.URL
, Dialer
) (Dialer
, error
)
58 // RegisterDialerType takes a URL scheme and a function to generate Dialers from
59 // a URL with that scheme and a forwarding Dialer. Registered schemes are used
61 func RegisterDialerType(scheme
string, f
func(*url
.URL
, Dialer
) (Dialer
, error
)) {
62 if proxySchemes
== nil {
63 proxySchemes
= make(map[string]func(*url
.URL
, Dialer
) (Dialer
, error
))
65 proxySchemes
[scheme
] = f
68 // FromURL returns a Dialer given a URL specification and an underlying
69 // Dialer for it to make network requests.
70 func FromURL(u
*url
.URL
, forward Dialer
) (Dialer
, error
) {
74 auth
.User
= u
.User
.Username()
75 if p
, ok
:= u
.User
.Password(); ok
{
82 return SOCKS5("tcp", u
.Host
, auth
, forward
)
85 // If the scheme doesn't match any of the built-in schemes, see if it
86 // was registered by another package.
87 if proxySchemes
!= nil {
88 if f
, ok
:= proxySchemes
[u
.Scheme
]; ok
{
93 return nil, errors
.New("proxy: unknown scheme: " + u
.Scheme
)