-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
105 lines (94 loc) · 2.67 KB
/
Copy pathhandler.go
File metadata and controls
105 lines (94 loc) · 2.67 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
package gitealimit
import (
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
func (m *GiteaIPLimit) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
now := time.Now()
ip := remoteIP(r)
if ip == "" {
return caddyhttp.Error(http.StatusForbidden, fmt.Errorf("unable to determine client IP"))
}
cooldownActive := false
m.mu.Lock()
m.cleanupLocked(now)
if expireAt, ok := m.trustedIPs[ip]; ok && now.Before(expireAt) {
m.mu.Unlock()
return next.ServeHTTP(w, r)
}
if nextAt, ok := m.nextVerifyAt[ip]; ok && now.Before(nextAt) {
cooldownActive = true
}
m.mu.Unlock()
attemptedVerify := false
if !cooldownActive {
cookie, err := r.Cookie(m.CookieName)
if err == nil && strings.TrimSpace(cookie.Value) != "" {
attemptedVerify = true
valid, verifyErr := m.verifyGiteaCookie(r.Context(), cookie)
if verifyErr != nil {
m.logger.Warn("gitea cookie verification failed", zap.String("ip", ip), zap.Error(verifyErr))
}
if valid {
m.mu.Lock()
m.trustedIPs[ip] = now.Add(time.Duration(m.TrustedFor))
delete(m.anonymousIPs, ip)
delete(m.nextVerifyAt, ip)
m.mu.Unlock()
return next.ServeHTTP(w, r)
}
}
}
if !cooldownActive && (m.TrustAuthorization == nil || *m.TrustAuthorization) {
auth := strings.TrimSpace(r.Header.Get("Authorization"))
if auth != "" {
attemptedVerify = true
valid, verifyErr := m.verifyGiteaAuthorization(r.Context(), auth)
if verifyErr != nil {
m.logger.Warn("gitea authorization verification failed", zap.String("ip", ip), zap.Error(verifyErr))
}
if valid {
m.mu.Lock()
m.trustedIPs[ip] = now.Add(time.Duration(m.TrustedFor))
delete(m.anonymousIPs, ip)
delete(m.nextVerifyAt, ip)
m.mu.Unlock()
return next.ServeHTTP(w, r)
}
}
}
if attemptedVerify && m.VerifyCooldown != nil && time.Duration(*m.VerifyCooldown) > 0 {
// Throttle repeated verification attempts from the same IP.
m.mu.Lock()
m.nextVerifyAt[ip] = now.Add(time.Duration(*m.VerifyCooldown))
m.mu.Unlock()
}
allowed, retryAfter := m.allowAnonymous(ip, now)
if !allowed {
if retryAfter > 0 {
w.Header().Set("Retry-After", strconv.Itoa(int(retryAfter.Seconds())))
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = io.WriteString(w, "rate limit exceeded for anonymous IP\n")
return nil
}
return next.ServeHTTP(w, r)
}
func remoteIP(r *http.Request) string {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
return host
}
if ip := net.ParseIP(r.RemoteAddr); ip != nil {
return ip.String()
}
return ""
}