country-geo-locations/pkg/geoloc/static.go

31 lines
529 B
Go
Raw Normal View History

2023-12-19 13:56:03 +01:00
package geoloc
import (
"fmt"
"math"
"net"
"strings"
"github.com/praserx/ipconv"
)
func GetCountry(input string) *IPInfo {
if c, exists := countries[strings.ToUpper(input)]; exists {
return &c
}
return nil
}
func GetIPInfo(ipaddress string) (uint, error) {
ip := net.ParseIP(ipaddress)
if ip == nil {
return 0, fmt.Errorf("no valid IP address")
}
ipnum, err := ipconv.IPv4ToInt(ip)
if err != nil {
return 0, err
}
ipnumfrom := uint(math.Floor(float64(ipnum)/float64(256))) * 256
return ipnumfrom, nil
}