Skip to content

Automatic Port Selection and Secured Communication#1038

Open
FlorianRappl wants to merge 3 commits intodevelopfrom
feature/socket-enhancements
Open

Automatic Port Selection and Secured Communication#1038
FlorianRappl wants to merge 3 commits intodevelopfrom
feature/socket-enhancements

Conversation

@FlorianRappl
Copy link
Collaborator

Follow up for #1021 - should be reviewed / merged after #1037.

Introduces an authentication token to be used in the communication between Node.js (Electron) and .NET. The same token can be used to also guard an ASP.NET Core server - allowing only access from within the boundaries of the running application.

The port is now OS-selected - guaranteeing a free accessible port. No tools to find free ports are necessary.

The owning part (e.g., Node.js for the WebSocket connection to control the Electron application) also owns / opens the port - independent of the used startup mode.

@FlorianRappl FlorianRappl added this to the 0.5.0 milestone Mar 5, 2026
@FlorianRappl FlorianRappl changed the base branch from main to develop March 5, 2026 10:57
@FlorianRappl FlorianRappl requested a review from softworkz March 5, 2026 22:21
@github-actions
Copy link

github-actions bot commented Mar 6, 2026

pr-comment: Run #60

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
1400 1238 0 0 0 162 0 4m 30s

🎉 All tests passed!

Github Test Reporter by CTRF 💚

🔄 This comment has been updated

@FlorianRappl
Copy link
Collaborator Author

Should we move this forward @softworkz ?

@softworkz
Copy link
Collaborator

Sorry, haven't come to this yet.

Why is there a merge conflict?

@FlorianRappl
Copy link
Collaborator Author

Sorry, haven't come to this yet.

Why is there a merge conflict?

There is none. There was initially one as planned due to the splitting.

@softworkz
Copy link
Collaborator

image image

@FlorianRappl
Copy link
Collaborator Author

FlorianRappl commented Mar 15, 2026

Shows different for me:

image

Also:

image

Furthermore:

$> git pull origin develop
From https://github.com/ElectronNET/Electron.NET
 * branch            develop    -> FETCH_HEAD
Already up to date.
$> git push origin feature/socket-enhancements
Everything up-to-date

Copy link
Collaborator

@softworkz softworkz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very sorry for responding so late!

Comment on lines +189 to +192
this.process.LineReceived += Read_SocketIO_Parameters;
this.process.Run(startCmd, args, directoriy);

await Task.Delay(500.ms()).ConfigureAwait(false);
await tcs.Task.ConfigureAwait(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If electron errors and doesn't print that line, the task will hang forever.
Whether process will still exit, I'm not sure, but it should keep hanging their either case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should hang there as we can't communicate.

What we can talk about is a barrier here (e.g., 10 seconds) - after that the process would terminate. I would, however, not make it too small. Reason for not including that right now is that I am not sure how viable it is - and choosing a number that is too low might create more problems than solve here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just register the exit event to try-cancel the tcs in addition to a comfortable timeout

Comment on lines +295 to +299
if (authToken && socket.request.headers.authorization !== authToken) {
console.warn('Electron Socket authentication failed!');
socket.disconnect(true);
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it look like the auth token would be optional, but it isn't
(yet it should be)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not make it optional - its part of the standard flow. There is no reason to have this optional.

Comment on lines +168 to +180
var match = extractor.Match(line);

if (match?.Success ?? false)
{
var port = int.Parse(match.Groups[1].Value);
var token = match.Groups[2].Value;

this.process.LineReceived -= Read_SocketIO_Parameters;
ElectronNetRuntime.ElectronAuthToken = token;
ElectronNetRuntime.ElectronSocketPort = port;
tcs.SetResult();
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is always providing the electronforcedport parameter, in that case it's pointless to monitor the output for a port number.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. We should always monitor here. Right now the forcedport is like a recommendation - it can be also "0" / or later on use a different port in case the recommended one was blocked.

I would always follow the same logic here - makes it easier to debug and reason.

Comment on lines +163 to +167
var tcs = new TaskCompletionSource();

void Read_SocketIO_Parameters(object sender, string line)
{
await Task.Delay(10.ms()).ConfigureAwait(false);
// Look for "Electron Socket: listening on port %s at ..."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a path completely without reading the process output.
(without auth token)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not do that. It's less secure and just more complicated. Let's keep it to the point.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not want to have that in our application that dotnet is relying on the console output of Electron.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have nothing against making the auth token mandatory, but it can be provided by the dotnet side as cli param, so it doesn't need to be read it from the console output. (that's no more and no less secure)

Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants