upgradeDownload.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if err != nil {
  85. return errors.Trace(err)
  86. }
  87. // If no handshake version is supplied, make an initial HEAD request
  88. // to get the current version from the version header.
  89. availableClientVersion := handshakeVersion
  90. if availableClientVersion == "" {
  91. request, err := http.NewRequest("HEAD", downloadURL.URL, nil)
  92. if err != nil {
  93. return errors.Trace(err)
  94. }
  95. request = request.WithContext(ctx)
  96. response, err := httpClient.Do(request)
  97. if err == nil && response.StatusCode != http.StatusOK {
  98. response.Body.Close()
  99. err = fmt.Errorf("unexpected response status code: %d", response.StatusCode)
  100. }
  101. if err != nil {
  102. return errors.Trace(err)
  103. }
  104. defer response.Body.Close()
  105. currentClientVersion, err := strconv.Atoi(config.ClientVersion)
  106. if err != nil {
  107. return errors.Trace(err)
  108. }
  109. // Note: if the header is missing, Header.Get returns "" and then
  110. // strconv.Atoi returns a parse error.
  111. availableClientVersion = response.Header.Get(clientVersionHeader)
  112. checkAvailableClientVersion, err := strconv.Atoi(availableClientVersion)
  113. if err != nil {
  114. // If the header is missing or malformed, we can't determine the available
  115. // version number. This is unexpected; but if it happens, it's likely due
  116. // to a server-side configuration issue. In this one case, we don't
  117. // return an error so that we don't go into a rapid retry loop making
  118. // ineffective HEAD requests (the client may still signal an upgrade
  119. // download later in the session).
  120. NoticeWarning(
  121. "failed to download upgrade: invalid %s header value %s: %s",
  122. clientVersionHeader, availableClientVersion, err)
  123. return nil
  124. }
  125. if currentClientVersion >= checkAvailableClientVersion {
  126. NoticeClientIsLatestVersion(availableClientVersion)
  127. return nil
  128. }
  129. }
  130. // Proceed with download
  131. // An intermediate filename is used since the presence of
  132. // config.GetUpgradeDownloadFilename() indicates a completed download.
  133. downloadFilename := fmt.Sprintf(
  134. "%s.%s", config.GetUpgradeDownloadFilename(), availableClientVersion)
  135. n, _, err := ResumeDownload(
  136. ctx,
  137. httpClient,
  138. downloadURL.URL,
  139. MakePsiphonUserAgent(config),
  140. downloadFilename,
  141. "")
  142. NoticeClientUpgradeDownloadedBytes(n)
  143. if err != nil {
  144. return errors.Trace(err)
  145. }
  146. err = os.Rename(downloadFilename, config.GetUpgradeDownloadFilename())
  147. if err != nil {
  148. return errors.Trace(err)
  149. }
  150. NoticeClientUpgradeDownloaded(config.GetUpgradeDownloadFilename())
  151. // Limitation: unlike the remote server list download case, DNS cache
  152. // extension is not invoked here since payload authentication is not
  153. // currently implemented at this level. iOS VPN, the primary use case for
  154. // DNS cache extension, does not use this side-load upgrade mechanism.
  155. return nil
  156. }