-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmiddleware.go
36 lines (30 loc) · 1.09 KB
/
middleware.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
package caddydefender
import (
"fmt"
"go.uber.org/zap"
"net"
"net/http"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/jasonlovesdoggo/caddy-defender/utils"
)
// ServeHTTP implements the middleware logic.
func (m Defender) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// Split the RemoteAddr into IP and port
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
m.log.Error("Invalid client IP format", zap.String("ip", r.RemoteAddr))
return caddyhttp.Error(http.StatusForbidden, fmt.Errorf("invalid client IP format"))
}
clientIP := net.ParseIP(host)
m.log.Debug("client IP", zap.String("ip", clientIP.String()))
if clientIP == nil {
m.log.Error("Invalid client IP", zap.String("ip", host))
return caddyhttp.Error(http.StatusForbidden, fmt.Errorf("invalid client IP"))
}
// Check if the client IP is in any of the additional ranges
if utils.IPInRanges(clientIP, m.AdditionalRanges, m.log) {
return m.responder.Respond(w, r)
}
// IP is not in any of the ranges, proceed to the next handler
return next.ServeHTTP(w, r)
}