remoteServerList.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. )
  26. // FetchRemoteServerList downloads a remote server list JSON record from
  27. // config.RemoteServerListUrl; validates its digital signature using the
  28. // public key config.RemoteServerListSignaturePublicKey; and parses the
  29. // data field into ServerEntry records.
  30. func FetchRemoteServerList(config *Config, dialConfig *DialConfig) (err error) {
  31. NoticeInfo("fetching remote server list")
  32. if config.RemoteServerListUrl == "" {
  33. return ContextError(errors.New("remote server list URL is blank"))
  34. }
  35. if config.RemoteServerListSignaturePublicKey == "" {
  36. return ContextError(errors.New("remote server list signature public key blank"))
  37. }
  38. httpClient, requestUrl, err := MakeUntunneledHttpsClient(
  39. dialConfig, nil, config.RemoteServerListUrl, FETCH_REMOTE_SERVER_LIST_TIMEOUT)
  40. if err != nil {
  41. return ContextError(err)
  42. }
  43. request, err := http.NewRequest("GET", requestUrl, nil)
  44. if err != nil {
  45. return ContextError(err)
  46. }
  47. etag, err := GetUrlETag(config.RemoteServerListUrl)
  48. if err != nil {
  49. return ContextError(err)
  50. }
  51. if etag != "" {
  52. request.Header.Add("If-None-Match", etag)
  53. }
  54. response, err := httpClient.Do(request)
  55. if err == nil &&
  56. (response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified) {
  57. response.Body.Close()
  58. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  59. }
  60. if err != nil {
  61. return ContextError(err)
  62. }
  63. defer response.Body.Close()
  64. if response.StatusCode == http.StatusNotModified {
  65. return nil
  66. }
  67. body, err := ioutil.ReadAll(response.Body)
  68. if err != nil {
  69. return ContextError(err)
  70. }
  71. remoteServerList, err := ReadAuthenticatedDataPackage(
  72. body, config.RemoteServerListSignaturePublicKey)
  73. if err != nil {
  74. return ContextError(err)
  75. }
  76. serverEntries, err := DecodeAndValidateServerEntryList(
  77. remoteServerList,
  78. GetCurrentTimestamp(),
  79. SERVER_ENTRY_SOURCE_REMOTE)
  80. if err != nil {
  81. return ContextError(err)
  82. }
  83. err = StoreServerEntries(serverEntries, true)
  84. if err != nil {
  85. return ContextError(err)
  86. }
  87. etag = response.Header.Get("ETag")
  88. if etag != "" {
  89. err := SetUrlETag(config.RemoteServerListUrl, etag)
  90. if err != nil {
  91. NoticeAlert("failed to set remote server list etag: %s", ContextError(err))
  92. // This fetch is still reported as a success, even if we can't store the etag
  93. }
  94. }
  95. return nil
  96. }