feat: allow bypassing of basic auth from a list of authorized cidrs (#50)
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good

This commit is contained in:
2025-08-05 16:24:41 +02:00
parent 9d10a69b0d
commit a50f926463
7 changed files with 208 additions and 56 deletions

42
internal/cidr/match.go Normal file
View File

@ -0,0 +1,42 @@
package cidr
import (
"net"
"strings"
"github.com/pkg/errors"
)
func MatchAny(hostPort string, CIDRs ...string) (bool, error) {
var remoteHost string
if strings.Contains(hostPort, ":") {
var err error
remoteHost, _, err = net.SplitHostPort(hostPort)
if err != nil {
return false, errors.WithStack(err)
}
} else {
remoteHost = hostPort
}
remoteAddr := net.ParseIP(remoteHost)
if remoteAddr == nil {
return false, errors.Errorf("remote host '%s' is not a valid ip address", remoteHost)
}
for _, rawCIDR := range CIDRs {
_, net, err := net.ParseCIDR(rawCIDR)
if err != nil {
return false, errors.WithStack(err)
}
match := net.Contains(remoteAddr)
if !match {
continue
}
return true, nil
}
return false, nil
}