Skip to content

Commit

Permalink
add log on path for desktop app
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Dec 10, 2024
1 parent 639e3ad commit 3142f33
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 127 deletions.
51 changes: 51 additions & 0 deletions BTCPayApp.Desktop/DesktopDataDirectoryProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using BTCPayApp.Core.Contracts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace BTCPayApp.Desktop;

public class DesktopDataDirectoryProvider : IDataDirectoryProvider
{
private readonly IConfiguration _configuration;
private readonly ILogger<DesktopDataDirectoryProvider> _logger;

public DesktopDataDirectoryProvider(IConfiguration configuration, ILogger<DesktopDataDirectoryProvider> logger)
{
_configuration = configuration;
_logger = logger;
}

private string? _result = null;
public virtual Task<string> GetAppDataDirectory()
{
if (_result != null)
return Task.FromResult(_result);
var def = "BTCPayApp";
var dirName = _configuration.GetValue("BTCPAYAPP_DIRNAME", def);
_result = GetDirectory(dirName?? def);
_logger.LogInformation($"Using data directory: {_result}");
return Task.FromResult(_result);
}

private string GetDirectory(string appDirectory)
{
var environmentVariable1 = _configuration.GetValue<string>("HOME");
var environmentVariable2 = _configuration.GetValue<string>("APPDATA");
string str;
if (!string.IsNullOrEmpty(environmentVariable1) && string.IsNullOrEmpty(environmentVariable2))
str = Path.Combine(environmentVariable1, "." + appDirectory.ToLowerInvariant());
else if (!string.IsNullOrEmpty(environmentVariable2))
{
str = Path.Combine(environmentVariable2, appDirectory);
}
else
{
throw new DirectoryNotFoundException(
"Could not find suitable datadir environment variables HOME or APPDATA are not set");
}

if (!Directory.Exists(str))
Directory.CreateDirectory(str);
return str;
}
}
66 changes: 66 additions & 0 deletions BTCPayApp.Desktop/DesktopSecureConfigProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Text.Json;
using BTCPayApp.Core.Contracts;
using Microsoft.AspNetCore.DataProtection;

namespace BTCPayApp.Desktop;

public class DesktopSecureConfigProvider: ISecureConfigProvider
{
private readonly IDataProtector _dataProtector;

public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector("SecureConfig");
_configDir = directoryProvider.GetAppDataDirectory().ContinueWith(task =>
{
var res = Path.Combine(task.Result, "config");
Directory.CreateDirectory(res);
return res;
});
}

private readonly Task<string> _configDir;

public async Task<T?> Get<T>(string key)
{
var dir = Path.Combine(await _configDir, key);
if (!File.Exists(dir))
{
return default;
}
var raw = await File.ReadAllTextAsync(dir);
var json = await ReadFromRaw(raw);
return JsonSerializer.Deserialize<T>(json);
}


public async Task Set<T>(string key, T? value)
{
var dir = Path.Combine(await _configDir, key);
if (value is null)
{
if (File.Exists(dir))
{
File.Delete(dir);
}
}
else
{
var raw = JsonSerializer.Serialize(value);
await File.WriteAllTextAsync(dir, await WriteFromRaw(raw));
}
}

public async Task<IEnumerable<string>> List(string prefix)
{
var dir = await _configDir;
if (!Directory.Exists(dir))
{
return Array.Empty<string>();
}
return Directory.GetFiles(dir, $"{prefix}*").Select(Path.GetFileName).Where(p => p?.StartsWith(prefix) is true)!;
}

protected Task<string> ReadFromRaw(string str) => Task.FromResult(_dataProtector.Unprotect(str));
protected Task<string> WriteFromRaw(string str) => Task.FromResult(_dataProtector.Protect(str));
}
129 changes: 2 additions & 127 deletions BTCPayApp.Desktop/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Text.Json;
using BTCPayApp.Core.Contracts;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Configuration;
using BTCPayApp.Core.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Plugin.Fingerprint;
using Plugin.Fingerprint.Abstractions;
Expand All @@ -22,126 +19,4 @@ public static IServiceCollection ConfigureBTCPayAppDesktop(this IServiceCollecti
serviceCollection.AddSingleton<IFingerprint, StubFingerprintProvider>();
return serviceCollection;
}
}

public class DesktopSecureConfigProvider: ISecureConfigProvider
{
private readonly IDataProtector _dataProtector;

public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector("SecureConfig");
_configDir = directoryProvider.GetAppDataDirectory().ContinueWith(task =>
{
var res = Path.Combine(task.Result, "config");
Directory.CreateDirectory(res);
return res;
});
}

private readonly Task<string> _configDir;

public async Task<T?> Get<T>(string key)
{
var dir = Path.Combine(await _configDir, key);
if (!File.Exists(dir))
{
return default;
}
var raw = await File.ReadAllTextAsync(dir);
var json = await ReadFromRaw(raw);
return JsonSerializer.Deserialize<T>(json);
}


public async Task Set<T>(string key, T? value)
{
var dir = Path.Combine(await _configDir, key);
if (value is null)
{
if (File.Exists(dir))
{
File.Delete(dir);
}
}
else
{
var raw = JsonSerializer.Serialize(value);
await File.WriteAllTextAsync(dir, await WriteFromRaw(raw));
}
}

public async Task<IEnumerable<string>> List(string prefix)
{
var dir = await _configDir;
if (!Directory.Exists(dir))
{
return Array.Empty<string>();
}
return Directory.GetFiles(dir, $"{prefix}*").Select(Path.GetFileName).Where(p => p?.StartsWith(prefix) is true)!;
}

protected Task<string> ReadFromRaw(string str) => Task.FromResult(_dataProtector.Unprotect(str));
protected Task<string> WriteFromRaw(string str) => Task.FromResult(_dataProtector.Protect(str));
}

public class StubFingerprintProvider: IFingerprint
{
public Task<FingerprintAvailability> GetAvailabilityAsync(bool allowAlternativeAuthentication = false)
{
return Task.FromResult(FingerprintAvailability.NoImplementation);
}

public Task<bool> IsAvailableAsync(bool allowAlternativeAuthentication = false)
{
return Task.FromResult(false);
}

public Task<FingerprintAuthenticationResult> AuthenticateAsync(AuthenticationRequestConfiguration authRequestConfig,
CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}

public Task<AuthenticationType> GetAuthenticationTypeAsync()
{
throw new NotImplementedException();
}
}
public class DesktopDataDirectoryProvider : IDataDirectoryProvider
{
private readonly IConfiguration _configuration;

public DesktopDataDirectoryProvider(IConfiguration configuration)
{
_configuration = configuration;
}
public virtual Task<string> GetAppDataDirectory()
{
var def = "BTCPayApp";
var dirName = _configuration.GetValue("BTCPAYAPP_DIRNAME", def);
return Task.FromResult(GetDirectory(dirName ?? def));
}

private string GetDirectory(string appDirectory)
{
var environmentVariable1 = _configuration.GetValue<string>("HOME");
var environmentVariable2 = _configuration.GetValue<string>("APPDATA");
string str;
if (!string.IsNullOrEmpty(environmentVariable1) && string.IsNullOrEmpty(environmentVariable2))
str = Path.Combine(environmentVariable1, "." + appDirectory.ToLowerInvariant());
else if (!string.IsNullOrEmpty(environmentVariable2))
{
str = Path.Combine(environmentVariable2, appDirectory);
}
else
{
throw new DirectoryNotFoundException(
"Could not find suitable datadir environment variables HOME or APPDATA are not set");
}

if (!Directory.Exists(str))
Directory.CreateDirectory(str);
return str;
}
}
}
27 changes: 27 additions & 0 deletions BTCPayApp.Desktop/StubFingerprintProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Plugin.Fingerprint.Abstractions;

namespace BTCPayApp.Desktop;

public class StubFingerprintProvider: IFingerprint
{
public Task<FingerprintAvailability> GetAvailabilityAsync(bool allowAlternativeAuthentication = false)
{
return Task.FromResult(FingerprintAvailability.NoImplementation);
}

public Task<bool> IsAvailableAsync(bool allowAlternativeAuthentication = false)
{
return Task.FromResult(false);
}

public Task<FingerprintAuthenticationResult> AuthenticateAsync(AuthenticationRequestConfiguration authRequestConfig,
CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
}

public Task<AuthenticationType> GetAuthenticationTypeAsync()
{
throw new NotImplementedException();
}
}
1 change: 1 addition & 0 deletions BTCPayApp.Maui/BTCPayApp.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<ArchiveOnBuild>true</ArchiveOnBuild>
<RuntimeIdentifiers>ios-arm64</RuntimeIdentifiers>
<UseInterpreter>True</UseInterpreter>
<EnableAssemblyILStripping>false</EnableAssemblyILStripping>
</PropertyGroup>


Expand Down

0 comments on commit 3142f33

Please sign in to comment.