/// <summary> /// Given an ip string this method will return the correct image directly. This allows specifying /// this endpoint as the src attribute of an image for lists. /// </summary> /// <param name="ip">a string that is parsable into an ip address</param> /// <returns>a flag image as a gif.</returns> public ActionResult LocationFlag(string ip) { var fName = "us.gif"; var dbPath = Server.MapPath(ConfigurationManager.ConnectionStrings["geoip"].ConnectionString); if (System.IO.File.Exists(dbPath)) { var ls = new MaxMind.GeoIP.LookupService(dbPath, MaxMind.GeoIP.LookupService.GEOIP_STANDARD); MaxMind.GeoIP.Location loc; if (!ip.StartsWith("10.")) loc = ls.getLocation(System.Net.IPAddress.Parse(ip)); else loc = new MaxMind.GeoIP.Location() { countryName = "US", countryCode = "US", city = "LAN", regionName = "Internal Address" }; if (loc != null) fName = String.Format("{0}.gif", loc.countryCode); } return new FilePathResult(Server.MapPath(String.Format("~/Contents/countryflags/{0}", fName)), "image/gif"); }
/// <summary> /// Returns GEOIP data for a given IP address. /// </summary> /// <remarks>While technically this is not data in the analysys portion of the database it is related</remarks> /// <param name="ip">a string that is parsable into an ip address</param> /// <returns>any location information that can be found for the ip address or null if no entry was found.</returns> public ActionResult Location(string ip) { var result = new JavaScriptResult(); var dbPath = Server.MapPath(ConfigurationManager.ConnectionStrings["geoip"].ConnectionString); if (System.IO.File.Exists(dbPath)) { var ls = new MaxMind.GeoIP.LookupService(dbPath, MaxMind.GeoIP.LookupService.GEOIP_STANDARD); MaxMind.GeoIP.Location loc; if (!ip.StartsWith("10.")) loc = ls.getLocation(System.Net.IPAddress.Parse(ip)); else loc = new MaxMind.GeoIP.Location() { countryName = "US", countryCode = "US", city = "LAN", regionName = "Internal Address" }; if (loc != null) result.Script = "{" + string.Format("areaCode:{0}, city:'{1}', countryCode:'{2}', countryName:'{3}', latitude:{4}, longitude:{5}, metroCode:{6}, postalCode:'{7}', region:'{8}', regionName:'{9}'", loc.area_code, loc.city, loc.countryCode, loc.countryName, loc.latitude, loc.longitude, loc.metro_code, loc.postalCode, loc.region, loc.regionName) + "}"; else result.Script = "{}"; } else result.Script = "{}"; return result; }