-
Notifications
You must be signed in to change notification settings - Fork 12
/
Program.cs
163 lines (133 loc) · 4.89 KB
/
Program.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Net.WebSockets;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using Microsoft.Extensions.Logging;
using Nostr.Client.Client;
using Nostr.Client.Communicator;
using Nostr.Client.Keys;
using Nostr.Client.Messages;
using Nostr.Client.Requests;
using Nostr.Client.Sample.Console;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
using Serilog.Sinks.SystemConsole.Themes;
var exitEvent = new ManualResetEvent(false);
var logFactory = InitLogging();
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
AssemblyLoadContext.Default.Unloading += DefaultOnUnloading;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Console.WriteLine("|======================|");
Console.WriteLine("| NOSTR CLIENT |");
Console.WriteLine("|======================|");
Console.WriteLine();
Log.Debug("====================================");
Log.Debug(" STARTING ");
Log.Debug("====================================");
var relays = new[]
{
new Uri("wss://relay.snort.social"),
new Uri("wss://relay.damus.io"),
new Uri("wss://eden.nostr.land"),
new Uri("wss://nostr-pub.wellorder.net"),
new Uri("wss://nos.lol"),
};
using var multiClient = new NostrMultiWebsocketClient(logFactory.CreateLogger<NostrWebsocketClient>());
var communicators = new List<NostrWebsocketCommunicator>();
foreach (var relay in relays)
{
var communicator = CreateCommunicator(relay);
communicators.Add(communicator);
multiClient.RegisterCommunicator(communicator);
}
var viewer = new NostrViewer(multiClient);
viewer.Subscribe();
communicators.ForEach(x => x.Start());
viewer.SendRequests();
exitEvent.WaitOne();
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
foreach (var communicator in communicators)
{
await communicator.Stop(WebSocketCloseStatus.NormalClosure, string.Empty);
await Task.Delay(500);
communicator.Dispose();
}
static SerilogLoggerFactory InitLogging()
{
Console.OutputEncoding = Encoding.UTF8;
var executingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) ?? Directory.GetCurrentDirectory();
var logPath = Path.Combine(executingDir, "logs", "verbose.log");
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
.WriteTo.Console(LogEventLevel.Debug,
outputTemplate: "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}",
theme: AnsiConsoleTheme.Code)
.CreateLogger();
Log.Logger = logger;
return new SerilogLoggerFactory(logger);
}
NostrWebsocketCommunicator CreateCommunicator(Uri uri)
{
var comm = new NostrWebsocketCommunicator(uri, () =>
{
var client = new ClientWebSocket();
client.Options.SetRequestHeader("Origin", "http://localhost");
return client;
});
comm.Name = uri.Host;
comm.ReconnectTimeout = null; //TimeSpan.FromSeconds(30);
comm.ErrorReconnectTimeout = TimeSpan.FromSeconds(60);
comm.ReconnectionHappened.Subscribe(info =>
Log.Information("[{relay}] Reconnection happened, type: {type}", comm.Name, info.Type));
comm.DisconnectionHappened.Subscribe(info =>
Log.Information("[{relay}] Disconnection happened, type: {type}, reason: {reason}", comm.Name, info.Type, info.CloseStatus));
return comm;
}
void CurrentDomainOnProcessExit(object? sender, EventArgs eventArgs)
{
Log.Warning("Exiting process");
exitEvent.Set();
}
void DefaultOnUnloading(AssemblyLoadContext assemblyLoadContext)
{
Log.Warning("Unloading process");
exitEvent.Set();
}
void ConsoleOnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
Log.Warning("Canceling process");
e.Cancel = true;
exitEvent.Set();
}
void SendEvent(INostrClient client, int counter)
{
var ev = new NostrEvent
{
Kind = NostrKind.ShortTextNote,
CreatedAt = DateTime.UtcNow,
Content = $"Test message {counter} from C# client"
};
var key = NostrPrivateKey.FromBech32("nsec1xjyhgzm2cjv2wp64wnh64d2n4s9ylguhwelekh5r38rlsfgk6mes62duaa");
var signed = ev.Sign(key);
client.Send(new NostrEventRequest(signed));
}
void SendDirectMessage(INostrClient client)
{
Log.Information("Sending encrypted direct message");
var sender = NostrPrivateKey.FromBech32("nsec1l0a7m5dlg4h9wurhnmgsq5nv9cqyvdwsutk4yf3w4fzzaqw7n80ssdfzkg");
var receiver = NostrPublicKey.FromHex("d27790fcb3f9afa0d709b2e9c5995151bc5ad008079bd0a474aa101d80e0eed3");
var ev = new NostrEvent
{
CreatedAt = DateTime.UtcNow,
Content = $"Test private message from C# client"
};
var encrypted = ev.EncryptDirect(sender, receiver);
var signed = encrypted.Sign(sender);
client.Send(new NostrEventRequest(signed));
}