remoteServerList.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/http"
  25. "time"
  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. httpClient, requestUrl, err := MakeUntunneledHttpsClient(
  40. dialConfig, nil, config.RemoteServerListUrl, time.Duration(*config.FetchRemoteServerListTimeoutSeconds)*time.Second)
  41. if err != nil {
  42. return ContextError(err)
  43. }
  44. request, err := http.NewRequest("GET", requestUrl, nil)
  45. if err != nil {
  46. return ContextError(err)
  47. }
  48. etag, err := GetUrlETag(config.RemoteServerListUrl)
  49. if err != nil {
  50. return ContextError(err)
  51. }
  52. if etag != "" {
  53. request.Header.Add("If-None-Match", etag)
  54. }
  55. response, err := httpClient.Do(request)
  56. if err == nil &&
  57. (response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified) {
  58. response.Body.Close()
  59. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  60. }
  61. if err != nil {
  62. return ContextError(err)
  63. }
  64. defer response.Body.Close()
  65. if response.StatusCode == http.StatusNotModified {
  66. return nil
  67. }
  68. body, err := ioutil.ReadAll(response.Body)
  69. if err != nil {
  70. return ContextError(err)
  71. }
  72. remoteServerList, err := ReadAuthenticatedDataPackage(
  73. body, config.RemoteServerListSignaturePublicKey)
  74. if err != nil {
  75. return ContextError(err)
  76. }
  77. serverEntries, err := DecodeAndValidateServerEntryList(
  78. remoteServerList,
  79. GetCurrentTimestamp(),
  80. SERVER_ENTRY_SOURCE_REMOTE)
  81. if err != nil {
  82. return ContextError(err)
  83. }
  84. err = StoreServerEntries(serverEntries, true)
  85. if err != nil {
  86. return ContextError(err)
  87. }
  88. etag = response.Header.Get("ETag")
  89. if etag != "" {
  90. err := SetUrlETag(config.RemoteServerListUrl, etag)
  91. if err != nil {
  92. NoticeAlert("failed to set remote server list etag: %s", ContextError(err))
  93. // This fetch is still reported as a success, even if we can't store the etag
  94. }
  95. }
  96. return nil
  97. }