Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.16.0: Fixes, Bootstrapper improvements and new Extensions #8

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install .NET SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout Repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install .NET SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'

- name: Restore dependencies
run: dotnet restore
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.100",
"version": "8.0.201",
"rollForward": "latestFeature"
}
}
12 changes: 7 additions & 5 deletions src/AesPasswordEncryptedPubSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OwlCore.Extensions;
using System.Security.Cryptography;
using System.Text;
using MemoryStream = System.IO.MemoryStream;
using PublishedMessage = OwlCore.Kubo.Models.PublishedMessage;

namespace OwlCore.Kubo;
Expand Down Expand Up @@ -48,16 +49,16 @@ public async Task PublishAsync(string topic, Stream message, CancellationToken c
message.Seek(0, SeekOrigin.Begin);

var aes = Aes.Create();
var passBytes = new Rfc2898DeriveBytes(password: _password, salt: Encoding.UTF8.GetBytes(_salt ?? string.Empty));
var passBytes = new Rfc2898DeriveBytes(password: _password, salt: Encoding.UTF8.GetBytes(_salt ?? string.Empty), iterations: 2000);

aes.Key = passBytes.GetBytes(aes.KeySize / 8);
aes.IV = passBytes.GetBytes(aes.BlockSize / 8);

using var encryptedOutputStream = new MemoryStream();
using var streamEncryptor = new CryptoStream(encryptedOutputStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

var unencryptedBytes = await message.ToBytesAsync();
streamEncryptor.Write(unencryptedBytes, 0, unencryptedBytes.Length);
var unencryptedBytes = await message.ToBytesAsync(cancellationToken: cancel);
await streamEncryptor.WriteAsync(unencryptedBytes, 0, unencryptedBytes.Length, cancel);
streamEncryptor.FlushFinalBlock();

encryptedOutputStream.Position = 0;
Expand All @@ -70,7 +71,7 @@ public Task SubscribeAsync(string topic, Action<IPublishedMessage> handler, Canc
{
return Inner.SubscribeAsync(topic, msg =>
{
if (TryTransformPublishedMessage(msg) is IPublishedMessage transformedMsg)
if (TryTransformPublishedMessage(msg) is { } transformedMsg)
{
handler(transformedMsg);
}
Expand All @@ -94,7 +95,8 @@ public Task<IEnumerable<string>> SubscribedTopicsAsync(CancellationToken cancel
try
{
using var outputStream = new MemoryStream();
using var streamDecryptor = new CryptoStream(publishedMessage.DataStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
using var inputStream = new MemoryStream(publishedMessage.DataBytes);
using var streamDecryptor = new CryptoStream(inputStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
streamDecryptor.CopyTo(outputStream);

var outputBytes = outputStream.ToBytes();
Expand Down
23 changes: 23 additions & 0 deletions src/BootstrapLaunchConflictMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace OwlCore.Kubo;

/// <summary>
/// The behavior when the repo is locked and a node is already running on the requested port.
/// </summary>
public enum BootstrapLaunchConflictMode
{
/// <summary>
/// Throw when the requested port is already in use.
/// </summary>
Throw,

/// <summary>
/// If the repo is locked, load the Api and Gateway port, attach to it and shut it down, then bootstrap a new process on the requested ports.
/// </summary>
Relaunch,

/// <summary>
/// If the repo is locked, load the configured (currently running) Api and Gateway port and attach to it without shutting it down or starting a new process. May discard the requested ports.
/// </summary>
/// <remarks>Note that attaching does not allow you to take control of the running process. See <see cref="Relaunch"/> instead if you need this functionality.</remarks>
Attach,
}
114 changes: 114 additions & 0 deletions src/Cache/CachedCoreApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Ipfs.CoreApi;
using OwlCore.ComponentModel;
using OwlCore.Storage;

namespace OwlCore.Kubo.Cache;

/// <summary>
/// A cached api layer for <see cref="ICoreApi"/>.
/// </summary>
public class CachedCoreApi : ICoreApi, IDelegable<ICoreApi>, IFlushable, IAsyncInit
{
/// <summary>
/// Creates a new instance of <see cref="CachedCoreApi"/>.
/// </summary>
/// <param name="cacheFolder">The folder to store cached data in.</param>
/// <param name="inner">The inner <see cref="ICoreApi"/> to wrap around.</param>
public CachedCoreApi(IModifiableFolder cacheFolder, ICoreApi inner)
{
Name = new CachedNameApi(cacheFolder) { Inner = inner.Name, KeyApi = inner.Key };
Key = new CachedKeyApi(cacheFolder) { Inner = inner.Key };
Inner = inner;
}

/// <summary>
/// The inner, unwrapped core api to delegate to.
/// </summary>
public ICoreApi Inner { get; }

/// <inheritdoc/>
public IBitswapApi Bitswap => Inner.Bitswap;

/// <inheritdoc/>
public IBlockApi Block => Inner.Block;

/// <inheritdoc/>
public IBlockRepositoryApi BlockRepository => Inner.BlockRepository;

/// <inheritdoc/>
public IBootstrapApi Bootstrap => Inner.Bootstrap;

/// <inheritdoc/>
public IConfigApi Config => Inner.Config;

/// <inheritdoc/>
public IDagApi Dag => Inner.Dag;

/// <inheritdoc/>
public IDhtApi Dht => Inner.Dht;

/// <inheritdoc/>
public IDnsApi Dns => Inner.Dns;

/// <inheritdoc/>
public IFileSystemApi FileSystem => Inner.FileSystem;

/// <inheritdoc/>
public IMfsApi Mfs => Inner.Mfs;

/// <inheritdoc/>
public IGenericApi Generic => Inner.Generic;

/// <inheritdoc/>
public IKeyApi Key { get; }

/// <inheritdoc/>
public INameApi Name { get; }

/// <inheritdoc/>
public IObjectApi Object => Inner.Object;

/// <inheritdoc/>
public IPinApi Pin => Inner.Pin;

/// <inheritdoc/>
public IPubSubApi PubSub => Inner.PubSub;

/// <inheritdoc/>
public IStatsApi Stats => Inner.Stats;

/// <inheritdoc/>
public ISwarmApi Swarm => Inner.Swarm;

/// <inheritdoc/>
public bool IsInitialized { get; set; }

/// <inheritdoc/>
public async Task FlushAsync(CancellationToken cancellationToken)
{
await ((CachedNameApi)Name).FlushAsync(cancellationToken);

await ((CachedNameApi)Name).SaveAsync(cancellationToken);
await ((CachedKeyApi)Key).SaveAsync(cancellationToken);
}

/// <inheritdoc/>
public async Task InitAsync(CancellationToken cancellationToken = default)
{
try
{
// Try loading data from API
await ((CachedKeyApi)Key).InitAsync(cancellationToken);
}
catch
{
// Load data from disk as fallback
await ((CachedKeyApi)Key).LoadAsync(cancellationToken);
}

await ((CachedNameApi)Name).LoadAsync(cancellationToken);

// Allow multiple initialization
IsInitialized = true;
}
}
85 changes: 85 additions & 0 deletions src/Cache/CachedKeyApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Ipfs;
using Ipfs.CoreApi;
using OwlCore.ComponentModel;
using OwlCore.Storage;

namespace OwlCore.Kubo.Cache;

/// <summary>
/// A cached api layer for <see cref="IKeyApi"/>.
/// </summary>
public class CachedKeyApi : SettingsBase, IKeyApi, IDelegable<IKeyApi>, IAsyncInit
{
/// <summary>
/// The cached record type for created or resolved <see cref="IKey"/>s.
/// </summary>
public record KeyInfo(string Name, Cid Id) : IKey;

/// <summary>
/// Creates a new instance of <see cref="CachedKeyApi"/>.
/// </summary>
/// <param name="folder">The folder to store cached name resolutions.</param>
public CachedKeyApi(IModifiableFolder folder)
: base(folder, KuboCacheSerializer.Singleton)
{
FlushDefaultValues = false;
}

/// <summary>
/// The resolved ipns keys.
/// </summary>
public List<KeyInfo> Keys
{
get => GetSetting(() => new List<KeyInfo>());
set => SetSetting(value);
}

/// <inheritdoc />
public required IKeyApi Inner { get; init; }

/// <inheritdoc />
public async Task<IKey> CreateAsync(string name, string keyType, int size, CancellationToken cancel = default)
{
var res = await Inner.CreateAsync(name, keyType, size, cancel);

var existing = Keys.FirstOrDefault(x => x.Name == res.Name);
if (existing is not null)
Keys.Remove(existing);

Keys.Add(new KeyInfo(Name: res.Name, Id: res.Id));
return res;
}

/// <inheritdoc />
public Task<IEnumerable<IKey>> ListAsync(CancellationToken cancel = default) => Task.FromResult<IEnumerable<IKey>>(Keys);

/// <inheritdoc />
public Task<IKey?> RemoveAsync(string name, CancellationToken cancel = default) => Inner.RemoveAsync(name, cancel);

/// <inheritdoc />
public Task<IKey> RenameAsync(string oldName, string newName, CancellationToken cancel = default) => Inner.RenameAsync(oldName, newName, cancel);

/// <inheritdoc />
public Task<string> ExportAsync(string name, char[] password, CancellationToken cancel = default) => Inner.ExportAsync(name, password, cancel);

/// <inheritdoc />
public Task<IKey> ImportAsync(string name, string pem, char[]? password = null, CancellationToken cancel = default) => Inner.ImportAsync(name, pem, password, cancel);

/// <summary>
/// Initializes local values with fresh data from the API.
/// </summary>
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
public async Task InitAsync(CancellationToken cancellationToken = default)
{
var res = await Inner.ListAsync(cancellationToken);
Keys = res.Select(x => new KeyInfo(x.Name, x.Id)).ToList();

await SaveAsync(cancellationToken);

// Allow multiple initialization
IsInitialized = true;
}

/// <inheritdoc/>
public bool IsInitialized { get; private set; }
}
Loading
Loading