2017-01-25 20:27:07 +03:00
|
|
|
// Run a web server capable of dumping bug reports sent by Riot.
|
|
|
|
// Requires Go 1.5+
|
2017-02-08 12:59:41 +03:00
|
|
|
// Usage: BUGS_USER=user BUGS_PASS=password go run rageshake.go PORT
|
|
|
|
// Example: BUGS_USER=alice BUGS_PASS=secret go run rageshake.go 8080
|
2017-01-25 20:27:07 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-26 14:28:38 +03:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2017-02-08 12:59:41 +03:00
|
|
|
"crypto/subtle"
|
2017-01-25 20:27:07 +03:00
|
|
|
"encoding/json"
|
2017-01-26 14:44:07 +03:00
|
|
|
"fmt"
|
2017-01-26 14:28:38 +03:00
|
|
|
"io/ioutil"
|
2017-01-25 20:27:07 +03:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-02-06 18:31:53 +03:00
|
|
|
"path/filepath"
|
2017-01-26 14:28:38 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-01-25 20:27:07 +03:00
|
|
|
)
|
|
|
|
|
2017-01-26 14:28:38 +03:00
|
|
|
var maxPayloadSize = 1024 * 1024 * 55 // 55 MB
|
|
|
|
|
2017-01-25 20:27:07 +03: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 14:28:38 +03:00
|
|
|
func respond(code int, w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:18:08 +03:00
|
|
|
func gzipAndSave(data []byte, dirname, fpath string) error {
|
2017-02-07 19:04:06 +03:00
|
|
|
_ = os.MkdirAll(filepath.Join("bugs", dirname), os.ModePerm)
|
2017-02-06 19:18:08 +03:00
|
|
|
fpath = filepath.Join("bugs", dirname, fpath)
|
2017-02-06 18:31:53 +03:00
|
|
|
|
|
|
|
if _, err := os.Stat(fpath); err == nil {
|
2017-01-26 14:57:56 +03:00
|
|
|
return fmt.Errorf("file already exists") // the user can just retry
|
|
|
|
}
|
2017-01-26 14:28:38 +03: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
|
|
|
|
}
|
2017-02-06 18:31:53 +03:00
|
|
|
if err := ioutil.WriteFile(fpath, b.Bytes(), 0644); err != nil {
|
2017-01-26 14:28:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:59:41 +03:00
|
|
|
func basicAuth(handler http.Handler, username, password, realm string) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, pass, ok := r.BasicAuth() // pull creds from the request
|
|
|
|
|
|
|
|
// check user and pass securely
|
|
|
|
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
|
w.WriteHeader(401)
|
|
|
|
w.Write([]byte("Unauthorised.\n"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-25 20:27:07 +03:00
|
|
|
func main() {
|
2017-02-06 18:31:53 +03:00
|
|
|
http.HandleFunc("/api/submit", func(w http.ResponseWriter, req *http.Request) {
|
2017-01-25 20:27:07 +03:00
|
|
|
if req.Method != "POST" && req.Method != "OPTIONS" {
|
2017-01-26 14:28:38 +03: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 20:27:07 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var p Payload
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
|
2017-01-26 14:28:38 +03:00
|
|
|
respond(400, w)
|
2017-01-25 20:27:07 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-26 14:28:38 +03: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
|
2017-01-26 15:17:47 +03:00
|
|
|
t := time.Now().UTC()
|
2017-02-07 19:04:06 +03:00
|
|
|
prefix := t.Format("2006-01-02/150405")
|
2017-01-26 14:44:07 +03: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,
|
|
|
|
)
|
2017-02-06 19:18:08 +03:00
|
|
|
if err := gzipAndSave([]byte(summary), prefix, "details.log.gz"); err != nil {
|
2017-01-26 14:28:38 +03:00
|
|
|
respond(500, w)
|
|
|
|
return
|
|
|
|
}
|
2017-01-26 14:44:07 +03:00
|
|
|
for i, log := range p.Logs {
|
2017-02-06 19:18:08 +03:00
|
|
|
if err := gzipAndSave([]byte(log.Lines), prefix, fmt.Sprintf("logs-%d.log.gz", i)); err != nil {
|
2017-01-26 14:44:07 +03:00
|
|
|
respond(500, w)
|
|
|
|
return // TODO: Rollback?
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 14:28:38 +03:00
|
|
|
respond(200, w)
|
2017-01-25 20:27:07 +03:00
|
|
|
})
|
|
|
|
|
2017-02-07 19:04:06 +03:00
|
|
|
// Make sure bugs directory exists
|
|
|
|
_ = os.Mkdir("bugs", os.ModePerm)
|
|
|
|
|
2017-02-08 12:59:41 +03:00
|
|
|
// serve files under "bugs"
|
|
|
|
fs := http.FileServer(http.Dir("bugs"))
|
|
|
|
fs = http.StripPrefix("/api/listing/", fs)
|
|
|
|
|
|
|
|
// set auth if env vars exist
|
|
|
|
usr := os.Getenv("BUGS_USER")
|
|
|
|
pass := os.Getenv("BUGS_PASS")
|
|
|
|
if usr == "" || pass == "" {
|
|
|
|
fmt.Println("BUGS_USER and BUGS_PASS env vars not found. No authentication is running for /api/listing")
|
|
|
|
} else {
|
2017-02-08 13:36:39 +03:00
|
|
|
fs = basicAuth(fs, usr, pass, "Riot bug reports")
|
2017-02-08 12:59:41 +03:00
|
|
|
}
|
|
|
|
http.Handle("/api/listing/", fs)
|
|
|
|
|
2017-01-25 20:27:07 +03:00
|
|
|
port := os.Args[1]
|
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
|
|
}
|