Skip to content

Commit 4b10fc6

Browse files
Add cross-platform named-pipe IPC
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b7759a7 commit 4b10fc6

5 files changed

Lines changed: 234 additions & 41 deletions

File tree

‎cli-arguments.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@
9393
| `package ignored remove --id id [--manager name] [--version v]` | Removes an ignored-update rule for a package and refreshes the updates view | 2026.1+ |
9494
| `package update-all` | Queues updates for all packages currently shown as upgradable | 2026.1+ |
9595
| `package update-manager --manager name` | Queues updates for all packages handled by the specified manager | 2026.1+ |
96-
| `--background-api-transport {tcp\|named-pipe}` | Selects which local automation transport UniGetUI uses for the background API when the app starts. On Windows, the default is `named-pipe`, so UniGetUI does not open a localhost TCP listener unless you explicitly choose `tcp`. | 2026.1+ |
96+
| `--background-api-transport {tcp\|named-pipe}` | Selects which local automation transport UniGetUI uses for the background API when the app starts. The default is `named-pipe`, so UniGetUI does not open a localhost TCP listener unless you explicitly choose `tcp`. On Windows this uses a Windows named pipe; on non-Windows it uses a Unix domain socket. | 2026.1+ |
9797
| `--background-api-port port` | Overrides the localhost TCP port used by the background API when `--background-api-transport tcp` is active | 2026.1+ |
98-
| `--background-api-pipe-name name` | Overrides the Windows named pipe name used by the background API when `--background-api-transport named-pipe` is active | 2026.1+ |
98+
| `--background-api-pipe-name name` | Overrides the local `named-pipe` endpoint used by the background API when `--background-api-transport named-pipe` is active. On Windows this is the pipe name; on non-Windows plain names resolve under `/tmp`, and absolute paths use that exact Unix socket path. | 2026.1+ |
9999
| `--transport {tcp\|named-pipe}` | Overrides the client-side automation transport used by direct CLI commands such as `status`, `app ...`, `package ...`, and the rest of the verb-based command tree | 2026.1+ |
100100
| `--tcp-port port` | Overrides the client-side localhost TCP port used by direct CLI commands | 2026.1+ |
101-
| `--pipe-name name` | Overrides the client-side named pipe used by direct CLI commands | 2026.1+ |
101+
| `--pipe-name name` | Overrides the client-side `named-pipe` endpoint used by direct CLI commands. On Windows this is the pipe name; on non-Windows plain names resolve under `/tmp`, and absolute paths use that exact Unix socket path. | 2026.1+ |
102102

103103
1. See the available list of setting keys [here](https://github.com/Devolutions/UniGetUI/blob/fc98f312a72b80e14a8ac10687f4fc506a5c9cc4/src/UniGetUI.Core.Settings/SettingsEngine_Names.cs#L5)
104104
2. See the available list of secure settings keys [here](https://github.com/Devolutions/UniGetUI/blob/fc98f312a72b80e14a8ac10687f4fc506a5c9cc4/src/UniGetUI.Core.SecureSettings/SecureSettings.cs#L10)

‎src/UniGetUI.Interface.BackgroundApi/BackgroundApi.cs‎

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class BackgroundApiRunner
3838
private IHost? _host;
3939
private BackgroundApiTransportOptions _transportOptions =
4040
BackgroundApiTransportOptions.LoadForServer();
41+
private string? _namedPipePath;
4142
private int _stopRequested;
4243

4344
public BackgroundApiRunner() { }
@@ -50,6 +51,8 @@ public static bool AuthenticateToken(string? token)
5051
public async Task Start()
5152
{
5253
_transportOptions = BackgroundApiTransportOptions.LoadForServer();
54+
_namedPipePath = _transportOptions.NamedPipePath;
55+
PrepareTransportEndpoint();
5356
ApiTokenHolder.Token = CoreTools.RandomString(64);
5457
Logger.Info("Generated a background API auth token for the current session");
5558

@@ -176,13 +179,16 @@ public async Task Start()
176179
catch
177180
{
178181
BackgroundApiTransportOptions.DeletePersistedMetadata(SessionId);
182+
CleanupTransportEndpoint();
179183
_host.Dispose();
180184
_host = null;
181185
throw;
182186
}
183187
Logger.Info(
184188
_transportOptions.TransportKind == BackgroundApiTransportKind.NamedPipe
185-
? $"Api running on named pipe {_transportOptions.NamedPipeName}"
189+
? OperatingSystem.IsWindows()
190+
? $"Api running on named pipe {_transportOptions.NamedPipeName}"
191+
: $"Api running on unix socket {_transportOptions.NamedPipeDisplayName}"
186192
: $"Api running on {_transportOptions.BaseAddressString}"
187193
);
188194
}
@@ -191,20 +197,95 @@ private void ConfigureTransport(KestrelServerOptions serverOptions)
191197
{
192198
if (_transportOptions.TransportKind == BackgroundApiTransportKind.NamedPipe)
193199
{
194-
serverOptions.ListenNamedPipe(
195-
_transportOptions.NamedPipeName,
196-
listenOptions =>
197-
{
198-
listenOptions.Protocols = HttpProtocols.Http1;
199-
}
200-
);
200+
if (OperatingSystem.IsWindows())
201+
{
202+
serverOptions.ListenNamedPipe(
203+
_transportOptions.NamedPipeName,
204+
listenOptions =>
205+
{
206+
listenOptions.Protocols = HttpProtocols.Http1;
207+
}
208+
);
209+
}
210+
else
211+
{
212+
serverOptions.ListenUnixSocket(
213+
_namedPipePath
214+
?? throw new InvalidOperationException(
215+
"The Unix socket path is not available for the current transport."
216+
),
217+
listenOptions =>
218+
{
219+
listenOptions.Protocols = HttpProtocols.Http1;
220+
}
221+
);
222+
}
201223
}
202224
else
203225
{
204226
serverOptions.ListenLocalhost(_transportOptions.TcpPort);
205227
}
206228
}
207229

230+
private void PrepareTransportEndpoint()
231+
{
232+
if (_transportOptions.TransportKind != BackgroundApiTransportKind.NamedPipe
233+
|| OperatingSystem.IsWindows())
234+
{
235+
return;
236+
}
237+
238+
if (string.IsNullOrWhiteSpace(_namedPipePath))
239+
{
240+
throw new InvalidOperationException(
241+
"The Unix socket path is required for the named-pipe transport."
242+
);
243+
}
244+
245+
string? directory = Path.GetDirectoryName(_namedPipePath);
246+
if (!string.IsNullOrWhiteSpace(directory))
247+
{
248+
Directory.CreateDirectory(directory);
249+
}
250+
251+
if (File.Exists(_namedPipePath))
252+
{
253+
if (HasExplicitUnixSocketPath())
254+
{
255+
throw new InvalidOperationException(
256+
$"Cannot bind the background API Unix socket because the explicit path \"{_namedPipePath}\" already exists."
257+
);
258+
}
259+
260+
DeleteUnixSocketFile(_namedPipePath);
261+
}
262+
}
263+
264+
private void CleanupTransportEndpoint()
265+
{
266+
if (_transportOptions.TransportKind != BackgroundApiTransportKind.NamedPipe
267+
|| OperatingSystem.IsWindows()
268+
|| string.IsNullOrWhiteSpace(_namedPipePath))
269+
{
270+
return;
271+
}
272+
273+
DeleteUnixSocketFile(_namedPipePath);
274+
}
275+
276+
private bool HasExplicitUnixSocketPath()
277+
{
278+
return _transportOptions.NamedPipeName.StartsWith("/", StringComparison.Ordinal);
279+
}
280+
281+
private static void DeleteUnixSocketFile(string path)
282+
{
283+
if (File.Exists(path))
284+
{
285+
File.Delete(path);
286+
}
287+
}
288+
208289
private async Task V3_Status(HttpContext context)
209290
{
210291
await context.Response.WriteAsJsonAsync(
@@ -218,6 +299,7 @@ await context.Response.WriteAsJsonAsync(
218299
},
219300
TcpPort = _transportOptions.TcpPort,
220301
NamedPipeName = _transportOptions.NamedPipeName,
302+
NamedPipePath = _transportOptions.NamedPipePath ?? "",
221303
BaseAddress = _transportOptions.BaseAddressString,
222304
Version = CoreData.VersionName,
223305
BuildNumber = CoreData.BuildNumber,
@@ -1888,6 +1970,7 @@ public async Task Stop()
18881970
}
18891971
finally
18901972
{
1973+
CleanupTransportEndpoint();
18911974
BackgroundApiTransportOptions.DeletePersistedMetadata(SessionId);
18921975
}
18931976
}

‎src/UniGetUI.Interface.BackgroundApi/BackgroundApiClient.cs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using System.IO.Pipes;
33
using System.Net.Http.Json;
4+
using System.Net.Sockets;
45
using System.Text.Json;
56
using UniGetUI.Core.Data;
67
using UniGetUI.Core.Logging;
@@ -107,6 +108,7 @@ public async Task<BackgroundApiStatus> GetStatusAsync()
107108
},
108109
TcpPort = TransportOptions.TcpPort,
109110
NamedPipeName = TransportOptions.NamedPipeName,
111+
NamedPipePath = TransportOptions.NamedPipePath ?? "",
110112
BaseAddress = TransportOptions.BaseAddressString,
111113
};
112114
}
@@ -1323,14 +1325,28 @@ private static HttpClient CreateHttpClient(BackgroundApiTransportOptions options
13231325
UseProxy = false,
13241326
ConnectCallback = async (_, cancellationToken) =>
13251327
{
1326-
var pipeClient = new NamedPipeClientStream(
1327-
".",
1328-
options.NamedPipeName,
1329-
PipeDirection.InOut,
1330-
PipeOptions.Asynchronous
1328+
if (OperatingSystem.IsWindows())
1329+
{
1330+
var pipeClient = new NamedPipeClientStream(
1331+
".",
1332+
options.NamedPipeName,
1333+
PipeDirection.InOut,
1334+
PipeOptions.Asynchronous
1335+
);
1336+
await pipeClient.ConnectAsync(cancellationToken);
1337+
return pipeClient;
1338+
}
1339+
1340+
string socketPath = options.NamedPipePath
1341+
?? throw new InvalidOperationException(
1342+
"The Unix socket path is not available for the named-pipe transport."
1343+
);
1344+
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
1345+
await socket.ConnectAsync(
1346+
new UnixDomainSocketEndPoint(socketPath),
1347+
cancellationToken
13311348
);
1332-
await pipeClient.ConnectAsync(cancellationToken);
1333-
return pipeClient;
1349+
return new NetworkStream(socket, ownsSocket: true);
13341350
},
13351351
};
13361352

‎src/UniGetUI.Interface.BackgroundApi/BackgroundApiTransport.cs‎

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ string NamedPipeName
2020
public const string HeadlessSessionKind = "headless";
2121
public const int DefaultTcpPort = 7058;
2222
public const string DefaultNamedPipeName = "UniGetUI.BackgroundApi";
23+
public const string DefaultUnixSocketDirectory = "/tmp";
24+
internal const int MaxUnixSocketPathLength = 104;
2325

2426
public const string TransportArgument = "--background-api-transport";
2527
public const string TcpPortArgument = "--background-api-port";
@@ -42,11 +44,19 @@ string NamedPipeName
4244
: new Uri($"http://localhost:{TcpPort}/");
4345

4446
public string BaseAddressString => BaseAddress.ToString().TrimEnd('/');
47+
public string? NamedPipePath =>
48+
TransportKind == BackgroundApiTransportKind.NamedPipe && !OperatingSystem.IsWindows()
49+
? ResolveUnixSocketPath(NamedPipeName)
50+
: null;
51+
public string NamedPipeDisplayName =>
52+
TransportKind != BackgroundApiTransportKind.NamedPipe
53+
? BaseAddressString
54+
: OperatingSystem.IsWindows()
55+
? NamedPipeName
56+
: NamedPipePath ?? NamedPipeName;
4557

4658
public static BackgroundApiTransportOptions Default { get; } = new(
47-
OperatingSystem.IsWindows()
48-
? BackgroundApiTransportKind.NamedPipe
49-
: BackgroundApiTransportKind.Tcp,
59+
BackgroundApiTransportKind.NamedPipe,
5060
DefaultTcpPort,
5161
DefaultNamedPipeName
5262
);
@@ -300,14 +310,6 @@ BackgroundApiTransportOptions fallback
300310
int tcpPort = ParseTcpPort(portValue, fallback.TcpPort);
301311
string pipeName = ParseNamedPipeName(pipeValue, fallback.NamedPipeName);
302312

303-
if (transport == BackgroundApiTransportKind.NamedPipe && !OperatingSystem.IsWindows())
304-
{
305-
Logger.Warn(
306-
"Named pipe background API transport is only supported on Windows. Falling back to TCP."
307-
);
308-
transport = BackgroundApiTransportKind.Tcp;
309-
}
310-
311313
return new BackgroundApiTransportOptions(transport, tcpPort, pipeName);
312314
}
313315

@@ -373,15 +375,48 @@ private static string ParseNamedPipeName(string? value, string fallback)
373375
}
374376

375377
string pipeName = value.Trim();
376-
if (pipeName.Length > 0)
378+
if (pipeName.Length == 0)
377379
{
378-
return pipeName;
380+
Logger.Warn(
381+
$"Invalid background API named pipe name \"{value}\". Falling back to {fallback}."
382+
);
383+
return fallback;
379384
}
380385

381-
Logger.Warn(
382-
$"Invalid background API named pipe name \"{value}\". Falling back to {fallback}."
383-
);
384-
return fallback;
386+
if (OperatingSystem.IsWindows() && Path.IsPathRooted(pipeName))
387+
{
388+
Logger.Warn(
389+
$"Absolute background API named pipe paths are not supported on Windows. Falling back to {fallback}."
390+
);
391+
return fallback;
392+
}
393+
394+
if (!OperatingSystem.IsWindows())
395+
{
396+
string resolvedPath = ResolveUnixSocketPath(pipeName);
397+
if (resolvedPath.Length > MaxUnixSocketPathLength)
398+
{
399+
Logger.Warn(
400+
$"Background API Unix socket path \"{resolvedPath}\" exceeds the supported length limit. Falling back to {fallback}."
401+
);
402+
return fallback;
403+
}
404+
}
405+
406+
return pipeName;
407+
}
408+
409+
internal static string ResolveUnixSocketPath(string pipeName)
410+
{
411+
ArgumentException.ThrowIfNullOrWhiteSpace(pipeName);
412+
413+
string trimmed = pipeName.Trim();
414+
if (trimmed.StartsWith("/", StringComparison.Ordinal))
415+
{
416+
return trimmed;
417+
}
418+
419+
return $"{DefaultUnixSocketDirectory.TrimEnd('/')}/{trimmed.TrimStart('/')}";
385420
}
386421

387422
private static string? GetArgumentValue(
@@ -409,6 +444,7 @@ public sealed class BackgroundApiStatus
409444
public string Transport { get; set; } = "tcp";
410445
public int TcpPort { get; set; }
411446
public string NamedPipeName { get; set; } = BackgroundApiTransportOptions.DefaultNamedPipeName;
447+
public string NamedPipePath { get; set; } = "";
412448
public string BaseAddress { get; set; } = "http://localhost:7058";
413449
public string Version { get; set; } = CoreData.VersionName;
414450
public int BuildNumber { get; set; } = CoreData.BuildNumber;

0 commit comments

Comments
 (0)