remoteServerList.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/http"
  24. )
  25. // FetchRemoteServerList downloads a remote server list JSON record from
  26. // config.RemoteServerListUrl; validates its digital signature using the
  27. // public key config.RemoteServerListSignaturePublicKey; and parses the
  28. // data field into ServerEntry records.
  29. func FetchRemoteServerList(config *Config, dialConfig *DialConfig) (err error) {
  30. NoticeInfo("fetching remote server list")
  31. if config.RemoteServerListUrl == "" {
  32. return ContextError(errors.New("remote server list URL is blank"))
  33. }
  34. if config.RemoteServerListSignaturePublicKey == "" {
  35. return ContextError(errors.New("remote server list signature public key blank"))
  36. }
  37. transport := &http.Transport{
  38. Dial: NewTCPDialer(dialConfig),
  39. }
  40. httpClient := http.Client{
  41. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  42. Transport: transport,
  43. }
  44. request, err := http.NewRequest("GET", config.RemoteServerListUrl, 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. return ContextError(err)
  58. }
  59. defer response.Body.Close()
  60. body, err := ioutil.ReadAll(response.Body)
  61. if err != nil {
  62. return ContextError(err)
  63. }
  64. remoteServerList, err := ReadAuthenticatedDataPackage(
  65. body, config.RemoteServerListSignaturePublicKey)
  66. if err != nil {
  67. return ContextError(err)
  68. }
  69. serverEntries, err := DecodeAndValidateServerEntryList(remoteServerList)
  70. if err != nil {
  71. return ContextError(err)
  72. }
  73. err = StoreServerEntries(serverEntries, true)
  74. if err != nil {
  75. return ContextError(err)
  76. }
  77. etag = response.Header.Get("ETag")
  78. if etag != "" {
  79. err := SetUrlETag(config.RemoteServerListUrl, etag)
  80. if err != nil {
  81. NoticeAlert("failed to set remote server list etag: %s", ContextError(err))
  82. // This fetch is still reported as a success, even if we can't store the etag
  83. }
  84. }
  85. return nil
  86. }