upgradeDownload.go 5.9 KB

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