Created
February 21, 2018 10:12
-
-
Save kevsersrca/37595c9c009acb0f0c29331e99358a9a to your computer and use it in GitHub Desktop.
is inside
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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