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 (
|
2023-01-23 07:38:55 +00:00
|
|
|
"context"
|
2020-10-07 08:05:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-07-27 19:27:52 +00:00
|
|
|
"path/filepath"
|
2023-01-23 07:38:55 +00:00
|
|
|
"time"
|
2020-10-07 08:05:13 +00:00
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
"github.com/alecthomas/kong"
|
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
|
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
type cli struct {
|
|
|
|
Dir string `help:"Parent directory to store crash and failure reports in" env:"REPORTS_DIR" default:"."`
|
|
|
|
DSN string `help:"Sentry DSN" env:"SENTRY_DSN"`
|
|
|
|
Listen string `help:"HTTP listen address" default:":8080" env:"LISTEN_ADDRESS"`
|
|
|
|
MaxDiskFiles int `help:"Maximum number of reports on disk" default:"100000" env:"MAX_DISK_FILES"`
|
|
|
|
MaxDiskSizeMB int64 `help:"Maximum disk space to use for reports" default:"1024" env:"MAX_DISK_SIZE_MB"`
|
|
|
|
CleanInterval time.Duration `help:"Interval between cleaning up old reports" default:"12h" env:"CLEAN_INTERVAL"`
|
|
|
|
SentryQueue int `help:"Maximum number of reports to queue for sending to Sentry" default:"64" env:"SENTRY_QUEUE"`
|
|
|
|
DiskQueue int `help:"Maximum number of reports to queue for writing to disk" default:"64" env:"DISK_QUEUE"`
|
|
|
|
}
|
|
|
|
|
2020-10-07 08:05:13 +00:00
|
|
|
func main() {
|
2023-01-23 07:38:55 +00:00
|
|
|
var params cli
|
|
|
|
kong.Parse(¶ms)
|
2020-10-07 08:05:13 +00:00
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
ds := &diskStore{
|
|
|
|
dir: filepath.Join(params.Dir, "crash_reports"),
|
|
|
|
inbox: make(chan diskEntry, params.DiskQueue),
|
|
|
|
maxFiles: params.MaxDiskFiles,
|
|
|
|
maxBytes: params.MaxDiskSizeMB << 20,
|
|
|
|
}
|
|
|
|
go ds.Serve(context.Background())
|
|
|
|
|
|
|
|
ss := &sentryService{
|
|
|
|
dsn: params.DSN,
|
|
|
|
inbox: make(chan sentryRequest, params.SentryQueue),
|
|
|
|
}
|
|
|
|
go ss.Serve(context.Background())
|
|
|
|
|
2020-10-07 08:05:13 +00:00
|
|
|
cr := &crashReceiver{
|
2023-01-23 07:38:55 +00:00
|
|
|
store: ds,
|
|
|
|
sentry: ss,
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
2023-01-23 07:38:55 +00:00
|
|
|
|
2020-10-07 08:05:13 +00:00
|
|
|
mux.Handle("/", cr)
|
2023-07-01 05:52:46 +00:00
|
|
|
mux.HandleFunc("/ping", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte("OK"))
|
|
|
|
})
|
2020-10-07 08:05:13 +00:00
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
if params.DSN != "" {
|
|
|
|
mux.HandleFunc("/newcrash/failure", handleFailureFn(params.DSN, filepath.Join(params.Dir, "failure_reports")))
|
2020-10-07 08:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(os.Stdout)
|
2023-01-23 07:38:55 +00:00
|
|
|
if err := http.ListenAndServe(params.Listen, mux); err != nil {
|
2020-10-07 08:05:13 +00:00
|
|
|
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)
|
2021-11-22 07:59:47 +00:00
|
|
|
bs, err := io.ReadAll(lr)
|
2020-10-07 08:05:13 +00:00
|
|
|
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
|
|
|
|
}
|
2022-07-28 14:51:03 +00:00
|
|
|
if r.Goroutines != "" {
|
2021-07-27 19:27:52 +00:00
|
|
|
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
|
|
|
}
|