remoteServerList.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "compress/zlib"
  22. "io/ioutil"
  23. "os"
  24. "strings"
  25. "time"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  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(
  33. config *Config,
  34. tunnel *Tunnel,
  35. untunneledDialConfig *DialConfig) error {
  36. NoticeInfo("fetching remote server list")
  37. // Select tunneled or untunneled configuration
  38. httpClient, requestUrl, err := MakeDownloadHttpClient(
  39. config,
  40. tunnel,
  41. untunneledDialConfig,
  42. config.RemoteServerListUrl,
  43. time.Duration(*config.FetchRemoteServerListTimeoutSeconds)*time.Second)
  44. if err != nil {
  45. return common.ContextError(err)
  46. }
  47. // Proceed with download
  48. downloadFilename := config.RemoteServerListDownloadFilename
  49. if downloadFilename == "" {
  50. splitPath := strings.Split(config.RemoteServerListUrl, "/")
  51. downloadFilename = splitPath[len(splitPath)-1]
  52. }
  53. lastETag, err := GetUrlETag(config.RemoteServerListUrl)
  54. if err != nil {
  55. return common.ContextError(err)
  56. }
  57. n, responseETag, err := ResumeDownload(
  58. httpClient, requestUrl, downloadFilename, lastETag)
  59. NoticeRemoteServerListDownloadedBytes(n)
  60. if err != nil {
  61. return common.ContextError(err)
  62. }
  63. if responseETag == lastETag {
  64. // The remote server list is unchanged and no data was downloaded
  65. return nil
  66. }
  67. NoticeRemoteServerListDownloaded(downloadFilename)
  68. // The downloaded content is a zlib compressed authenticated
  69. // data package containing a list of encoded server entries.
  70. downloadContent, err := os.Open(downloadFilename)
  71. if err != nil {
  72. return common.ContextError(err)
  73. }
  74. defer downloadContent.Close()
  75. zlibReader, err := zlib.NewReader(downloadContent)
  76. if err != nil {
  77. return common.ContextError(err)
  78. }
  79. dataPackage, err := ioutil.ReadAll(zlibReader)
  80. zlibReader.Close()
  81. if err != nil {
  82. return common.ContextError(err)
  83. }
  84. remoteServerList, err := ReadAuthenticatedDataPackage(
  85. dataPackage, config.RemoteServerListSignaturePublicKey)
  86. if err != nil {
  87. return common.ContextError(err)
  88. }
  89. serverEntries, err := DecodeAndValidateServerEntryList(
  90. remoteServerList,
  91. common.GetCurrentTimestamp(),
  92. common.SERVER_ENTRY_SOURCE_REMOTE)
  93. if err != nil {
  94. return common.ContextError(err)
  95. }
  96. err = StoreServerEntries(serverEntries, true)
  97. if err != nil {
  98. return common.ContextError(err)
  99. }
  100. // Now that the server entries are successfully imported, store the response
  101. // ETag so we won't re-download this same data again.
  102. if responseETag != "" {
  103. err := SetUrlETag(config.RemoteServerListUrl, responseETag)
  104. if err != nil {
  105. NoticeAlert("failed to set remote server list ETag: %s", common.ContextError(err))
  106. // This fetch is still reported as a success, even if we can't store the etag
  107. }
  108. }
  109. return nil
  110. }