forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
124 lines (111 loc) · 2.92 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"bytes"
"fmt"
"net/url"
"os"
"text/template"
"time"
)
var errPageRawTmpl = `<!DOCTYPE html>
<html>
<head> <title>Proxy error</title> </head>
<body>
<h1>[Error] {{.H1}}</h1>
{{.Msg}}
{{.Form}}
<hr />
Generated by <i>cow-proxy</i> at {{.T}}
</body>
</html>
`
var blockedFormRawTmpl = `<p> </p>
<form action="http://{{.ListenAddr}}/blocked/{{.RequestURI}}" method="get">
<input type="submit" name="retry" value="Retry">
<input type="submit" name="add" value="Add {{.Domain}} to blocked sites">
</form>
`
// Do not end with "\r\n" so we can add more header later
var headRawTmpl = "HTTP/1.1 {{.CodeReason}}\r\n" +
"Connection: keep-alive\r\n" +
"Cache-Control: no-cache\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: {{.Length}}\r\n"
var errPageTmpl, headTmpl, blockedFormTmpl *template.Template
func init() {
var err error
if headTmpl, err = template.New("errorHead").Parse(headRawTmpl); err != nil {
fmt.Println("Internal error on generating error head template")
os.Exit(1)
}
if errPageTmpl, err = template.New("errorPage").Parse(errPageRawTmpl); err != nil {
fmt.Println("Internal error on generating error page template")
os.Exit(1)
}
if blockedFormTmpl, err = template.New("blockedForm").Parse(blockedFormRawTmpl); err != nil {
fmt.Println("Internal error on generating blocked form template")
os.Exit(1)
}
}
func genErrorPage(errMsg, detailedMsg, form string) (string, error) {
var err error
data := struct {
H1 string
Msg string
Form string
T string
}{
errMsg,
detailedMsg,
form,
time.Now().Format(time.ANSIC),
}
buf := new(bytes.Buffer)
err = errPageTmpl.Execute(buf, data)
return buf.String(), err
}
func sendErrorPageGeneric(w *bufio.Writer, errCodeReason, errMsg, detailedMsg, form string) {
page, err := genErrorPage(errMsg, detailedMsg, form)
if err != nil {
errl.Println("Error generating error page:", err)
return
}
data := struct {
CodeReason string
Length int
}{
errCodeReason,
len(page),
}
buf := new(bytes.Buffer)
if err := headTmpl.Execute(buf, data); err != nil {
errl.Println("Error generating error page header:", err)
return
}
w.WriteString(buf.String())
w.WriteString("\r\n")
w.WriteString(page)
w.Flush()
}
func sendErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string) {
sendErrorPageGeneric(w, errCodeReason, errMsg, detailedMsg, "")
}
func sendBlockedErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string, r *Request) {
data := struct {
ListenAddr string
RequestURI string
Domain string
}{
config.listenAddr,
url.QueryEscape(r.toURI()), // escape URI to put it in request url
requestDomain(r),
}
buf := new(bytes.Buffer)
if err := blockedFormTmpl.Execute(buf, data); err != nil {
errl.Println("Error generating blocked form:", err)
return
}
sendErrorPageGeneric(w, errCodeReason, errMsg, detailedMsg, buf.String())
}