From 68caa8d7a0ecb97e324501be83a7a6f116e96f03 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sun, 30 Oct 2022 20:39:12 +0100 Subject: [PATCH] Include user-agent in remote requests --- Pal.Client/RemoteApi.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Pal.Client/RemoteApi.cs b/Pal.Client/RemoteApi.cs index 523d0d6..3082fa1 100644 --- a/Pal.Client/RemoteApi.cs +++ b/Pal.Client/RemoteApi.cs @@ -1,5 +1,6 @@ using Account; using Grpc.Core; +using Grpc.Core.Interceptors; using Grpc.Net.Client; using Palace; using System; @@ -19,6 +20,8 @@ namespace Pal.Client #else private const string remoteUrl = "https://pal.μ.tv"; #endif + private readonly string UserAgent = $"{typeof(RemoteApi).Assembly.GetName().Name?.Replace(" ", "")}/{typeof(RemoteApi).Assembly.GetName().Version?.ToString(2)}"; + private GrpcChannel? _channel; private LoginReply? _lastLoginReply; @@ -49,7 +52,7 @@ namespace Pal.Client #endif if (string.IsNullOrEmpty(accountId)) { - var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); + var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); if (createAccountReply.Success) { accountId = createAccountReply.AccountId; @@ -68,7 +71,7 @@ namespace Pal.Client if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now) { - _lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = accountId }, deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); + _lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = accountId }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); if (!_lastLoginReply.Success) { if (_lastLoginReply.Error == LoginError.InvalidAccountId) @@ -145,9 +148,15 @@ namespace Pal.Client return (statisticsReply.Success, statisticsReply.FloorStatistics.ToList()); } + private Metadata UnauthorizedHeaders() => new Metadata + { + { "User-Agent", UserAgent }, + }; + private Metadata AuthorizedHeaders() => new Metadata { { "Authorization", $"Bearer {_lastLoginReply?.AuthToken}" }, + { "User-Agent", UserAgent }, }; public void Dispose()