1: using System.Runtime.Serialization;
2: using System.Web.Http;
3:
4: namespace Ia.Cl.Model.Api.Controller
5: {
6: ////////////////////////////////////////////////////////////////////////////
7:
8: /// <summary publish="true">
9: /// GeoIp API Controller class of Internet Application project model.
10: /// </summary>
11: ///
12: /// <remarks>
13: /// Copyright © 2006-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
14: ///
15: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
16: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
17: ///
18: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20: ///
21: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
22: ///
23: /// Copyright notice: This notice may not be removed or altered from any source distribution.
24: /// </remarks>
25: public class GeoIpController : ApiController
26: {
27: ////////////////////////////////////////////////////////////////////////////
28:
29: /// <summary/>
30: [DataContract]
31: public class CountryInfo
32: {
33: /// <summary/>
34: [DataMember]
35: public string Name { get; set; }
36:
37: /// <summary/>
38: [DataMember]
39: public string IsoCode { get; set; }
40: }
41:
42: ////////////////////////////////////////////////////////////////////////////
43:
44: /// <summary/>
45: [DataContract]
46: public class CityInfo
47: {
48: /// <summary/>
49: [DataMember]
50: public string Name { get; set; }
51:
52: /// <summary/>
53: [DataMember]
54: public double? Latitude { get; set; }
55:
56: /// <summary/>
57: [DataMember]
58: public double? Longitude { get; set; }
59: }
60:
61: ////////////////////////////////////////////////////////////////////////////
62:
63: /// <summary>
64: ///
65: /// </summary>
66: public GeoIpController()
67: {
68: }
69:
70: ////////////////////////////////////////////////////////////////////////////
71:
72: /// <summary>
73: ///
74: /// </summary>
75: [HttpGet]
76: [Route("api/geoip/country/{ip}")]
77: public CountryInfo Country(string ip)
78: {
79: CountryInfo countryInfo;
80:
81: countryInfo = new CountryInfo();
82:
83: if (Model.Default.IsValidIpv4(ip))
84: {
85: using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(Ia.Cl.Model.Default.AbsolutePath() + "app_data\\geoip\\GeoLite2-Country.mmdb", MaxMind.Db.FileAccessMode.Memory))
86: {
87: MaxMind.GeoIP2.Responses.AbstractCountryResponse abstractCountryResponse;
88: abstractCountryResponse = reader.Country(ip);
89:
90: countryInfo.Name = abstractCountryResponse.Country.Name;
91: countryInfo.IsoCode = abstractCountryResponse.Country.IsoCode;
92: }
93: }
94: else countryInfo = null;
95:
96: return countryInfo;
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: [HttpGet]
105: [Route("api/geoip/country-location/{ip}")]
106: public CityInfo CityAndLocation(string ip)
107: {
108: CityInfo cityInfo;
109:
110: cityInfo = new CityInfo();
111:
112: if (Model.Default.IsValidIpv4(ip))
113: {
114: using (MaxMind.GeoIP2.DatabaseReader reader = new MaxMind.GeoIP2.DatabaseReader(Ia.Cl.Model.Default.AbsolutePath() + "app_data\\geoip\\GeoLite2-City.mmdb", MaxMind.Db.FileAccessMode.Memory))
115: {
116: MaxMind.GeoIP2.Responses.AbstractCityResponse abstractCityResponse;
117: abstractCityResponse = reader.City(ip);
118:
119: cityInfo.Name = abstractCityResponse.City.Name;
120: cityInfo.Latitude = abstractCityResponse.Location.Latitude;
121: cityInfo.Longitude = abstractCityResponse.Location.Longitude;
122: }
123: }
124: else cityInfo = null;
125:
126: return cityInfo;
127: }
128:
129: ////////////////////////////////////////////////////////////////////////////
130: ////////////////////////////////////////////////////////////////////////////
131: }
132: }