upgradeDownload.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  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. p := config.clientParameters.Get()
  69. urls := p.DownloadURLs(parameters.UpgradeDownloadURLs)
  70. clientVersionHeader := p.String(parameters.UpgradeDownloadClientVersionHeader)
  71. downloadTimeout := p.Duration(parameters.FetchUpgradeTimeout)
  72. p = nil
  73. var cancelFunc context.CancelFunc
  74. ctx, cancelFunc = context.WithTimeout(ctx, downloadTimeout)
  75. defer cancelFunc()
  76. // Select tunneled or untunneled configuration
  77. downloadURL, _, skipVerify := urls.Select(attempt)
  78. httpClient, err := MakeDownloadHTTPClient(
  79. ctx,
  80. config,
  81. tunnel,
  82. untunneledDialConfig,
  83. skipVerify)
  84. // If no handshake version is supplied, make an initial HEAD request
  85. // to get the current version from the version header.
  86. availableClientVersion := handshakeVersion
  87. if availableClientVersion == "" {
  88. request, err := http.NewRequest("HEAD", downloadURL, nil)
  89. if err != nil {
  90. return common.ContextError(err)
  91. }
  92. request = request.WithContext(ctx)
  93. response, err := httpClient.Do(request)
  94. if err == nil && response.StatusCode != http.StatusOK {
  95. response.Body.Close()
  96. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  97. }
  98. if err != nil {
  99. return common.ContextError(err)
  100. }
  101. defer response.Body.Close()
  102. currentClientVersion, err := strconv.Atoi(config.ClientVersion)
  103. if err != nil {
  104. return common.ContextError(err)
  105. }
  106. // Note: if the header is missing, Header.Get returns "" and then
  107. // strconv.Atoi returns a parse error.
  108. availableClientVersion = response.Header.Get(clientVersionHeader)
  109. checkAvailableClientVersion, err := strconv.Atoi(availableClientVersion)
  110. if err != nil {
  111. // If the header is missing or malformed, we can't determine the available
  112. // version number. This is unexpected; but if it happens, it's likely due
  113. // to a server-side configuration issue. In this one case, we don't
  114. // return an error so that we don't go into a rapid retry loop making
  115. // ineffective HEAD requests (the client may still signal an upgrade
  116. // download later in the session).
  117. NoticeAlert(
  118. "failed to download upgrade: invalid %s header value %s: %s",
  119. clientVersionHeader, availableClientVersion, err)
  120. return nil
  121. }
  122. if currentClientVersion >= checkAvailableClientVersion {
  123. NoticeClientIsLatestVersion(availableClientVersion)
  124. return nil
  125. }
  126. }
  127. // Proceed with download
  128. // An intermediate filename is used since the presence of
  129. // config.UpgradeDownloadFilename indicates a completed download.
  130. downloadFilename := fmt.Sprintf(
  131. "%s.%s", config.UpgradeDownloadFilename, availableClientVersion)
  132. n, _, err := ResumeDownload(
  133. ctx,
  134. httpClient,
  135. downloadURL,
  136. MakePsiphonUserAgent(config),
  137. downloadFilename,
  138. "")
  139. NoticeClientUpgradeDownloadedBytes(n)
  140. if err != nil {
  141. return common.ContextError(err)
  142. }
  143. err = os.Rename(downloadFilename, config.UpgradeDownloadFilename)
  144. if err != nil {
  145. return common.ContextError(err)
  146. }
  147. NoticeClientUpgradeDownloaded(config.UpgradeDownloadFilename)
  148. return nil
  149. }