本文整理汇总了Golang中net/http.ProxyFromEnvironment函数的典型用法代码示例。如果您正苦于以下问题:Golang ProxyFromEnvironment函数的具体用法?Golang ProxyFromEnvironment怎么用?Golang ProxyFromEnvironment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ProxyFromEnvironment函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: proxySettings
// proxySettings will default to the default proxy settings if none are provided
// if settings are provided – they will override the environment variables
func (ro RequestOptions) proxySettings(req *http.Request) (*url.URL, error) {
// No proxies – lets use the default
if len(ro.Proxies) == 0 {
return http.ProxyFromEnvironment(req)
}
// There was a proxy specified – do we support the protocol?
if _, ok := ro.Proxies[req.URL.Scheme]; ok {
return ro.Proxies[req.URL.Scheme], nil
}
// Proxies were specified but not for any protocol that we use
return http.ProxyFromEnvironment(req)
}
示例2: NewClient
// Creates a new Twitter client with the supplied OAuth configuration.
// Supports the use of HTTP proxies through the $HTTP_PROXY env var.
// For example:
// export HTTP_PROXY=http://localhost:8888
func NewClient(config *oauth1a.ClientConfig, user *oauth1a.UserConfig) *Client {
var (
host = "api.twitter.com"
base = "https://" + host
req, _ = http.NewRequest("GET", "https://api.twitter.com", nil)
proxy, _ = http.ProxyFromEnvironment(req)
transport *http.Transport
)
if proxy != nil {
transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
}
} else {
transport = &http.Transport{}
}
return &Client{
Host: host,
HttpClient: &http.Client{
Transport: transport,
},
User: user,
AppToken: nil,
OAuth: &oauth1a.Service{
RequestURL: base + "/oauth/request_token",
AuthorizeURL: base + "/oauth/authorize",
AccessURL: base + "/oauth/access_token",
ClientConfig: config,
Signer: new(oauth1a.HmacSha1Signer),
},
}
}
示例3: NewClient
// NewClient creates a new Client object.
// The token can be empty if you plan on creating
// the token using the `CreateToken` func. If you want
// to use your existing token, you need to pass it as the paramter.
// It returns a Client object that is pre-configured for usage.
func NewClient(token string) *Client {
// Perhaps we should allow people to configure this through some
// variable. It would make the package almost 100% backwards compatible
// with the public api (https://developer.kwikdesk.com)
var (
host = "platform.kwikdesk.com"
base = "https://" + host
req, _ = http.NewRequest("GET", base, nil)
proxy, _ = http.ProxyFromEnvironment(req)
transport *http.Transport
)
transport = &http.Transport{}
if proxy != nil {
transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
}
}
return &Client{
Host: host,
FullHost: base,
HttpClient: &http.Client{
Transport: transport,
},
ContentType: kdHTTPContentType,
XToken: token,
}
}
示例4: NewClient
// Creates a new Infoblox client with the supplied user/pass configuration.
// Supports the use of HTTP proxies through the $HTTP_PROXY env var.
// For example:
// export HTTP_PROXY=http://localhost:8888
//
// When using a proxy, disable TLS certificate verification with the following:
// sslVerify = false
func NewClient(host, username, password string, sslVerify bool) *Client {
var (
req, _ = http.NewRequest("GET", host, nil)
proxy, _ = http.ProxyFromEnvironment(req)
transport *http.Transport
tlsconfig *tls.Config
)
tlsconfig = &tls.Config{
InsecureSkipVerify: !sslVerify,
}
if tlsconfig.InsecureSkipVerify {
fmt.Println("WARNING: SSL cert verification disabled!")
}
transport = &http.Transport{
TLSClientConfig: tlsconfig,
}
if proxy != nil {
transport.Proxy = http.ProxyURL(proxy)
}
return &Client{
Host: host,
HttpClient: &http.Client{
Transport: transport,
},
Username: username,
Password: password,
}
}
示例5: getProxy
func getProxy() *url.URL {
req, err := http.NewRequest("GET", "https://api.heroku.com", nil)
PrintError(err, false)
proxy, err := http.ProxyFromEnvironment(req)
PrintError(err, false)
return proxy
}
示例6: NewMultipleHostReverseProxy
// NewMultipleHostReverseProxy creates a reverse proxy that will randomly
// select a host from the passed `targets`
func NewMultipleHostReverseProxy(targets []*url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
println("CALLING DIRECTOR")
target := targets[rand.Int()%len(targets)]
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
}
return &httputil.ReverseProxy{
Director: director,
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
println("CALLING PROXY")
return http.ProxyFromEnvironment(req)
},
Dial: func(network, addr string) (net.Conn, error) {
println("CALLING DIAL")
conn, err := (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial(network, addr)
if err != nil {
println("Error during DIAL:", err.Error())
}
return conn, err
},
TLSHandshakeTimeout: 10 * time.Second,
},
}
}
示例7: SetCertificate
func (c *Client) SetCertificate(cert tls.Certificate) {
t := c.Client.Transport.(*http.Transport)
// Extension certificate
t.TLSClientConfig.Certificates = []tls.Certificate{cert}
// Proxy to vCenter host on port 80
host, _ := splitHostPort(c.u.Host)
// Should be no reason to change the default port other than testing
port := os.Getenv("GOVC_TUNNEL_PROXY_PORT")
if port != "" {
host += ":" + port
}
c.p = &url.URL{
Scheme: "http",
Host: host,
}
t.Proxy = func(r *http.Request) (*url.URL, error) {
// Only sdk requests should be proxied
if r.URL.Path == "/sdk" {
return c.p, nil
}
return http.ProxyFromEnvironment(r)
}
// Rewrite url Host to use the sdk tunnel, required for a certificate request.
c.u.Host = sdkTunnel
}
示例8: dial
// dial dials the host specified by req, using TLS if appropriate, optionally
// using a proxy server if one is configured via environment variables.
func (s *SpdyRoundTripper) dial(req *http.Request) (net.Conn, error) {
proxyURL, err := http.ProxyFromEnvironment(req)
if err != nil {
return nil, err
}
if proxyURL == nil {
return s.dialWithoutProxy(req.URL)
}
// ensure we use a canonical host with proxyReq
targetHost := netutil.CanonicalAddr(req.URL)
// proxying logic adapted from http://blog.h6t.eu/post/74098062923/golang-websocket-with-http-proxy-support
proxyReq := http.Request{
Method: "CONNECT",
URL: &url.URL{},
Host: targetHost,
}
proxyDialConn, err := s.dialWithoutProxy(proxyURL)
if err != nil {
return nil, err
}
proxyClientConn := httputil.NewProxyClientConn(proxyDialConn, nil)
_, err = proxyClientConn.Do(&proxyReq)
if err != nil && err != httputil.ErrPersistEOF {
return nil, err
}
rwc, _ := proxyClientConn.Hijack()
if req.URL.Scheme != "https" {
return rwc, nil
}
host, _, err := net.SplitHostPort(req.URL.Host)
if err != nil {
return nil, err
}
if len(s.tlsConfig.ServerName) == 0 {
s.tlsConfig.ServerName = host
}
tlsConn := tls.Client(rwc, s.tlsConfig)
// need to manually call Handshake() so we can call VerifyHostname() below
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
if err := tlsConn.VerifyHostname(host); err != nil {
return nil, err
}
return tlsConn, nil
}
示例9: getProxy
func getProxy(req *http.Request) (*url.URL, error) {
if config.ProxyServer != "" {
u, err := url.Parse(config.ProxyServer)
if err == nil {
return u, nil
}
}
return http.ProxyFromEnvironment(req)
}
示例10: query
func (s *SQS) query(queueUrl string, params map[string]string, resp interface{}) (err error) {
var url_ *url.URL
if queueUrl != "" && len(queueUrl) > len(s.Region.SQSEndpoint) {
url_, err = url.Parse(queueUrl)
} else {
url_, err = url.Parse(s.Region.SQSEndpoint)
}
if err != nil {
return err
}
params["Version"] = "2012-11-05"
hreq, err := http.NewRequest("POST", url_.String(), strings.NewReader(multimap(params).Encode()))
if err != nil {
return err
}
// respect the environmnet's proxy settings
if prox_url, _ := http.ProxyFromEnvironment(hreq); prox_url != nil {
hreq.URL = prox_url
}
hreq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
hreq.Header.Set("X-Amz-Date", time.Now().UTC().Format(aws.ISO8601BasicFormat))
if s.Auth.Token() != "" {
hreq.Header.Set("X-Amz-Security-Token", s.Auth.Token())
}
signer := aws.NewV4Signer(s.Auth, "sqs", s.Region)
signer.Sign(hreq)
r, err := http.DefaultClient.Do(hreq)
if err != nil {
return err
}
defer r.Body.Close()
if debug {
dump, _ := httputil.DumpResponse(r, true)
log.Printf("DUMP:\n", string(dump))
}
if r.StatusCode != 200 {
return buildError(r)
}
err = xml.NewDecoder(r.Body).Decode(resp)
io.Copy(ioutil.Discard, r.Body)
return err
}
示例11: getMetadataForgeModule
// getMetadataForgeModule queries the configured Puppet Forge and return
func getMetadataForgeModule(fm ForgeModule) ForgeModule {
baseUrl := config.Forge.Baseurl
if len(fm.baseUrl) > 0 {
baseUrl = fm.baseUrl
}
url := baseUrl + "/v3/releases/" + fm.author + "-" + fm.name + "-" + fm.version
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "https://github.com/xorpaul/g10k/")
req.Header.Set("Connection", "close")
proxyURL, err := http.ProxyFromEnvironment(req)
if err != nil {
Fatalf("getMetadataForgeModule(): Error while getting http proxy with golang http.ProxyFromEnvironment()" + err.Error())
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
before := time.Now()
Debugf("GETing " + url)
resp, err := client.Do(req)
duration := time.Since(before).Seconds()
Verbosef("GETing Forge metadata from " + url + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
mutex.Lock()
syncForgeTime += duration
mutex.Unlock()
if err != nil {
Fatalf("getMetadataForgeModule(): Error while querying metadata for Forge module " + fm.name + " from " + url + ": " + err.Error())
}
defer resp.Body.Close()
if resp.Status == "200 OK" {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Fatalf("getMetadataForgeModule(): Error while reading response body for Forge module " + fm.name + " from " + url + ": " + err.Error())
}
before := time.Now()
currentRelease := gjson.Parse(string(body)).Map()
duration := time.Since(before).Seconds()
modulemd5sum := currentRelease["file_md5"].String()
moduleFilesize := currentRelease["file_size"].Int()
Debugf("module: " + fm.author + "/" + fm.name + " modulemd5sum: " + modulemd5sum + " moduleFilesize: " + strconv.FormatInt(moduleFilesize, 10))
mutex.Lock()
forgeJsonParseTime += duration
mutex.Unlock()
return ForgeModule{md5sum: modulemd5sum, fileSize: moduleFilesize}
} else {
Fatalf("getMetadataForgeModule(): Unexpected response code while GETing " + url + " " + resp.Status)
}
return ForgeModule{}
}
示例12: NewMultipleHostReverseProxy
func NewMultipleHostReverseProxy(reg Registry) *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = req.Host
},
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return http.ProxyFromEnvironment(req)
},
Dial: func(network, addr string) (net.Conn, error) {
return loadBalance(network, addr, reg)
},
TLSHandshakeTimeout: 10 * time.Second,
},
}
}
示例13: new_proxy
func (this *curler) new_proxy() proxy_func {
if this.proxy_policy == CurlProxyPolicyAlwayseProxy {
return http.ProxyFromEnvironment
}
if this.proxy_policy == CurlProxyPolicyNoProxy {
return nil
}
if this.ruler != nil {
return func(req *http.Request) (*url.URL, error) {
uri := req.URL.String()
blocked := this.ruler.IsBlocked(uri)
if blocked {
return http.ProxyFromEnvironment(req)
}
return nil, nil
}
}
return nil
}
示例14: newPeterHostReverseProxy
// NewMultipleHostReverseProxy creates a reverse proxy that will randomly
// select a host from the passed `targets`
func newPeterHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
fmt.Println("")
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
fmt.Printf("DIRECTOR====> Scheme='%s' Host='%s' Path='%s'\n",
req.URL.Scheme, req.URL.Host, req.URL.Path)
os.Stdout.Sync()
}
return &httputil.ReverseProxy{
Director: director,
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
fmt.Printf("PROXY ====>req=%#v\n", req)
length := req.ContentLength
fmt.Printf("length=%d\n", length)
bodyReader := req.Body
body := readBody(bodyReader, int(length))
fmt.Printf("body=%v\n", body)
os.Stdout.Sync()
return http.ProxyFromEnvironment(req)
},
Dial: func(network, addr string) (net.Conn, error) {
fmt.Printf("DIAL ====>addr='%s'\n", addr)
conn, err := (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial(network, addr)
if err != nil {
println("Error during DIAL:", err.Error())
}
os.Stdout.Sync()
return conn, err
},
TLSHandshakeTimeout: 10 * time.Second,
},
}
}
示例15: NewClient
// Creates a new Twitter client with the supplied OAuth configuration.
// Supports the use of HTTP proxies through the $HTTP_PROXY env var.
// For example:
// export HTTP_PROXY=http://localhost:8888
//
// When using a proxy, disable TLS certificate verification with the following:
// export TLS_INSECURE=1
func NewClient(config *oauth1a.ClientConfig, user *oauth1a.UserConfig) *Client {
var (
host = "api.twitter.com"
base = "https://" + host
req, _ = http.NewRequest("GET", "https://api.twitter.com", nil)
proxy, _ = http.ProxyFromEnvironment(req)
transport *http.Transport
tlsconfig *tls.Config
)
if proxy != nil {
tlsconfig = &tls.Config{
InsecureSkipVerify: getEnvEitherCase("TLS_INSECURE") != "",
}
if tlsconfig.InsecureSkipVerify {
log.Printf("WARNING: SSL cert verification disabled\n")
}
transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSClientConfig: tlsconfig,
}
} else {
transport = &http.Transport{}
}
return &Client{
Host: host,
HttpClient: &http.Client{
Transport: transport,
},
User: user,
AppToken: nil,
OAuth: &oauth1a.Service{
RequestURL: base + "/oauth/request_token",
AuthorizeURL: base + "/oauth/authorize",
AccessURL: base + "/oauth/access_token",
ClientConfig: config,
Signer: new(oauth1a.HmacSha1Signer),
},
}
}