28 lines
593 B
Go
28 lines
593 B
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/oschwald/geoip2-golang"
|
|
"net"
|
|
)
|
|
|
|
func HandleGeoIPCommand(ipAddress string, dbPath string) (string, error) {
|
|
db, err := geoip2.Open(dbPath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error opening GeoIP database: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
ip := net.ParseIP(ipAddress)
|
|
record, err := db.City(ip)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error looking up IP address: %v", err)
|
|
}
|
|
|
|
if record.Country.IsoCode != "" {
|
|
return fmt.Sprintf("IP: %s, Country: %s", ipAddress, record.Country.IsoCode), nil
|
|
}
|
|
|
|
return "Country not found", nil
|
|
}
|