upgrade_supported.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. fname, err := readRelease(filepath.Dir(binary), asset.URL)
  74. if err != nil {
  75. return err
  76. }
  77. old := binary + ".old"
  78. _ = os.Remove(old)
  79. err = os.Rename(binary, old)
  80. if err != nil {
  81. return err
  82. }
  83. err = os.Rename(fname, binary)
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. }
  90. return ErrVersionUnknown
  91. }
  92. func readRelease(dir, url string) (string, error) {
  93. if debug {
  94. l.Debugf("loading %q", url)
  95. }
  96. req, err := http.NewRequest("GET", url, nil)
  97. if err != nil {
  98. return "", err
  99. }
  100. req.Header.Add("Accept", "application/octet-stream")
  101. resp, err := http.DefaultClient.Do(req)
  102. if err != nil {
  103. return "", err
  104. }
  105. defer resp.Body.Close()
  106. switch runtime.GOOS {
  107. case "windows":
  108. return readZip(dir, resp.Body)
  109. default:
  110. return readTarGz(dir, resp.Body)
  111. }
  112. }
  113. func readTarGz(dir string, r io.Reader) (string, error) {
  114. gr, err := gzip.NewReader(r)
  115. if err != nil {
  116. return "", err
  117. }
  118. tr := tar.NewReader(gr)
  119. var tempName, actualMD5, expectedMD5 string
  120. // Iterate through the files in the archive.
  121. fileLoop:
  122. for {
  123. hdr, err := tr.Next()
  124. if err == io.EOF {
  125. // end of tar archive
  126. break
  127. }
  128. if err != nil {
  129. return "", err
  130. }
  131. shortName := path.Base(hdr.Name)
  132. if debug {
  133. l.Debugf("considering file %q", shortName)
  134. }
  135. switch shortName {
  136. case "syncthing":
  137. if debug {
  138. l.Debugln("writing and hashing binary")
  139. }
  140. tempName, actualMD5, err = writeBinary(dir, tr)
  141. if err != nil {
  142. return "", err
  143. }
  144. if expectedMD5 != "" {
  145. // We're done
  146. break fileLoop
  147. }
  148. case "syncthing.md5":
  149. bs, err := ioutil.ReadAll(tr)
  150. if err != nil {
  151. return "", err
  152. }
  153. expectedMD5 = strings.TrimSpace(string(bs))
  154. if debug {
  155. l.Debugln("expected md5 is", actualMD5)
  156. }
  157. if actualMD5 != "" {
  158. // We're done
  159. break fileLoop
  160. }
  161. }
  162. }
  163. if tempName != "" && actualMD5 != "" {
  164. // We found and saved something to disk.
  165. if expectedMD5 == "" {
  166. if debug {
  167. l.Debugln("there is no md5 to compare with")
  168. }
  169. } else if actualMD5 != expectedMD5 {
  170. // There was an md5 file included in the archive, and it doesn't
  171. // match what we just wrote to disk.
  172. return "", fmt.Errorf("incorrect MD5 checksum")
  173. }
  174. return tempName, nil
  175. }
  176. return "", fmt.Errorf("no upgrade found")
  177. }
  178. func readZip(dir string, r io.Reader) (string, error) {
  179. body, err := ioutil.ReadAll(r)
  180. if err != nil {
  181. return "", err
  182. }
  183. archive, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
  184. if err != nil {
  185. return "", err
  186. }
  187. var tempName, actualMD5, expectedMD5 string
  188. // Iterate through the files in the archive.
  189. fileLoop:
  190. for _, file := range archive.File {
  191. shortName := path.Base(file.Name)
  192. if debug {
  193. l.Debugf("considering file %q", shortName)
  194. }
  195. switch shortName {
  196. case "syncthing.exe":
  197. if debug {
  198. l.Debugln("writing and hashing binary")
  199. }
  200. inFile, err := file.Open()
  201. if err != nil {
  202. return "", err
  203. }
  204. tempName, actualMD5, err = writeBinary(dir, inFile)
  205. if err != nil {
  206. return "", err
  207. }
  208. if expectedMD5 != "" {
  209. // We're done
  210. break fileLoop
  211. }
  212. case "syncthing.exe.md5":
  213. inFile, err := file.Open()
  214. if err != nil {
  215. return "", err
  216. }
  217. bs, err := ioutil.ReadAll(inFile)
  218. if err != nil {
  219. return "", err
  220. }
  221. expectedMD5 = strings.TrimSpace(string(bs))
  222. if debug {
  223. l.Debugln("expected md5 is", actualMD5)
  224. }
  225. if actualMD5 != "" {
  226. // We're done
  227. break fileLoop
  228. }
  229. }
  230. }
  231. if tempName != "" && actualMD5 != "" {
  232. // We found and saved something to disk.
  233. if expectedMD5 == "" {
  234. if debug {
  235. l.Debugln("there is no md5 to compare with")
  236. }
  237. } else if actualMD5 != expectedMD5 {
  238. // There was an md5 file included in the archive, and it doesn't
  239. // match what we just wrote to disk.
  240. return "", fmt.Errorf("incorrect MD5 checksum")
  241. }
  242. return tempName, nil
  243. }
  244. return "", fmt.Errorf("No upgrade found")
  245. }
  246. func writeBinary(dir string, inFile io.Reader) (filename, md5sum string, err error) {
  247. outFile, err := ioutil.TempFile(dir, "syncthing")
  248. if err != nil {
  249. return "", "", err
  250. }
  251. // Write the binary both a temporary file and to the MD5 hasher.
  252. h := md5.New()
  253. mw := io.MultiWriter(h, outFile)
  254. _, err = io.Copy(mw, inFile)
  255. if err != nil {
  256. os.Remove(outFile.Name())
  257. return "", "", err
  258. }
  259. err = outFile.Close()
  260. if err != nil {
  261. os.Remove(outFile.Name())
  262. return "", "", err
  263. }
  264. err = os.Chmod(outFile.Name(), os.FileMode(0755))
  265. if err != nil {
  266. os.Remove(outFile.Name())
  267. return "", "", err
  268. }
  269. actualMD5 := fmt.Sprintf("%x", h.Sum(nil))
  270. if debug {
  271. l.Debugln("actual md5 is", actualMD5)
  272. }
  273. return outFile.Name(), actualMD5, nil
  274. }