2023-12-19 13:56:03 +01:00
|
|
|
package geoloc
|
|
|
|
|
|
|
|
import (
|
2024-11-26 13:35:54 +01:00
|
|
|
"errors"
|
2023-12-19 13:56:03 +01:00
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/praserx/ipconv"
|
|
|
|
)
|
|
|
|
|
2024-11-26 13:35:54 +01:00
|
|
|
var ErrNoValidIP = errors.New("no valid IP address")
|
|
|
|
|
|
|
|
func CalculateIPNum(ipaddress string) (uint, error) {
|
2023-12-19 13:56:03 +01:00
|
|
|
ip := net.ParseIP(ipaddress)
|
|
|
|
if ip == nil {
|
2024-11-26 13:35:54 +01:00
|
|
|
return 0, ErrNoValidIP
|
2023-12-19 13:56:03 +01:00
|
|
|
}
|
|
|
|
ipnum, err := ipconv.IPv4ToInt(ip)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-11-26 13:35:54 +01:00
|
|
|
ipnumfrom := uint(math.Floor(float64(ipnum)/float64(CalculationValue))) * CalculationValue
|
2023-12-19 13:56:03 +01:00
|
|
|
return ipnumfrom, nil
|
|
|
|
}
|