Exemplo n.º 1
0
 public static string GetCountryCode(string ip, MaxmindSource mms = MaxmindSource.LocalDb, bool throwOnError = false, bool doNotWarnOnNotInDb = false)
 {
     try {
         using (WebServiceClient wsc = new MaxMind.GeoIP2.WebServiceClient(MAXMIND_WS_USER_ID, MAXMIND_WS_KEY)) {
             var ir = wsc.Insights(ip);
             LogIt.D(ir.Country.IsoCode + "|" + ir.City.Name);
             return(ir.Country.IsoCode);
         }
     } catch (Exception ex) {
         if (doNotWarnOnNotInDb && ex.Message.Contains("is not in the database"))
         {
             //LogIt.W(ip + "|" + ex.Message);
         }
         else
         {
             LogIt.W(ip + "|" + ex.Message);
         }
         if (throwOnError)
         {
             throw ex;
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 2
0
 private void DebugLog(string message)
 {
     if (this.debug)
     {
         var now = Convert.ToString(CurrentTime());
         //Console.WriteLine("[{0}] {1}", now, message);
         LogIt.D(message);
     }
 }
Exemplo n.º 3
0
 public static void RemoveFromPool(WebProxy wp)
 {
     lock (mLouwp) {
         if (_louwp.Contains(wp))
         {
             _louwp.Remove(wp);
             LogIt.D(wp.Address);
         }
     }
 }
 public static void Execute()
 {
     try {
         foreach (var ci in CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures).OrderBy(ci => ci.Name))
         {
             LogIt.D(ci.Name + "|" + ci.EnglishName);
         }
     } catch (Exception ex) {
         LogIt.E(ex);
     }
 }
Exemplo n.º 5
0
        public static Coordinate Execute(string address)
        {
            var c = new Coordinate {
                g = Geocoder.Esri
            };

            try {
                if (!string.IsNullOrEmpty(address) && !address.Equals("0"))
                {
                    var uea = HttpUtility.UrlEncode(address.Trim());

                    //http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?text=1700 Penny ave washingtn dc&f=pjson&forStorage=false&maxLocations=1
                    var client   = new RestClient("http://geocode.arcgis.com/");
                    var request  = new RestRequest("/arcgis/rest/services/World/GeocodeServer/find?text=" + uea + "&f=pjson&forStorage=false&maxLocations=1", Method.GET);
                    var response = client.Execute(request);

                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        LogIt.D(response.StatusCode + "|" + address);
                    }
                    else
                    {
                        var     content = response.Content;
                        dynamic json    = Newtonsoft.Json.Linq.JObject.Parse(content);

                        if (json.locations.Count > 0)
                        {
                            var geometry = json.locations[0].feature.geometry;
                            if (geometry != null)
                            {
                                var lng = Convert.ToDecimal(geometry.x.Value);
                                var lat = Convert.ToDecimal(geometry.y.Value);
                                if ((lat != 0) && (lng != 0))
                                {
                                    c.lng = lng;
                                    c.lat = lat;
                                }
                            }
                            var precision = json.locations[0].feature.attributes;
                            if (precision != null)
                            {
                                var pc = precision.Score.Value;
                                c.precision = Convert.ToString(pc);
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LogIt.W(address);
                LogIt.E(ex);
            }

            return(c);
        }
Exemplo n.º 6
0
        /// <summary>
        /// account for possbility of ELB sheilding the public ip
        /// </summary>
        /// <returns></returns>
        public static string Execute(
            HttpContext hc
            )
        {
            var remoteIpAddress = "";
            var xForwardedFor   = "";
            var remoteAddr      = "";
            var ip     = "";
            var status = "";

            try {
                //https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core
                //TODO: get if not passed

                if ((hc == null) || (hc.Request == null))
                {
                    status = "http context was null";
                    return(null);
                }

                remoteIpAddress = hc.Features.Get <IHttpConnectionFeature>()?.RemoteIpAddress.ToString();
                if (!string.IsNullOrEmpty(hc.Request.Headers["X-Forwarded-For"]))
                {
                    xForwardedFor = hc.Request.Headers["X-Forwarded-For"];
                }
                if (!string.IsNullOrEmpty(hc.Request.Headers["REMOTE_ADDR"]))
                {
                    remoteAddr = hc.Request.Headers["REMOTE_ADDR"];
                }

                var loIp = new List <string> {
                    xForwardedFor, remoteAddr, remoteIpAddress
                };
                loIp = loIp.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
                ip   = loIp.FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(ip) && ip.Contains(","))
                {
                    ip = ip.Split(",").First();
                }

                return(ip);
            } catch (Exception ex) {
                LogIt.E(ex);
            } finally {
                LogIt.D(JsonConvert.SerializeObject(new {
                    ip,
                    status,
                    remoteIpAddress,
                    xForwardedFor,
                    remoteAddr
                }, Formatting.Indented));
            }
            return(null);
        }
Exemplo n.º 7
0
 public static void Execute()
 {
     try {
         foreach (var rh in HttpContext.Current.Request.Headers)
         {
             LogIt.D(rh.ToString() + "|" + HttpContext.Current.Request.Headers[rh.ToString()]);
         }
     } catch (Exception ex) {
         LogIt.E(ex);
     }
 }
Exemplo n.º 8
0
 public static InsightsResponse GetOmniResponse(string ip)
 {
     try {
         using (WebServiceClient wsc = new MaxMind.GeoIP2.WebServiceClient(MAXMIND_WS_USER_ID, MAXMIND_WS_KEY)) {
             var ir = wsc.Insights(ip);
             LogIt.D(ir.Country.IsoCode + "|" + ir.City.Name);
             return(ir);
         }
     } catch (Exception ex) {
         LogIt.E(ex);
         LogIt.W(ip);
         throw ex;
     }
 }
Exemplo n.º 9
0
        public static bool Detect(
            string googe_service_account_json,
            string applicationName,
            string base64Image,
            out string summary
            )
        {
            summary = "";

            try {
                var vs = GetVisionServiceByServiceAccount(
                    googe_service_account_json: googe_service_account_json,
                    applicationName: applicationName

                    );
                var loair = DetectLabels(vs, base64Image);
                // Check if label annotations were found
                if (loair != null)
                {
                    // Loop through and output label annotations for the image
                    var lod = new List <string> {
                    };
                    foreach (var air in loair)
                    {
                        foreach (var ea in air.LabelAnnotations)
                        {
                            var p = ((decimal)ea.Score).ToString("P1", CultureInfo.InvariantCulture);
                            lod.Add(ea.Description + " (" + p + ")");
                        }
                    }
                    //var lod = loair.Select(
                    // air => air.LabelAnnotations.Select(
                    //    ea=> ea.Description + " (" + (Math.Round((decimal)ea.Score,2) * 100).ToString() + "%)"
                    //   )
                    //   ).ToList();
                    summary = string.Join(", ", lod);
                }
                else
                {
                    LogIt.D("No labels found.");
                }
                return(true);
            } catch (Exception ex) {
                LogIt.E(ex);
            }
            return(false);
        }
Exemplo n.º 10
0
 public static string GetIsp(string ip, bool throwOnError = false)
 {
     try {
         using (WebServiceClient wsc = new MaxMind.GeoIP2.WebServiceClient(MAXMIND_WS_USER_ID, MAXMIND_WS_KEY)) {
             var ir = wsc.Insights(ip);
             LogIt.D(ir.Country.IsoCode + "|" + ir.City.Name);
             return(ir.Traits.Isp);
         }
     } catch (Exception ex) {
         LogIt.W(ip + "|" + ex.Message);
         if (throwOnError)
         {
             throw ex;
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 11
0
        public static string GetLocation(string ip, bool throwOnError = false, bool warnOnNotInDb = false)
        {
            string result = "";

            try {
                var cr = maxmindDr.City(ip);
                var r  = maxmindIspDr.Isp(ip);
                result = string.Join(",",
                                     new List <object> {
                    (cr == null) ? null : cr.City.Name,
                    (cr == null) ? null : cr.Country.IsoCode,
                    (r == null) ? null : r.Isp,
                    (r == null) ? null : r.Organization
                });
                return(result);
            } catch (Exception ex) {
                if (!warnOnNotInDb && ex.Message.Contains("is not in the database"))
                {
                    //LogIt.W(ip + "|" + ex.Message);
                }
                else
                {
                    LogIt.W(ip + "|" + ex.Message);
                }
                if (throwOnError)
                {
                    throw ex;
                }
                else
                {
                    return(null);
                }
            } finally {
                LogIt.D(result);
            }
        }