forked from bauljamic123arlijam/neko-u-krovu-bot
added ipv4 validation
This commit is contained in:
10
ping/ping.go
10
ping/ping.go
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -56,7 +57,16 @@ func getNetworkPrefix(ip net.IP) string {
|
||||
return fmt.Sprintf("%d.%d.%d.0", ip[0], ip[1], ip[2])
|
||||
}
|
||||
|
||||
func isValidIPv4(ip string) bool {
|
||||
ipv4Regex := `^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`
|
||||
re := regexp.MustCompile(ipv4Regex)
|
||||
return re.MatchString(ip)
|
||||
}
|
||||
|
||||
func ping(ip string) bool {
|
||||
if !isValidIPv4(ip) {
|
||||
return false
|
||||
}
|
||||
output, err := exec.Command("ping", "-c", "1", "-W", "1", ip).CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user