cubefs
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.
4
5package proxy6
7import (8"context"9"net"10"strings"11)
12
13// A PerHost directs connections to a default Dialer unless the host name
14// requested matches one of a number of exceptions.
15type PerHost struct {16def, bypass Dialer17
18bypassNetworks []*net.IPNet19bypassIPs []net.IP20bypassZones []string21bypassHosts []string22}
23
24// NewPerHost returns a PerHost Dialer that directs connections to either
25// defaultDialer or bypass, depending on whether the connection matches one of
26// the configured rules.
27func NewPerHost(defaultDialer, bypass Dialer) *PerHost {28return &PerHost{29def: defaultDialer,30bypass: bypass,31}32}
33
34// Dial connects to the address addr on the given network through either
35// defaultDialer or bypass.
36func (p *PerHost) Dial(network, addr string) (c net.Conn, err error) {37host, _, err := net.SplitHostPort(addr)38if err != nil {39return nil, err40}41
42return p.dialerForRequest(host).Dial(network, addr)43}
44
45// DialContext connects to the address addr on the given network through either
46// defaultDialer or bypass.
47func (p *PerHost) DialContext(ctx context.Context, network, addr string) (c net.Conn, err error) {48host, _, err := net.SplitHostPort(addr)49if err != nil {50return nil, err51}52d := p.dialerForRequest(host)53if x, ok := d.(ContextDialer); ok {54return x.DialContext(ctx, network, addr)55}56return dialContext(ctx, d, network, addr)57}
58
59func (p *PerHost) dialerForRequest(host string) Dialer {60if ip := net.ParseIP(host); ip != nil {61for _, net := range p.bypassNetworks {62if net.Contains(ip) {63return p.bypass64}65}66for _, bypassIP := range p.bypassIPs {67if bypassIP.Equal(ip) {68return p.bypass69}70}71return p.def72}73
74for _, zone := range p.bypassZones {75if strings.HasSuffix(host, zone) {76return p.bypass77}78if host == zone[1:] {79// For a zone ".example.com", we match "example.com"80// too.81return p.bypass82}83}84for _, bypassHost := range p.bypassHosts {85if bypassHost == host {86return p.bypass87}88}89return p.def90}
91
92// AddFromString parses a string that contains comma-separated values
93// specifying hosts that should use the bypass proxy. Each value is either an
94// IP address, a CIDR range, a zone (*.example.com) or a host name
95// (localhost). A best effort is made to parse the string and errors are
96// ignored.
97func (p *PerHost) AddFromString(s string) {98hosts := strings.Split(s, ",")99for _, host := range hosts {100host = strings.TrimSpace(host)101if len(host) == 0 {102continue103}104if strings.Contains(host, "/") {105// We assume that it's a CIDR address like 127.0.0.0/8106if _, net, err := net.ParseCIDR(host); err == nil {107p.AddNetwork(net)108}109continue110}111if ip := net.ParseIP(host); ip != nil {112p.AddIP(ip)113continue114}115if strings.HasPrefix(host, "*.") {116p.AddZone(host[1:])117continue118}119p.AddHost(host)120}121}
122
123// AddIP specifies an IP address that will use the bypass proxy. Note that
124// this will only take effect if a literal IP address is dialed. A connection
125// to a named host will never match an IP.
126func (p *PerHost) AddIP(ip net.IP) {127p.bypassIPs = append(p.bypassIPs, ip)128}
129
130// AddNetwork specifies an IP range that will use the bypass proxy. Note that
131// this will only take effect if a literal IP address is dialed. A connection
132// to a named host will never match.
133func (p *PerHost) AddNetwork(net *net.IPNet) {134p.bypassNetworks = append(p.bypassNetworks, net)135}
136
137// AddZone specifies a DNS suffix that will use the bypass proxy. A zone of
138// "example.com" matches "example.com" and all of its subdomains.
139func (p *PerHost) AddZone(zone string) {140if strings.HasSuffix(zone, ".") {141zone = zone[:len(zone)-1]142}143if !strings.HasPrefix(zone, ".") {144zone = "." + zone145}146p.bypassZones = append(p.bypassZones, zone)147}
148
149// AddHost specifies a host name that will use the bypass proxy.
150func (p *PerHost) AddHost(host string) {151if strings.HasSuffix(host, ".") {152host = host[:len(host)-1]153}154p.bypassHosts = append(p.bypassHosts, host)155}
156