-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserAgent.cs
More file actions
81 lines (70 loc) · 2.61 KB
/
Copy pathUserAgent.cs
File metadata and controls
81 lines (70 loc) · 2.61 KB
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
using System.Reflection;
using System.Runtime.InteropServices;
namespace Coder.Desktop.CoderSdk;
/// <summary>
/// Identifies which Coder Desktop process is making a request.
/// </summary>
public enum CoderComponent
{
/// <summary>The tray application.</summary>
Desktop,
/// <summary>The privileged background service that manages the VPN tunnel.</summary>
Core,
}
/// <summary>
/// Builds the User-Agent header value sent by every Coder Desktop HTTP client.
/// </summary>
public static class UserAgent
{
private const string DesktopToken = "coder-desktop";
private const string CoreToken = "coder-desktop-core";
private const string UnknownVersion = "0.0.0";
private const string UnknownPlatform = "unknown";
/// <summary>
/// Builds a User-Agent for <paramref name="component" />, taking the version from the entry assembly.
/// </summary>
public static string Build(CoderComponent component)
{
return Build(component, Assembly.GetEntryAssembly());
}
/// <summary>
/// Builds a User-Agent for component, taking the version from versionSource. Prefer the single-argument overload outside of tests.
/// </summary>
public static string Build(CoderComponent component, Assembly? versionSource)
{
return $"{TokenOf(component)}/{VersionOf(versionSource)} ({Goos()}/{Goarch()})";
}
private static string TokenOf(CoderComponent component)
{
return component switch
{
CoderComponent.Desktop => DesktopToken,
CoderComponent.Core => CoreToken,
_ => throw new ArgumentOutOfRangeException(nameof(component), component, null),
};
}
private static string VersionOf(Assembly? assembly)
{
// Assembly versions are four-part (0.8.4.0); the User-Agent reports the three-part release.
var version = assembly?.GetName().Version;
return version is null ? UnknownVersion : $"{version.Major}.{version.Minor}.{version.Build}";
}
// Platform names deliberately match Go's GOOS/GOARCH Desktop clients, the CLI and the vpn-daemon
private static string Goos()
{
if (OperatingSystem.IsWindows()) return "windows";
if (OperatingSystem.IsMacOS()) return "darwin";
if (OperatingSystem.IsLinux()) return "linux";
return UnknownPlatform;
}
private static string Goarch()
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "amd64",
Architecture.Arm => "arm",
Architecture.Arm64 => "arm64",
_ => UnknownPlatform,
};
}
}