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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
// <copyright file="Application.cs" company="slskd Team">
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
using Microsoft.Extensions.Options;
namespace slskd
{
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using slskd.Configuration;
using slskd.Core.API;
using slskd.Integrations.Pushbullet;
using slskd.Messaging;
using slskd.Search;
using slskd.Shares;
using slskd.Transfers;
using slskd.Users;
using Soulseek;
using Soulseek.Diagnostics;
public interface IApplication : IHostedService
{
public Task CheckVersionAsync();
public Task RescanSharesAsync();
public void CollectGarbage();
}
public sealed class Application : IApplication
{
private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
public Application(
OptionsAtStartup optionsAtStartup,
IOptionsMonitor<Options> optionsMonitor,
IManagedState<State> state,
ISoulseekClient soulseekClient,
ITransferTracker transferTracker,
IBrowseTracker browseTracker,
IConversationTracker conversationTracker,
IRoomTracker roomTracker,
IRoomService roomService,
ISharedFileCache sharedFileCache,
IPushbulletService pushbulletService,
IHubContext<ApplicationHub> applicationHub,
IHubContext<LogsHub> logHub)
{
OptionsAtStartup = optionsAtStartup;
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(async options => await OptionsMonitor_OnChange(options));
PreviousOptions = OptionsMonitor.CurrentValue;
State = state;
State.OnChange(state => State_OnChange(state));
SharedFileCache = sharedFileCache;
SharedFileCache.StateMonitor.OnChange(state => SharedFileCacheState_OnChange(state));
TransferTracker = transferTracker;
BrowseTracker = browseTracker;
ConversationTracker = conversationTracker;
Pushbullet = pushbulletService;
RoomService = roomService;
ApplicationHub = applicationHub;
LogHub = logHub;
Program.LogEmitted += (_, log) => LogHub.EmitLogAsync(log);
Client = soulseekClient;
Client.DiagnosticGenerated += Client_DiagnosticGenerated;
Client.TransferStateChanged += Client_TransferStateChanged;
Client.TransferProgressUpdated += Client_TransferProgressUpdated;
Client.BrowseProgressUpdated += Client_BrowseProgressUpdated;
Client.UserStatusChanged += Client_UserStatusChanged;
Client.PrivateMessageReceived += Client_PrivateMessageRecieved;
Client.PrivateRoomMembershipAdded += (e, room) => Console.WriteLine($"Added to private room {room}");
Client.PrivateRoomMembershipRemoved += (e, room) => Console.WriteLine($"Removed from private room {room}");
Client.PrivateRoomModerationAdded += (e, room) => Console.WriteLine($"Promoted to moderator in private room {room}");
Client.PrivateRoomModerationRemoved += (e, room) => Console.WriteLine($"Demoted from moderator in private room {room}");
Client.PublicChatMessageReceived += Client_PublicChatMessageReceived;
Client.RoomMessageReceived += Client_RoomMessageReceived;
Client.Disconnected += Client_Disconnected;
Client.Connected += Client_Connected;
Client.LoggedIn += Client_LoggedIn;
Client.StateChanged += Client_StateChanged;
}
private ISoulseekClient Client { get; set; }
private IRoomService RoomService { get; set; }
private IBrowseTracker BrowseTracker { get; set; }
private IConversationTracker ConversationTracker { get; set; }
private ILogger Log { get; set; } = Serilog.Log.ForContext<Application>();
private ConcurrentDictionary<string, ILogger> Loggers { get; } = new ConcurrentDictionary<string, ILogger>();
private Options Options => OptionsMonitor.CurrentValue;
private OptionsAtStartup OptionsAtStartup { get; set; }
private IOptionsMonitor<Options> OptionsMonitor { get; set; }
private SemaphoreSlim OptionsSyncRoot { get; } = new SemaphoreSlim(1, 1);
private Options PreviousOptions { get; set; }
private IPushbulletService Pushbullet { get; }
private ISharedFileCache SharedFileCache { get; set; }
private DateTime SharesRefreshStarted { get; set; }
private IManagedState<State> State { get; }
private ITransferTracker TransferTracker { get; set; }
private IHubContext<ApplicationHub> ApplicationHub { get; set; }
private IHubContext<LogsHub> LogHub { get; set; }
public void CollectGarbage()
{
var sw = new Stopwatch();
Log.Debug("Collecting garbage");
sw.Start();
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
#pragma warning disable S1215 // "GC.Collect" should not be called
GC.Collect(2, GCCollectionMode.Forced, blocking: false, compacting: true);
#pragma warning restore S1215 // "GC.Collect" should not be called
sw.Stop();
Log.Debug("Garbage collection completed in {Duration}ms", sw.ElapsedMilliseconds);
}
/// <summary>
/// Gets the version of the latest application release.
/// </summary>
/// <returns>The operation context.</returns>
public async Task CheckVersionAsync()
{
if (Program.IsDevelopment)
{
Log.Information("Skipping version check for Development build");
return;
}
if (Program.IsCanary)
{
// todo: use the docker hub API to find the latest canary tag
Log.Information("Skipping version check for Canary build; check for updates manually.");
return;
}
Log.Information("Checking GitHub Releases for latest version");
try
{
var latestVersion = await GitHub.GetLatestReleaseVersion(
organization: Program.AppName,
repository: Program.AppName,
userAgent: $"{Program.AppName} v{Program.FullVersion}");
if (latestVersion > Version.Parse(Program.SemanticVersion))
{
State.SetValue(state => state with { Version = state.Version with { Latest = latestVersion.ToString(), IsUpdateAvailable = true } });
Log.Information("A new version is available! {CurrentVersion} -> {LatestVersion}", Program.SemanticVersion, latestVersion);
}
else
{
State.SetValue(state => state with { Version = state.Version with { Latest = Program.SemanticVersion, IsUpdateAvailable = false } });
Log.Information("Version {CurrentVersion} is up to date.", Program.SemanticVersion);
}
}
catch (Exception ex)
{
Log.Warning("Failed to check version: {Message}", ex.Message);
throw;
}
}
/// <summary>
/// Re-scans shared directories.
/// </summary>
/// <returns>The operation context.</returns>
public Task RescanSharesAsync() => SharedFileCache.FillAsync();
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
Log.Information("Configuring client");
ProxyOptions proxyOptions = default;
if (OptionsAtStartup.Soulseek.Connection.Proxy.Enabled)
{
proxyOptions = new ProxyOptions(
address: OptionsAtStartup.Soulseek.Connection.Proxy.Address,
port: OptionsAtStartup.Soulseek.Connection.Proxy.Port.Value,
username: OptionsAtStartup.Soulseek.Connection.Proxy.Username,
password: OptionsAtStartup.Soulseek.Connection.Proxy.Password);
}
var connectionOptions = new ConnectionOptions(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Read,
writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Write,
writeQueueSize: OptionsAtStartup.Soulseek.Connection.Buffer.WriteQueue,
connectTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Connect,
inactivityTimeout: OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity,
proxyOptions: proxyOptions);
var configureKeepAlive = new Action<Socket>(socket =>
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionsAtStartup.Soulseek.Connection.Timeout.Inactivity / 1000);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3);
});
var serverOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
var distributedOptions = connectionOptions.With(configureSocketAction: configureKeepAlive);
var transferOptions = connectionOptions.With(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer);
var patch = new SoulseekClientOptionsPatch(
listenPort: OptionsAtStartup.Soulseek.ListenPort,
enableListener: true,
userEndPointCache: new UserEndPointCache(),
distributedChildLimit: OptionsAtStartup.Soulseek.DistributedNetwork.ChildLimit,
enableDistributedNetwork: !OptionsAtStartup.Soulseek.DistributedNetwork.Disabled,
acceptDistributedChildren: !OptionsAtStartup.Soulseek.DistributedNetwork.DisableChildren,
autoAcknowledgePrivateMessages: false,
acceptPrivateRoomInvitations: true,
serverConnectionOptions: serverOptions,
peerConnectionOptions: connectionOptions,
transferConnectionOptions: transferOptions,
distributedConnectionOptions: distributedOptions,
userInfoResponseResolver: UserInfoResponseResolver,
browseResponseResolver: BrowseResponseResolver,
directoryContentsResponseResolver: DirectoryContentsResponseResolver,
enqueueDownloadAction: (username, endpoint, filename) => EnqueueDownloadAction(username, endpoint, filename, TransferTracker),
searchResponseCache: new SearchResponseCache(),
searchResponseResolver: SearchResponseResolver);
await Client.ReconfigureOptionsAsync(patch);
Log.Information("Client configured");
Log.Information("Listening on port {Port}", OptionsAtStartup.Soulseek.ListenPort);
if (OptionsAtStartup.Soulseek.Connection.Proxy.Enabled)
{
Log.Information($"Using Proxy {OptionsAtStartup.Soulseek.Connection.Proxy.Address}:{OptionsAtStartup.Soulseek.Connection.Proxy.Port}");
}
if (!OptionsAtStartup.NoVersionCheck)
{
_ = CheckVersionAsync();
}
if (OptionsAtStartup.NoShareScan)
{
Log.Warning("Not scanning shares; 'no-share-scan' option is enabled. Search and browse results will remain disabled until a manual scan is completed.");
}
else
{
_ = SharedFileCache.FillAsync();
}
if (OptionsAtStartup.NoConnect)
{
Log.Warning("Not connecting to the Soulseek server; 'no-connect' option is enabled");
}
else if (string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Username) || string.IsNullOrEmpty(OptionsAtStartup.Soulseek.Password))
{
Log.Warning($"Not connecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart.");
}
else
{
await Client.ConnectAsync(OptionsAtStartup.Soulseek.Username, OptionsAtStartup.Soulseek.Password).ConfigureAwait(false);
}
}
Task IHostedService.StopAsync(CancellationToken cancellationToken)
{
Client.Disconnect("Shutting down", new ApplicationShutdownException("Shutting down"));
Client.Dispose();
Log.Information("Client stopped");
return Task.CompletedTask;
}
/// <summary>
/// Creates and returns an instances of <see cref="BrowseResponse"/> in response to a remote request.
/// </summary>
/// <param name="username">The username of the requesting user.</param>
/// <param name="endpoint">The IP endpoint of the requesting user.</param>
/// <returns>A Task resolving an IEnumerable of Soulseek.Directory.</returns>
private Task<BrowseResponse> BrowseResponseResolver(string username, IPEndPoint endpoint)
{
var directories = SharedFileCache.Browse()
.Select(d => new Soulseek.Directory(d.Name.Replace('/', '\\'), d.Files)); // Soulseek NS requires backslashes
return Task.FromResult(new BrowseResponse(directories));
}
private void Client_BrowseProgressUpdated(object sender, BrowseProgressUpdatedEventArgs args)
{
BrowseTracker.AddOrUpdate(args.Username, args);
}
private void Client_Connected(object sender, EventArgs e)
{
Log.Information("Connected to the Soulseek server");
}
private void Client_DiagnosticGenerated(object sender, DiagnosticEventArgs args)
{
static LogEventLevel TranslateLogLevel(DiagnosticLevel diagnosticLevel) => diagnosticLevel switch
{
DiagnosticLevel.Debug => LogEventLevel.Debug,
DiagnosticLevel.Info => LogEventLevel.Information,
DiagnosticLevel.Warning => LogEventLevel.Warning,
DiagnosticLevel.None => default,
_ => default,
};
var logger = Loggers.GetOrAdd(sender.GetType().FullName, Log.ForContext("SourceContext", "Soulseek").ForContext("SoulseekContext", sender.GetType().FullName));
logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message);
}
private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args)
{
if (State.CurrentValue.PendingReconnect)
{
State.SetValue(state => state with { PendingReconnect = false });
}
if (args.Exception is ObjectDisposedException || args.Exception is ApplicationShutdownException)
{
Log.Information("Disconnected from the Soulseek server: the client is shutting down");
}
else if (args.Exception is IntentionalDisconnectException)
{
Log.Information("Disconnected from the Soulseek server: disconnected by the user");
}
else if (args.Exception is LoginRejectedException)
{
Log.Error("Disconnected from the Soulseek server: invalid username or password");
}
else if (args.Exception is KickedFromServerException)
{
Log.Error("Disconnected from the Soulseek server: another client logged in using the username {Username}", Client.Username);
}
else
{
Log.Error("Disconnected from the Soulseek server: {Message}", args.Exception?.Message ?? args.Message);
if (string.IsNullOrEmpty(Options.Soulseek.Username) || string.IsNullOrEmpty(Options.Soulseek.Password))
{
Log.Warning($"Not reconnecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart.");
return;
}
var attempts = 1;
while (true)
{
var (delay, jitter) = Compute.ExponentialBackoffDelay(
iteration: attempts,
maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000);
Log.Information($"Waiting about {(approximateDelay == 1 ? "a second" : $"{approximateDelay} seconds")} before reconnecting");
await Task.Delay(delay + jitter);
Log.Information($"Attempting to reconnect (#{attempts})...", attempts);
try
{
// reconnect with the latest configuration values we have for username and password, instead of the
// options that were captured at startup. if a user has updated these values prior to the disconnect, the
// changes will take effect now.
await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
break;
}
catch (Exception ex)
{
attempts++;
Log.Error("Failed to reconnect: {Message}", ex.Message);
}
}
}
}
private void Client_LoggedIn(object sender, EventArgs e)
{
Log.Information("Logged in to the Soulseek server as {Username}", Client.Username);
// send whatever counts we have currently. we'll probably connect before the cache is primed, so these will be zero
// initially, but we'll update them when the cache is filled.
_ = Client.SetSharedCountsAsync(State.CurrentValue.SharedFileCache.Directories, State.CurrentValue.SharedFileCache.Files);
}
private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args)
{
ConversationTracker.AddOrUpdate(args.Username, PrivateMessage.FromEventArgs(args));
if (Options.Integration.Pushbullet.Enabled && !args.Replayed)
{
Console.WriteLine("Pushing...");
_ = Pushbullet.PushAsync($"Private Message from {args.Username}", args.Username, args.Message);
}
}
private void Client_PublicChatMessageReceived(object sender, PublicChatMessageReceivedEventArgs args)
{
Console.WriteLine($"[PUBLIC CHAT] [{args.RoomName}] [{args.Username}]: {args.Message}");
if (Options.Integration.Pushbullet.Enabled && args.Message.Contains(Client.Username))
{
_ = Pushbullet.PushAsync($"Room Mention by {args.Username} in {args.RoomName}", args.RoomName, args.Message);
}
}
private void Client_RoomMessageReceived(object sender, RoomMessageReceivedEventArgs args)
{
var message = RoomMessage.FromEventArgs(args, DateTime.UtcNow);
if (Options.Integration.Pushbullet.Enabled && message.Message.Contains(Client.Username))
{
_ = Pushbullet.PushAsync($"Room Mention by {message.Username} in {message.RoomName}", message.RoomName, message.Message);
}
}
private void Client_StateChanged(object sender, SoulseekClientStateChangedEventArgs e)
{
State.SetValue(state => state with { Server = state.Server with { Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, Username = Client.Username } });
}
private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args)
{
// this is really verbose. Console.WriteLine($"[{args.Transfer.Direction.ToString().ToUpper()}]
// [{args.Transfer.Username}/{Path.GetFileName(args.Transfer.Filename)}]
// {args.Transfer.BytesTransferred}/{args.Transfer.Size} {args.Transfer.PercentComplete}% {args.Transfer.AverageSpeed}kb/s");
}
private void Client_TransferStateChanged(object sender, TransferStateChangedEventArgs args)
{
var xfer = args.Transfer;
var direction = xfer.Direction.ToString().ToUpper();
var user = xfer.Username;
var file = Path.GetFileName(xfer.Filename);
var oldState = args.PreviousState;
var state = xfer.State;
var completed = xfer.State.HasFlag(TransferStates.Completed);
Console.WriteLine($"[{direction}] [{user}/{file}] {oldState} => {state}{(completed ? $" ({xfer.BytesTransferred}/{xfer.Size} = {xfer.PercentComplete}%) @ {xfer.AverageSpeed.SizeSuffix()}/s" : string.Empty)}");
if (xfer.Direction == TransferDirection.Upload && xfer.State.HasFlag(TransferStates.Completed | TransferStates.Succeeded))
{
_ = Client.SendUploadSpeedAsync((int)args.Transfer.AverageSpeed);
}
}
private void Client_UserStatusChanged(object sender, UserStatus args)
{
Console.WriteLine($"[USER] {args.Username}: {args.Presence}");
}
/// <summary>
/// Creates and returns a <see cref="Soulseek.Directory"/> in response to a remote request.
/// </summary>
/// <param name="username">The username of the requesting user.</param>
/// <param name="endpoint">The IP endpoint of the requesting user.</param>
/// <param name="token">The unique token for the request, supplied by the requesting user.</param>
/// <param name="directory">The requested directory.</param>
/// <returns>A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory.</returns>
private Task<Soulseek.Directory> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory)
{
var dir = SharedFileCache.List(directory);
return Task.FromResult(dir);
}
/// <summary>
/// Invoked upon a remote request to download a file.
/// </summary>
/// <param name="username">The username of the requesting user.</param>
/// <param name="endpoint">The IP endpoint of the requesting user.</param>
/// <param name="filename">The filename of the requested file.</param>
/// <param name="tracker">(for example purposes) the ITransferTracker used to track progress.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
/// <exception cref="DownloadEnqueueException">
/// Thrown when the download is rejected. The Exception message will be passed to the remote user.
/// </exception>
/// <exception cref="Exception">
/// Thrown on any other Exception other than a rejection. A generic message will be passed to the remote user for
/// security reasons.
/// </exception>
private Task EnqueueDownloadAction(string username, IPEndPoint endpoint, string filename, ITransferTracker tracker)
{
_ = endpoint;
var localFilename = SharedFileCache.Resolve(filename).ToLocalOSPath();
var fileInfo = new FileInfo(localFilename);
if (!fileInfo.Exists)
{
Console.WriteLine($"[UPLOAD REJECTED] File {localFilename} not found.");
throw new DownloadEnqueueException($"File not shared.");
}
if (tracker.TryGet(TransferDirection.Upload, username, filename, out _))
{
// in this case, a re-requested file is a no-op. normally we'd want to respond with a PlaceInQueueResponse
Console.WriteLine($"[UPLOAD RE-REQUESTED] [{username}/{filename}]");
return Task.CompletedTask;
}
// create a new cancellation token source so that we can cancel the upload from the UI.
var cts = new CancellationTokenSource();
var topts = new TransferOptions(stateChanged: (e) => tracker.AddOrUpdate(e, cts), progressUpdated: (e) => tracker.AddOrUpdate(e, cts), governor: (t, c) => Task.Delay(1, c));
// accept all download requests, and begin the upload immediately. normally there would be an internal queue, and
// uploads would be handled separately.
Task.Run(async () =>
{
using var stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
await Client.UploadAsync(username, filename, fileInfo.Length, stream, options: topts, cancellationToken: cts.Token);
}).ContinueWith(t =>
{
Console.WriteLine($"[UPLOAD FAILED] {t.Exception}");
}, TaskContinuationOptions.NotOnRanToCompletion); // fire and forget
// return a completed task so that the invoking code can respond to the remote client.
return Task.CompletedTask;
}
private async Task OptionsMonitor_OnChange(Options newOptions)
{
// this code is known to fire more than once per update. i'm not sure whether these might be executed concurrently.
// lock to be safe, because we need to accurately track the last value of Options for diffing purposes. threading
// shenanigans here could lead to missed updates.
await OptionsSyncRoot.WaitAsync();
try
{
var pendingRestart = false;
var pendingReconnect = false;
var soulseekRequiresReconnect = false;
var diff = PreviousOptions.DiffWith(newOptions);
// don't react to duplicate/no-change events https://github.com/slskd/slskd/issues/126
if (!diff.Any())
{
return;
}
foreach (var (property, fqn, left, right) in diff)
{
static bool HasAttribute<T>(PropertyInfo property) => property.CustomAttributes.Any(a => a.AttributeType == typeof(T));
var requiresRestart = HasAttribute<RequiresRestartAttribute>(property);
var requiresReconnect = HasAttribute<RequiresReconnectAttribute>(property);
Log.Debug($"{fqn} changed from '{left.ToJson() ?? "<null>"}' to '{right.ToJson() ?? "<null>"}'{(requiresRestart ? "; Restart required to take effect." : string.Empty)}{(requiresReconnect ? "; Reconnect required to take effect." : string.Empty)}");
pendingRestart |= requiresRestart;
pendingReconnect |= requiresReconnect;
}
if (PreviousOptions.Directories.Shared.Except(newOptions.Directories.Shared).Any()
|| newOptions.Directories.Shared.Except(PreviousOptions.Directories.Shared).Any())
{
State.SetValue(state => state with { PendingShareRescan = true });
Log.Information("Shared directory configuration changed. Shares must be re-scanned for changes to take effect.");
}
if (PreviousOptions.Filters.Share.Except(newOptions.Filters.Share).Any()
|| newOptions.Filters.Share.Except(PreviousOptions.Filters.Share).Any())
{
State.SetValue(state => state with { PendingShareRescan = true });
Log.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect.");
}
if (PreviousOptions.Rooms.Except(newOptions.Rooms).Any()
|| newOptions.Rooms.Except(PreviousOptions.Rooms).Any())
{
Log.Information("Room configuration changed. Joining any newly added rooms.");
_ = RoomService.TryJoinAsync(newOptions.Rooms);
}
// determine whether any Soulseek options changed. if so, we need to construct a patch and invoke ReconfigureOptionsAsync().
var slskDiff = PreviousOptions.Soulseek.DiffWith(newOptions.Soulseek);
if (slskDiff.Any())
{
var old = PreviousOptions.Soulseek;
var update = newOptions.Soulseek;
Log.Debug("Soulseek options changed from {Previous} to {Current}", old.ToJson(), update.ToJson());
// determine whether any Connection options changed. if so, replace the whole object. Soulseek.NET doesn't
// offer a way to patch parts of connection options. the updates only affect new connections, so a partial
// patch doesn't make a lot of sense anyway.
var connectionDiff = old.Connection.DiffWith(update.Connection);
ConnectionOptions connectionPatch = null;
ConnectionOptions serverPatch = null;
ConnectionOptions distributedPatch = null;
ConnectionOptions transferPatch = null;
if (connectionDiff.Any())
{
var connection = update.Connection;
ProxyOptions proxyPatch = null;
if (connection.Proxy.Enabled)
{
proxyPatch = new ProxyOptions(
address: connection.Proxy.Address,
port: connection.Proxy.Port.Value,
username: connection.Proxy.Username,
password: connection.Proxy.Password);
}
connectionPatch = new ConnectionOptions(
readBufferSize: connection.Buffer.Read,
writeBufferSize: connection.Buffer.Write,
writeQueueSize: connection.Buffer.WriteQueue,
connectTimeout: connection.Timeout.Connect,
inactivityTimeout: connection.Timeout.Inactivity,
proxyOptions: proxyPatch);
var configureKeepAlive = new Action<Socket>(socket =>
{
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, connection.Timeout.Inactivity / 1000);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, connection.Timeout.Inactivity / 1000);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3);
});
serverPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
distributedPatch = connectionPatch.With(configureSocketAction: configureKeepAlive);
transferPatch = connectionPatch.With(
readBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer,
writeBufferSize: OptionsAtStartup.Soulseek.Connection.Buffer.Transfer);
}
var patch = new SoulseekClientOptionsPatch(
listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort,
enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled,
distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit,
acceptDistributedChildren: old.DistributedNetwork.DisableChildren == update.DistributedNetwork.DisableChildren ? null : !update.DistributedNetwork.DisableChildren,
serverConnectionOptions: serverPatch,
peerConnectionOptions: connectionPatch,
transferConnectionOptions: transferPatch,
incomingConnectionOptions: connectionPatch,
distributedConnectionOptions: distributedPatch);
soulseekRequiresReconnect = await Client.ReconfigureOptionsAsync(patch);
}
// require a reconnect if the client is connected and any options marked [RequiresReconnect] changed, OR if the
// call to reconfigure the client requires a reconnect
if ((Client.State.HasFlag(SoulseekClientStates.Connected) && pendingReconnect) || soulseekRequiresReconnect)
{
State.SetValue(state => state with { PendingReconnect = true });
Log.Information("One or more updated Soulseek options requires the client to be disconnected, then reconnected to the network to take effect.");
}
if (pendingRestart)
{
State.SetValue(state => state with { PendingRestart = true });
Log.Information("One or more updated options requires an application restart to take effect.");
}
PreviousOptions = newOptions;
_ = ApplicationHub.BroadcastOptionsAsync(newOptions);
Log.Information("Options updated successfully.");
}
catch (Exception ex)
{
Log.Error(ex, "Failed to apply option update: {Message}", ex.Message);
}
finally
{
OptionsSyncRoot.Release();
}
}
/// <summary>
/// Creates and returns a <see cref="Response"/> in response to the given <paramref name="query"/>.
/// </summary>
/// <param name="username">The username of the requesting user.</param>
/// <param name="token">The search token.</param>
/// <param name="query">The search query.</param>
/// <returns>A Task resolving a SearchResponse, or null.</returns>
private async Task<SearchResponse> SearchResponseResolver(string username, int token, SearchQuery query)
{
// some bots continually query for very common strings. blacklist known names here.
var blacklist = new[] { "Lola45", "Lolo51", "rajah" };
if (blacklist.Contains(username))
{
return null;
}
// some bots and perhaps users search for very short terms. only respond to queries >= 3 characters. sorry, U2 fans.
if (query.Query.Length < 3)
{
return null;
}
var results = await SharedFileCache.SearchAsync(query);
if (results.Any())
{
Console.WriteLine($"[SENDING SEARCH RESULTS]: {results.Count()} records to {username} for query {query.SearchText}");
return new SearchResponse(
Client.Username,
token,
freeUploadSlots: 1,
uploadSpeed: 0,
queueLength: 0,
fileList: results);
}
// if no results, either return null or an instance of SearchResponse with a fileList of length 0 in either case, no
// response will be sent to the requestor.
return null;
}
private void SharedFileCacheState_OnChange((SharedFileCacheState Previous, SharedFileCacheState Current) state)
{
var (previous, current) = state;
if (!previous.Filling && current.Filling)
{
SharesRefreshStarted = DateTime.UtcNow;
State.SetValue(s => s with { SharedFileCache = current });
Log.Information("Scanning shares");
}
var lastProgress = Math.Round(previous.FillProgress * 100);
var currentProgress = Math.Round(current.FillProgress * 100);
if (lastProgress != currentProgress && Math.Round(currentProgress, 0) % 10 == 0)
{
State.SetValue(s => s with { SharedFileCache = current });
Log.Information("Scanned {Percent}% of shared directories. Found {Files} files so far.", currentProgress, current.Files);
}
if (previous.Filling && !current.Filling)
{
State.SetValue(s => s with { SharedFileCache = current });
if (current.Faulted)
{
Log.Error("Failed to scan shares.");
}
else
{
State.SetValue(s => s with { PendingShareRescan = false });
Log.Information("Shares scanned successfully. Found {Directories} directories and {Files} files in {Duration}ms", current.Directories, current.Files, (DateTime.UtcNow - SharesRefreshStarted).TotalMilliseconds);
SharesRefreshStarted = default;
if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn))
{
_ = Client.SetSharedCountsAsync(State.CurrentValue.SharedFileCache.Directories, State.CurrentValue.SharedFileCache.Files);
}
}
}
}
private void State_OnChange((State Previous, State Current) state)
{
Log.Debug("State changed from {Previous} to {Current}", state.Previous.ToJson(), state.Current.ToJson());
_ = ApplicationHub.BroadcastStateAsync(state.Current);
}
/// <summary>
/// Creates and returns a <see cref="UserInfo"/> object in response to a remote request.
/// </summary>
/// <param name="username">The username of the requesting user.</param>
/// <param name="endpoint">The IP endpoint of the requesting user.</param>
/// <returns>A Task resolving the UserInfo instance.</returns>
private Task<UserInfo> UserInfoResponseResolver(string username, IPEndPoint endpoint)
{
var info = new UserInfo(
description: $"Soulseek.NET Web Example! also, your username is {username}, and IP endpoint is {endpoint}",
uploadSlots: 1,
queueLength: 0,
hasFreeUploadSlot: false);
return Task.FromResult(info);
}
}
}
|