remoteServerList.go 3.5 KB

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