main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // Command stcrashreceiver is a trivial HTTP server that allows two things:
  7. //
  8. // - uploading files (crash reports) named like a SHA256 hash using a PUT request
  9. // - checking whether such file exists using a HEAD request
  10. //
  11. // Typically this should be deployed behind something that manages HTTPS.
  12. package main
  13. import (
  14. "encoding/json"
  15. "flag"
  16. "fmt"
  17. "io"
  18. "log"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "github.com/syncthing/syncthing/lib/sha256"
  23. "github.com/syncthing/syncthing/lib/ur"
  24. raven "github.com/getsentry/raven-go"
  25. )
  26. const maxRequestSize = 1 << 20 // 1 MiB
  27. func main() {
  28. dir := flag.String("dir", ".", "Parent directory to store crash and failure reports in")
  29. dsn := flag.String("dsn", "", "Sentry DSN")
  30. listen := flag.String("listen", ":22039", "HTTP listen address")
  31. flag.Parse()
  32. mux := http.NewServeMux()
  33. cr := &crashReceiver{
  34. dir: filepath.Join(*dir, "crash_reports"),
  35. dsn: *dsn,
  36. }
  37. mux.Handle("/", cr)
  38. if *dsn != "" {
  39. mux.HandleFunc("/newcrash/failure", handleFailureFn(*dsn, filepath.Join(*dir, "failure_reports")))
  40. }
  41. log.SetOutput(os.Stdout)
  42. if err := http.ListenAndServe(*listen, mux); err != nil {
  43. log.Fatalln("HTTP serve:", err)
  44. }
  45. }
  46. func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *http.Request) {
  47. return func(w http.ResponseWriter, req *http.Request) {
  48. lr := io.LimitReader(req.Body, maxRequestSize)
  49. bs, err := io.ReadAll(lr)
  50. req.Body.Close()
  51. if err != nil {
  52. http.Error(w, err.Error(), 500)
  53. return
  54. }
  55. var reports []ur.FailureReport
  56. err = json.Unmarshal(bs, &reports)
  57. if err != nil {
  58. http.Error(w, err.Error(), 400)
  59. return
  60. }
  61. if len(reports) == 0 {
  62. // Shouldn't happen
  63. log.Printf("Got zero failure reports")
  64. return
  65. }
  66. version, err := parseVersion(reports[0].Version)
  67. if err != nil {
  68. http.Error(w, err.Error(), 400)
  69. return
  70. }
  71. for _, r := range reports {
  72. pkt := packet(version, "failure")
  73. pkt.Message = r.Description
  74. pkt.Extra = raven.Extra{
  75. "count": r.Count,
  76. }
  77. for k, v := range r.Extra {
  78. pkt.Extra[k] = v
  79. }
  80. if r.Goroutines != "" {
  81. url, err := saveFailureWithGoroutines(r.FailureData, failureDir)
  82. if err != nil {
  83. log.Println("Saving failure report:", err)
  84. http.Error(w, "Internal server error", http.StatusInternalServerError)
  85. return
  86. }
  87. pkt.Extra["goroutinesURL"] = url
  88. }
  89. message := sanitizeMessageLDB(r.Description)
  90. pkt.Fingerprint = []string{message}
  91. if err := sendReport(dsn, pkt, userIDFor(req)); err != nil {
  92. log.Println("Failed to send failure report:", err)
  93. } else {
  94. log.Println("Sent failure report:", r.Description)
  95. }
  96. }
  97. }
  98. }
  99. func saveFailureWithGoroutines(data ur.FailureData, failureDir string) (string, error) {
  100. bs := make([]byte, len(data.Description)+len(data.Goroutines))
  101. copy(bs, data.Description)
  102. copy(bs[len(data.Description):], data.Goroutines)
  103. id := fmt.Sprintf("%x", sha256.Sum256(bs))
  104. path := fullPathCompressed(failureDir, id)
  105. err := compressAndWrite(bs, path)
  106. if err != nil {
  107. return "", err
  108. }
  109. return reportServer + path, nil
  110. }