地理围栏是一种虚拟地理边界,用于确定应用程序的行为。给定一个特定的坐标(纬度和经度),地理围栏引擎可以确定该位置属于哪个或哪些虚拟地理区域,并据此采取行动。
本项目的IP地理围栏引擎基于特定的IP地址运行。引擎首先将IP地址转换为地理位置,使用的是项目中包含的IP2Location提供的免费CSV数据库。
然后,引擎确定配置的地理区域中哪些包含该位置,并为每个匹配项触发相应的配置动作。地理区域可以通过导入GeoJSON格式的文件在引擎中配置。项目中包含了用于测试目的的示例GeoJSON文件,这些文件也可以在OpenDataSoft网站上免费获取。
当IP地址可用但坐标不可用时,该引擎特别有用。以下是Web应用程序可以使用IP地址的引擎的典型场景:
规则由三个元素构成:名称、谓词和动作。
以下是配置地理围栏引擎的示例代码:
var engine = new IPGeoFencingEngineBuilder()
.AddIP2LocationFromCSVFile(@"\\geofencing\data\IP2LOCATION-LITE-DB11.CSV")
.AddGeographicAreasFromGeoJSONFile(@"\\geofencing\data\demo.geojson")
.AddRule("New York", predicate: (areas, ip, location) => {
return areas.Any(A => A.Name == "New York");
}, action: (areas, ip, location) => {
Console.WriteLine($"The IP Address: {ip} is in New York State!");
})
.AddRule("Montana", predicate: (areas, ip, location) => {
return areas.Any(A => A.Name == "Montana");
}, action: (areas, ip, location) => {
Console.WriteLine($"The IP Address: {ip} is in Montana!");
})
.AddRule("Billings", predicate: (areas, ip, location) => {
return areas.Any(A => A.Name == "Billings");
}, action: (areas, ip, location) => {
Console.WriteLine($"The IP Address: {ip} is in Billings, MT");
})
.AddRule("Montana but not Billings", predicate: (areas, ip, location) => {
return areas.Any(A => A.Name == "Montana") && !areas.Any(A => A.Name == "Billings");
}, action: (areas, ip, location) => {
Console.WriteLine($"The IP Address: {ip} is in Montana but not in Billings!");
})
.AddRule("New York or Montana", predicate: (areas, ip, location) => {
return areas.Any(A => A.Name == "Montana") || areas.Any(A => A.Name == "New York");
}, action: (areas, ip, location) => {
Console.WriteLine($"The IP Address: {ip} is in New York State or Montana!");
})
.AddDefaultAction((ip, loc) => Console.WriteLine($"The IP Address: {ip} is outside all the areas provided"))
.Build();
以下是运行引擎的示例代码:
engine.Run("98.127.147.57");
// Billings, MT IP Address
Console.WriteLine("The IP Address: 98.127.147.57 is in Montana!");
Console.WriteLine("The IP Address: 98.127.147.57 is in Billings, MT");
Console.WriteLine("The IP Address: 98.127.147.57 is in New York State or Montana!");
engine.Run("172.254.112.210");
// New York, NY IP Address
Console.WriteLine("The IP Address: 172.254.112.210 is in New York State!");
Console.WriteLine("The IP Address: 172.254.112.210 is in New York State or Montana!");
engine.Run("157.240.3.35");
// Seattle, WA IP Address
Console.WriteLine("The IP Address: 157.240.3.35 is outside all the areas provided");
解决方案附带了一个简单的控制台应用程序,演示了引擎的配置和执行。在运行演示之前,请确保编辑项目中包含的appsettings.json文件,并设置文件系统中数据文件夹的正确完整路径。
{
"DataFolder": "D:\\IPGeoFencing\\Data"
}
引擎使用的核心服务可以轻松扩展以丰富其功能:
IIP2LocationProvider myIP2LocationProvider = new myOwnIP2LocationProviderImplementation();
IGeographicAreasProvider myGeographicAreasProvider = new myOwnGeographicAreasProviderImplementation();
var engine = new IPGeoFencingEngineBuilder()
.AddIP2LocationProvider(myIP2LocationProvider)
.AddGeographicAreasProvider(myGeographicAreasProvider)
.AddRule(...);