Compare commits
11 Commits
af492dedff
...
no-cache
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e9b61d9b2 | |||
| e68e3b14cd | |||
| 01ccc1ab91 | |||
| 14f8ea618a | |||
| 04d00e0fe9 | |||
| 8a18e71a6f | |||
| e49ec42f30 | |||
| 5b367121c4 | |||
| db05b0b815 | |||
| 16ee1f98ed | |||
| fec33bff1d |
@@ -10,7 +10,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -15,34 +15,50 @@ namespace AS1024.GeoFeed.Controllers
|
||||
{
|
||||
private readonly IGeoFeedProvider builder;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private readonly ILogger<GeofeedController> logger;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public GeofeedController(IGeoFeedProvider builder,
|
||||
IMemoryCache memoryCache) {
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment,
|
||||
ILogger<GeofeedController> logger) {
|
||||
this.logger = logger;
|
||||
this.builder = builder;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("")]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||
try
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
if (environment.IsProduction())
|
||||
{
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
}
|
||||
}
|
||||
|
||||
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||
string contentType = "text/csv";
|
||||
|
||||
return new FileContentResult(contentBytes, contentType)
|
||||
{
|
||||
FileDownloadName = "geofeed.csv"
|
||||
};
|
||||
} catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Geofeed generation failed. Exception: {ex}");
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
var csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||
var contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||
var contentType = "text/csv";
|
||||
|
||||
return new FileContentResult(contentBytes, contentType)
|
||||
{
|
||||
FileDownloadName = "geofeed.csv"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
|
||||
USER app
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
ARG TARGETARCH
|
||||
WORKDIR /src
|
||||
COPY ["AS1024.GeoFeed/AS1024.GeoFeed.csproj", "AS1024.GeoFeed/"]
|
||||
RUN dotnet restore "./AS1024.GeoFeed/./AS1024.GeoFeed.csproj"
|
||||
RUN dotnet restore "./AS1024.GeoFeed/./AS1024.GeoFeed.csproj" -a $TARGETARCH
|
||||
COPY . .
|
||||
WORKDIR "/src/AS1024.GeoFeed"
|
||||
RUN dotnet build "./AS1024.GeoFeed.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
RUN dotnet build "./AS1024.GeoFeed.csproj" -c $BUILD_CONFIGURATION -o /app/build -a $TARGETARCH
|
||||
|
||||
FROM build AS publish
|
||||
FROM --platform=$BUILDPLATFORM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./AS1024.GeoFeed.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
ARG TARGETARCH
|
||||
RUN dotnet publish "./AS1024.GeoFeed.csproj" -c $BUILD_CONFIGURATION -o /app/publish -a $TARGETARCH /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
|
||||
@@ -9,9 +9,9 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
{
|
||||
StringBuilder csvContent = new();
|
||||
|
||||
foreach (var feed in geoFeeds)
|
||||
foreach (IPGeoFeed feed in geoFeeds)
|
||||
{
|
||||
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},");
|
||||
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}");
|
||||
}
|
||||
|
||||
return csvContent.ToString();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using AS1024.GeoFeed.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Sockets;
|
||||
using System.Web;
|
||||
@@ -31,12 +31,12 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
|
||||
public async Task<List<IPGeoFeed>> GetGeoFeedData()
|
||||
{
|
||||
var geoFeed = new List<IPGeoFeed>();
|
||||
using var client = httpClientFactory.CreateClient();
|
||||
List<IPGeoFeed> geoFeed = new List<IPGeoFeed>();
|
||||
using HttpClient client = httpClientFactory.CreateClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]);
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
foreach (var family in addressFamilies)
|
||||
foreach (AddressFamily family in addressFamilies)
|
||||
{
|
||||
Uri uri = BuildNetBoxURI(family);
|
||||
NetboxData? jsonData = null;
|
||||
@@ -45,18 +45,20 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
{
|
||||
logger.LogDebug($"Making request to {uri}...");
|
||||
|
||||
using var result = await client.GetAsync(uri);
|
||||
using HttpResponseMessage result = await client.GetAsync(uri);
|
||||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
var stringResult = await result.Content.ReadAsStringAsync();
|
||||
jsonData = JsonConvert.DeserializeObject<NetboxData>(stringResult);
|
||||
string stringResult = await result.Content.ReadAsStringAsync();
|
||||
jsonData = JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions {
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
});
|
||||
|
||||
if (jsonData?.Results == null || jsonData.Results.Count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
foreach (var data in jsonData.Results)
|
||||
foreach (Result data in jsonData.Results)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -66,7 +68,8 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
GeolocCity = data.CustomFields.GeolocCity,
|
||||
GeolocRegion = data.CustomFields.GeolocRegion,
|
||||
GeolocCountry = data.CustomFields.GeolocCountry,
|
||||
GeolocHasLocation = data.CustomFields.GeolocHasLocation
|
||||
GeolocHasLocation = data.CustomFields.GeolocHasLocation,
|
||||
GeolocPostalCode = data.CustomFields.GeolocPostalCode
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -91,7 +94,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
|
||||
protected Uri BuildNetBoxURI(AddressFamily family)
|
||||
{
|
||||
var queryParameters = HttpUtility.ParseQueryString(string.Empty);
|
||||
System.Collections.Specialized.NameValueCollection queryParameters = HttpUtility.ParseQueryString(string.Empty);
|
||||
queryParameters["cf_geoloc_has_location"] = "true";
|
||||
queryParameters["limit"] = "50";
|
||||
|
||||
@@ -108,7 +111,7 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
break;
|
||||
}
|
||||
|
||||
var endUrl = new UriBuilder
|
||||
UriBuilder endUrl = new UriBuilder
|
||||
{
|
||||
Path = "api/ipam/prefixes/",
|
||||
Query = queryParameters.ToString(),
|
||||
|
||||
@@ -9,22 +9,37 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
||||
private readonly ILogger<PreLoadGeoFeed> logger;
|
||||
private readonly IGeoFeedProvider provider;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public PreLoadGeoFeed(ILogger<PreLoadGeoFeed> logger,
|
||||
IGeoFeedProvider provider,
|
||||
IMemoryCache memoryCache)
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.provider = provider;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (environment.IsProduction())
|
||||
await StartPreLoad();
|
||||
} catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning($"Failed to preload, exception settings below:\n{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StartPreLoad()
|
||||
{
|
||||
logger.LogInformation("Preloading GeoFeed data in memory...");
|
||||
var feed = await provider.GetGeoFeedData();
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||
List<Models.IPGeoFeed> feed = await provider.GetGeoFeedData();
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
namespace AS1024.GeoFeed.Models
|
||||
{
|
||||
public class NetboxData
|
||||
@@ -12,24 +11,40 @@ namespace AS1024.GeoFeed.Models
|
||||
public class Result
|
||||
{
|
||||
public string? Prefix { get; set; }
|
||||
[JsonProperty("custom_fields")]
|
||||
public CustomFields? CustomFields { get; set; }
|
||||
}
|
||||
|
||||
public class CustomFields
|
||||
{
|
||||
[JsonProperty("geoloc_city")]
|
||||
/// <summary>
|
||||
/// Represents the city associated with the IP address. This field is optional.
|
||||
/// </summary>
|
||||
public string? GeolocCity { get; set; }
|
||||
[JsonProperty("geoloc_country")]
|
||||
/// <summary>
|
||||
/// Represents the country associated with the IP address. This field is optional and expected to be a selection field in NetBox.
|
||||
/// </summary>
|
||||
public string? GeolocCountry { get; set; }
|
||||
[JsonProperty("geoloc_has_location")]
|
||||
/// <summary>
|
||||
/// Indicates whether geolocation data is available for the IP address. This field is required.
|
||||
/// </summary>
|
||||
public bool? GeolocHasLocation { get; set; }
|
||||
[JsonProperty("geoloc_region")]
|
||||
/// <summary>
|
||||
/// Represents the region or state associated with the IP address. This field is optional and expected to be a selection field in NetBox.
|
||||
/// </summary>
|
||||
public string? GeolocRegion { get; set; }
|
||||
/// <summary>
|
||||
/// Represents the postal code associated with the IP address. This field is optional.
|
||||
/// </summary>
|
||||
public string? GeolocPostalCode { get; set; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This class represents the IP GeoFeed Entry
|
||||
/// </summary>
|
||||
public class IPGeoFeed : CustomFields {
|
||||
/// <summary>
|
||||
/// Represents the IP Prefix for the associated GeoFeed entry
|
||||
/// </summary>
|
||||
public string? Prefix { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user