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.
12
// A PerHost directs connections to a default Dialer unless the hostname
13
// requested matches one of a number of exceptions.
17
bypassNetworks []*net.IPNet
23
// NewPerHost returns a PerHost Dialer that directs connections to either
24
// defaultDialer or bypass, depending on whether the connection matches one of
25
// the configured rules.
26
func NewPerHost(defaultDialer, bypass Dialer) *PerHost {
33
// Dial connects to the address addr on the given network through either
34
// defaultDialer or bypass.
35
func (p *PerHost) Dial(network, addr string) (c net.Conn, err error) {
36
host, _, err := net.SplitHostPort(addr)
41
return p.dialerForRequest(host).Dial(network, addr)
44
func (p *PerHost) dialerForRequest(host string) Dialer {
45
if ip := net.ParseIP(host); ip != nil {
46
for _, net := range p.bypassNetworks {
51
for _, bypassIP := range p.bypassIPs {
52
if bypassIP.Equal(ip) {
59
for _, zone := range p.bypassZones {
60
if strings.HasSuffix(host, zone) {
64
// For a zone "example.com", we match "example.com"
69
for _, bypassHost := range p.bypassHosts {
70
if bypassHost == host {
77
// AddFromString parses a string that contains comma-separated values
78
// specifying hosts that should use the bypass proxy. Each value is either an
79
// IP address, a CIDR range, a zone (*.example.com) or a hostname
80
// (localhost). A best effort is made to parse the string and errors are
82
func (p *PerHost) AddFromString(s string) {
83
hosts := strings.Split(s, ",")
84
for _, host := range hosts {
85
host = strings.TrimSpace(host)
89
if strings.Contains(host, "/") {
90
// We assume that it's a CIDR address like 127.0.0.0/8
91
if _, net, err := net.ParseCIDR(host); err == nil {
96
if ip := net.ParseIP(host); ip != nil {
100
if strings.HasPrefix(host, "*.") {
108
// AddIP specifies an IP address that will use the bypass proxy. Note that
109
// this will only take effect if a literal IP address is dialed. A connection
110
// to a named host will never match an IP.
111
func (p *PerHost) AddIP(ip net.IP) {
112
p.bypassIPs = append(p.bypassIPs, ip)
115
// AddNetwork specifies an IP range that will use the bypass proxy. Note that
116
// this will only take effect if a literal IP address is dialed. A connection
117
// to a named host will never match.
118
func (p *PerHost) AddNetwork(net *net.IPNet) {
119
p.bypassNetworks = append(p.bypassNetworks, net)
122
// AddZone specifies a DNS suffix that will use the bypass proxy. A zone of
123
// "example.com" matches "example.com" and all of its subdomains.
124
func (p *PerHost) AddZone(zone string) {
125
if strings.HasSuffix(zone, ".") {
126
zone = zone[:len(zone)-1]
128
if !strings.HasPrefix(zone, ".") {
131
p.bypassZones = append(p.bypassZones, zone)
134
// AddHost specifies a hostname that will use the bypass proxy.
135
func (p *PerHost) AddHost(host string) {
136
if strings.HasSuffix(host, ".") {
137
host = host[:len(host)-1]
139
p.bypassHosts = append(p.bypassHosts, host)