2017-01-25 18:27:07 +01:00
|
|
|
// Run a web server capable of dumping bug reports sent by Riot.
|
|
|
|
// Requires Go 1.5+
|
|
|
|
// Usage: go run rageshake.go PORT
|
|
|
|
// Example: go run rageshake.go 8080
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-26 12:28:38 +01:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2017-01-25 18:27:07 +01:00
|
|
|
"encoding/json"
|
2017-01-26 12:44:07 +01:00
|
|
|
"fmt"
|
2017-01-26 12:28:38 +01:00
|
|
|
"io/ioutil"
|
2017-01-25 18:27:07 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-01-26 12:28:38 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-01-25 18:27:07 +01:00
|
|
|
)
|
|
|
|
|
2017-01-26 12:28:38 +01:00
|
|
|
var maxPayloadSize = 1024 * 1024 * 55 // 55 MB
|
|
|
|
|
2017-01-25 18:27:07 +01:00
|
|
|
type LogEntry struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Lines string `json:"lines"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Payload struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
UserAgent string `json:"user_agent"`
|
|
|
|
Logs []LogEntry `json:"logs"`
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:28:38 +01:00
|
|
|
func respond(code int, w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gzipAndSave(data []byte, filepath string) error {
|
2017-01-26 12:57:56 +01:00
|
|
|
if _, err := os.Stat(filepath); err == nil {
|
|
|
|
return fmt.Errorf("file already exists") // the user can just retry
|
|
|
|
}
|
2017-01-26 12:28:38 +01:00
|
|
|
var b bytes.Buffer
|
|
|
|
gz := gzip.NewWriter(&b)
|
|
|
|
if _, err := gz.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gz.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gz.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath, b.Bytes(), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-25 18:27:07 +01:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != "POST" && req.Method != "OPTIONS" {
|
2017-01-26 12:28:38 +01:00
|
|
|
respond(405, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Set CORS
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
|
|
|
|
if req.Method == "OPTIONS" {
|
|
|
|
respond(200, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if length, err := strconv.Atoi(req.Header.Get("Content-Length")); err != nil || length > maxPayloadSize {
|
|
|
|
respond(413, w)
|
2017-01-25 18:27:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var p Payload
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
|
2017-01-26 12:28:38 +01:00
|
|
|
respond(400, w)
|
2017-01-25 18:27:07 +01:00
|
|
|
return
|
|
|
|
}
|
2017-01-26 12:28:38 +01:00
|
|
|
// Dump bug report to disk as form:
|
|
|
|
// "bugreport-20170115-112233.log.gz" => user text, version, user agent, # logs
|
|
|
|
// "bugreport-20170115-112233-0.log.gz" => most recent log
|
|
|
|
// "bugreport-20170115-112233-1.log.gz" => ...
|
|
|
|
// "bugreport-20170115-112233-N.log.gz" => oldest log
|
|
|
|
t := time.Now()
|
|
|
|
prefix := t.Format("bugreport-20060102-150405")
|
2017-01-26 12:44:07 +01:00
|
|
|
summary := fmt.Sprintf(
|
|
|
|
"%s\n\nNumber of logs: %d\nVersion: %s\nUser-Agent: %s\n", p.Text, len(p.Logs), p.Version, p.UserAgent,
|
|
|
|
)
|
|
|
|
if err := gzipAndSave([]byte(summary), prefix+".log.gz"); err != nil {
|
2017-01-26 12:28:38 +01:00
|
|
|
respond(500, w)
|
|
|
|
return
|
|
|
|
}
|
2017-01-26 12:44:07 +01:00
|
|
|
for i, log := range p.Logs {
|
|
|
|
if err := gzipAndSave([]byte(log.Lines), fmt.Sprintf("%s-%d.log.gz", prefix, i)); err != nil {
|
|
|
|
respond(500, w)
|
|
|
|
return // TODO: Rollback?
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 12:28:38 +01:00
|
|
|
respond(200, w)
|
2017-01-25 18:27:07 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
port := os.Args[1]
|
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
|
|
}
|