Skip to content
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

Feature: disable ipv4 addresses #81

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {

Options struct {
DisableIPv6 bool `yaml:"disableIPv6"` // prohibits DNS resolved IPv6 addresses
DisableIPv4 bool `yaml:"disableIPv4"` // prohibits DNS resolved IPv4 addresses
} `yaml:"options"`
}

Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
dnsRefresh = kingpin.Flag("dns.refresh", "Interval for refreshing DNS records and updating targets accordingly (0 if disabled)").Default("1m").Duration()
dnsNameServer = kingpin.Flag("dns.nameserver", "DNS server used to resolve hostname of targets").Default("").String()
disableIPv6 = kingpin.Flag("options.disable-ipv6", "Disable DNS from resolving IPv6 AAAA records").Default().Bool()
disableIPv4 = kingpin.Flag("options.disable-ipv4", "Disable DNS from resolving IPv4 A records").Default().Bool()
logLevel = kingpin.Flag("log.level", "Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]").Default("info").String()
targets = kingpin.Arg("targets", "A list of targets to ping").Strings()
)
Expand Down Expand Up @@ -158,32 +159,32 @@ func startMonitor(cfg *config.Config) (*mon.Monitor, error) {
}
targets[i] = t

err := t.addOrUpdateMonitor(monitor, cfg.Options.DisableIPv6)
err := t.addOrUpdateMonitor(monitor, cfg.Options.DisableIPv6, cfg.Options.DisableIPv4)
if err != nil {
log.Errorln(err)
}
}

go startDNSAutoRefresh(cfg.DNS.Refresh.Duration(), targets, monitor, cfg.Options.DisableIPv6)
go startDNSAutoRefresh(cfg.DNS.Refresh.Duration(), targets, monitor, cfg.Options.DisableIPv6, cfg.Options.DisableIPv4)

return monitor, nil
}

func startDNSAutoRefresh(interval time.Duration, targets []*target, monitor *mon.Monitor, disableIPv6 bool) {
func startDNSAutoRefresh(interval time.Duration, targets []*target, monitor *mon.Monitor, disableIPv6 bool, disableIPv4 bool) {
if interval <= 0 {
return
}

for range time.NewTicker(interval).C {
refreshDNS(targets, monitor, disableIPv6)
refreshDNS(targets, monitor, disableIPv6, disableIPv4)
}
}

func refreshDNS(targets []*target, monitor *mon.Monitor, disableIPv6 bool) {
func refreshDNS(targets []*target, monitor *mon.Monitor, disableIPv6 bool, disableIPv4 bool) {
log.Infoln("refreshing DNS")
for _, t := range targets {
go func(ta *target) {
err := ta.addOrUpdateMonitor(monitor, disableIPv6)
err := ta.addOrUpdateMonitor(monitor, disableIPv6, disableIPv4)
if err != nil {
log.Errorf("could not refresh dns: %v", err)
}
Expand Down Expand Up @@ -331,6 +332,9 @@ func addFlagToConfig(cfg *config.Config) {
if !cfg.Options.DisableIPv6 {
cfg.Options.DisableIPv6 = *disableIPv6
}
if !cfg.Options.DisableIPv4 {
cfg.Options.DisableIPv4 = *disableIPv4
}
}

const indexHTML = `<!doctype html>
Expand Down
20 changes: 10 additions & 10 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
ipv6 ipVersion = 6
)

func (t *target) addOrUpdateMonitor(monitor *mon.Monitor, disableIPv6 bool) error {
func (t *target) addOrUpdateMonitor(monitor *mon.Monitor, disableIPv6 bool, disableIPv4 bool) error {
t.mutex.Lock()
defer t.mutex.Unlock()

Expand All @@ -40,16 +40,16 @@ func (t *target) addOrUpdateMonitor(monitor *mon.Monitor, disableIPv6 bool) erro
}

var sanitizedAddrs []net.IPAddr
if disableIPv6 {
for _, addr := range addrs {
if getIPVersion(addr) == ipv6 {
log.Infof("IPv6 disabled: skipping target for host %s (%v)", t.host, addr)
continue
}
sanitizedAddrs = append(sanitizedAddrs, addr)
for _, addr := range addrs {
if getIPVersion(addr) == ipv6 && disableIPv6 {
log.Infof("IPv6 disabled: skipping target for host %s (%v)", t.host, addr)
continue
}
} else {
sanitizedAddrs = addrs
if getIPVersion(addr) == ipv4 && disableIPv4 {
log.Infof("IPv4 disabled: skipping target for host %s (%v)", t.host, addr)
continue
}
sanitizedAddrs = append(sanitizedAddrs, addr)
}

for _, addr := range sanitizedAddrs {
Expand Down