netutil.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutil
  15. import (
  16. "encoding/base64"
  17. "net"
  18. "net/http"
  19. "net/url"
  20. "reflect"
  21. "sort"
  22. "strings"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  24. "github.com/coreos/etcd/pkg/types"
  25. )
  26. var (
  27. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "netutil")
  28. // indirection for testing
  29. resolveTCPAddr = net.ResolveTCPAddr
  30. )
  31. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  32. // resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
  33. // are resolved.
  34. func resolveTCPAddrs(urls [][]url.URL) ([][]url.URL, error) {
  35. newurls := make([][]url.URL, 0)
  36. for _, us := range urls {
  37. nus := make([]url.URL, len(us))
  38. for i, u := range us {
  39. nu, err := url.Parse(u.String())
  40. if err != nil {
  41. return nil, err
  42. }
  43. nus[i] = *nu
  44. }
  45. for i, u := range nus {
  46. host, _, err := net.SplitHostPort(u.Host)
  47. if err != nil {
  48. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  49. return nil, err
  50. }
  51. if host == "localhost" {
  52. continue
  53. }
  54. if net.ParseIP(host) != nil {
  55. continue
  56. }
  57. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  58. if err != nil {
  59. plog.Errorf("could not resolve host %s", u.Host)
  60. return nil, err
  61. }
  62. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  63. nus[i].Host = tcpAddr.String()
  64. }
  65. newurls = append(newurls, nus)
  66. }
  67. return newurls, nil
  68. }
  69. // urlsEqual checks equality of url.URLS between two arrays.
  70. // This check pass even if an URL is in hostname and opposite is in IP address.
  71. func urlsEqual(a []url.URL, b []url.URL) bool {
  72. if len(a) != len(b) {
  73. return false
  74. }
  75. urls, err := resolveTCPAddrs([][]url.URL{a, b})
  76. if err != nil {
  77. return false
  78. }
  79. a, b = urls[0], urls[1]
  80. sort.Sort(types.URLs(a))
  81. sort.Sort(types.URLs(b))
  82. for i := range a {
  83. if !reflect.DeepEqual(a[i], b[i]) {
  84. return false
  85. }
  86. }
  87. return true
  88. }
  89. func URLStringsEqual(a []string, b []string) bool {
  90. if len(a) != len(b) {
  91. return false
  92. }
  93. urlsA := make([]url.URL, 0)
  94. for _, str := range a {
  95. u, err := url.Parse(str)
  96. if err != nil {
  97. return false
  98. }
  99. urlsA = append(urlsA, *u)
  100. }
  101. urlsB := make([]url.URL, 0)
  102. for _, str := range b {
  103. u, err := url.Parse(str)
  104. if err != nil {
  105. return false
  106. }
  107. urlsB = append(urlsB, *u)
  108. }
  109. return urlsEqual(urlsA, urlsB)
  110. }
  111. // BasicAuth returns the username and password provided in the request's
  112. // Authorization header, if the request uses HTTP Basic Authentication.
  113. // See RFC 2617, Section 2.
  114. // Based on the BasicAuth method from the Golang standard lib.
  115. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  116. func BasicAuth(r *http.Request) (username, password string, ok bool) {
  117. auth := r.Header.Get("Authorization")
  118. if auth == "" {
  119. return
  120. }
  121. return parseBasicAuth(auth)
  122. }
  123. // parseBasicAuth parses an HTTP Basic Authentication string.
  124. // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
  125. // Taken from the Golang standard lib.
  126. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  127. func parseBasicAuth(auth string) (username, password string, ok bool) {
  128. if !strings.HasPrefix(auth, "Basic ") {
  129. return
  130. }
  131. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
  132. if err != nil {
  133. return
  134. }
  135. cs := string(c)
  136. s := strings.IndexByte(cs, ':')
  137. if s < 0 {
  138. return
  139. }
  140. return cs[:s], cs[s+1:], true
  141. }