File: http-1_7.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,748 kB
  • sloc: makefile: 20
file content (35 lines) | stat: -rw-r--r-- 904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

//+build go1.7

package utils

import (
	"context"
	"fmt"
	"net"
	"net/http"
	"time"
)

var ctxtDialer = &net.Dialer{
	Timeout:   30 * time.Second,
	KeepAlive: 30 * time.Second,
}

// installHTTPDialShim patches the default HTTP transport so
// that it fails when an attempt is made to dial a non-local
// host.
//
// Note that this is Go version dependent because in Go 1.7 and above,
// the DialContext field was introduced (and set in http.DefaultTransport)
// which overrides the Dial field.
func installHTTPDialShim(t *http.Transport) {
	t.DialContext = func(ctxt context.Context, network, addr string) (net.Conn, error) {
		if !OutgoingAccessAllowed && !isLocalAddr(addr) {
			return nil, fmt.Errorf("access to address %q not allowed", addr)
		}
		return ctxtDialer.DialContext(ctxt, network, addr)
	}
}