ipv6-test-backend/internal/pkg/utils/network.go
thehrz b66129d29c
All checks were successful
CI / deploy (push) Successful in 2m27s
pref: update myip API
2024-08-18 23:23:18 +08:00

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")
}
}