/
error.go
83 lines (73 loc) · 2.47 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/json"
"fmt"
"html"
"html/template"
"log"
"net/http"
"runtime/debug"
"strings"
"github.com/adamyi/CTFProxy/infra/ctfproxy/defaultfiles"
"github.com/adamyi/CTFProxy/infra/ctfproxy/templates"
)
type CPError struct {
Title string
Description string
PublicDebug string
InternalDebug string
Type string
Debug template.HTML
Code int
}
var errorTemplate *template.Template
func init() {
errorTemplate = template.Must(template.New("error").Parse(templates.Data["error.html"]))
}
func (e *CPError) SetType(t string) {
e.Type = t
}
func NewCPError(code int, title, description, publicDebug, internalDebug string) *CPError {
ret := &CPError{Code: code, Title: title, Description: description, PublicDebug: publicDebug, InternalDebug: internalDebug}
ret.InternalDebug += "\n\n===CTFProxy Stack Trace===\n" + string(debug.Stack())
ret.Type = "cp"
return ret
}
func (e CPError) Write(w http.ResponseWriter, r *http.Request) {
if e.Code >= 300 && e.Code < 400 {
w.Header().Add("Location", e.Description)
} else if e.Code == 404 && len(defaultfiles.Data[r.URL.Path[1:]]) > 0 {
w.Write(defaultfiles.Data[r.URL.Path[1:]])
return
}
w.WriteHeader(e.Code)
dbgstr := e.PublicDebug
if w.Header().Get("X-CTFProxy-I-Debug") == "1" {
dbgstr += "\n===Internal Debug Info because you're in ctfproxy-debug@===\n" + e.InternalDebug
}
e.Debug = template.HTML(strings.Replace(html.EscapeString(dbgstr), "\n", "<br>\n", -1))
errorTemplate.Execute(w, e)
}
func WrapHandlerWithRecovery(wrappedHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err != nil {
log.Println("[PANIC RECOVERY TRIGGERED] something terrible happened.")
log.Println(err)
NewCPError(http.StatusInternalServerError, "Server Internal Error", "Something went terribly wrong...", "", fmt.Sprintf("===panic recovery===\n%v", err)).Write(w, r)
}
}()
wrappedHandler.ServeHTTP(w, r)
})
}
func handleUpstreamCPError(rw http.ResponseWriter, resp *http.Response, req *http.Request) {
defer resp.Body.Close()
var e CPError
err := json.NewDecoder(resp.Body).Decode(&e)
if err != nil {
e = *NewCPError(http.StatusInternalServerError, "Server Internal Error", "Something went wrong with the service, please try again later", "", "upstream service returned ctfproxy/uperror but failed to decode CPError json "+err.Error())
}
e.SetType("s")
e.Write(rw, req)
}