Ejemplo n.º 1
0
        public static AjaxCallResult GuessCountry()
        {
            // IMPORTANT: If you're implementing a sensitive organization, this should use YOUR OWN geoip server and not freegeoip.com, which
            // may potentially be eavesdroppable. Look at the freegeoip.com for how to download their database.

            if (HttpContext.Current.Request.UserHostAddress == "::1" && !Debugger.IsAttached)
            {
                // yeah, we're running from localhost, no big point trying to geoip this
                return(new AjaxCallResult {
                    Success = false
                });
            }

            try
            {
                string ipAddress = Logic.Support.SupportFunctions.GetMostLikelyRemoteIPAddress();

                NGeoIP.RawData rawData = (NGeoIP.RawData)GuidCache.Get(ipAddress);

                Persistence.Key["DebugData"] = HttpContext.Current.Request.ToRaw();

                if (rawData == null)
                {
                    NGeoIP.Request request = new Request()
                    {
                        Format = Format.Json,
                        IP     = ipAddress
                    };
                    NGeoClient client = new NGeoClient(request);
                    rawData = client.Execute();
                }

                if (!string.IsNullOrEmpty(rawData.CountryCode))
                {
                    // Successful lookup

                    GuidCache.Set(ipAddress, rawData);  // store lookup results in cache for later

                    return(new AjaxCallResult {
                        Success = true, DisplayMessage = rawData.CountryCode
                    });
                }
            }
            catch (Exception)
            {
                // We failed, return failure in the statement following this block
            }

            return(new AjaxCallResult {
                Success = false
            });
        }
Ejemplo n.º 2
0
        public static string Lookup(string ip)
        {
            NGeoIP.Request request = new Request()
            {
                Format = Format.Json,
                IP     = HttpContext.Current.Request.UserHostAddress
            };
            NGeoClient client = new NGeoClient(request);

            NGeoIP.RawData rawData = client.Execute();

            return(rawData.CountryCode);
        }