upgrade_supported.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build !noupgrade
  16. package upgrade
  17. import (
  18. "archive/tar"
  19. "archive/zip"
  20. "bytes"
  21. "compress/gzip"
  22. "crypto/md5"
  23. "encoding/json"
  24. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "net/http"
  28. "os"
  29. "path"
  30. "path/filepath"
  31. "runtime"
  32. "strings"
  33. )
  34. // Returns the latest release, including prereleases or not depending on the argument
  35. func LatestRelease(prerelease bool) (Release, error) {
  36. resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10")
  37. if err != nil {
  38. return Release{}, err
  39. }
  40. if resp.StatusCode > 299 {
  41. return Release{}, fmt.Errorf("API call returned HTTP error: %s", resp.Status)
  42. }
  43. var rels []Release
  44. json.NewDecoder(resp.Body).Decode(&rels)
  45. resp.Body.Close()
  46. if len(rels) == 0 {
  47. return Release{}, ErrVersionUnknown
  48. }
  49. if prerelease {
  50. // We are a beta version. Use the latest.
  51. return rels[0], nil
  52. }
  53. // We are a regular release. Only consider non-prerelease versions for upgrade.
  54. for _, rel := range rels {
  55. if !rel.Prerelease {
  56. return rel, nil
  57. }
  58. }
  59. return Release{}, ErrVersionUnknown
  60. }
  61. // Upgrade to the given release, saving the previous binary with a ".old" extension.
  62. func upgradeTo(binary string, rel Release) error {
  63. expectedRelease := releaseName(rel.Tag)
  64. if debug {
  65. l.Debugf("expected release asset %q", expectedRelease)
  66. }
  67. for _, asset := range rel.Assets {
  68. assetName := path.Base(asset.Name)
  69. if debug {
  70. l.Debugln("considering release", assetName)
  71. }
  72. if strings.HasPrefix(assetName, expectedRelease) {
  73. return upgradeToURL(binary, asset.URL)
  74. }
  75. }
  76. return ErrVersionUnknown
  77. }
  78. // Upgrade to the given release, saving the previous binary with a ".old" extension.
  79. func upgradeToURL(binary string, url string) error {
  80. fname, err := readRelease(filepath.Dir(binary), url)
  81. if err != nil {
  82. return err
  83. }
  84. old := binary + ".old"
  85. _ = os.Remove(old)
  86. err = os.Rename(binary, old)
  87. if err != nil {
  88. return err
  89. }
  90. err = os.Rename(fname, binary)
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. func readRelease(dir, url string) (string, error) {
  97. if debug {
  98. l.Debugf("loading %q", url)
  99. }
  100. req, err := http.NewRequest("GET", url, nil)
  101. if err != nil {
  102. return "", err
  103. }
  104. req.Header.Add("Accept", "application/octet-stream")
  105. resp, err := http.DefaultClient.Do(req)
  106. if err != nil {
  107. return "", err
  108. }
  109. defer resp.Body.Close()
  110. switch runtime.GOOS {
  111. case "windows":
  112. return readZip(dir, resp.Body)
  113. default:
  114. return readTarGz(dir, resp.Body)
  115. }
  116. }
  117. func readTarGz(dir string, r io.Reader) (string, error) {
  118. gr, err := gzip.NewReader(r)
  119. if err != nil {
  120. return "", err
  121. }
  122. tr := tar.NewReader(gr)
  123. var tempName, actualMD5, expectedMD5 string
  124. // Iterate through the files in the archive.
  125. fileLoop:
  126. for {
  127. hdr, err := tr.Next()
  128. if err == io.EOF {
  129. // end of tar archive
  130. break
  131. }
  132. if err != nil {
  133. return "", err
  134. }
  135. shortName := path.Base(hdr.Name)
  136. if debug {
  137. l.Debugf("considering file %q", shortName)
  138. }
  139. switch shortName {
  140. case "syncthing":
  141. if debug {
  142. l.Debugln("writing and hashing binary")
  143. }
  144. tempName, actualMD5, err = writeBinary(dir, tr)
  145. if err != nil {
  146. return "", err
  147. }
  148. if expectedMD5 != "" {
  149. // We're done
  150. break fileLoop
  151. }
  152. case "syncthing.md5":
  153. bs, err := ioutil.ReadAll(tr)
  154. if err != nil {
  155. return "", err
  156. }
  157. expectedMD5 = strings.TrimSpace(string(bs))
  158. if debug {
  159. l.Debugln("expected md5 is", actualMD5)
  160. }
  161. if actualMD5 != "" {
  162. // We're done
  163. break fileLoop
  164. }
  165. }
  166. }
  167. if tempName != "" && actualMD5 != "" {
  168. // We found and saved something to disk.
  169. if expectedMD5 == "" {
  170. if debug {
  171. l.Debugln("there is no md5 to compare with")
  172. }
  173. } else if actualMD5 != expectedMD5 {
  174. // There was an md5 file included in the archive, and it doesn't
  175. // match what we just wrote to disk.
  176. return "", fmt.Errorf("incorrect MD5 checksum")
  177. }
  178. return tempName, nil
  179. }
  180. return "", fmt.Errorf("no upgrade found")
  181. }
  182. func readZip(dir string, r io.Reader) (string, error) {
  183. body, err := ioutil.ReadAll(r)
  184. if err != nil {
  185. return "", err
  186. }
  187. archive, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
  188. if err != nil {
  189. return "", err
  190. }
  191. var tempName, actualMD5, expectedMD5 string
  192. // Iterate through the files in the archive.
  193. fileLoop:
  194. for _, file := range archive.File {
  195. shortName := path.Base(file.Name)
  196. if debug {
  197. l.Debugf("considering file %q", shortName)
  198. }
  199. switch shortName {
  200. case "syncthing.exe":
  201. if debug {
  202. l.Debugln("writing and hashing binary")
  203. }
  204. inFile, err := file.Open()
  205. if err != nil {
  206. return "", err
  207. }
  208. tempName, actualMD5, err = writeBinary(dir, inFile)
  209. if err != nil {
  210. return "", err
  211. }
  212. if expectedMD5 != "" {
  213. // We're done
  214. break fileLoop
  215. }
  216. case "syncthing.exe.md5":
  217. inFile, err := file.Open()
  218. if err != nil {
  219. return "", err
  220. }
  221. bs, err := ioutil.ReadAll(inFile)
  222. if err != nil {
  223. return "", err
  224. }
  225. expectedMD5 = strings.TrimSpace(string(bs))
  226. if debug {
  227. l.Debugln("expected md5 is", actualMD5)
  228. }
  229. if actualMD5 != "" {
  230. // We're done
  231. break fileLoop
  232. }
  233. }
  234. }
  235. if tempName != "" && actualMD5 != "" {
  236. // We found and saved something to disk.
  237. if expectedMD5 == "" {
  238. if debug {
  239. l.Debugln("there is no md5 to compare with")
  240. }
  241. } else if actualMD5 != expectedMD5 {
  242. // There was an md5 file included in the archive, and it doesn't
  243. // match what we just wrote to disk.
  244. return "", fmt.Errorf("incorrect MD5 checksum")
  245. }
  246. return tempName, nil
  247. }
  248. return "", fmt.Errorf("No upgrade found")
  249. }
  250. func writeBinary(dir string, inFile io.Reader) (filename, md5sum string, err error) {
  251. outFile, err := ioutil.TempFile(dir, "syncthing")
  252. if err != nil {
  253. return "", "", err
  254. }
  255. // Write the binary both a temporary file and to the MD5 hasher.
  256. h := md5.New()
  257. mw := io.MultiWriter(h, outFile)
  258. _, err = io.Copy(mw, inFile)
  259. if err != nil {
  260. os.Remove(outFile.Name())
  261. return "", "", err
  262. }
  263. err = outFile.Close()
  264. if err != nil {
  265. os.Remove(outFile.Name())
  266. return "", "", err
  267. }
  268. err = os.Chmod(outFile.Name(), os.FileMode(0755))
  269. if err != nil {
  270. os.Remove(outFile.Name())
  271. return "", "", err
  272. }
  273. actualMD5 := fmt.Sprintf("%x", h.Sum(nil))
  274. if debug {
  275. l.Debugln("actual md5 is", actualMD5)
  276. }
  277. return outFile.Name(), actualMD5, nil
  278. }