package third

import (
	_ "embed"
	"github.com/oschwald/geoip2-golang"
	"net"
)

//go:embed GeoLite2-Country.mmdb
var geoDb []byte

func GetCountryByIP(ip string) (string, error) {
	// 打开GeoIP数据库
	db, err := geoip2.FromBytes(geoDb)
	if err != nil {
		return "default", err
	}
	defer db.Close()

	// 查询IP地址
	country, err := db.Country(net.ParseIP(ip))
	if err != nil {
		return "default", err
	}

	// 返回国家名称
	return country.Country.IsoCode, nil
}
