Include vaguely helpful plugin log information for connections

rendering v1.17
Liza 2022-11-24 07:24:45 +01:00
parent 8c747ba661
commit ce1285bff9
3 changed files with 45 additions and 4 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net6.0-windows</TargetFramework>
<LangVersion>9.0</LangVersion> <LangVersion>9.0</LangVersion>
<Version>1.16.0.0</Version> <Version>1.17.0.0</Version>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>

View File

@ -1,4 +1,5 @@
using Account; using Account;
using Dalamud.Logging;
using Grpc.Core; using Grpc.Core;
using Grpc.Net.Client; using Grpc.Net.Client;
using Palace; using Palace;
@ -39,15 +40,22 @@ namespace Pal.Client
} }
} }
private string PartialAccountId =>
AccountId?.ToString()?.PadRight(14).Substring(0, 13) ?? "[no account id]";
private async Task<(bool Success, string Error)> TryConnect(CancellationToken cancellationToken, bool retry = true) private async Task<(bool Success, string Error)> TryConnect(CancellationToken cancellationToken, bool retry = true)
{ {
if (Service.Configuration.Mode != Configuration.EMode.Online) if (Service.Configuration.Mode != Configuration.EMode.Online)
{
PluginLog.Debug("TryConnect: Not Online, not attempting to establish a connection");
return (false, "You are not online."); return (false, "You are not online.");
}
if (_channel == null || !(_channel.State == ConnectivityState.Ready || _channel.State == ConnectivityState.Idle)) if (_channel == null || !(_channel.State == ConnectivityState.Ready || _channel.State == ConnectivityState.Idle))
{ {
Dispose(); Dispose();
PluginLog.Information("TryConnect: Creating new gRPC channel");
_channel = GrpcChannel.ForAddress(remoteUrl, new GrpcChannelOptions _channel = GrpcChannel.ForAddress(remoteUrl, new GrpcChannelOptions
{ {
HttpHandler = new SocketsHttpHandler HttpHandler = new SocketsHttpHandler
@ -56,20 +64,26 @@ namespace Pal.Client
SslOptions = GetSslClientAuthenticationOptions(), SslOptions = GetSslClientAuthenticationOptions(),
} }
}); });
PluginLog.Information($"TryConnect: Connecting to upstream service at {remoteUrl}");
await _channel.ConnectAsync(cancellationToken); await _channel.ConnectAsync(cancellationToken);
} }
var accountClient = new AccountService.AccountServiceClient(_channel); var accountClient = new AccountService.AccountServiceClient(_channel);
if (AccountId == null) if (AccountId == null)
{ {
PluginLog.Information($"TryConnect: No account information saved for {remoteUrl}, creating new account");
var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), 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) if (createAccountReply.Success)
{ {
AccountId = Guid.Parse(createAccountReply.AccountId); AccountId = Guid.Parse(createAccountReply.AccountId);
PluginLog.Information($"TryConnect: Account created with id {PartialAccountId}");
Service.Configuration.Save(); Service.Configuration.Save();
} }
else else
{ {
PluginLog.Error($"TryConnect: Account creation failed with error {createAccountReply.Error}");
if (createAccountReply.Error == CreateAccountError.UpgradeRequired && !_warnedAboutUpgrade) if (createAccountReply.Error == CreateAccountError.UpgradeRequired && !_warnedAboutUpgrade)
{ {
Service.Chat.PrintError("[Palace Pal] Your version of Palace Pal is outdated, please update the plugin using the Plugin Installer."); Service.Chat.PrintError("[Palace Pal] Your version of Palace Pal is outdated, please update the plugin using the Plugin Installer.");
@ -80,19 +94,31 @@ namespace Pal.Client
} }
if (AccountId == null) if (AccountId == null)
{
PluginLog.Warning("TryConnect: No account id to login with");
return (false, "No account-id after account was attempted to be created."); return (false, "No account-id after account was attempted to be created.");
}
if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now) if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now)
{ {
PluginLog.Information($"TryConnect: Logging in with account id {PartialAccountId}");
_lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = AccountId?.ToString() }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); _lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = AccountId?.ToString() }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken);
if (!_lastLoginReply.Success) if (_lastLoginReply.Success)
{ {
PluginLog.Information($"TryConnect: Login successful with account id: {PartialAccountId}, auth token: {_lastLoginReply.AuthToken}");
}
else
{
PluginLog.Error($"TryConnect: Login failed with error { _lastLoginReply.Error}");
if (_lastLoginReply.Error == LoginError.InvalidAccountId) if (_lastLoginReply.Error == LoginError.InvalidAccountId)
{ {
AccountId = null; AccountId = null;
Service.Configuration.Save(); Service.Configuration.Save();
if (retry) if (retry)
{
PluginLog.Information("TryConnect: Attempting connection retry without account id");
return await TryConnect(cancellationToken, retry: false); return await TryConnect(cancellationToken, retry: false);
}
else else
return (false, "Invalid account id."); return (false, "Invalid account id.");
} }
@ -106,7 +132,10 @@ namespace Pal.Client
} }
if (_lastLoginReply == null) if (_lastLoginReply == null)
{
PluginLog.Error("TryConnect: No account available");
return (false, "No login information available."); return (false, "No login information available.");
}
bool success = !string.IsNullOrEmpty(_lastLoginReply?.AuthToken); bool success = !string.IsNullOrEmpty(_lastLoginReply?.AuthToken);
if (!success) if (!success)
@ -129,6 +158,7 @@ namespace Pal.Client
if (!connectionResult.Success) if (!connectionResult.Success)
return $"Could not connect to server: {connectionResult.Error}"; return $"Could not connect to server: {connectionResult.Error}";
PluginLog.Information("VerifyConnection: Connection established, trying to verify auth token");
var accountClient = new AccountService.AccountServiceClient(_channel); var accountClient = new AccountService.AccountServiceClient(_channel);
await accountClient.VerifyAsync(new VerifyRequest(), headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); await accountClient.VerifyAsync(new VerifyRequest(), headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken);
return "Connection successful."; return "Connection successful.";
@ -227,20 +257,24 @@ namespace Pal.Client
var bytes = new byte[manifestResourceStream.Length]; var bytes = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(bytes, 0, bytes.Length); manifestResourceStream.Read(bytes, 0, bytes.Length);
var certificate = new X509Certificate2(bytes, pass, X509KeyStorageFlags.DefaultKeySet);
PluginLog.Debug($"Using client certificate {certificate.GetCertHashString()}");
return new SslClientAuthenticationOptions return new SslClientAuthenticationOptions
{ {
ClientCertificates = new X509CertificateCollection() ClientCertificates = new X509CertificateCollection()
{ {
new X509Certificate2(bytes, pass, X509KeyStorageFlags.DefaultKeySet), certificate,
}, },
}; };
#else #else
PluginLog.Debug("Not using client certificate");
return null; return null;
#endif #endif
} }
public void Dispose() public void Dispose()
{ {
PluginLog.Debug("Disposing gRPC channel");
_channel?.Dispose(); _channel?.Dispose();
_channel = null; _channel = null;
} }

View File

@ -1,5 +1,6 @@
using Dalamud.Interface.Components; using Dalamud.Interface.Components;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Logging;
using ECommons.Reflection; using ECommons.Reflection;
using ECommons.SplatoonAPI; using ECommons.SplatoonAPI;
using ImGuiNET; using ImGuiNET;
@ -9,6 +10,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Pal.Client.Windows namespace Pal.Client.Windows
@ -124,12 +126,17 @@ namespace Pal.Client.Windows
Task.Run(async () => Task.Run(async () =>
{ {
_connectionText = "Testing..."; _connectionText = "Testing...";
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(60));
try try
{ {
_connectionText = await Service.RemoteApi.VerifyConnection(); _connectionText = await Service.RemoteApi.VerifyConnection(cts.Token);
} }
catch (Exception e) catch (Exception e)
{ {
PluginLog.Error(e, "Could not establish remote connection");
_connectionText = e.ToString(); _connectionText = e.ToString();
} }
}); });