upgradeDownload.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/errors"
  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.GetUpgradeDownloadFilename().
  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.GetUpgradeDownloadFilename()
  43. // is actually the version specified in handshakeVersion.
  44. //
  45. // TODO: This logic requires the outer client to *omit* config.UpgradeDownloadURLs, disabling
  46. // upgrade downloads, when there's already a downloaded upgrade pending. This is because the
  47. // outer client currently handles the authenticated package phase, and because the outer client
  48. // deletes the intermediate files (including config.GetUpgradeDownloadFilename()). So if the outer
  49. // client does not disable upgrade downloads 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.GetUpgradeDownloadFilename()); err == nil {
  65. NoticeClientUpgradeDownloaded(config.GetUpgradeDownloadFilename())
  66. return nil
  67. }
  68. p := config.GetParameters().Get()
  69. urls := p.TransferURLs(parameters.UpgradeDownloadURLs)
  70. clientVersionHeader := p.String(parameters.UpgradeDownloadClientVersionHeader)
  71. downloadTimeout := p.Duration(parameters.FetchUpgradeTimeout)
  72. p.Close()
  73. var cancelFunc context.CancelFunc
  74. ctx, cancelFunc = context.WithTimeout(ctx, downloadTimeout)
  75. defer cancelFunc()
  76. // Select tunneled or untunneled configuration
  77. downloadURL := urls.Select(attempt)
  78. httpClient, _, _, err := MakeDownloadHTTPClient(
  79. ctx,
  80. config,
  81. tunnel,
  82. untunneledDialConfig,
  83. downloadURL.SkipVerify,
  84. config.DisableSystemRootCAs,
  85. downloadURL.FrontingSpecs,
  86. func(frontingProviderID string) {
  87. NoticeInfo(
  88. "DownloadUpgrade: selected fronting provider %s for %s",
  89. frontingProviderID, downloadURL.URL)
  90. })
  91. if err != nil {
  92. return errors.Trace(err)
  93. }
  94. // If no handshake version is supplied, make an initial HEAD request
  95. // to get the current version from the version header.
  96. availableClientVersion := handshakeVersion
  97. if availableClientVersion == "" {
  98. request, err := http.NewRequest("HEAD", downloadURL.URL, nil)
  99. if err != nil {
  100. return errors.Trace(err)
  101. }
  102. request = request.WithContext(ctx)
  103. response, err := httpClient.Do(request)
  104. if err == nil && response.StatusCode != http.StatusOK {
  105. response.Body.Close()
  106. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  107. }
  108. if err != nil {
  109. return errors.Trace(err)
  110. }
  111. defer response.Body.Close()
  112. currentClientVersion, err := strconv.Atoi(config.ClientVersion)
  113. if err != nil {
  114. return errors.Trace(err)
  115. }
  116. // Note: if the header is missing, Header.Get returns "" and then
  117. // strconv.Atoi returns a parse error.
  118. availableClientVersion = response.Header.Get(clientVersionHeader)
  119. checkAvailableClientVersion, err := strconv.Atoi(availableClientVersion)
  120. if err != nil {
  121. // If the header is missing or malformed, we can't determine the available
  122. // version number. This is unexpected; but if it happens, it's likely due
  123. // to a server-side configuration issue. In this one case, we don't
  124. // return an error so that we don't go into a rapid retry loop making
  125. // ineffective HEAD requests (the client may still signal an upgrade
  126. // download later in the session).
  127. NoticeWarning(
  128. "failed to download upgrade: invalid %s header value %s: %s",
  129. clientVersionHeader, availableClientVersion, err)
  130. return nil
  131. }
  132. if currentClientVersion >= checkAvailableClientVersion {
  133. NoticeClientIsLatestVersion(availableClientVersion)
  134. return nil
  135. }
  136. }
  137. // Proceed with download
  138. // An intermediate filename is used since the presence of
  139. // config.GetUpgradeDownloadFilename() indicates a completed download.
  140. downloadFilename := fmt.Sprintf(
  141. "%s.%s", config.GetUpgradeDownloadFilename(), availableClientVersion)
  142. n, _, err := ResumeDownload(
  143. ctx,
  144. httpClient,
  145. downloadURL.URL,
  146. MakePsiphonUserAgent(config),
  147. downloadFilename,
  148. "")
  149. NoticeClientUpgradeDownloadedBytes(n)
  150. if err != nil {
  151. return errors.Trace(err)
  152. }
  153. err = os.Rename(downloadFilename, config.GetUpgradeDownloadFilename())
  154. if err != nil {
  155. return errors.Trace(err)
  156. }
  157. NoticeClientUpgradeDownloaded(config.GetUpgradeDownloadFilename())
  158. // Limitation: unlike the remote server list download case, DNS cache
  159. // extension is not invoked here since payload authentication is not
  160. // currently implemented at this level. iOS VPN, the primary use case for
  161. // DNS cache extension, does not use this side-load upgrade mechanism.
  162. return nil
  163. }