26 lines
380 B
Go
26 lines
380 B
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
)
|
|
|
|
const (
|
|
IPv4 = 4
|
|
IPv6 = 6
|
|
)
|
|
|
|
func GetIPVersion(ip string) (int, error) {
|
|
parsedIP := net.ParseIP(ip)
|
|
if parsedIP == nil {
|
|
return 0, errors.New("invalid IP address")
|
|
}
|
|
if parsedIP.To4() != nil {
|
|
return IPv4, nil
|
|
} else if parsedIP.To16() != nil {
|
|
return IPv6, nil
|
|
} else {
|
|
return 0, errors.New("invalid IP address")
|
|
}
|
|
}
|