Skip to content

Enable DNS Resolution #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,41 @@ func (p Plugin) Lookup(ip string) (string, error) {
return record.Country_short, nil
}

// Create IP Networks using CIDR block array
// Create IP Networks using CIDR block array or try to resolve via DNS Lookup
func initIPBlocks(ipBlocks []string) ([]*net.IPNet, error) {

var ipBlocksNet []*net.IPNet

for _, cidr := range ipBlocks {
_, block, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("parse error on %q: %v", cidr, err)
}
ipBlocksNet = append(ipBlocksNet, block)
}

return ipBlocksNet, nil
var ipBlocksNet []*net.IPNet

for _, item := range ipBlocks {
// Try to parse as CIDR first
_, block, err := net.ParseCIDR(item)
if err == nil {
ipBlocksNet = append(ipBlocksNet, block)
continue
}

// If CIDR parsing fails, try DNS resolution
ips, err := net.LookupIP(item)
if err != nil {
return nil, fmt.Errorf("failed to parse CIDR or resolve DNS for %q: %v", item, err)
}

// Convert resolved IPs to /32 (IPv4) or /128 (IPv6) networks
for _, ip := range ips {
var cidr string
if ip.To4() != nil {
cidr = ip.String() + "/32"
} else {
cidr = ip.String() + "/128"
}
_, block, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("failed to create CIDR for resolved IP %s: %v", ip, err)
}
ipBlocksNet = append(ipBlocksNet, block)
}
}

return ipBlocksNet, nil
}

// isAllowedIPBlocks checks if an IP is allowed base on the allowed CIDR blocks
Expand Down