Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
379 lines (342 loc) · 10.8 KB
/
Copy pathhandler.go
File metadata and controls
379 lines (342 loc) · 10.8 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2026 RedirHub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package redirects is a Caddy module for bulk redirects and 404 monitoring:
// it serves the redirects of a remote rules file (format v1, published by
// RedirHub Integration Mode), answers the ownership check path, reports
// redirects and 404s back, and can gate on-demand TLS to the listed hostnames.
package redirects
import (
"errors"
"fmt"
"html"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
// Version is sent in the User-Agent of rules downloads and reports.
const Version = "0.4.0"
func init() {
caddy.RegisterModule(Handler{})
caddy.RegisterModule(Permission{})
}
// Handler serves the redirects of a RedirHub workspace rules file.
//
// For GET and HEAD requests on a hostname listed in the rules file it
// answers the check path, redirects exact sources (host/path) before the rest of
// the route runs and, when the site itself answers 404, redirects to the
// host's catch-all rule. Every other request passes through untouched.
type Handler struct {
// The workspace rules address from the RedirHub dashboard
// (Integration key panel, "Workspace rules address").
RulesURL string `json:"rules_url,omitempty"`
// The workspace integration key, used only to report redirects and
// 404s. Without it nothing is reported.
Key string `json:"key,omitempty"`
// Which 404 answers of the site are reported: "rules" (default) for
// the hostnames whose @host line has report404, "all" for every
// hostname this handler serves, listed in the rules or not.
Report404 string `json:"report_404,omitempty"`
rulesURL string
key string
rules *fetcher
reporter *reporter
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (Handler) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.redirects",
New: func() caddy.Module { return new(Handler) },
}
}
// Provision sets up the shared rules poller and reporter.
func (h *Handler) Provision(ctx caddy.Context) error {
h.logger = ctx.Logger()
repl := caddy.NewReplacer()
h.rulesURL = repl.ReplaceAll(h.RulesURL, "")
h.key = repl.ReplaceAll(h.Key, "")
// Checked here, before the shared poller starts: Caddy calls Validate
// only after Provision.
if err := validateReport404(h.Report404); err != nil {
return err
}
if err := validateRulesURL(h.rulesURL); err != nil {
return err
}
f, err := acquireFetcher(h.rulesURL, h.logger)
if err != nil {
return err
}
h.rules = f
r, err := acquireReporter(h.rulesURL, h.key, f, h.logger)
if err != nil {
releaseFetcher(h.rulesURL)
h.rules = nil
return err
}
h.reporter = r
return nil
}
func validateReport404(value string) error {
switch value {
case "", Report404Rules, Report404All:
return nil
}
return fmt.Errorf("report_404 must be %q or %q, not %q", Report404Rules, Report404All, value)
}
// The values of report_404.
const (
Report404Rules = "rules"
Report404All = "all"
)
func validateRulesURL(raw string) error {
if raw == "" {
return errors.New("rules_url is required")
}
u, err := url.Parse(raw)
if err != nil {
return fmt.Errorf("rules_url: %w", err)
}
if (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
return errors.New("rules_url must be an absolute http(s) URL")
}
return nil
}
// Cleanup releases the shared poller and reporter.
func (h *Handler) Cleanup() error {
if h.reporter != nil {
releaseReporter(h.rulesURL, h.key)
}
if h.rules != nil {
releaseFetcher(h.rulesURL)
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
return next.ServeHTTP(w, r)
}
var rs *RuleSet
if h.rules != nil {
rs = h.rules.Rules()
}
host := rs.Host(r.Host)
listed := host != nil
if !listed {
if h.Report404 != Report404All || rs == nil {
return next.ServeHTTP(w, r)
}
// report_404 all: an unlisted hostname has no rules and no check
// path; only its 404s are reported.
host = &Host{Name: normalizeHost(r.Host)}
if host.Name == "" {
return next.ServeHTTP(w, r)
}
}
report404 := host.Report404 || h.Report404 == Report404All
if listed && r.URL.Path == rs.CheckPath {
body := "token=" + host.Token + " etag=" + rs.ETag
hdr := w.Header()
hdr.Set("Content-Type", "text/plain; charset=utf-8")
hdr.Set("Cache-Control", "no-store")
hdr.Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusOK)
if r.Method != http.MethodHead {
_, _ = w.Write([]byte(body))
}
return nil
}
if rule, ok := host.Match(r); ok {
h.redirect(w, r, rs, host, rule)
return nil
}
if host.CatchAll == nil && !report404 {
return next.ServeHTTP(w, r)
}
// Let the site answer; a 404 that the catch-all replaces is discarded
// as it is written, never buffered. Everything else streams through.
catchAll := host.CatchAll
saved := w.Header().Clone()
rec := ¬FoundRecorder{ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w}, discard404: catchAll != nil}
err := next.ServeHTTP(rec, r)
if err != nil {
// A handler such as file_server reports a missing file as an error
// that the error routes turn into the 404 page.
var he caddyhttp.HandlerError
if errors.As(err, &he) && he.StatusCode == http.StatusNotFound && rec.Status() == 0 {
if catchAll != nil {
restoreHeader(w.Header(), saved)
h.redirect(w, r, rs, host, *catchAll)
return nil
}
if report404 {
h.report(r, host, http.StatusNotFound, 0, "", "")
}
}
return err
}
if rec.discarded {
restoreHeader(w.Header(), saved)
h.redirect(w, r, rs, host, *catchAll)
return nil
}
if rec.Status() == http.StatusNotFound && report404 {
h.report(r, host, http.StatusNotFound, rec.Size(), "", "")
}
return nil
}
// redirect answers a rule: a redirect to its Location or, for a frame
// rule, a page showing the Location in a full-window iframe. With
// @powered-by in the rules file it names the platform in X-Powered-By, as
// the platform's own edge does.
func (h *Handler) redirect(w http.ResponseWriter, r *http.Request, rs *RuleSet, host *Host, rule Rule) {
location := rule.Location(r)
if rs.PoweredBy != "" {
w.Header().Set("X-Powered-By", rs.PoweredBy)
}
if rule.Frame {
page := framePage(location)
hdr := w.Header()
hdr.Set("Content-Type", "text/html; charset=utf-8")
hdr.Set("Content-Length", strconv.Itoa(len(page)))
w.WriteHeader(http.StatusOK)
if r.Method != http.MethodHead {
_, _ = w.Write(page)
}
// Reported like a redirect, with body size 0 and the src as its
// destination.
h.report(r, host, http.StatusOK, 0, rule.Source, location)
return
}
w.Header().Set("Location", location)
w.WriteHeader(rule.Status)
h.report(r, host, rule.Status, 0, rule.Source, location)
}
// framePage is the frame page for src, as on RedirHub's edge: one
// full-window iframe showing src, titled with its hostname. The src is the
// Location exactly as a redirect would send it (an http(s) URL, as the rules
// file only holds such targets), HTML-escaped and otherwise unchanged.
func framePage(src string) []byte {
title := src
if u, err := url.Parse(src); err == nil && u.Hostname() != "" {
title = u.Hostname()
}
return []byte(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>` + html.EscapeString(title) + `</title>
<style>html,body{margin:0;padding:0;height:100%;overflow:hidden}iframe{display:block;width:100%;height:100%;border:0}</style>
</head>
<body>
<iframe src="` + html.EscapeString(src) + `" allowfullscreen></iframe>
</body>
</html>
`)
}
// report queues one report line; a redirect carries the rule's source and
// the Location it sent, a 404 neither.
func (h *Handler) report(r *http.Request, host *Host, status, size int, source, location string) {
if h.reporter == nil || h.key == "" {
return
}
h.reporter.Enqueue(hitLine(r, host.Name, status, size, clock(), source, location))
}
// clock is the time a report line is stamped with; tests fix it.
var clock = time.Now
// restoreHeader makes dst equal to saved, dropping what the discarded
// 404 response set.
func restoreHeader(dst, saved http.Header) {
for k := range dst {
delete(dst, k)
}
for k, v := range saved {
dst[k] = v
}
}
// notFoundRecorder passes the site's response through and records its
// status and size; with discard404 a 404 is not sent but discarded as it is
// written, so its body costs no memory however large it is.
type notFoundRecorder struct {
*caddyhttp.ResponseWriterWrapper
discard404 bool
status int
size int
wroteHeader bool
discarded bool // the 404 was discarded: nothing was sent
}
func (rec *notFoundRecorder) WriteHeader(status int) {
if rec.wroteHeader {
return
}
rec.status = status
if status >= 100 && status <= 199 {
// informational, not the final answer: passed on
rec.ResponseWriterWrapper.WriteHeader(status)
return
}
rec.wroteHeader = true
if rec.discard404 && status == http.StatusNotFound {
rec.discarded = true
return
}
rec.ResponseWriterWrapper.WriteHeader(status)
}
func (rec *notFoundRecorder) Write(p []byte) (int, error) {
rec.WriteHeader(http.StatusOK)
if rec.discarded {
rec.size += len(p)
return len(p), nil
}
n, err := rec.ResponseWriterWrapper.Write(p)
rec.size += n
return n, err
}
func (rec *notFoundRecorder) ReadFrom(r io.Reader) (int64, error) {
rec.WriteHeader(http.StatusOK)
var n int64
var err error
if rec.discarded {
n, err = io.Copy(io.Discard, r)
} else {
n, err = rec.ResponseWriterWrapper.ReadFrom(r)
}
rec.size += int(n)
return n, err
}
// FlushError flushes what is sent; a discarded 404 has nothing to flush.
func (rec *notFoundRecorder) FlushError() error {
if rec.discarded {
return nil
}
//nolint:bodyclose
return http.NewResponseController(rec.ResponseWriterWrapper).Flush()
}
// Status is the status the site wrote, 0 for none.
func (rec *notFoundRecorder) Status() int { return rec.status }
// Size is the number of body bytes the site wrote.
func (rec *notFoundRecorder) Size() int { return rec.size }
// Interface guards
var (
_ caddy.Provisioner = (*Handler)(nil)
_ caddy.CleanerUpper = (*Handler)(nil)
_ caddyhttp.MiddlewareHandler = (*Handler)(nil)
)