Deleted Niko and Relay auth

This commit is contained in:
linmeideli committed 2026-09-25 10:20:17 +08:00
1 parent 8767bcbd4d
commit edfff9ae19
15 files changed
+5 -493

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-11
View File
@@ -3,9 +3,6 @@ namespace Empostor.Api.Config;
public enum AuthApiMode
{
Innersloth,
Niko,
Both,
Relay,
Ume,
}
@@ -15,14 +12,6 @@ public class AuthApiConfig
public AuthApiMode Mode { get; set; } = AuthApiMode.Innersloth;
public string NikoApiKey { get; set; } = string.Empty;
public string NikoApiBaseUrl { get; set; } = "https://au-verify.niko233.top";
public string RelayApiBaseUrl { get; set; } = "http://localhost:5100";
public string RelayApiKey { get; set; } = string.Empty;
public string UmeApiBaseUrl { get; set; } = "https://auverify.hayashiume.top";
public string UmeApiKey { get; set; } = "sk-empostor-globalapikey";
+3 -353
View File
@@ -30,9 +30,6 @@ public sealed class TokenController : ControllerBase
private static readonly object FileLock = new object();
private string? _nikoVerifyCode;
private bool _nikoFriendCodeConfirmed;
private readonly ILogger<TokenController> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly AuthCacheService _authCache;
@@ -77,15 +74,11 @@ public sealed class TokenController : ControllerBase
return Unauthorized(new { error = "Invalid token content" });
}
_nikoVerifyCode = null;
_nikoFriendCodeConfirmed = false;
var friendCode = await GetFriendCodeAsync(eosToken, productUserId);
var matchmakerToken = GenerateMatchmakerToken(productUserId);
var clientIp = GetClientIp();
_authCache.Store(productUserId, matchmakerToken, friendCode, clientIp,
verifyCode: _nikoVerifyCode, friendCodeConfirmed: _nikoFriendCodeConfirmed);
_authCache.Store(productUserId, matchmakerToken, friendCode, clientIp);
// Allocate a delta UDP port to match the TCP auth session to the
// subsequent UDP connection. In fixed-port mode (pool disabled) no
@@ -99,8 +92,6 @@ public sealed class TokenController : ControllerBase
FriendCode = friendCode ?? string.Empty,
ClientIp = clientIp != null ? NormalizeIpString(clientIp) : null,
CreatedAt = DateTime.UtcNow,
VerifyCode = _nikoVerifyCode,
FriendCodeConfirmed = _nikoFriendCodeConfirmed,
};
// Extract name from friend code (e.g., "ufolud" from "ufolud#1234")
@@ -225,51 +216,11 @@ public sealed class TokenController : ControllerBase
string? friendCode = null;
if (mode == AuthApiMode.Relay)
{
friendCode = await FetchFromRelayAsync(eosToken, productUserId);
}
if (string.IsNullOrEmpty(friendCode) && mode == AuthApiMode.Ume)
if (mode == AuthApiMode.Ume)
{
friendCode = await FetchFromUmeAsync(eosToken, productUserId);
}
if (string.IsNullOrEmpty(friendCode) && mode == AuthApiMode.Niko)
{
friendCode = await FetchFromNikoAsync(eosToken, productUserId);
}
if (string.IsNullOrEmpty(friendCode) && mode == AuthApiMode.Both)
{
var nikoKeyIsCustom = !string.IsNullOrEmpty(_authApiConfig.NikoApiKey)
&& _authApiConfig.NikoApiKey != "niko-request-api-key";
if (nikoKeyIsCustom)
{
_logger.LogDebug("TokenController Both mode: trying Niko first (custom key)");
friendCode = await FetchFromNikoAsync(eosToken, productUserId);
if (string.IsNullOrEmpty(friendCode))
{
friendCode = await FetchFromUmeAsync(eosToken, productUserId);
}
}
else
{
_logger.LogDebug("TokenController Both mode: trying Ume first (default Niko key)");
friendCode = await FetchFromUmeAsync(eosToken, productUserId);
if (string.IsNullOrEmpty(friendCode))
{
friendCode = await FetchFromNikoAsync(eosToken, productUserId);
}
}
if (string.IsNullOrEmpty(friendCode))
{
friendCode = await FetchFromInnerslothAsync(eosToken, productUserId);
}
}
if (string.IsNullOrEmpty(friendCode) && mode == AuthApiMode.Innersloth)
{
friendCode = await FetchFromInnerslothAsync(eosToken, productUserId);
@@ -394,238 +345,6 @@ public sealed class TokenController : ControllerBase
return null;
}
private async Task<string?> FetchFromNikoAsync(string eosToken, string productUserId)
{
if (string.IsNullOrEmpty(_authApiConfig.NikoApiKey))
{
_logger.LogWarning("TokenControllerNikoApiKey is empty, skipping Niko API");
return null;
}
var baseUrl = _authApiConfig.NikoApiBaseUrl.TrimEnd('/');
var apiUrl = $"{baseUrl}/api/verify";
try
{
using var client = _httpClientFactory.CreateClient("niko");
// PUT to create a verification request
var putBody = JsonSerializer.SerializeToUtf8Bytes(new NikoPutRequest
{
ApiKey = _authApiConfig.NikoApiKey,
FriendCode = string.Empty,
});
var putReq = new HttpRequestMessage(HttpMethod.Put, apiUrl)
{
Content = new ByteArrayContent(putBody),
};
putReq.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
var putResp = await client.SendAsync(putReq);
if (!putResp.IsSuccessStatusCode)
{
_logger.LogWarning(
"TokenControllerNiko PUT returned {Status} for PUID={Puid}",
putResp.StatusCode, productUserId);
return null;
}
var putJson = await putResp.Content.ReadAsStringAsync();
var createResult = JsonSerializer.Deserialize<NikoCreateResponse>(putJson);
if (createResult == null || string.IsNullOrEmpty(createResult.VerifyCode))
{
_logger.LogWarning(
"TokenControllerNiko PUT response missing VerifyCode for PUID={Puid}", productUserId);
return null;
}
var verifyCode = createResult.VerifyCode;
_nikoVerifyCode = verifyCode;
_logger.LogInformation(
"TokenControllerNiko verify request created: Code={Code} for PUID={Puid}",
verifyCode, productUserId);
// Proxy EOS token to Niko to trigger HTTP auth
await ProxyEosTokenToNikoAsync(client, baseUrl, eosToken, productUserId);
// Poll GET briefly for verification result
var queryUrl = $"{apiUrl}?apikey={Uri.EscapeDataString(_authApiConfig.NikoApiKey)}&verifycode={Uri.EscapeDataString(verifyCode)}";
for (var i = 0; i < 2; i++)
{
if (i > 0)
{
await Task.Delay(300);
}
var getReq = new HttpRequestMessage(HttpMethod.Get, queryUrl);
var getResp = await client.SendAsync(getReq);
if (!getResp.IsSuccessStatusCode)
{
continue;
}
var getJson = await getResp.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<NikoVerifyApiResponse>(getJson);
if (result == null)
{
continue;
}
if (!string.IsNullOrEmpty(result.FriendCode)
&& (result.VerifyStatus == "HttpPending" || result.VerifyStatus == "Verified"))
{
if (!string.IsNullOrEmpty(result.Puid)
&& !string.Equals(result.Puid, productUserId, StringComparison.OrdinalIgnoreCase))
{
_logger.LogWarning(
"TokenControllerNiko PUID mismatch: expected={Expected} got={Got}",
productUserId, result.Puid);
return null;
}
_nikoFriendCodeConfirmed = true;
_logger.LogInformation(
"TokenControllerFriendCode fetched from Niko: PUID={Puid} FC={FC} Status={Status}",
productUserId, result.FriendCode, result.VerifyStatus);
_ = DeleteNikoVerificationAsync(client, apiUrl, verifyCode);
return result.FriendCode;
}
_logger.LogDebug(
"TokenControllerNiko poll {Attempt}: Status={Status} for PUID={Puid}",
i + 1, result.VerifyStatus ?? "null", productUserId);
}
_logger.LogInformation(
"TokenControllerNiko friend code not yet available for PUID={Puid}, VerifyCode={Code} deferred to handshake",
productUserId, verifyCode);
}
catch (Exception ex)
{
_logger.LogError(ex, "TokenControllerException calling Niko API for PUID={Puid}", productUserId);
}
return null;
}
private async Task ProxyEosTokenToNikoAsync(HttpClient client, string baseUrl, string eosToken, string productUserId)
{
try
{
var userApiUrl = $"{baseUrl}/api/user";
var req = new HttpRequestMessage(HttpMethod.Post, userApiUrl)
{
Content = new StringContent("{}", Encoding.UTF8, "application/json"),
};
req.Headers.TryAddWithoutValidation("Authorization", "Bearer " + eosToken);
req.Headers.TryAddWithoutValidation("Accept", "application/vnd.api+json");
var resp = await client.SendAsync(req);
_logger.LogDebug(
"TokenControllerNiko proxy auth returned {Status} for PUID={Puid}",
resp.StatusCode, productUserId);
}
catch (Exception ex)
{
_logger.LogWarning(ex,
"TokenControllerFailed to proxy EOS token to Niko for PUID={Puid}", productUserId);
}
}
private async Task DeleteNikoVerificationAsync(HttpClient client, string apiUrl, string verifyCode)
{
try
{
var body = JsonSerializer.SerializeToUtf8Bytes(new NikoDeleteRequest
{
ApiKey = _authApiConfig.NikoApiKey,
VerifyCode = verifyCode,
});
var req = new HttpRequestMessage(HttpMethod.Delete, apiUrl)
{
Content = new ByteArrayContent(body),
};
req.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
await client.SendAsync(req);
}
catch
{
// Best-effort cleanup
}
}
private async Task<string?> FetchFromRelayAsync(string eosToken, string productUserId)
{
if (string.IsNullOrEmpty(_authApiConfig.RelayApiBaseUrl)
|| string.IsNullOrEmpty(_authApiConfig.RelayApiKey))
{
_logger.LogWarning("TokenControllerRelayApi config incomplete, skipping relay");
return null;
}
try
{
using var client = _httpClientFactory.CreateClient("relay");
var body = JsonSerializer.SerializeToUtf8Bytes(new RelayVerifyRequest
{
EosToken = eosToken,
ProductUserId = productUserId,
});
var req = new HttpRequestMessage(HttpMethod.Post,
$"{_authApiConfig.RelayApiBaseUrl.TrimEnd('/')}/api/verify")
{
Content = new ByteArrayContent(body),
};
req.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _authApiConfig.RelayApiKey);
req.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
var resp = await client.SendAsync(req);
if (!resp.IsSuccessStatusCode)
{
_logger.LogWarning(
"TokenControllerRelay returned {Status} for PUID={Puid}",
resp.StatusCode, productUserId);
return null;
}
var json = await resp.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<RelayVerifyResult>(json);
if (result == null || string.IsNullOrEmpty(result.FriendCode))
{
_logger.LogWarning(
"TokenControllerRelay response missing FriendCode for PUID={Puid}",
productUserId);
return null;
}
if (string.IsNullOrEmpty(result.ProductUserId)
|| !string.Equals(result.ProductUserId, productUserId, StringComparison.OrdinalIgnoreCase))
{
_logger.LogWarning(
"TokenControllerRelay PUID mismatch: expected={Expected} got={Got}",
productUserId, result.ProductUserId);
return null;
}
_logger.LogInformation(
"TokenControllerFriendCode from Relay: PUID={Puid} FC={FC}",
productUserId, result.FriendCode);
return result.FriendCode;
}
catch (Exception ex)
{
_logger.LogError(ex, "TokenControllerException calling Relay for PUID={Puid}", productUserId);
}
return null;
}
private async Task<string?> FetchFromUmeAsync(string eosToken, string productUserId)
{
if (string.IsNullOrEmpty(_authApiConfig.UmeApiBaseUrl)
@@ -720,7 +439,7 @@ public sealed class TokenController : ControllerBase
return $"failauth#{disc:D4}";
}
#region Niko Request Datas
#region Request Datas
public sealed class TokenRequest
{
@@ -752,75 +471,6 @@ public sealed class TokenController : ControllerBase
public DateTime ExpiresAt { get; init; } = new DateTime(2012, 12, 21);
}
private sealed class NikoPutRequest
{
[JsonPropertyName("ApiKey")]
public required string ApiKey { get; init; }
[JsonPropertyName("FriendCode")]
public string FriendCode { get; init; } = string.Empty;
}
private sealed class NikoCreateResponse
{
[JsonPropertyName("VerifyStatus")]
public string? VerifyStatus { get; init; }
[JsonPropertyName("VerifyCode")]
public string? VerifyCode { get; init; }
[JsonPropertyName("FriendCode")]
public string? FriendCode { get; init; }
[JsonPropertyName("ExpiresAt")]
public string? ExpiresAt { get; init; }
}
private sealed class NikoVerifyApiResponse
{
[JsonPropertyName("VerifyStatus")]
public string? VerifyStatus { get; init; }
[JsonPropertyName("FriendCode")]
public string? FriendCode { get; init; }
[JsonPropertyName("Puid")]
public string? Puid { get; init; }
[JsonPropertyName("PlayerName")]
public string? PlayerName { get; init; }
[JsonPropertyName("TokenPlatform")]
public string? TokenPlatform { get; init; }
}
private sealed class NikoDeleteRequest
{
[JsonPropertyName("apikey")]
public required string ApiKey { get; init; }
[JsonPropertyName("verifycode")]
public required string VerifyCode { get; init; }
}
private sealed class RelayVerifyRequest
{
[JsonPropertyName("EosToken")]
public required string EosToken { get; init; }
[JsonPropertyName("ProductUserId")]
public required string ProductUserId { get; init; }
}
private sealed class RelayVerifyResult
{
[JsonPropertyName("FriendCode")]
public string? FriendCode { get; init; }
[JsonPropertyName("ProductUserId")]
public string? ProductUserId { get; init; }
}
private sealed class UmeVerifyRequest
{
[JsonPropertyName("ApiKey")]
@@ -2,8 +2,6 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Empostor.Api.Config;
@@ -34,8 +32,6 @@ namespace Empostor.Server.Net.Manager
private readonly IClientFactory _clientFactory;
private readonly AuthCacheService _authCache;
private readonly PlayerConnectStore _playerConnectStore;
private readonly IHttpClientFactory _httpClientFactory;
private readonly AuthApiConfig _authApiConfig;
private readonly PortPoolService _portPool;
private readonly IpGeolocationService _ipGeo;
private readonly ClientIdStore _clientIdStore;
@@ -49,8 +45,6 @@ namespace Empostor.Server.Net.Manager
IOptions<CompatibilityConfig> compatibilityConfig,
AuthCacheService authCache,
PlayerConnectStore playerConnectStore,
IHttpClientFactory httpClientFactory,
IOptions<AuthApiConfig> authApiConfig,
PortPoolService portPool,
IpGeolocationService ipGeo,
ClientIdStore clientIdStore)
@@ -63,8 +57,6 @@ namespace Empostor.Server.Net.Manager
_compatibilityConfig = compatibilityConfig.Value;
_authCache = authCache;
_playerConnectStore = playerConnectStore;
_httpClientFactory = httpClientFactory;
_authApiConfig = authApiConfig.Value;
_portPool = portPool;
_ipGeo = ipGeo;
_clientIdStore = clientIdStore;
@@ -187,27 +179,6 @@ namespace Empostor.Server.Net.Manager
friendCode = authInfo.FriendCode;
if (!string.IsNullOrEmpty(authInfo.VerifyCode) && !authInfo.FriendCodeConfirmed)
{
var nikoResult = await QueryNikoVerifyAsync(authInfo.VerifyCode);
if (nikoResult != null)
{
if (!string.IsNullOrEmpty(nikoResult.Value.Puid)
&& string.Equals(nikoResult.Value.Puid, authInfo.ProductUserId, StringComparison.OrdinalIgnoreCase))
{
friendCode = nikoResult.Value.FriendCode;
authInfo.FriendCode = friendCode;
authInfo.FriendCodeConfirmed = true;
}
else
{
_logger.LogWarning(
"Niko PUID mismatch for {Name}: expected {Expected} got {Got}",
name, authInfo.ProductUserId, nikoResult.Value.Puid);
}
}
}
_logger.LogInformation(
"#{Id} {Name} │ port {Port} │ {Location} │ {Lang} │ {Platform} │ FC {FriendCode} │ {HashPuid}{Reactor}",
id, name, deltaPort, locationStr, lang, platformStr, friendCode ?? "unknown", HashPuid(authInfo.ProductUserId), reactorStr);
@@ -252,70 +223,6 @@ namespace Empostor.Server.Net.Manager
&& _clients.TryGetValue(client.Id, out var c)
&& ReferenceEquals(client, c);
private async Task<(string FriendCode, string? Puid)?> QueryNikoVerifyAsync(string verifyCode)
{
var baseUrl = _authApiConfig.NikoApiBaseUrl.TrimEnd('/');
var apiUrl = $"{baseUrl}/api/verify";
var queryUrl = $"{apiUrl}?apikey={Uri.EscapeDataString(_authApiConfig.NikoApiKey)}&verifycode={Uri.EscapeDataString(verifyCode)}";
try
{
using var client = _httpClientFactory.CreateClient("niko");
var resp = await client.GetAsync(queryUrl);
if (!resp.IsSuccessStatusCode)
{
return null;
}
var json = await resp.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var status = root.TryGetProperty("VerifyStatus", out var s) ? s.GetString() : null;
var friendCode = root.TryGetProperty("FriendCode", out var fc) ? fc.GetString() : null;
var puid = root.TryGetProperty("Puid", out var p) ? p.GetString() : null;
if (string.IsNullOrEmpty(friendCode)
|| (status != "HttpPending" && status != "Verified"))
{
_logger.LogDebug("Niko GET status={Status} for VerifyCode={Code}", status, verifyCode);
return null;
}
_logger.LogInformation(
"Niko GET success: FC={FC} PUID={Puid} Status={Status}",
friendCode, puid, status);
_ = DeleteNikoVerifyAsync(apiUrl, verifyCode);
return (friendCode, puid);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Niko GET failed for VerifyCode={Code}", verifyCode);
return null;
}
}
private async Task DeleteNikoVerifyAsync(string apiUrl, string verifyCode)
{
try
{
using var client = _httpClientFactory.CreateClient("niko");
var body = JsonSerializer.SerializeToUtf8Bytes(new { apikey = _authApiConfig.NikoApiKey, verifycode = verifyCode });
var req = new HttpRequestMessage(HttpMethod.Delete, apiUrl)
{
Content = new ByteArrayContent(body),
};
req.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
await client.SendAsync(req);
}
catch
{
// Best-effort cleanup
}
}
private static string NormalizeIp(IPAddress? addr)
{
if (addr == null)
-11
View File
@@ -140,17 +140,6 @@ namespace Empostor.Server
"UnityPlayer/2022.3.44f1 (UnityWebRequest/1.0, libcurl/7.84.0-DEV)");
client.DefaultRequestHeaders.Add("X-Unity-Version", "2022.3.44f1");
});
services.AddHttpClient("niko", client =>
{
client.Timeout = TimeSpan.FromSeconds(15);
client.DefaultRequestHeaders.Add("User-Agent",
"UnityPlayer/2022.3.44f1 (UnityWebRequest/1.0, libcurl/7.84.0-DEV)");
client.DefaultRequestHeaders.Add("X-Unity-Version", "2022.3.44f1");
});
services.AddHttpClient("relay", client =>
{
client.Timeout = TimeSpan.FromSeconds(15);
});
services.AddHttpClient("ume", client =>
{
client.Timeout = TimeSpan.FromSeconds(15);
@@ -35,8 +35,7 @@ public sealed class AuthCacheService : IDisposable
TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
}
public void Store(string productUserId, string matchmakerToken, string? friendCode, IPAddress? clientIp,
string? verifyCode = null, bool friendCodeConfirmed = false)
public void Store(string productUserId, string matchmakerToken, string? friendCode, IPAddress? clientIp)
{
if (string.IsNullOrEmpty(productUserId) || string.IsNullOrEmpty(matchmakerToken))
{
@@ -50,8 +49,6 @@ public sealed class AuthCacheService : IDisposable
FriendCode = friendCode ?? string.Empty,
ClientIp = clientIp != null ? NormalizeIp(clientIp) : null,
CreatedAt = DateTime.UtcNow,
VerifyCode = verifyCode,
FriendCodeConfirmed = friendCodeConfirmed,
};
_byToken[matchmakerToken] = info;
@@ -112,18 +109,6 @@ public sealed class AuthCacheService : IDisposable
public (int TokenCount, int PortCount) GetStats()
=> (_byToken.Count, _byPort.Count);
public bool UpdateFriendCode(string matchmakerToken, string friendCode)
{
if (!_byToken.TryGetValue(matchmakerToken, out var info) || Expired(info))
{
return false;
}
info.FriendCode = friendCode;
info.FriendCodeConfirmed = true;
return true;
}
private void Cleanup()
{
// Clean token-based entries
@@ -182,8 +167,4 @@ public sealed class UserAuthInfo
public string? ClientIp { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public string? VerifyCode { get; set; }
public bool FriendCodeConfirmed { get; set; }
}
+1 -5
View File
@@ -43,11 +43,7 @@
"GameRecorderPath": ""
},
"AuthApi": {
"Mode": "Both",
"NikoApiKey": "niko-request-api-key",
"NikoApiBaseUrl": "https://au-verify.niko233.top",
"RelayApiBaseUrl": "http://localhost:5100",
"RelayApiKey": "empostor-relay-api-key-change-me",
"Mode": "Ume",
"UmeApiBaseUrl": "https://auverify.hayashiume.top",
"UmeApiKey": "sk-empostor-globalapikey"
},