fix(nodes): locate agents behind a proxy and retry missing countries [CI]
This commit is contained in:
1 parent
1145a53004
commit
0d532caf08
13 files changed
+93
-15
No files matched your search
@@ -20,6 +20,7 @@ public static class ApiModule
|
||||
services.AddSingleton<SseLimiter>();
|
||||
services.AddSingleton<MoShiro.Backend.Application.IReleaseKey, EmbeddedReleaseKey>();
|
||||
services.AddSingleton<ClientAddressResolver>();
|
||||
services.AddSingleton<Fleet.Server.IClientAddressSource>(p => p.GetRequiredService<ClientAddressResolver>());
|
||||
services.Configure<JsonOptions>(options =>
|
||||
{
|
||||
JsonSerializerOptions json = options.SerializerOptions;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace MoShiro.Backend.Api;
|
||||
/// 且从右往左跳过所有可信代理,取第一个不可信的地址(客户端自己伪造的在最左边,不会被采信)。
|
||||
/// 没配置就一律用套接字地址:宁可限流和审计里是代理的地址,也不让客户端自己报地址。
|
||||
/// </summary>
|
||||
public sealed class ClientAddressResolver
|
||||
public sealed class ClientAddressResolver : Fleet.Server.IClientAddressSource
|
||||
{
|
||||
private readonly IPNetwork[] _trusted;
|
||||
private readonly string _header;
|
||||
@@ -43,9 +43,13 @@ public sealed class ClientAddressResolver
|
||||
return _trusted.Any(n => n.Contains(normalized));
|
||||
}
|
||||
|
||||
public string Resolve(HttpContext http)
|
||||
string? Fleet.Server.IClientAddressSource.Resolve(IPAddress? socket, Func<string, IEnumerable<string>> header) => Resolve(socket, header);
|
||||
|
||||
public string Resolve(HttpContext http) =>
|
||||
Resolve(http.Connection.RemoteIpAddress, name => http.Request.Headers[name].Select(v => v ?? ""));
|
||||
|
||||
private string Resolve(IPAddress? remote, Func<string, IEnumerable<string>> headers)
|
||||
{
|
||||
IPAddress? remote = http.Connection.RemoteIpAddress;
|
||||
if (remote is null)
|
||||
{
|
||||
return "unknown";
|
||||
@@ -56,7 +60,7 @@ public sealed class ClientAddressResolver
|
||||
return remote.ToString();
|
||||
}
|
||||
|
||||
string[] chain = [.. http.Request.Headers[_header].SelectMany(v => (v ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))];
|
||||
string[] chain = [.. headers(_header).SelectMany(v => (v ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))];
|
||||
for (int i = chain.Length - 1; i >= 0; i--)
|
||||
{
|
||||
if (!IPAddress.TryParse(chain[i], out IPAddress? hop))
|
||||
|
||||
Reference in new issue
Block a user