-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
190 lines (175 loc) · 4.72 KB
/
handler.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package npad
import (
"net/http"
"strings"
"paepcke.de/logsec"
"paepcke.de/npad/compress"
)
//
// MUX HANDLER FUNCTIONS
//
const (
_ua = "User-Agent"
_utf8 = "text/html;charset=utf-8"
_txt = "text/plain"
_svg = "image/svg+xml"
_ctype = "Content-Type"
_title = "Title"
_err_syntax = "[syntax] ["
_err_plain = "[plain] ["
)
func _headPlain(r http.ResponseWriter) http.ResponseWriter {
r.Header().Set(_ctype, _txt)
r.Header().Set(_title, c.App)
return r
}
func _headHTML(r http.ResponseWriter) http.ResponseWriter {
r.Header().Set(_ctype, _utf8)
r.Header().Set(_title, c.App)
return r
}
func _headSVG(r http.ResponseWriter) http.ResponseWriter {
r.Header().Set(_ctype, _svg)
r.Header().Set(_title, c.App)
return r
}
// plain text display
func getPlainHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
var err error
page := ""
switch {
case strings.Contains(strings.Join(q.Header[_ua], " "), "curl"):
r = _headPlain(r)
page, err = getPlainText(q.URL.Path[c.i.plainOFFSET:])
default:
r = _headHTML(r)
page, err = getPlainHTML(q.URL.Path[c.i.plainOFFSET:])
}
if err != nil {
logsec.LogErr <- _err_plain + err.Error() + "]"
http.NotFound(r, q)
return
}
compress.WriteTransportCompressedPage(page, r, q, true)
}
return http.HandlerFunc(h)
}
// syntax display
func getMagicHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
r = _headHTML(r)
page, err := getMagicHTML(q.URL.Path[c.i.magicOFFSET:])
if err != nil {
logsec.LogErr <- _err_syntax + err.Error() + "]"
http.NotFound(r, q)
return
}
compress.WriteTransportCompressedPage(page, r, q, true)
}
return http.HandlerFunc(h)
}
// qr code
func getQRHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
r = _headHTML(r)
page, err := getQRHTML(q.URL.Path[c.i.qrOFFSET:], q.URL)
if err != nil {
logsec.LogErr <- "[qr] [" + err.Error() + "]"
http.NotFound(r, q)
return
}
compress.WriteTransportCompressedPage(page, r, q, true)
}
return http.HandlerFunc(h)
}
// raw download
func getDownloadHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
page, err := readPaste(q.URL.Path[c.i.downloadOFFSET:], true)
if err != nil {
logsec.LogErr <- err.Error()
http.NotFound(r, q)
return
}
name := q.URL.Path[c.i.downloadOFFSET+1:]
s := strings.Split(name, "@")
if len(s) == 3 {
name = s[2]
}
r.Header().Set("Content-disposition", "attachment; filename="+name+compress.GetFileExtension(c.Calgo))
compress.WriteTransportCompressedPage(page, r, q, false)
}
return http.HandlerFunc(h)
}
// input ["start"] page
func getStartHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
r = _headHTML(r)
switch q.Method {
case "GET":
compress.WriteTransportCompressedPage(getStartHTML(), r, q, true)
case "POST":
err := q.ParseForm()
if err != nil {
logsec.LogErr <- err.Error() // blocks in case of global [ddos|err rate limit]
internalServerError(r)
return
}
expire, err := atoi(q.FormValue("ex"))
if err != nil {
logsec.LogInfo <- "[new] [parse form data] [invalid expire option] " + err.Error()
internalServerError(r)
return
}
newKey, err := savePaste(q.FormValue("pa"), q.FormValue("na"), expire)
if err != nil {
logsec.LogInfo <- "[new] [save paste] " + err.Error()
internalServerError(r)
return
}
logsec.LogInfo <- "[new] " + newKey // optional log info event
http.Redirect(r, q, _plain+newKey, http.StatusFound)
default:
inf := "Error: Method Not Allowed (405) [" + q.Method + "]"
logsec.LogErr <- "[handler] [/] [" + inf + "]"
http.Error(r, inf, http.StatusMethodNotAllowed)
}
}
return http.HandlerFunc(h)
}
// client connection diagnosis
func getDiagHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
var err error
page := ""
switch {
case strings.Contains(strings.Join(q.Header[_ua], " "), "curl"):
r = _headPlain(r)
page, err = getDiagText(q)
default:
_headHTML(r)
page, err = getDiagHTML(q)
}
if err != nil {
logsec.LogErr <- "[handler] [/diag] [" + err.Error() + "]"
}
compress.WriteTransportCompressedPage(page, r, q, true)
}
return http.HandlerFunc(h)
}
// app source code forwarder
func getSourceCodeHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
http.Redirect(r, q, "https://"+gorepo, http.StatusTemporaryRedirect)
}
return http.HandlerFunc(h)
}
// favicon [shared target cross all pages]
func getFavIconHandler() http.Handler {
h := func(r http.ResponseWriter, q *http.Request) {
r = _headSVG(r)
compress.WriteTransportCompressedPage(_icon, r, q, true)
}
return http.HandlerFunc(h)
}