-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
296 lines (263 loc) · 6.95 KB
/
middleware.go
File metadata and controls
296 lines (263 loc) · 6.95 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
package main
import (
"compress/gzip"
"net/http"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/crypto/bcrypt"
"golang.org/x/time/rate"
)
// --- gzip compression middleware ---
func compressMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
}
gz := &gzipResponseWriter{ResponseWriter: w}
defer gz.finish()
w.Header().Set("Vary", "Accept-Encoding")
next.ServeHTTP(gz, r)
})
}
type gzipResponseWriter struct {
http.ResponseWriter
gzWriter *gzip.Writer
decided bool
compressing bool
}
func (g *gzipResponseWriter) decide() {
g.decided = true
h := g.ResponseWriter.Header()
if h.Get("Content-Encoding") != "" {
return
}
if isCompressible(h.Get("Content-Type")) {
g.compressing = true
g.gzWriter = gzip.NewWriter(g.ResponseWriter)
}
}
func (g *gzipResponseWriter) WriteHeader(code int) {
if !g.decided {
g.decide()
}
if g.compressing {
g.ResponseWriter.Header().Del("Content-Length")
g.ResponseWriter.Header().Set("Content-Encoding", "gzip")
}
g.ResponseWriter.WriteHeader(code)
}
func (g *gzipResponseWriter) Write(b []byte) (int, error) {
if !g.decided {
if g.ResponseWriter.Header().Get("Content-Type") == "" {
g.ResponseWriter.Header().Set("Content-Type", http.DetectContentType(b))
}
g.decide()
if g.compressing {
g.ResponseWriter.Header().Del("Content-Length")
g.ResponseWriter.Header().Set("Content-Encoding", "gzip")
}
}
if g.compressing {
return g.gzWriter.Write(b)
}
return g.ResponseWriter.Write(b)
}
func (g *gzipResponseWriter) Flush() {
if g.gzWriter != nil {
_ = g.gzWriter.Flush()
}
if f, ok := g.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func (g *gzipResponseWriter) finish() {
if g.gzWriter != nil {
_ = g.gzWriter.Close()
}
}
func (g *gzipResponseWriter) Unwrap() http.ResponseWriter {
return g.ResponseWriter
}
func isCompressible(contentType string) bool {
ct := strings.ToLower(contentType)
// Strip parameters (e.g. "; charset=utf-8")
if i := strings.IndexByte(ct, ';'); i >= 0 {
ct = ct[:i]
}
ct = strings.TrimSpace(ct)
if strings.HasPrefix(ct, "text/") {
return true
}
switch ct {
case "application/json",
"application/javascript",
"application/xml",
"application/xhtml+xml",
"application/wasm",
"application/manifest+json",
"application/vnd.api+json",
"application/atom+xml",
"application/rss+xml",
"image/svg+xml":
return true
}
return false
}
// --- rate limiting middleware ---
type limiterEntry struct {
limiter *rate.Limiter
lastSeen time.Time
}
type ipRateLimiter struct {
mu sync.Mutex
limiters map[string]*limiterEntry
limit rate.Limit
burst int
checks int
}
func (rl *ipRateLimiter) allow(ip string) bool {
rl.mu.Lock()
defer rl.mu.Unlock()
// Periodic cleanup: every 1000 checks, evict entries idle > 3 min.
rl.checks++
if rl.checks >= 1000 {
rl.checks = 0
cutoff := time.Now().Add(-3 * time.Minute)
for k, v := range rl.limiters {
if v.lastSeen.Before(cutoff) {
delete(rl.limiters, k)
}
}
}
entry, ok := rl.limiters[ip]
if !ok {
entry = &limiterEntry{limiter: rate.NewLimiter(rl.limit, rl.burst)}
rl.limiters[ip] = entry
}
entry.lastSeen = time.Now()
return entry.limiter.Allow()
}
func newRateLimitMiddleware(next http.Handler, rps float64, burst int) http.Handler {
if burst <= 0 {
burst = max(int(rps), 1)
}
rl := &ipRateLimiter{
limiters: make(map[string]*limiterEntry),
limit: rate.Limit(rps),
burst: burst,
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, _ := strings.Cut(r.RemoteAddr, ":")
// Handle IPv6 [::1]:port format
if strings.HasPrefix(r.RemoteAddr, "[") {
if i := strings.LastIndex(r.RemoteAddr, "]"); i >= 0 {
ip = r.RemoteAddr[1:i]
}
}
if !rl.allow(ip) {
http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
})
}
// --- request body size limit middleware ---
func maxBodyMiddleware(next http.Handler, maxBytes int64) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxBytes)
next.ServeHTTP(w, r)
})
}
// --- CORS middleware ---
func corsMiddleware(next http.Handler, cfg *CORSConfig) http.Handler {
allowedOrigins := make(map[string]bool)
allowAll := false
for _, o := range cfg.AllowedOrigins {
if o == "*" {
allowAll = true
}
allowedOrigins[o] = true
}
methods := "GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD"
if len(cfg.AllowedMethods) > 0 {
methods = strings.Join(cfg.AllowedMethods, ", ")
}
headers := "Content-Type, Authorization, X-Request-ID"
if len(cfg.AllowedHeaders) > 0 {
headers = strings.Join(cfg.AllowedHeaders, ", ")
}
maxAge := "86400"
if cfg.MaxAge > 0 {
maxAge = strconv.Itoa(cfg.MaxAge)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin != "" && (allowAll || allowedOrigins[origin]) {
allow := origin
if allowAll {
allow = "*"
}
w.Header().Set("Access-Control-Allow-Origin", allow)
w.Header().Set("Access-Control-Allow-Methods", methods)
w.Header().Set("Access-Control-Allow-Headers", headers)
w.Header().Set("Access-Control-Max-Age", maxAge)
if !allowAll {
w.Header().Set("Vary", "Origin")
}
}
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
// --- basic auth middleware ---
func basicAuthMiddleware(next http.Handler, username, passwordHash, realm string) http.Handler {
if realm == "" {
realm = "Restricted"
}
challenge := `Basic realm="` + realm + `"`
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || user != username || bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(pass)) != nil {
w.Header().Set("WWW-Authenticate", challenge)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
// --- redirect middleware ---
func redirectMiddleware(next http.Handler, rules []RedirectRule) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, rule := range rules {
if strings.HasPrefix(r.URL.Path, rule.From) {
target := rule.To + strings.TrimPrefix(r.URL.Path, rule.From)
status := rule.Status
if status == 0 {
status = http.StatusMovedPermanently
}
http.Redirect(w, r, target, status)
return
}
}
next.ServeHTTP(w, r)
})
}
// --- connection limit middleware ---
func maxConnectionsMiddleware(next http.Handler, maxConns int) http.Handler {
sem := make(chan struct{}, maxConns)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case sem <- struct{}{}:
defer func() { <-sem }()
next.ServeHTTP(w, r)
default:
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
}
})
}