2020-10-07 08:05:13 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
// Command stcrashreceiver is a trivial HTTP server that allows two things:
|
|
|
|
//
|
|
|
|
// - uploading files (crash reports) named like a SHA256 hash using a PUT request
|
|
|
|
// - checking whether such file exists using a HEAD request
|
|
|
|
//
|
|
|
|
// Typically this should be deployed behind something that manages HTTPS.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-07-27 19:27:52 +00:00
|
|
|
"path/filepath"
|
2020-10-07 08:05:13 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
|
|
|
"github.com/syncthing/syncthing/lib/ur"
|
|
|
|
|
|
|
|
raven "github.com/getsentry/raven-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxRequestSize = 1 << 20 // 1 MiB
|
|
|
|
|
|
|
|
func main() {
|
2021-07-27 19:27:52 +00:00
|
|
|
dir := flag.String("dir", ".", "Parent directory to store crash and failure reports in")
|
2020-10-07 08:05:13 +00:00
|
|
|
dsn := flag.String("dsn", "", "Sentry DSN")
|
|
|
|
listen := flag.String("listen", ":22039", "HTTP listen address")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
cr := &crashReceiver{
|
2021-07-27 19:27:52 +00:00
|
|
|
dir: filepath.Join(*dir, "crash_reports"),
|
2020-10-07 08:05:13 +00:00
|
|
|
dsn: *dsn,
|
|
|
|
}
|
|
|
|
mux.Handle("/", cr)
|
|
|
|
|
|
|
|
if *dsn != "" {
|
2021-07-27 19:27:52 +00:00
|
|
|
mux.HandleFunc("/newcrash/failure", handleFailureFn(*dsn, filepath.Join(*dir, "failure_reports")))
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
if err := http.ListenAndServe(*listen, mux); err != nil {
|
|
|
|
log.Fatalln("HTTP serve:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:27:52 +00:00
|
|
|
func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *http.Request) {
|
2020-10-07 08:05:13 +00:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
lr := io.LimitReader(req.Body, maxRequestSize)
|
|
|
|
bs, err := ioutil.ReadAll(lr)
|
|
|
|
req.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reports []ur.FailureReport
|
|
|
|
err = json.Unmarshal(bs, &reports)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(reports) == 0 {
|
|
|
|
// Shouldn't happen
|
2020-11-28 19:09:22 +00:00
|
|
|
log.Printf("Got zero failure reports")
|
2020-10-07 08:05:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
version, err := parseVersion(reports[0].Version)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, r := range reports {
|
2021-02-18 13:16:32 +00:00
|
|
|
pkt := packet(version, "failure")
|
2020-10-07 08:05:13 +00:00
|
|
|
pkt.Message = r.Description
|
|
|
|
pkt.Extra = raven.Extra{
|
|
|
|
"count": r.Count,
|
|
|
|
}
|
2021-07-27 19:27:52 +00:00
|
|
|
for k, v := range r.Extra {
|
|
|
|
pkt.Extra[k] = v
|
|
|
|
}
|
|
|
|
if len(r.Goroutines) != 0 {
|
|
|
|
url, err := saveFailureWithGoroutines(r.FailureData, failureDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Saving failure report:", err)
|
|
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pkt.Extra["goroutinesURL"] = url
|
|
|
|
}
|
2021-07-22 09:16:24 +00:00
|
|
|
message := sanitizeMessageLDB(r.Description)
|
|
|
|
pkt.Fingerprint = []string{message}
|
2020-10-07 08:05:13 +00:00
|
|
|
|
|
|
|
if err := sendReport(dsn, pkt, userIDFor(req)); err != nil {
|
2020-11-28 19:09:22 +00:00
|
|
|
log.Println("Failed to send failure report:", err)
|
|
|
|
} else {
|
|
|
|
log.Println("Sent failure report:", r.Description)
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:27:52 +00:00
|
|
|
func saveFailureWithGoroutines(data ur.FailureData, failureDir string) (string, error) {
|
|
|
|
bs := make([]byte, len(data.Description)+len(data.Goroutines))
|
|
|
|
copy(bs, data.Description)
|
|
|
|
copy(bs[len(data.Description):], data.Goroutines)
|
|
|
|
id := fmt.Sprintf("%x", sha256.Sum256(bs))
|
|
|
|
path := fullPathCompressed(failureDir, id)
|
|
|
|
err := compressAndWrite(bs, path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
2021-07-27 19:27:52 +00:00
|
|
|
return reportServer + path, nil
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|