43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using IRaCIS.Core.Domain.Models;
|
|
using MaxMind.GeoIP2;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IRaCIS.Core.Application.Helper
|
|
{
|
|
public static class IPCityHelper
|
|
{
|
|
|
|
public static string GetCityResponse(string ip)
|
|
{
|
|
var path = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "GeoLite2-City.mmdb");
|
|
|
|
try
|
|
{
|
|
using (var reader = new DatabaseReader(path))
|
|
{
|
|
|
|
var city = reader.City(ip);
|
|
|
|
//Console.WriteLine(city.Country.IsoCode); // 'US' 'CN'
|
|
//Console.WriteLine(city.Country.Name); // 'United States' 'China'
|
|
////Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'
|
|
//Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei'
|
|
//Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan'
|
|
|
|
return $"{city.Country.Name} | {city.MostSpecificSubdivision.Name} | {city.City.Name}";
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
return $"UN | UN | {ip}";
|
|
}
|
|
}
|
|
}
|
|
}
|