-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCredentialServiceManager.cs
62 lines (53 loc) · 2.01 KB
/
CredentialServiceManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SimpleJSON;
namespace UnityBuilderDiscordBot.Services;
public class CredentialServiceManager : IHostedService
{
private ILogger<CredentialServiceManager> _logger;
public readonly Dictionary<JSONNode, SshCredentialService> RegisteredSshCredentialServices = new();
private readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(); // 添加Console输出提供程序
});
public static CredentialServiceManager Instance;
public CredentialServiceManager(ILogger<CredentialServiceManager> logger)
{
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Instance = this;
_logger.LogInformation($"[{GetType()}] Standing by.");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
foreach (var kvp in RegisteredSshCredentialServices)
{
RemoveSshCredentialService(kvp.Key);
}
return Task.CompletedTask;
}
/// <summary>
/// 注册一个Ssh客户端
/// </summary>
/// <param name="node"></param>
public bool RegisterSshCredentialService(JSONNode node)
{
var logger = _loggerFactory.CreateLogger<SshCredentialService>();
_logger.LogInformation($"[{GetType()}] {node["address"]}({node["user"]}) registered!");
return RegisteredSshCredentialServices.TryAdd(node, new SshCredentialService(logger, node));
}
/// <summary>
/// 反注册一个Ssh客户端
/// </summary>
/// <param name="node"></param>
public void RemoveSshCredentialService(JSONNode node)
{
if (!RegisteredSshCredentialServices.TryGetValue(node, out var service)) return;
service.StopAsync(CancellationToken.None);
RegisteredSshCredentialServices.Remove(node);
_logger.LogInformation($"[{GetType()}] {node["address"]}({node["user"]}) removed!");
}
}