remoteServerList.go 3.7 KB

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