🐛 Fixes bugs, endless CA issue, and endless unsecure grpc
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using dotnet_etcd.interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
|
||||
namespace DysonNetwork.Gateway.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("/.well-known")]
|
||||
public class WellKnownController(
|
||||
IConfiguration configuration,
|
||||
IProxyConfigProvider proxyConfigProvider,
|
||||
IEtcdClient etcdClient)
|
||||
: ControllerBase
|
||||
{
|
||||
public class IpCheckResponse
|
||||
{
|
||||
[JsonPropertyName("remote_ip")] public string? RemoteIp { get; set; }
|
||||
[JsonPropertyName("x_forwarded_for")] public string? XForwardedFor { get; set; }
|
||||
[JsonPropertyName("x_forwarded_proto")] public string? XForwardedProto { get; set; }
|
||||
[JsonPropertyName("x_forwarded_host")] public string? XForwardedHost { get; set; }
|
||||
[JsonPropertyName("x_real_ip")] public string? XRealIp { get; set; }
|
||||
}
|
||||
|
||||
[HttpGet("ip-check")]
|
||||
public ActionResult<IpCheckResponse> GetIpCheck()
|
||||
{
|
||||
var ip = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
|
||||
var xForwardedFor = Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
var xForwardedProto = Request.Headers["X-Forwarded-Proto"].FirstOrDefault();
|
||||
var xForwardedHost = Request.Headers["X-Forwarded-Host"].FirstOrDefault();
|
||||
var realIp = Request.Headers["X-Real-IP"].FirstOrDefault();
|
||||
|
||||
return Ok(new IpCheckResponse
|
||||
{
|
||||
RemoteIp = ip,
|
||||
XForwardedFor = xForwardedFor,
|
||||
XForwardedProto = xForwardedProto,
|
||||
XForwardedHost = xForwardedHost,
|
||||
XRealIp = realIp
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet("domains")]
|
||||
public IActionResult GetDomainMappings()
|
||||
{
|
||||
var domainMappings = configuration.GetSection("DomainMappings").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
return Ok(domainMappings);
|
||||
}
|
||||
|
||||
[HttpGet("services")]
|
||||
public IActionResult GetServices()
|
||||
{
|
||||
var local = configuration.GetValue<bool>("LocalMode");
|
||||
var response = etcdClient.GetRange("/services/");
|
||||
var kvs = response.Kvs;
|
||||
|
||||
var serviceMap = kvs.ToDictionary(
|
||||
kv => Encoding.UTF8.GetString(kv.Key.ToByteArray()).Replace("/services/", ""),
|
||||
kv => Encoding.UTF8.GetString(kv.Value.ToByteArray())
|
||||
);
|
||||
|
||||
if (local) return Ok(serviceMap);
|
||||
|
||||
var domainMappings = configuration.GetSection("DomainMappings").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
foreach (var (key, _) in serviceMap.ToList())
|
||||
{
|
||||
if (!domainMappings.TryGetValue(key, out var domain)) continue;
|
||||
if (domain is not null)
|
||||
serviceMap[key] = "http://" + domain;
|
||||
}
|
||||
|
||||
return Ok(serviceMap);
|
||||
}
|
||||
|
||||
[HttpGet("routes")]
|
||||
public IActionResult GetProxyRules()
|
||||
{
|
||||
var config = proxyConfigProvider.GetConfig();
|
||||
var rules = config.Routes.Select(r => new
|
||||
{
|
||||
r.RouteId,
|
||||
r.ClusterId,
|
||||
Match = new
|
||||
{
|
||||
r.Match.Path,
|
||||
Hosts = r.Match.Hosts != null ? string.Join(", ", r.Match.Hosts) : null
|
||||
},
|
||||
Transforms = r.Transforms?.Select(t => t.Select(kv => $"{kv.Key}: {kv.Value}").ToList())
|
||||
}).ToList();
|
||||
|
||||
var clusters = config.Clusters.Select(c => new
|
||||
{
|
||||
c.ClusterId,
|
||||
Destinations = c.Destinations?.Select(d => new
|
||||
{
|
||||
d.Key,
|
||||
d.Value.Address
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
|
||||
return Ok(new { Rules = rules, Clusters = clusters });
|
||||
}
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["DysonNetwork.Gateway/DysonNetwork.Gateway.csproj", "DysonNetwork.Gateway/"]
|
||||
RUN dotnet restore "DysonNetwork.Gateway/DysonNetwork.Gateway.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/DysonNetwork.Gateway"
|
||||
RUN dotnet build "./DysonNetwork.Gateway.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./DysonNetwork.Gateway.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "DysonNetwork.Gateway.dll"]
|
@@ -1,24 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="dotnet-etcd" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.7" />
|
||||
<PackageReference Include="Nerdbank.GitVersioning" Version="3.7.115">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Yarp.ReverseProxy" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DysonNetwork.ServiceDefaults\DysonNetwork.ServiceDefaults.csproj" />
|
||||
<ProjectReference Include="..\DysonNetwork.Shared\DysonNetwork.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@@ -1,40 +0,0 @@
|
||||
using DysonNetwork.Gateway.Startup;
|
||||
using DysonNetwork.Shared.Http;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
builder.Host.UseContentRoot(Directory.GetCurrentDirectory());
|
||||
builder.WebHost.ConfigureKestrel(options =>
|
||||
{
|
||||
options.Limits.MaxRequestBodySize = long.MaxValue;
|
||||
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
|
||||
options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
|
||||
});
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddGateway(builder.Configuration);
|
||||
builder.Services.AddControllers();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapDefaultEndpoints();
|
||||
|
||||
app.ConfigureForwardedHeaders(app.Configuration);
|
||||
|
||||
app.UseRequestTimeouts();
|
||||
app.UseCors(opts =>
|
||||
opts.SetIsOriginAllowed(_ => true)
|
||||
.WithExposedHeaders("*")
|
||||
.WithHeaders("*")
|
||||
.AllowCredentials()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
);
|
||||
|
||||
app.MapControllers();
|
||||
app.MapReverseProxy();
|
||||
|
||||
app.Run();
|
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5094",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7034;http://0.0.0.0:5094",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,239 +0,0 @@
|
||||
using System.Text;
|
||||
using dotnet_etcd.interfaces;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
using Yarp.ReverseProxy.Forwarder;
|
||||
|
||||
namespace DysonNetwork.Gateway;
|
||||
|
||||
public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable
|
||||
{
|
||||
private readonly object _lock = new();
|
||||
private readonly IEtcdClient _etcdClient;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<RegistryProxyConfigProvider> _logger;
|
||||
private readonly CancellationTokenSource _watchCts = new();
|
||||
private CancellationTokenSource _cts;
|
||||
private IProxyConfig _config;
|
||||
|
||||
public RegistryProxyConfigProvider(
|
||||
IEtcdClient etcdClient,
|
||||
IConfiguration configuration,
|
||||
ILogger<RegistryProxyConfigProvider> logger
|
||||
)
|
||||
{
|
||||
_etcdClient = etcdClient;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_cts = new CancellationTokenSource();
|
||||
_config = LoadConfig();
|
||||
|
||||
// Watch for changes in etcd
|
||||
_etcdClient.WatchRange("/services/", _ =>
|
||||
{
|
||||
_logger.LogInformation("Etcd configuration changed. Reloading proxy config.");
|
||||
ReloadConfig();
|
||||
}, cancellationToken: _watchCts.Token);
|
||||
}
|
||||
|
||||
public IProxyConfig GetConfig() => _config;
|
||||
|
||||
private void ReloadConfig()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var oldCts = _cts;
|
||||
_cts = new CancellationTokenSource();
|
||||
_config = LoadConfig();
|
||||
oldCts.Cancel();
|
||||
oldCts.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private IProxyConfig LoadConfig()
|
||||
{
|
||||
_logger.LogInformation("Generating new proxy config.");
|
||||
var response = _etcdClient.GetRange("/services/");
|
||||
var kvs = response.Kvs;
|
||||
|
||||
var serviceMap = kvs.ToDictionary(
|
||||
kv => Encoding.UTF8.GetString(kv.Key.ToByteArray()).Replace("/services/", ""),
|
||||
kv => Encoding.UTF8.GetString(kv.Value.ToByteArray())
|
||||
);
|
||||
|
||||
var clusters = new List<ClusterConfig>();
|
||||
var routes = new List<RouteConfig>();
|
||||
|
||||
var domainMappings = _configuration.GetSection("DomainMappings").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
var pathAliases = _configuration.GetSection("PathAliases").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
var directRoutes = _configuration.GetSection("DirectRoutes").Get<List<DirectRouteConfig>>() ??
|
||||
[];
|
||||
|
||||
_logger.LogInformation("Indexing {ServiceCount} services from Etcd.", kvs.Count);
|
||||
|
||||
var gatewayServiceName = _configuration["Service:Name"];
|
||||
|
||||
// Add direct routes
|
||||
foreach (var directRoute in directRoutes)
|
||||
{
|
||||
if (serviceMap.TryGetValue(directRoute.Service, out var serviceUrl))
|
||||
{
|
||||
var existingCluster = clusters.FirstOrDefault(c => c.ClusterId == directRoute.Service);
|
||||
if (existingCluster is null)
|
||||
{
|
||||
var cluster = new ClusterConfig
|
||||
{
|
||||
ClusterId = directRoute.Service,
|
||||
Destinations = new Dictionary<string, DestinationConfig>
|
||||
{
|
||||
{ "destination1", new DestinationConfig { Address = serviceUrl } }
|
||||
},
|
||||
};
|
||||
clusters.Add(cluster);
|
||||
}
|
||||
|
||||
var route = new RouteConfig
|
||||
{
|
||||
RouteId = $"direct-{directRoute.Service}-{directRoute.Path.Replace("/", "-")}",
|
||||
ClusterId = directRoute.Service,
|
||||
Match = new RouteMatch { Path = directRoute.Path },
|
||||
};
|
||||
routes.Add(route);
|
||||
_logger.LogInformation(" Added Direct Route: {Path} -> {Service}", directRoute.Path,
|
||||
directRoute.Service);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning(" Direct route service {Service} not found in Etcd.", directRoute.Service);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var serviceName in serviceMap.Keys)
|
||||
{
|
||||
if (serviceName == gatewayServiceName)
|
||||
{
|
||||
_logger.LogInformation("Skipping gateway service: {ServiceName}", serviceName);
|
||||
continue;
|
||||
}
|
||||
|
||||
var serviceUrl = serviceMap[serviceName];
|
||||
|
||||
// Determine the path alias
|
||||
string? pathAlias;
|
||||
pathAlias = pathAliases.TryGetValue(serviceName, out var alias)
|
||||
? alias
|
||||
: serviceName.Split('.').Last().ToLowerInvariant();
|
||||
|
||||
_logger.LogInformation(" Service: {ServiceName}, URL: {ServiceUrl}, Path Alias: {PathAlias}", serviceName,
|
||||
serviceUrl, pathAlias);
|
||||
|
||||
// Check if the cluster already exists
|
||||
var existingCluster = clusters.FirstOrDefault(c => c.ClusterId == serviceName);
|
||||
if (existingCluster == null)
|
||||
{
|
||||
var cluster = new ClusterConfig
|
||||
{
|
||||
ClusterId = serviceName,
|
||||
Destinations = new Dictionary<string, DestinationConfig>
|
||||
{
|
||||
{ "destination1", new DestinationConfig { Address = serviceUrl } }
|
||||
}
|
||||
};
|
||||
clusters.Add(cluster);
|
||||
_logger.LogInformation(" Added Cluster: {ServiceName}", serviceName);
|
||||
}
|
||||
else if (existingCluster.Destinations is not null)
|
||||
{
|
||||
// Create a new cluster with merged destinations
|
||||
var newDestinations = new Dictionary<string, DestinationConfig>(existingCluster.Destinations)
|
||||
{
|
||||
{
|
||||
$"destination{existingCluster.Destinations.Count + 1}",
|
||||
new DestinationConfig { Address = serviceUrl }
|
||||
}
|
||||
};
|
||||
|
||||
var mergedCluster = new ClusterConfig
|
||||
{
|
||||
ClusterId = serviceName,
|
||||
Destinations = newDestinations
|
||||
};
|
||||
|
||||
// Replace the existing cluster with the merged one
|
||||
var index = clusters.IndexOf(existingCluster);
|
||||
clusters[index] = mergedCluster;
|
||||
|
||||
_logger.LogInformation(" Updated Cluster {ServiceName} with {DestinationCount} destinations",
|
||||
serviceName, mergedCluster.Destinations.Count);
|
||||
}
|
||||
|
||||
// Host-based routing
|
||||
if (domainMappings.TryGetValue(serviceName, out var domain) && domain is not null)
|
||||
{
|
||||
var hostRoute = new RouteConfig
|
||||
{
|
||||
RouteId = $"{serviceName}-host",
|
||||
ClusterId = serviceName,
|
||||
Match = new RouteMatch
|
||||
{
|
||||
Hosts = [domain],
|
||||
Path = "/{**catch-all}"
|
||||
},
|
||||
};
|
||||
routes.Add(hostRoute);
|
||||
_logger.LogInformation(" Added Host-based Route: {Host}", domain);
|
||||
}
|
||||
|
||||
// Path-based routing
|
||||
var pathRoute = new RouteConfig
|
||||
{
|
||||
RouteId = $"{serviceName}-path",
|
||||
ClusterId = serviceName,
|
||||
Match = new RouteMatch { Path = $"/{pathAlias}/{{**catch-all}}" },
|
||||
Transforms = new List<Dictionary<string, string>>
|
||||
{
|
||||
new() { { "PathRemovePrefix", $"/{pathAlias}" } },
|
||||
new() { { "PathPrefix", "/api" } },
|
||||
},
|
||||
Timeout = TimeSpan.FromSeconds(5)
|
||||
};
|
||||
routes.Add(pathRoute);
|
||||
_logger.LogInformation(" Added Path-based Route: {Path}", pathRoute.Match.Path);
|
||||
}
|
||||
|
||||
return new CustomProxyConfig(
|
||||
routes,
|
||||
clusters,
|
||||
new Microsoft.Extensions.Primitives.CancellationChangeToken(_cts.Token)
|
||||
);
|
||||
}
|
||||
|
||||
private class CustomProxyConfig(
|
||||
IReadOnlyList<RouteConfig> routes,
|
||||
IReadOnlyList<ClusterConfig> clusters,
|
||||
Microsoft.Extensions.Primitives.IChangeToken changeToken
|
||||
)
|
||||
: IProxyConfig
|
||||
{
|
||||
public IReadOnlyList<RouteConfig> Routes { get; } = routes;
|
||||
public IReadOnlyList<ClusterConfig> Clusters { get; } = clusters;
|
||||
public Microsoft.Extensions.Primitives.IChangeToken ChangeToken { get; } = changeToken;
|
||||
}
|
||||
|
||||
public record DirectRouteConfig
|
||||
{
|
||||
public required string Path { get; set; }
|
||||
public required string Service { get; set; }
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
_cts.Cancel();
|
||||
_cts.Dispose();
|
||||
_watchCts.Cancel();
|
||||
_watchCts.Dispose();
|
||||
}
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
using System.Net.Security;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
using Yarp.ReverseProxy.Transforms;
|
||||
|
||||
namespace DysonNetwork.Gateway.Startup;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddGateway(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddRequestTimeouts();
|
||||
|
||||
services
|
||||
.AddReverseProxy()
|
||||
.ConfigureHttpClient((context, handler) =>
|
||||
{
|
||||
// var caCert = X509CertificateLoader.LoadCertificateFromFile(configuration["CaCert"]!);
|
||||
handler.SslOptions = new SslClientAuthenticationOptions
|
||||
{
|
||||
RemoteCertificateValidationCallback = (sender, cert, chain, errors) => true
|
||||
};
|
||||
})
|
||||
.AddTransforms(context =>
|
||||
{
|
||||
context.CopyRequestHeaders = true;
|
||||
context.AddOriginalHost();
|
||||
context.AddForwarded(action: ForwardedTransformActions.Set);
|
||||
context.AddXForwarded(action: ForwardedTransformActions.Set);
|
||||
});
|
||||
|
||||
services.AddSingleton<IProxyConfigProvider, RegistryProxyConfigProvider>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
using DysonNetwork.Shared.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DysonNetwork.Gateway;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/version")]
|
||||
public class VersionController : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public IActionResult Get()
|
||||
{
|
||||
return Ok(new AppVersion
|
||||
{
|
||||
Version = ThisAssembly.AssemblyVersion,
|
||||
Commit = ThisAssembly.GitCommitId,
|
||||
UpdateDate = ThisAssembly.GitCommitDate
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
{
|
||||
"LocalMode": true,
|
||||
"CaCert": "../Certificates/ca.crt",
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"Etcd": "etcd.orb.local:2379"
|
||||
},
|
||||
"Etcd": {
|
||||
"Insecure": true
|
||||
},
|
||||
"Service": {
|
||||
"Name": "DysonNetwork.Gateway",
|
||||
"Url": "https://localhost:7034"
|
||||
},
|
||||
"DomainMappings": {
|
||||
"DysonNetwork.Pass": "id.solsynth.dev",
|
||||
"DysonNetwork.Drive": "drive.solsynth.dev",
|
||||
"DysonNetwork.Ring": "push.solsynth.dev",
|
||||
"DysonNetwork.Sphere": "sphere.solsynth.dev"
|
||||
},
|
||||
"PathAliases": {
|
||||
"DysonNetwork.Pass": "id",
|
||||
"DysonNetwork.Drive": "drive"
|
||||
},
|
||||
"DirectRoutes": [
|
||||
{
|
||||
"Path": "/ws",
|
||||
"Service": "DysonNetwork.Ring"
|
||||
},
|
||||
{
|
||||
"Path": "/api/tus",
|
||||
"Service": "DysonNetwork.Drive"
|
||||
},
|
||||
{
|
||||
"Path": "/.well-known/openid-configuration",
|
||||
"Service": "DysonNetwork.Pass"
|
||||
},
|
||||
{
|
||||
"Path": "/.well-known/jwks",
|
||||
"Service": "DysonNetwork.Pass"
|
||||
}
|
||||
]
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"version": "1.0",
|
||||
"publicReleaseRefSpec": ["^refs/heads/main$"],
|
||||
"cloudBuild": {
|
||||
"setVersionVariables": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user