123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using DnsClient;
- using Masuit.MyBlogs.Core.Common;
- using Masuit.MyBlogs.Core.Configs;
- using Masuit.Tools.AspNetCore.Mime;
- using Masuit.Tools.Core.Validator;
- using Masuit.Tools.Models;
- using MaxMind.GeoIP2.Exceptions;
- using MaxMind.GeoIP2.Responses;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Net.Http.Headers;
- using Newtonsoft.Json;
- using Polly;
- using System.Net;
- using TimeZoneConverter;
- namespace Masuit.MyBlogs.Core.Controllers;
- /// <summary>
- /// 黑科技
- /// </summary>
- [Route("tools")]
- public sealed class ToolsController : BaseController
- {
- private readonly HttpClient _httpClient;
- public ToolsController(IHttpClientFactory httpClientFactory)
- {
- _httpClient = httpClientFactory.CreateClient();
- }
- /// <summary>
- /// 获取ip地址详细信息
- /// </summary>
- /// <param name="ip"></param>
- /// <returns></returns>
- [Route("ip"), Route("ip/{ip?}", Order = 1), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "ip" })]
- public async Task<ActionResult> GetIpInfo([IsIPAddress] string ip)
- {
- if (!IPAddress.TryParse(ip, out var ipAddress))
- {
- ipAddress = ClientIP;
- }
- if (ipAddress.IsPrivateIP())
- {
- return Ok("内网IP");
- }
- ViewBag.IP = ip;
- var loc = ipAddress.GetIPLocation();
- var asn = ipAddress.GetIPAsn();
- var nslookup = new LookupClient();
- using var cts = new CancellationTokenSource(2000);
- var domain = await nslookup.QueryReverseAsync(ipAddress, cts.Token).ContinueWith(t => t.IsCompletedSuccessfully ? t.Result.Answers.Select(r => r.ToString()).Join("; ") : "无");
- var address = new IpInfo
- {
- Location = loc.Coodinate,
- Address = loc.Address,
- Address2 = loc.Address2,
- Network = new NetworkInfo
- {
- Asn = asn.AutonomousSystemNumber,
- Router = asn.Network + "",
- Organization = loc.ISP
- },
- TimeZone = loc.Coodinate.TimeZone + $" UTC{TZConvert.GetTimeZoneInfo(loc.Coodinate.TimeZone ?? "Asia/Shanghai").BaseUtcOffset.Hours:+#;-#;0}",
- IsProxy = loc.Network.Contains(new[] { "cloud", "Compute", "Serv", "Tech", "Solution", "Host", "云", "Datacenter", "Data Center", "Business", "ASN" }) || domain.Length > 1 || await ipAddress.IsProxy(cts.Token),
- Domain = domain
- };
- if (Request.Method.Equals(HttpMethods.Get) || (Request.Headers[HeaderNames.Accept] + "").StartsWith(ContentType.Json))
- {
- return View(address);
- }
- return Json(address);
- }
- /// <summary>
- /// 根据经纬度获取详细地理信息
- /// </summary>
- /// <param name="lat"></param>
- /// <param name="lng"></param>
- /// <returns></returns>
- [HttpGet("pos"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "lat", "lng" })]
- public async Task<ActionResult> Position(string lat, string lng)
- {
- if (string.IsNullOrEmpty(lat) || string.IsNullOrEmpty(lng))
- {
- var ip = ClientIP;
- #if DEBUG
- var r = new Random();
- ip = IPAddress.Parse($"{r.Next(210)}.{r.Next(255)}.{r.Next(255)}.{r.Next(255)}");
- #endif
- var location = Policy<CityResponse>.Handle<AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ip));
- var address = new PhysicsAddress()
- {
- Status = 0,
- AddressResult = new AddressResult()
- {
- FormattedAddress = ip.GetIPLocation().Address,
- Location = new Location()
- {
- Lng = (decimal)location.Location.Longitude.GetValueOrDefault(),
- Lat = (decimal)location.Location.Latitude.GetValueOrDefault()
- }
- }
- };
- return View(address);
- }
- using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
- var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={AppConfig.BaiduAK}", cts.Token).ContinueWith(t =>
- {
- if (t.IsCompletedSuccessfully)
- {
- return JsonConvert.DeserializeObject<PhysicsAddress>(t.Result);
- }
- return new PhysicsAddress();
- });
- return View(s);
- }
- /// <summary>
- /// 详细地理信息转经纬度
- /// </summary>
- /// <param name="addr"></param>
- /// <returns></returns>
- [Route("addr"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "addr" })]
- public async Task<ActionResult> Address(string addr)
- {
- if (string.IsNullOrEmpty(addr))
- {
- var ip = ClientIP;
- #if DEBUG
- Random r = new Random();
- ip = IPAddress.Parse($"{r.Next(210)}.{r.Next(255)}.{r.Next(255)}.{r.Next(255)}");
- #endif
- var location = Policy<CityResponse>.Handle<AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ip));
- var address = new PhysicsAddress()
- {
- Status = 0,
- AddressResult = new AddressResult
- {
- FormattedAddress = ip.GetIPLocation().Address,
- Location = new Location
- {
- Lng = (decimal)location.Location.Longitude.GetValueOrDefault(),
- Lat = (decimal)location.Location.Latitude.GetValueOrDefault()
- }
- }
- };
- ViewBag.Address = address.AddressResult.FormattedAddress;
- if (Request.Method.Equals(HttpMethods.Get) || (Request.Headers[HeaderNames.Accept] + "").StartsWith(ContentType.Json))
- {
- return View(address.AddressResult.Location);
- }
- return Json(address.AddressResult.Location);
- }
- ViewBag.Address = addr;
- using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
- var physicsAddress = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}", cts.Token).ContinueWith(t =>
- {
- if (t.IsCompletedSuccessfully)
- {
- return JsonConvert.DeserializeObject<PhysicsAddress>(t.Result);
- }
- return new PhysicsAddress();
- });
- if (Request.Method.Equals(HttpMethods.Get) || (Request.Headers[HeaderNames.Accept] + "").StartsWith(ContentType.Json))
- {
- return View(physicsAddress?.AddressResult?.Location);
- }
- return Json(physicsAddress?.AddressResult?.Location);
- }
- }
|