Skip to content

Commit af16667

Browse files
committed
feat: only allow secured connection to api
1 parent 531d13e commit af16667

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/WinService/Services/WebSocketService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace WinService.Services;
1212

13-
public class WebSocketService(ILogger<WebSocketService> logger, IApiManager apiManager, IPipeService pipeService): BackgroundService
13+
public class WebSocketService(ILogger<WebSocketService> logger, IHostApplicationLifetime applicationLifetime, IApiManager apiManager, IPipeService pipeService): BackgroundService
1414
{
1515
private readonly EnergyManager _energyManager = new();
1616
private readonly PeriodicTimer _timer = new(TimeSpan.FromSeconds(1));
@@ -30,10 +30,17 @@ private async Task RunAsync(CancellationToken stoppingToken)
3030
var uri = new Uri(apiManager.ApiUrl ?? string.Empty);
3131
if (uri == null)
3232
throw new Exception("Invalid API URL");
33+
34+
if (uri.Scheme != "https")
35+
{
36+
logger.LogCritical("Specified URL is not HTTPS");
37+
applicationLifetime.StopApplication();
38+
return;
39+
}
3340

3441
try
3542
{
36-
await ConnectAsync($"ws://{uri.Authority}/ws/computers", stoppingToken); // todo: use wss:// as default
43+
await ConnectAsync($"wss://{uri.Authority}/ws/computers", stoppingToken);
3744

3845
// start listening for commands
3946
_ = Task.Run(() => ReadCommandsAsync(stoppingToken), stoppingToken);

0 commit comments

Comments
 (0)