Skip to content

Instantly share code, notes, and snippets.

@kevsersrca
Created February 21, 2018 10:12
Show Gist options
  • Save kevsersrca/37595c9c009acb0f0c29331e99358a9a to your computer and use it in GitHub Desktop.
Save kevsersrca/37595c9c009acb0f0c29331e99358a9a to your computer and use it in GitHub Desktop.
is inside
func isInside(adress string, masks []string, timeout time.Duration, done chan net.IP) {
c1 := make(chan net.IP)
c2 := make(chan error)
var p net.IP
var err error
go func() {
ipv4 := adress
p = net.ParseIP(ipv4).To4()
for i := 0; i < len(masks); i++ {
_, pc, _ := net.ParseCIDR(masks[i])
if pc.Contains(p) {
c1 <- p
}
}
if err != nil {
c2 <- err
}
}()
select {
case p = <-c1:
case err = <-c2:
case <-time.After(timeout):
done <- nil
}
if err != nil {
done <- nil
}
done <- p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment