|
3 | 3 | using Microsoft.Deployment.WindowsInstaller; |
4 | 4 | using Microsoft.Win32; |
5 | 5 | using Newtonsoft.Json; |
| 6 | +using Newtonsoft.Json.Linq; |
6 | 7 | using System; |
7 | 8 | using System.Collections.Generic; |
8 | 9 | using System.ComponentModel; |
|
12 | 13 | using System.Linq; |
13 | 14 | using System.Runtime.InteropServices; |
14 | 15 | using System.Security.Claims; |
| 16 | +using System.Text.RegularExpressions; |
15 | 17 | using System.Threading; |
16 | 18 | using WixSharp; |
17 | 19 | using File = System.IO.File; |
@@ -318,6 +320,155 @@ public static ActionResult SetFeaturesToConfigure(Session session) |
318 | 320 | return ActionResult.Success; |
319 | 321 | } |
320 | 322 |
|
| 323 | + [CustomAction] |
| 324 | + public static ActionResult EnrollAgentTunnel(Session session) |
| 325 | + { |
| 326 | + string enrollmentString = session.Property(AgentProperties.AgentTunnelEnrollmentString); |
| 327 | + string subnetsRaw = session.Property(AgentProperties.AgentTunnelAdvertiseSubnets); |
| 328 | + string domainsRaw = session.Property(AgentProperties.AgentTunnelAdvertiseDomains); |
| 329 | + |
| 330 | + if (string.IsNullOrWhiteSpace(enrollmentString)) |
| 331 | + { |
| 332 | + session.Log("Agent tunnel enrollment string not provided, skipping tunnel setup"); |
| 333 | + return ActionResult.Success; |
| 334 | + } |
| 335 | + |
| 336 | + try |
| 337 | + { |
| 338 | + // Parse enrollment string to extract gateway URL, token, and name. |
| 339 | + // Format: dgw-enroll:v1:<base64 JSON payload> |
| 340 | + const string prefix = "dgw-enroll:v1:"; |
| 341 | + if (!enrollmentString.StartsWith(prefix)) |
| 342 | + { |
| 343 | + session.Log("Invalid enrollment string prefix"); |
| 344 | + return ActionResult.Failure; |
| 345 | + } |
| 346 | + |
| 347 | + // base64url -> base64. Strip whitespace (line breaks from copy-paste across wrapped |
| 348 | + // terminal output are common) and pad to length % 4 == 0 (RFC 4648 §5 allows omitting `=`). |
| 349 | + string base64 = Regex.Replace(enrollmentString.Substring(prefix.Length), @"\s+", "") |
| 350 | + .Replace('-', '+').Replace('_', '/'); |
| 351 | + base64 = base64.PadRight((base64.Length + 3) & ~3, '='); |
| 352 | + string json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64)); |
| 353 | + |
| 354 | + JObject payload = JsonConvert.DeserializeObject<JObject>(json); |
| 355 | + string apiBaseUrl = payload?["api_base_url"]?.Value<string>(); |
| 356 | + string enrollmentToken = payload?["enrollment_token"]?.Value<string>(); |
| 357 | + string agentName = payload?["name"]?.Value<string>(); |
| 358 | + |
| 359 | + if (string.IsNullOrWhiteSpace(apiBaseUrl) || string.IsNullOrWhiteSpace(enrollmentToken)) |
| 360 | + { |
| 361 | + session.Log("Enrollment payload missing api_base_url or enrollment_token"); |
| 362 | + return ActionResult.Failure; |
| 363 | + } |
| 364 | + if (string.IsNullOrWhiteSpace(agentName)) agentName = Environment.MachineName; |
| 365 | + |
| 366 | + // Build CLI arguments for: devolutions-agent.exe enroll <url> <token> <name> [subnets] |
| 367 | + // Advertise domains are not a CLI flag — the agent reads them from the Tunnel |
| 368 | + // section of agent.json. We persist them after enrollment writes the file. |
| 369 | + string installDir = session.Property(AgentProperties.InstallDir); |
| 370 | + string exePath = Path.Combine(installDir, Includes.EXECUTABLE_NAME); |
| 371 | + |
| 372 | + string subnetsArg = subnetsRaw?.Trim() ?? string.Empty; |
| 373 | + string domainsArg = domainsRaw?.Trim() ?? string.Empty; |
| 374 | + |
| 375 | + string arguments = $"enroll \"{apiBaseUrl}\" \"{enrollmentToken}\" \"{agentName}\""; |
| 376 | + if (subnetsArg.Length != 0) |
| 377 | + { |
| 378 | + arguments += $" \"{subnetsArg}\""; |
| 379 | + } |
| 380 | + |
| 381 | + string Redact(string s) => s.Replace(enrollmentToken, "***"); |
| 382 | + session.Log($"Running enrollment: {exePath} {Redact(arguments)}"); |
| 383 | + |
| 384 | + ProcessStartInfo startInfo = new(exePath, arguments) |
| 385 | + { |
| 386 | + UseShellExecute = false, |
| 387 | + RedirectStandardOutput = true, |
| 388 | + RedirectStandardError = true, |
| 389 | + CreateNoWindow = true, |
| 390 | + WorkingDirectory = ProgramDataDirectory, |
| 391 | + }; |
| 392 | + |
| 393 | + using Process process = Process.Start(startInfo); |
| 394 | + if (!process.WaitForExit(60_000)) |
| 395 | + { |
| 396 | + try { process.Kill(); } catch { /* already gone */ } |
| 397 | + session.Log("Enrollment process timed out after 60 seconds"); |
| 398 | + return ActionResult.Failure; |
| 399 | + } |
| 400 | + string stdout = process.StandardOutput.ReadToEnd(); |
| 401 | + string stderr = process.StandardError.ReadToEnd(); |
| 402 | + |
| 403 | + if (!string.IsNullOrEmpty(stdout)) session.Log($"enrollment stdout: {Redact(stdout)}"); |
| 404 | + if (!string.IsNullOrEmpty(stderr)) session.Log($"enrollment stderr: {Redact(stderr)}"); |
| 405 | + |
| 406 | + if (process.ExitCode != 0) |
| 407 | + { |
| 408 | + session.Log($"Enrollment failed with exit code {process.ExitCode}"); |
| 409 | + return ActionResult.Failure; |
| 410 | + } |
| 411 | + |
| 412 | + if (domainsArg.Length != 0) |
| 413 | + { |
| 414 | + WriteAdvertiseDomainsToConfig(session, domainsArg); |
| 415 | + } |
| 416 | + |
| 417 | + session.Log("Agent tunnel enrollment completed successfully"); |
| 418 | + return ActionResult.Success; |
| 419 | + } |
| 420 | + catch (Exception e) |
| 421 | + { |
| 422 | + session.Log($"Agent tunnel enrollment failed: {e}"); |
| 423 | + return ActionResult.Failure; |
| 424 | + } |
| 425 | + } |
| 426 | + |
| 427 | + private static void WriteAdvertiseDomainsToConfig(Session session, string domainsCsv) |
| 428 | + { |
| 429 | + string configPath = Path.Combine(ProgramDataDirectory, "agent.json"); |
| 430 | + if (!File.Exists(configPath)) |
| 431 | + { |
| 432 | + session.Log($"agent.json not found at {configPath}; cannot persist advertise_domains"); |
| 433 | + return; |
| 434 | + } |
| 435 | + |
| 436 | + try |
| 437 | + { |
| 438 | + string[] domains = domainsCsv |
| 439 | + .Split(',') |
| 440 | + .Select(d => d.Trim()) |
| 441 | + .Where(d => !string.IsNullOrEmpty(d)) |
| 442 | + .ToArray(); |
| 443 | + |
| 444 | + if (domains.Length == 0) |
| 445 | + { |
| 446 | + return; |
| 447 | + } |
| 448 | + |
| 449 | + JObject root = JObject.Parse(File.ReadAllText(configPath)); |
| 450 | + |
| 451 | + // ConfFile uses serde rename_all = "PascalCase", so the tunnel section is keyed |
| 452 | + // "Tunnel" and the field is "AdvertiseDomains". |
| 453 | + if (root["Tunnel"] is not JObject tunnel) |
| 454 | + { |
| 455 | + session.Log("agent.json has no Tunnel section after enrollment; skipping advertise_domains write"); |
| 456 | + return; |
| 457 | + } |
| 458 | + |
| 459 | + tunnel["AdvertiseDomains"] = new JArray(domains); |
| 460 | + |
| 461 | + File.WriteAllText(configPath, root.ToString(Formatting.Indented)); |
| 462 | + session.Log($"Wrote {domains.Length} advertise_domains entries to agent.json"); |
| 463 | + } |
| 464 | + catch (Exception e) |
| 465 | + { |
| 466 | + // Don't fail the install over this — the tunnel works fine without domain |
| 467 | + // advertisements (subnets cover IP routing on their own). |
| 468 | + session.Log($"Failed to write advertise_domains to agent.json: {e}"); |
| 469 | + } |
| 470 | + } |
| 471 | + |
321 | 472 | [CustomAction] |
322 | 473 | public static ActionResult ConfigureFeatures(Session session) |
323 | 474 | { |
|
0 commit comments