31 lines
529 B
Go
31 lines
529 B
Go
|
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
|
||
|
}
|