-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrayWindowViewModel.cs
272 lines (230 loc) · 9.81 KB
/
TrayWindowViewModel.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Coder.Desktop.App.Models;
using Coder.Desktop.App.Services;
using Coder.Desktop.Vpn.Proto;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Google.Protobuf;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Exception = System.Exception;
namespace Coder.Desktop.App.ViewModels;
public partial class TrayWindowViewModel : ObservableObject
{
private const int MaxAgents = 5;
private const string DefaultDashboardUrl = "https://coder.com";
private readonly IRpcController _rpcController;
private readonly ICredentialManager _credentialManager;
private DispatcherQueue? _dispatcherQueue;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowEnableSection))]
[NotifyPropertyChangedFor(nameof(ShowWorkspacesHeader))]
[NotifyPropertyChangedFor(nameof(ShowNoAgentsSection))]
[NotifyPropertyChangedFor(nameof(ShowAgentsSection))]
public partial VpnLifecycle VpnLifecycle { get; set; } = VpnLifecycle.Unknown;
// This is a separate property because we need the switch to be 2-way.
[ObservableProperty] public partial bool VpnSwitchActive { get; set; } = false;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowEnableSection))]
[NotifyPropertyChangedFor(nameof(ShowWorkspacesHeader))]
[NotifyPropertyChangedFor(nameof(ShowNoAgentsSection))]
[NotifyPropertyChangedFor(nameof(ShowAgentsSection))]
[NotifyPropertyChangedFor(nameof(ShowAgentOverflowButton))]
[NotifyPropertyChangedFor(nameof(ShowFailedSection))]
public partial string? VpnFailedMessage { get; set; } = null;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(VisibleAgents))]
[NotifyPropertyChangedFor(nameof(ShowNoAgentsSection))]
[NotifyPropertyChangedFor(nameof(ShowAgentsSection))]
[NotifyPropertyChangedFor(nameof(ShowAgentOverflowButton))]
public partial List<AgentViewModel> Agents { get; set; } = [];
public bool ShowEnableSection => VpnFailedMessage is null && VpnLifecycle is not VpnLifecycle.Started;
public bool ShowWorkspacesHeader => VpnFailedMessage is null && VpnLifecycle is VpnLifecycle.Started;
public bool ShowNoAgentsSection =>
VpnFailedMessage is null && Agents.Count == 0 && VpnLifecycle is VpnLifecycle.Started;
public bool ShowAgentsSection =>
VpnFailedMessage is null && Agents.Count > 0 && VpnLifecycle is VpnLifecycle.Started;
public bool ShowFailedSection => VpnFailedMessage is not null;
public bool ShowAgentOverflowButton => VpnFailedMessage is null && Agents.Count > MaxAgents;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(VisibleAgents))]
public partial bool ShowAllAgents { get; set; } = false;
public IEnumerable<AgentViewModel> VisibleAgents => ShowAllAgents ? Agents : Agents.Take(MaxAgents);
[ObservableProperty] public partial string DashboardUrl { get; set; } = "https://coder.com";
public TrayWindowViewModel(IRpcController rpcController, ICredentialManager credentialManager)
{
_rpcController = rpcController;
_credentialManager = credentialManager;
}
public void Initialize(DispatcherQueue dispatcherQueue)
{
_dispatcherQueue = dispatcherQueue;
_rpcController.StateChanged += (_, rpcModel) => UpdateFromRpcModel(rpcModel);
UpdateFromRpcModel(_rpcController.GetState());
_credentialManager.CredentialsChanged += (_, credentialModel) => UpdateFromCredentialsModel(credentialModel);
UpdateFromCredentialsModel(_credentialManager.GetCachedCredentials());
}
private void UpdateFromRpcModel(RpcModel rpcModel)
{
// Ensure we're on the UI thread.
if (_dispatcherQueue == null) return;
if (!_dispatcherQueue.HasThreadAccess)
{
_dispatcherQueue.TryEnqueue(() => UpdateFromRpcModel(rpcModel));
return;
}
// As a failsafe, if RPC is disconnected we disable the switch. The
// Window should not show the current Page if the RPC is disconnected.
if (rpcModel.RpcLifecycle is RpcLifecycle.Disconnected)
{
VpnLifecycle = VpnLifecycle.Unknown;
VpnSwitchActive = false;
Agents = [];
return;
}
VpnLifecycle = rpcModel.VpnLifecycle;
VpnSwitchActive = rpcModel.VpnLifecycle is VpnLifecycle.Starting or VpnLifecycle.Started;
// Get the current dashboard URL.
var credentialModel = _credentialManager.GetCachedCredentials();
Uri? coderUri = null;
if (credentialModel.State == CredentialState.Valid && !string.IsNullOrWhiteSpace(credentialModel.CoderUrl))
try
{
coderUri = new Uri(credentialModel.CoderUrl, UriKind.Absolute);
}
catch
{
// Ignore
}
// Add every known agent.
HashSet<ByteString> workspacesWithAgents = [];
List<AgentViewModel> agents = [];
foreach (var agent in rpcModel.Agents)
{
// Find the FQDN with the least amount of dots and split it into
// prefix and suffix.
var fqdn = agent.Fqdn
.Select(a => a.Trim('.'))
.Where(a => !string.IsNullOrWhiteSpace(a))
.Aggregate((a, b) => a.Count(c => c == '.') < b.Count(c => c == '.') ? a : b);
if (string.IsNullOrWhiteSpace(fqdn))
continue;
var fqdnPrefix = fqdn;
var fqdnSuffix = "";
if (fqdn.Contains('.'))
{
fqdnPrefix = fqdn[..fqdn.LastIndexOf('.')];
fqdnSuffix = fqdn[fqdn.LastIndexOf('.')..];
}
var lastHandshakeAgo = DateTime.UtcNow.Subtract(agent.LastHandshake.ToDateTime());
workspacesWithAgents.Add(agent.WorkspaceId);
var workspace = rpcModel.Workspaces.FirstOrDefault(w => w.Id == agent.WorkspaceId);
agents.Add(new AgentViewModel
{
Hostname = fqdnPrefix,
HostnameSuffix = fqdnSuffix,
ConnectionStatus = lastHandshakeAgo < TimeSpan.FromMinutes(5)
? AgentConnectionStatus.Green
: AgentConnectionStatus.Yellow,
DashboardUrl = WorkspaceUri(coderUri, workspace?.Name),
});
}
// For every stopped workspace that doesn't have any agents, add a
// dummy agent row.
foreach (var workspace in rpcModel.Workspaces.Where(w =>
w.Status == Workspace.Types.Status.Stopped && !workspacesWithAgents.Contains(w.Id)))
agents.Add(new AgentViewModel
{
// We just assume that it's a single-agent workspace.
Hostname = workspace.Name,
HostnameSuffix = ".coder",
ConnectionStatus = AgentConnectionStatus.Gray,
DashboardUrl = WorkspaceUri(coderUri, workspace.Name),
});
// Sort by status green, red, gray, then by hostname.
agents.Sort((a, b) =>
{
if (a.ConnectionStatus != b.ConnectionStatus)
return a.ConnectionStatus.CompareTo(b.ConnectionStatus);
return string.Compare(a.FullHostname, b.FullHostname, StringComparison.Ordinal);
});
Agents = agents;
if (Agents.Count < MaxAgents) ShowAllAgents = false;
}
private string WorkspaceUri(Uri? baseUri, string? workspaceName)
{
if (baseUri == null) return DefaultDashboardUrl;
if (string.IsNullOrWhiteSpace(workspaceName)) return baseUri.ToString();
try
{
return new Uri(baseUri, $"/@me/{workspaceName}").ToString();
}
catch
{
return DefaultDashboardUrl;
}
}
private void UpdateFromCredentialsModel(CredentialModel credentialModel)
{
// HACK: the HyperlinkButton crashes the whole app if the initial URI
// or this URI is invalid. CredentialModel.CoderUrl should never be
// null while the Page is active as the Page is only displayed when
// CredentialModel.Status == Valid.
DashboardUrl = credentialModel.CoderUrl ?? DefaultDashboardUrl;
}
public void VpnSwitch_Toggled(object sender, RoutedEventArgs e)
{
if (sender is not ToggleSwitch toggleSwitch) return;
VpnFailedMessage = null;
// The start/stop methods will call back to update the state.
if (toggleSwitch.IsOn && VpnLifecycle is VpnLifecycle.Stopped)
_ = StartVpn(); // in the background
else if (!toggleSwitch.IsOn && VpnLifecycle is VpnLifecycle.Started)
_ = StopVpn(); // in the background
else
toggleSwitch.IsOn = VpnLifecycle is VpnLifecycle.Starting or VpnLifecycle.Started;
}
private async Task StartVpn()
{
try
{
await _rpcController.StartVpn();
}
catch (Exception e)
{
VpnFailedMessage = "Failed to start Coder Connect: " + MaybeUnwrapTunnelError(e);
}
}
private async Task StopVpn()
{
try
{
await _rpcController.StopVpn();
}
catch (Exception e)
{
VpnFailedMessage = "Failed to stop Coder Connect: " + MaybeUnwrapTunnelError(e);
}
}
private static string MaybeUnwrapTunnelError(Exception e)
{
if (e is VpnLifecycleException vpnError) return vpnError.Message;
return e.ToString();
}
[RelayCommand]
public void ToggleShowAllAgents()
{
ShowAllAgents = !ShowAllAgents;
}
[RelayCommand]
public void SignOut()
{
if (VpnLifecycle is not VpnLifecycle.Stopped)
return;
_credentialManager.ClearCredentials();
}
}