-
Notifications
You must be signed in to change notification settings - Fork 13
/
serve.go
227 lines (199 loc) · 5.85 KB
/
serve.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package mailout
import (
"encoding/json"
"fmt"
"image/color"
"net/http"
"net/url"
"strings"
"github.com/SchumacherFM/mailout/bufpool"
"github.com/gorilla/sessions"
"github.com/juju/ratelimit"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/quasoft/memstore"
"github.com/steambap/captcha"
)
// StatusUnprocessableEntity gets returned whenever parsing of the form fails.
const StatusUnprocessableEntity = 422
// StatusEmpty returned by mailout middleware because the proper status gets
// written previously
const StatusEmpty = 0
const (
headerContentType = "Content-Type"
headerApplicationJSONUTF8 = "application/json; charset=utf-8"
headerPNG = "image/png"
)
type ReCaptchaResp struct {
Success bool `json:"success"`
ChallengeTs string `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
func newHandler(mc *config, mailPipe chan<- *http.Request) *handler {
return &handler{
rlBucket: ratelimit.NewBucket(mc.rateLimitInterval, mc.rateLimitCapacity),
reqPipe: mailPipe,
config: mc,
memStore: memstore.NewMemStore(
[]byte("authkey123"),
[]byte("40Rf16fa4d0ba972048{40639e8012?a"),
),
}
}
type handler struct {
// rlBucket rate limit bucket
rlBucket *ratelimit.Bucket
// reqPipe send request to somewhere else. can be nil for testing.
reqPipe chan<- *http.Request
config *config
Next httpserver.Handler
memStore *memstore.MemStore
}
// ServeHTTP serves a request
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (_ int, err error) {
var session *sessions.Session
// captcha
if h.config.Captcha {
if r.URL.Path == h.config.endpoint+"/captcha" {
data, _ := captcha.New(150, 60, func(o *captcha.Options) {
o.BackgroundColor = color.White
o.CharPreset = "ABCDEFGHKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"
o.FontDPI = 82
o.CurveNumber = 1
o.TextLength = 5
})
session, err = h.memStore.New(r, "captcha")
if err != nil {
return h.writeJSON(JSONError{
Code: http.StatusInternalServerError,
Error: err.Error(),
}, w)
}
session.Values["captcha"] = data.Text
err = h.memStore.Save(r, w, session)
if err != nil {
return h.writeJSON(JSONError{
Code: http.StatusInternalServerError,
Error: err.Error(),
}, w)
}
w.Header().Set(headerContentType, headerPNG)
return http.StatusOK, data.WriteImage(w)
}
}
if r.URL.Path != h.config.endpoint {
return h.Next.ServeHTTP(w, r)
}
if r.Method != "POST" {
return h.writeJSON(JSONError{
Code: http.StatusMethodNotAllowed,
Error: http.StatusText(http.StatusMethodNotAllowed),
}, w)
}
if _, ok := h.rlBucket.TakeMaxDuration(1, h.config.rateLimitInterval); !ok {
return h.writeJSON(JSONError{
Code: http.StatusTooManyRequests,
Error: http.StatusText(http.StatusTooManyRequests),
}, w)
}
if err := r.ParseForm(); err != nil {
return h.writeJSON(JSONError{
Code: http.StatusBadRequest,
Error: err.Error(),
}, w)
}
// captcha
if h.config.Captcha {
session, err = h.memStore.Get(r, "captcha")
if err != nil {
return h.writeJSON(JSONError{
Code: http.StatusBadRequest,
Error: err.Error(),
}, w)
}
text := r.PostFormValue("captcha_text")
if correct, ok := session.Values["captcha"].(string); (ok && text != correct) || !ok {
session.Values["captcha"] = ""
h.memStore.Save(r, w, session)
return h.writeJSON(JSONError{
Code: http.StatusUnauthorized,
Error: "Wrong captcha_text: " + text + " Correct: " + correct,
}, w)
}
}
// recaptcha
if h.config.ReCaptcha {
RecaptchaText := r.PostFormValue("g-recaptcha-response")
httpclient := http.Client{}
parts := url.Values{}
parts.Set("secret", h.config.ReCaptchaSecret)
parts.Set("response", RecaptchaText)
parts.Set("remoteip", r.RemoteAddr)
r, err := httpclient.PostForm("https://www.google.com/recaptcha/api/siteverify", parts)
if err != nil {
return h.writeJSON(JSONError{
Code: http.StatusInternalServerError,
Error: err.Error(),
}, w)
}
resp := &ReCaptchaResp{}
err = json.NewDecoder(r.Body).Decode(resp)
if err != nil {
return h.writeJSON(JSONError{
Code: http.StatusInternalServerError,
Error: err.Error(),
}, w)
}
if resp.Success != true {
return h.writeJSON(JSONError{
Code: http.StatusInternalServerError,
Error: strings.Join(resp.ErrorCodes, "; "),
}, w)
}
}
if e := r.PostFormValue("email"); !isValidEmail(e) {
return h.writeJSON(JSONError{
Code: StatusUnprocessableEntity,
Error: fmt.Sprintf("Invalid email address: %q", e),
}, w)
}
// captcha
if h.config.Captcha {
session.Values["captcha"] = ""
h.memStore.Save(r, w, session)
}
if h.reqPipe != nil {
h.reqPipe <- r // might block if the mail daemon is busy
}
// redirection
if h.config.redirectField != "" {
target := r.PostFormValue(h.config.redirectField)
if target != "" {
http.Redirect(w, r, target, http.StatusSeeOther)
}
}
return h.writeJSON(JSONError{Code: http.StatusOK}, w)
}
// JSONError defines how an REST JSON looks like.
// Code 200 and empty Error specifies a successful request
// Any other Code value s an error.
type JSONError struct {
// Code represents the HTTP Status Code, a work around.
Code int `json:"code,omitempty"`
// Error the underlying error, if there is one.
Error string `json:"error,omitempty"`
}
func (h *handler) writeJSON(je JSONError, w http.ResponseWriter) (int, error) {
buf := bufpool.Get()
defer bufpool.Put(buf)
w.Header().Set(headerContentType, headerApplicationJSONUTF8)
// https://github.com/caddyserver/caddy/issues/637#issuecomment-189599332
w.WriteHeader(je.Code)
if err := json.NewEncoder(buf).Encode(je); err != nil {
return http.StatusInternalServerError, err
}
if _, err := w.Write(buf.Bytes()); err != nil {
return http.StatusInternalServerError, err
}
return StatusEmpty, nil
}