added ipv4 validation
This commit is contained in:
parent
97e41eece0
commit
12f28a6623
@ -86,13 +86,13 @@ func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) {
|
||||
|
||||
func (nkbot *NekoUKrovuBot) sanitizeInput(input string) bool {
|
||||
if len(input) > 50 {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
if !utf8.ValidString(input) {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
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 {
|
||||
|
25
ping/ping_test.go
Normal file
25
ping/ping_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package ping
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_isValidIPv4(t *testing.T) {
|
||||
type args struct {
|
||||
ip string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{name: "1", args: args{ip: "192.168.1.1"}, want: true},
|
||||
{name: "2", args: args{ip: "192.168.277.1"}, want: false},
|
||||
{name: "3", args: args{ip: "kurac palac"}, want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isValidIPv4(tt.args.ip); got != tt.want {
|
||||
t.Errorf("isValidIPv4() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user