Skip to content

Commit

Permalink
Merge pull request #16 from ildyria/multi-hostnames
Browse files Browse the repository at this point in the history
Support multiple host names, comma separated
  • Loading branch information
wollomatic committed May 5, 2024
2 parents ff2769d + 1c4b2a6 commit 4b88260
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions cmd/socket-proxy/handlehttprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net"
"net/http"
"strings"
)

// handleHttpRequest checks if the request is allowed and sends it to the proxy.
Expand Down Expand Up @@ -61,14 +62,18 @@ func isAllowedClient(remoteAddr string) (bool, error) {
return allowedIPNet.Contains(clientIP), nil
} else {
// AllowFrom is not a valid CIDR, so try to resolve it via DNS
ips, err := net.LookupIP(cfg.AllowFrom)
if err != nil {
return false, errors.New("error looking up allowed client hostname: " + err.Error())
}
for _, ip := range ips {
// Check if IP address is one of the resolved IPs
if ip.Equal(clientIP) {
return true, nil
// split over comma to support multiple hostnames
allowFroms := strings.Split(cfg.AllowFrom, ",")
for _, allowFrom := range allowFroms {
ips, err := net.LookupIP(allowFrom)
if err != nil {
return false, errors.New("error looking up allowed client hostname: " + err.Error())
}
for _, ip := range ips {
// Check if IP address is one of the resolved IPs
if ip.Equal(clientIP) {
return true, nil
}
}
}
return false, nil
Expand Down

0 comments on commit 4b88260

Please sign in to comment.