remoteServerList.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "errors"
  22. "fmt"
  23. "io/ioutil"
  24. "net"
  25. "net/http"
  26. "net/url"
  27. )
  28. // FetchRemoteServerList downloads a remote server list JSON record from
  29. // config.RemoteServerListUrl; validates its digital signature using the
  30. // public key config.RemoteServerListSignaturePublicKey; and parses the
  31. // data field into ServerEntry records.
  32. func FetchRemoteServerList(config *Config, dialConfig *DialConfig) (err error) {
  33. NoticeInfo("fetching remote server list")
  34. if config.RemoteServerListUrl == "" {
  35. return ContextError(errors.New("remote server list URL is blank"))
  36. }
  37. if config.RemoteServerListSignaturePublicKey == "" {
  38. return ContextError(errors.New("remote server list signature public key blank"))
  39. }
  40. dialer := NewTCPDialer(dialConfig)
  41. // When the URL is HTTPS, use the custom TLS dialer with the
  42. // UseIndistinguishableTLS option.
  43. // TODO: refactor into helper function
  44. requestUrl, err := url.Parse(config.RemoteServerListUrl)
  45. if err != nil {
  46. return ContextError(err)
  47. }
  48. if requestUrl.Scheme == "https" {
  49. dialer = NewCustomTLSDialer(
  50. &CustomTLSConfig{
  51. Dial: dialer,
  52. SendServerName: true,
  53. SkipVerify: false,
  54. UseIndistinguishableTLS: config.UseIndistinguishableTLS,
  55. TrustedCACertificatesFilename: config.TrustedCACertificatesFilename,
  56. })
  57. // Change the scheme to "http"; otherwise http.Transport will try to do
  58. // another TLS handshake inside the explicit TLS session. Also need to
  59. // force the port to 443,as the default for "http", 80, won't talk TLS.
  60. requestUrl.Scheme = "http"
  61. host, _, err := net.SplitHostPort(requestUrl.Host)
  62. if err != nil {
  63. // Assume there's no port
  64. host = requestUrl.Host
  65. }
  66. requestUrl.Host = net.JoinHostPort(host, "443")
  67. }
  68. transport := &http.Transport{
  69. Dial: dialer,
  70. }
  71. httpClient := http.Client{
  72. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  73. Transport: transport,
  74. }
  75. request, err := http.NewRequest("GET", requestUrl.String(), nil)
  76. if err != nil {
  77. return ContextError(err)
  78. }
  79. etag, err := GetUrlETag(config.RemoteServerListUrl)
  80. if err != nil {
  81. return ContextError(err)
  82. }
  83. if etag != "" {
  84. request.Header.Add("If-None-Match", etag)
  85. }
  86. response, err := httpClient.Do(request)
  87. if err == nil &&
  88. (response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified) {
  89. response.Body.Close()
  90. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  91. }
  92. if err != nil {
  93. return ContextError(err)
  94. }
  95. defer response.Body.Close()
  96. if response.StatusCode == http.StatusNotModified {
  97. return nil
  98. }
  99. body, err := ioutil.ReadAll(response.Body)
  100. if err != nil {
  101. return ContextError(err)
  102. }
  103. remoteServerList, err := ReadAuthenticatedDataPackage(
  104. body, config.RemoteServerListSignaturePublicKey)
  105. if err != nil {
  106. return ContextError(err)
  107. }
  108. serverEntries, err := DecodeAndValidateServerEntryList(remoteServerList)
  109. if err != nil {
  110. return ContextError(err)
  111. }
  112. err = StoreServerEntries(serverEntries, true)
  113. if err != nil {
  114. return ContextError(err)
  115. }
  116. etag = response.Header.Get("ETag")
  117. if etag != "" {
  118. err := SetUrlETag(config.RemoteServerListUrl, etag)
  119. if err != nil {
  120. NoticeAlert("failed to set remote server list etag: %s", ContextError(err))
  121. // This fetch is still reported as a success, even if we can't store the etag
  122. }
  123. }
  124. return nil
  125. }