Example #1
0
 public static void GeoIPRoute(IEndpointRouteBuilder endpoints)
 {
     endpoints.Map(Config.IpPerfix,
                   async context => await context.WriteResponseAsync(RealIP.Get(context).ToString()));
     endpoints.Map(Config.IpPerfix + "/source", async context =>
     {
         var jObject = new JObject
         {
             { "IP", RealIP.Get(context).ToString() }, { "UserHostAddress", context.Connection.RemoteIpAddress.ToString() }
         };
         if (context.Request.Headers.TryGetValue("X-Forwarded-For", out var xFwdValue))
         {
             jObject.Add("X-Forwarded-For", xFwdValue.ToString());
         }
         if (context.Request.Headers.TryGetValue("CF-Connecting-IP", out var xCfValue))
         {
             jObject.Add("CF-Connecting-IP", xCfValue.ToString());
         }
         if (context.Request.Headers.TryGetValue("X-Real-IP", out var xRealValue))
         {
             jObject.Add("X-Real-IP", xRealValue.ToString());
         }
         await context.WriteResponseAsync(jObject.ToString(), type: "application/json");
     });
     endpoints.Map(Config.IpPerfix + "/json", async context =>
     {
         var(responseAsn, responseCity) = GeoIP.GetAsnCityValueTuple(context.Request.Query.ContainsKey("ip")
             ? IPAddress.Parse(context.Request.Query["ip"].ToString())
             : RealIP.Get(context));
         var jObject = new JObject
         {
             { "IP", responseAsn.IPAddress },
             { "ASN", responseAsn.AutonomousSystemNumber },
             { "Organization", responseAsn.AutonomousSystemOrganization },
             { "CountryCode", responseCity.Country.IsoCode },
             { "Country", responseCity.Country.Name }
         };
         if (!string.IsNullOrWhiteSpace(responseCity.MostSpecificSubdivision.IsoCode))
         {
             jObject.Add("ProvinceCode", responseCity.MostSpecificSubdivision.IsoCode);
         }
         if (!string.IsNullOrWhiteSpace(responseCity.MostSpecificSubdivision.Name))
         {
             jObject.Add("Province", responseCity.MostSpecificSubdivision.Name);
         }
         if (!string.IsNullOrWhiteSpace(responseCity.City.Name))
         {
             jObject.Add("City", responseCity.City.Name);
         }
         var cnIsp = GeoIP.GetCnISP(responseAsn, responseCity);
         if (!string.IsNullOrWhiteSpace(cnIsp))
         {
             jObject.Add("ISP", cnIsp);
         }
         await context.WriteResponseAsync(jObject.ToString(), type: "application/json");
     });
 }
Example #2
0
        private static void GeoIPRoute(IEndpointRouteBuilder endpoints)
        {
            endpoints.Map(Config.IpPerfix, async context =>
            {
                context.Response.ContentType = "text/plain";
                await context.Response.WriteAsync(RealIP.Get(context).ToString());
            });
            endpoints.Map(Config.IpPerfix + "/source", async context =>
            {
                var jObject = new JObject
                {
                    { "IP", RealIP.Get(context) },
                    {
                        "UserHostAddress",
                        context.Connection.RemoteIpAddress.ToString()
                    }
                };
                if (context.Request.Headers.ContainsKey("X-Forwarded-For"))
                {
                    jObject.Add("X-Forwarded-For", context.Request.Headers["X-Forwarded-For"].ToString());
                }
                if (context.Request.Headers.ContainsKey("CF-Connecting-IP"))
                {
                    jObject.Add("CF-Connecting-IP", context.Request.Headers["CF-Connecting-IP"].ToString());
                }
                if (context.Request.Headers.ContainsKey("X-Real-IP"))
                {
                    jObject.Add("X-Real-IP", context.Request.Headers["X-Real-IP"].ToString());
                }
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(jObject.ToString());
            });
            endpoints.Map(Config.IpPerfix + "/json", async context =>
            {
                var jObject = new JObject();
                var asnCity = GeoIP.GetAsnCityValueTuple(context.Request.Query.ContainsKey("ip")
                    ? context.Request.Query["ip"].ToString()
                    : RealIP.Get(context));
                var responseAsn  = asnCity.Item1;
                var responseCity = asnCity.Item2;
                jObject.Add("IP", responseAsn.IPAddress);
                jObject.Add("ASN", responseAsn.AutonomousSystemNumber);
                jObject.Add("Organization", responseAsn.AutonomousSystemOrganization);
                jObject.Add("CountryCode", responseCity.Country.IsoCode);
                jObject.Add("Country", responseCity.Country.Name);
                if (!string.IsNullOrWhiteSpace(responseCity.MostSpecificSubdivision.IsoCode))
                {
                    jObject.Add("ProvinceCode", responseCity.MostSpecificSubdivision.IsoCode);
                }
                if (!string.IsNullOrWhiteSpace(responseCity.MostSpecificSubdivision.Name))
                {
                    jObject.Add("Province", responseCity.MostSpecificSubdivision.Name);
                }
                if (!string.IsNullOrWhiteSpace(responseCity.City.Name))
                {
                    jObject.Add("City", responseCity.City.Name);
                }
                var cnIsp = GeoIP.GetCnISP(responseAsn, responseCity);
                if (!string.IsNullOrWhiteSpace(cnIsp))
                {
                    jObject.Add("ISP", cnIsp);
                }

                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(jObject.ToString());
            });
        }