per_host.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. package proxy
  5. import (
  6. "net"
  7. "strings"
  8. )
  9. // A PerHost directs connections to a default Dialer unless the hostname
  10. // requested matches one of a number of exceptions.
  11. type PerHost struct {
  12. def, bypass Dialer
  13. bypassNetworks []*net.IPNet
  14. bypassIPs []net.IP
  15. bypassZones []string
  16. bypassHosts []string
  17. }
  18. // NewPerHost returns a PerHost Dialer that directs connections to either
  19. // defaultDialer or bypass, depending on whether the connection matches one of
  20. // the configured rules.
  21. func NewPerHost(defaultDialer, bypass Dialer) *PerHost {
  22. return &PerHost{
  23. def: defaultDialer,
  24. bypass: bypass,
  25. }
  26. }
  27. // Dial connects to the address addr on the given network through either
  28. // defaultDialer or bypass.
  29. func (p *PerHost) Dial(network, addr string) (c net.Conn, err error) {
  30. host, _, err := net.SplitHostPort(addr)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return p.dialerForRequest(host).Dial(network, addr)
  35. }
  36. func (p *PerHost) dialerForRequest(host string) Dialer {
  37. if ip := net.ParseIP(host); ip != nil {
  38. for _, net := range p.bypassNetworks {
  39. if net.Contains(ip) {
  40. return p.bypass
  41. }
  42. }
  43. for _, bypassIP := range p.bypassIPs {
  44. if bypassIP.Equal(ip) {
  45. return p.bypass
  46. }
  47. }
  48. return p.def
  49. }
  50. for _, zone := range p.bypassZones {
  51. if strings.HasSuffix(host, zone) {
  52. return p.bypass
  53. }
  54. if host == zone[1:] {
  55. // For a zone "example.com", we match "example.com"
  56. // too.
  57. return p.bypass
  58. }
  59. }
  60. for _, bypassHost := range p.bypassHosts {
  61. if bypassHost == host {
  62. return p.bypass
  63. }
  64. }
  65. return p.def
  66. }
  67. // AddFromString parses a string that contains comma-separated values
  68. // specifying hosts that should use the bypass proxy. Each value is either an
  69. // IP address, a CIDR range, a zone (*.example.com) or a hostname
  70. // (localhost). A best effort is made to parse the string and errors are
  71. // ignored.
  72. func (p *PerHost) AddFromString(s string) {
  73. hosts := strings.Split(s, ",")
  74. for _, host := range hosts {
  75. host = strings.TrimSpace(host)
  76. if len(host) == 0 {
  77. continue
  78. }
  79. if strings.Contains(host, "/") {
  80. // We assume that it's a CIDR address like 127.0.0.0/8
  81. if _, net, err := net.ParseCIDR(host); err == nil {
  82. p.AddNetwork(net)
  83. }
  84. continue
  85. }
  86. if ip := net.ParseIP(host); ip != nil {
  87. p.AddIP(ip)
  88. continue
  89. }
  90. if strings.HasPrefix(host, "*.") {
  91. p.AddZone(host[1:])
  92. continue
  93. }
  94. p.AddHost(host)
  95. }
  96. }
  97. // AddIP specifies an IP address that will use the bypass proxy. Note that
  98. // this will only take effect if a literal IP address is dialed. A connection
  99. // to a named host will never match an IP.
  100. func (p *PerHost) AddIP(ip net.IP) {
  101. p.bypassIPs = append(p.bypassIPs, ip)
  102. }
  103. // AddNetwork specifies an IP range that will use the bypass proxy. Note that
  104. // this will only take effect if a literal IP address is dialed. A connection
  105. // to a named host will never match.
  106. func (p *PerHost) AddNetwork(net *net.IPNet) {
  107. p.bypassNetworks = append(p.bypassNetworks, net)
  108. }
  109. // AddZone specifies a DNS suffix that will use the bypass proxy. A zone of
  110. // "example.com" matches "example.com" and all of its subdomains.
  111. func (p *PerHost) AddZone(zone string) {
  112. if strings.HasSuffix(zone, ".") {
  113. zone = zone[:len(zone)-1]
  114. }
  115. if !strings.HasPrefix(zone, ".") {
  116. zone = "." + zone
  117. }
  118. p.bypassZones = append(p.bypassZones, zone)
  119. }
  120. // AddHost specifies a hostname that will use the bypass proxy.
  121. func (p *PerHost) AddHost(host string) {
  122. if strings.HasSuffix(host, ".") {
  123. host = host[:len(host)-1]
  124. }
  125. p.bypassHosts = append(p.bypassHosts, host)
  126. }