Host on edges

This commit is contained in:
2023-08-03 18:34:53 +01:00
parent 52ce412491
commit 5a2bd21cec
7 changed files with 125 additions and 4 deletions

View File

@ -1,6 +1,7 @@
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using MaxMind.GeoIP2;
using Newtonsoft.Json;
using Nostr.Client.Json;
using Nostr.Client.Keys;
@ -50,6 +51,46 @@ public static class Extensions
return !string.IsNullOrEmpty(user.Tags) ?
user.Tags.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) : Array.Empty<string>();
}
public static (double lat, double lon)? GetLocation(this HttpContext ctx, IGeoIP2DatabaseReader db)
{
var ip = ctx.GetRealIp();
var loc = db.TryCity(ip, out var city) ? city?.Location : default;
if ((loc?.Latitude.HasValue ?? false) && loc.Longitude.HasValue)
{
return (loc.Latitude.Value, loc.Longitude.Value);
}
return default;
}
public static string GetRealIp(this HttpContext ctx)
{
var cci = ctx.Request.Headers.TryGetValue("CF-Connecting-IP", out var xx) ? xx.ToString() : null;
if (!string.IsNullOrEmpty(cci))
{
return cci;
}
var xff = ctx.Request.Headers.TryGetValue("X-Forwarded-For", out var x) ? x.ToString() : null;
if (!string.IsNullOrEmpty(xff))
{
return xff.Split(",", StringSplitOptions.RemoveEmptyEntries).First();
}
return ctx.Connection.RemoteIpAddress!.ToString();
}
public static double GetDistance(double longitude, double latitude, double otherLongitude, double otherLatitude)
{
var d1 = latitude * (Math.PI / 180.0);
var num1 = longitude * (Math.PI / 180.0);
var d2 = otherLatitude * (Math.PI / 180.0);
var num2 = otherLongitude * (Math.PI / 180.0) - num1;
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
}
}
public class Variant