upgradeDownload.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "fmt"
  22. "net/http"
  23. "os"
  24. "strconv"
  25. )
  26. // DownloadUpgrade performs a resumable download of client upgrade files.
  27. //
  28. // While downloading/resuming, a temporary file is used. Once the download is complete,
  29. // a notice is issued and the upgrade is available at the destination specified in
  30. // config.UpgradeDownloadFilename.
  31. //
  32. // The upgrade download may be either tunneled or untunneled. As the untunneled case may
  33. // happen with no handshake request response, the downloader cannot rely on having the
  34. // upgrade_client_version output from handshake and instead this logic performs a
  35. // comparison between the config.ClientVersion and the client version recorded in the
  36. // remote entity's UpgradeDownloadClientVersionHeader. A HEAD request is made to check the
  37. // version before proceeding with a full download.
  38. //
  39. // NOTE: This code does not check that any existing file at config.UpgradeDownloadFilename
  40. // is actually the version specified in handshakeVersion.
  41. //
  42. // TODO: This logic requires the outer client to *omit* config.UpgradeDownloadFilename
  43. // when there's already a downloaded upgrade pending. Because the outer client currently
  44. // handles the authenticated package phase, and because the outer client deletes the
  45. // intermediate files (including config.UpgradeDownloadFilename), if the outer client
  46. // does not omit config.UpgradeDownloadFilename then the new version will be downloaded
  47. // repeatedly. Implement a new scheme where tunnel core does the authenticated package phase
  48. // and tracks the the output by version number so that (a) tunnel core knows when it's not
  49. // necessary to re-download; (b) newer upgrades will be downloaded even when an older
  50. // upgrade is still pending install by the outer client.
  51. func DownloadUpgrade(
  52. config *Config,
  53. handshakeVersion string,
  54. tunnel *Tunnel,
  55. untunneledDialConfig *DialConfig) error {
  56. // Check if complete file already downloaded
  57. if _, err := os.Stat(config.UpgradeDownloadFilename); err == nil {
  58. NoticeClientUpgradeDownloaded(config.UpgradeDownloadFilename)
  59. return nil
  60. }
  61. // Select tunneled or untunneled configuration
  62. httpClient, requestUrl, err := MakeDownloadHttpClient(
  63. config,
  64. tunnel,
  65. untunneledDialConfig,
  66. config.UpgradeDownloadUrl,
  67. DOWNLOAD_UPGRADE_TIMEOUT)
  68. // If no handshake version is supplied, make an initial HEAD request
  69. // to get the current version from the version header.
  70. availableClientVersion := handshakeVersion
  71. if availableClientVersion == "" {
  72. request, err := http.NewRequest("HEAD", requestUrl, nil)
  73. if err != nil {
  74. return ContextError(err)
  75. }
  76. response, err := httpClient.Do(request)
  77. if err == nil && response.StatusCode != http.StatusOK {
  78. response.Body.Close()
  79. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  80. }
  81. if err != nil {
  82. return ContextError(err)
  83. }
  84. defer response.Body.Close()
  85. currentClientVersion, err := strconv.Atoi(config.ClientVersion)
  86. if err != nil {
  87. return ContextError(err)
  88. }
  89. // Note: if the header is missing, Header.Get returns "" and then
  90. // strconv.Atoi returns a parse error.
  91. availableClientVersion = response.Header.Get(config.UpgradeDownloadClientVersionHeader)
  92. checkAvailableClientVersion, err := strconv.Atoi(availableClientVersion)
  93. if err != nil {
  94. // If the header is missing or malformed, we can't determine the available
  95. // version number. This is unexpected; but if it happens, it's likely due
  96. // to a server-side configuration issue. In this one case, we don't
  97. // return an error so that we don't go into a rapid retry loop making
  98. // ineffective HEAD requests (the client may still signal an upgrade
  99. // download later in the session).
  100. NoticeAlert(
  101. "failed to download upgrade: invalid %s header value %s: %s",
  102. config.UpgradeDownloadClientVersionHeader, availableClientVersion, err)
  103. return nil
  104. }
  105. if currentClientVersion >= checkAvailableClientVersion {
  106. NoticeClientIsLatestVersion(availableClientVersion)
  107. return nil
  108. }
  109. }
  110. // Proceed with download
  111. // An intermediate filename is used since the presence of
  112. // config.UpgradeDownloadFilename indicates a completed download.
  113. downloadFilename := fmt.Sprintf(
  114. "%s.%s", config.UpgradeDownloadFilename, availableClientVersion)
  115. n, _, err := ResumeDownload(
  116. httpClient, requestUrl, downloadFilename, "")
  117. NoticeClientUpgradeDownloadedBytes(n)
  118. if err != nil {
  119. return ContextError(err)
  120. }
  121. err = os.Rename(downloadFilename, config.UpgradeDownloadFilename)
  122. if err != nil {
  123. return ContextError(err)
  124. }
  125. NoticeClientUpgradeDownloaded(config.UpgradeDownloadFilename)
  126. return nil
  127. }