Improve performance for ACL (#113)

This also in some sense mitigates DoS attacks by flooding servers with a lot of denied hostnames, which could lead to overloading the DNS services.

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
This commit is contained in:
Mygod 2024-07-18 16:08:34 -04:00 committed by GitHub
parent ec0619230b
commit 02be81e696
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -486,6 +486,18 @@ func (h Handler) dialContextCheckACL(ctx context.Context, network, hostPort stri
fmt.Errorf("port %s is not allowed", port)) fmt.Errorf("port %s is not allowed", port))
} }
match:
for _, rule := range h.aclRules {
if _, ok := rule.(*aclDomainRule); ok {
switch rule.tryMatch(nil, host) {
case aclDecisionDeny:
return nil, caddyhttp.Error(http.StatusForbidden, fmt.Errorf("disallowed host %s", host))
case aclDecisionAllow:
break match
}
}
}
// in case IP was provided, net.LookupIP will simply return it // in case IP was provided, net.LookupIP will simply return it
IPs, err := net.LookupIP(host) IPs, err := net.LookupIP(host)
if err != nil { if err != nil {