upgradeDownload.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "io"
  23. "net"
  24. "net/http"
  25. "os"
  26. )
  27. // DownloadUpgrade performs a tunneled, resumable download of client upgrade files.
  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. // NOTE: this code does not check that any existing file at config.UpgradeDownloadFilename
  32. // is actually the version specified in clientUpgradeVersion.
  33. func DownloadUpgrade(config *Config, clientUpgradeVersion string, tunnel *Tunnel) error {
  34. // Check if complete file already downloaded
  35. if _, err := os.Stat(config.UpgradeDownloadFilename); err == nil {
  36. NoticeClientUpgradeDownloaded(config.UpgradeDownloadFilename)
  37. return nil
  38. }
  39. partialFilename := fmt.Sprintf(
  40. "%s.%s.part", config.UpgradeDownloadFilename, clientUpgradeVersion)
  41. file, err := os.OpenFile(partialFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  42. if err != nil {
  43. return ContextError(err)
  44. }
  45. defer file.Close()
  46. fileInfo, err := file.Stat()
  47. if err != nil {
  48. return ContextError(err)
  49. }
  50. request, err := http.NewRequest("GET", config.UpgradeDownloadUrl, nil)
  51. if err != nil {
  52. return ContextError(err)
  53. }
  54. request.Header.Add("Range", fmt.Sprintf("bytes=%d-", fileInfo.Size()))
  55. tunneledDialer := func(_, addr string) (conn net.Conn, err error) {
  56. return tunnel.sshClient.Dial("tcp", addr)
  57. }
  58. transport := &http.Transport{
  59. Dial: tunneledDialer,
  60. ResponseHeaderTimeout: DOWNLOAD_UPGRADE_TIMEOUT,
  61. }
  62. httpClient := &http.Client{
  63. Transport: transport,
  64. Timeout: DOWNLOAD_UPGRADE_TIMEOUT,
  65. }
  66. response, err := httpClient.Do(request)
  67. if err != nil {
  68. return ContextError(err)
  69. }
  70. defer response.Body.Close()
  71. n, err := io.Copy(NewSyncFileWriter(file), response.Body)
  72. if err != nil {
  73. return ContextError(err)
  74. }
  75. NoticeInfo("client upgrade downloaded bytes: %d", n)
  76. // Ensure the file is flushed to disk. The deferred close
  77. // will be a noop when this succeeds.
  78. err = file.Close()
  79. if err != nil {
  80. return ContextError(err)
  81. }
  82. err = os.Rename(partialFilename, config.UpgradeDownloadFilename)
  83. if err != nil {
  84. return ContextError(err)
  85. }
  86. NoticeClientUpgradeDownloaded(config.UpgradeDownloadFilename)
  87. return nil
  88. }