remoteServerList.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "io/ioutil"
  23. "net"
  24. "net/http"
  25. "net/url"
  26. )
  27. // FetchRemoteServerList downloads a remote server list JSON record from
  28. // config.RemoteServerListUrl; validates its digital signature using the
  29. // public key config.RemoteServerListSignaturePublicKey; and parses the
  30. // data field into ServerEntry records.
  31. func FetchRemoteServerList(config *Config, dialConfig *DialConfig) (err error) {
  32. NoticeInfo("fetching remote server list")
  33. if config.RemoteServerListUrl == "" {
  34. return ContextError(errors.New("remote server list URL is blank"))
  35. }
  36. if config.RemoteServerListSignaturePublicKey == "" {
  37. return ContextError(errors.New("remote server list signature public key blank"))
  38. }
  39. dialer := NewTCPDialer(dialConfig)
  40. // When the URL is HTTPS, use the custom TLS dialer with the
  41. // UseIndistinguishableTLS option.
  42. // TODO: refactor into helper function
  43. requestUrl, err := url.Parse(config.RemoteServerListUrl)
  44. if err != nil {
  45. return ContextError(err)
  46. }
  47. if requestUrl.Scheme == "https" {
  48. dialer = NewCustomTLSDialer(
  49. &CustomTLSConfig{
  50. Dial: dialer,
  51. SendServerName: true,
  52. SkipVerify: false,
  53. UseIndistinguishableTLS: config.UseIndistinguishableTLS,
  54. SystemCACertificateDirectory: config.SystemCACertificateDirectory,
  55. })
  56. // Change the scheme to "http"; otherwise http.Transport will try to do
  57. // another TLS handshake inside the explicit TLS session. Also need to
  58. // force the port to 443,as the default for "http", 80, won't talk TLS.
  59. requestUrl.Scheme = "http"
  60. host, _, err := net.SplitHostPort(requestUrl.Host)
  61. if err != nil {
  62. // Assume there's no port
  63. host = requestUrl.Host
  64. }
  65. requestUrl.Host = net.JoinHostPort(host, "443")
  66. }
  67. transport := &http.Transport{
  68. Dial: dialer,
  69. }
  70. httpClient := http.Client{
  71. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  72. Transport: transport,
  73. }
  74. request, err := http.NewRequest("GET", requestUrl.String(), nil)
  75. if err != nil {
  76. return ContextError(err)
  77. }
  78. etag, err := GetUrlETag(config.RemoteServerListUrl)
  79. if err != nil {
  80. return ContextError(err)
  81. }
  82. if etag != "" {
  83. request.Header.Add("If-None-Match", etag)
  84. }
  85. response, err := httpClient.Do(request)
  86. if err != nil {
  87. return ContextError(err)
  88. }
  89. defer response.Body.Close()
  90. if response.StatusCode == http.StatusNotModified {
  91. return nil
  92. }
  93. body, err := ioutil.ReadAll(response.Body)
  94. if err != nil {
  95. return ContextError(err)
  96. }
  97. remoteServerList, err := ReadAuthenticatedDataPackage(
  98. body, config.RemoteServerListSignaturePublicKey)
  99. if err != nil {
  100. return ContextError(err)
  101. }
  102. serverEntries, err := DecodeAndValidateServerEntryList(remoteServerList)
  103. if err != nil {
  104. return ContextError(err)
  105. }
  106. err = StoreServerEntries(serverEntries, true)
  107. if err != nil {
  108. return ContextError(err)
  109. }
  110. etag = response.Header.Get("ETag")
  111. if etag != "" {
  112. err := SetUrlETag(config.RemoteServerListUrl, etag)
  113. if err != nil {
  114. NoticeAlert("failed to set remote server list etag: %s", ContextError(err))
  115. // This fetch is still reported as a success, even if we can't store the etag
  116. }
  117. }
  118. return nil
  119. }