-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
839 lines (744 loc) · 30.3 KB
/
MainForm.cs
File metadata and controls
839 lines (744 loc) · 30.3 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using SlipNetPortableLauncher.Models;
using SlipNetPortableLauncher.Services;
namespace SlipNetPortableLauncher;
internal sealed class MainForm : Form
{
private static readonly Regex AnsiEscapeRegex = new(@"\x1B\[[0-9;]*[A-Za-z]", RegexOptions.Compiled);
private readonly PortableStorage storage = new();
private readonly SlipNetConfigCodec configCodec = new();
private readonly TunnelRuntime tunnelRuntime;
private readonly BindingSource profileBindingSource = new();
private readonly List<SlipNetProfile> profiles;
private AppSettings settings;
private bool loadingProfile;
private bool closingAfterTunnelStop;
private bool stoppingTunnelForClose;
private readonly ListBox profileListBox = new() { Dock = DockStyle.Fill };
private readonly TextBox nameTextBox = new();
private readonly ComboBox tunnelTypeComboBox = new() { DropDownStyle = ComboBoxStyle.DropDownList };
private readonly TextBox domainTextBox = new();
private readonly TextBox resolversTextBox = new() { Multiline = true, Height = 48, ScrollBars = ScrollBars.Vertical };
private readonly CheckBox authModeCheckBox = new() { Text = "Authoritative mode" };
private readonly NumericUpDown keepAliveNumeric = new() { Minimum = 0, Maximum = 600000, Increment = 500 };
private readonly ComboBox congestionControlComboBox = new() { DropDownStyle = ComboBoxStyle.DropDownList };
private readonly TextBox hostTextBox = new();
private readonly NumericUpDown listenPortNumeric = new() { Minimum = 1, Maximum = 65535 };
private readonly ComboBox dnsTransportComboBox = new() { DropDownStyle = ComboBoxStyle.DropDownList };
private readonly TextBox publicKeyTextBox = new();
private readonly CheckBox useLocalHttpProxyCheckBox = new() { Text = "Expose local HTTP proxy" };
private readonly CheckBox autoConfigureWindowsProxyCheckBox = new() { Text = "Set Windows proxy while connected" };
private readonly NumericUpDown httpProxyPortNumeric = new() { Minimum = 1024, Maximum = 65535 };
private readonly TextBox slipNetCliPathTextBox = new();
private readonly TextBox slipstreamClientPathTextBox = new();
private readonly Button saveButton = new() { Text = "Save Profile", AutoSize = true };
private readonly Button startButton = new() { Text = "Start Tunnel", AutoSize = true };
private readonly Button stopButton = new() { Text = "Stop", AutoSize = true, Enabled = false };
private readonly Button copyConfigButton = new() { Text = "Copy Config", AutoSize = true };
private readonly RichTextBox logTextBox = new()
{
ReadOnly = true,
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(14, 19, 24),
ForeColor = Color.FromArgb(214, 230, 242),
BorderStyle = BorderStyle.None,
Font = new Font("Cascadia Code", 9F, FontStyle.Regular, GraphicsUnit.Point)
};
public MainForm()
{
tunnelRuntime = new TunnelRuntime(configCodec);
profiles = storage.LoadProfiles().ToList();
settings = storage.LoadSettings();
Text = "SlipNet Portable Launcher";
Width = 1280;
Height = 820;
MinimumSize = new Size(1060, 700);
StartPosition = FormStartPosition.CenterScreen;
Font = new Font("Segoe UI Variable Text", 9F, FontStyle.Regular, GraphicsUnit.Point);
BackColor = Color.FromArgb(245, 239, 228);
BuildLayout();
BindData();
LoadSettingsIntoControls();
AutoDetectBundledTools();
if (profiles.Count > 0)
{
profileListBox.SelectedIndex = 0;
}
}
protected override async void OnFormClosing(FormClosingEventArgs e)
{
if (closingAfterTunnelStop || !tunnelRuntime.IsRunning)
{
base.OnFormClosing(e);
return;
}
if (stoppingTunnelForClose)
{
e.Cancel = true;
return;
}
var result = MessageBox.Show(
this,
"A tunnel is still running. Stop the tunnel and close the program?",
"Tunnel Running",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning);
if (result != DialogResult.OK)
{
e.Cancel = true;
return;
}
e.Cancel = true;
stoppingTunnelForClose = true;
UseWaitCursor = true;
Enabled = false;
try
{
await tunnelRuntime.StopAsync();
Log("Tunnel stopped.");
startButton.Enabled = true;
stopButton.Enabled = false;
closingAfterTunnelStop = true;
BeginInvoke(new Action(Close));
}
catch (Exception ex)
{
Enabled = true;
UseWaitCursor = false;
startButton.Enabled = !tunnelRuntime.IsRunning;
stopButton.Enabled = tunnelRuntime.IsRunning;
MessageBox.Show(this, ex.Message, "Tunnel Stop Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log($"Stop failed: {ex.Message}");
}
finally
{
if (!closingAfterTunnelStop)
{
stoppingTunnelForClose = false;
}
}
}
private void BuildLayout()
{
var mainSplit = new SplitContainer
{
Dock = DockStyle.Fill,
SplitterDistance = 240,
BackColor = BackColor,
FixedPanel = FixedPanel.Panel1
};
mainSplit.Panel1.Padding = new Padding(12);
mainSplit.Panel2.Padding = new Padding(12);
Controls.Add(mainSplit);
var leftPanel = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 3,
ColumnCount = 1
};
leftPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
leftPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
leftPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
mainSplit.Panel1.Controls.Add(leftPanel);
leftPanel.Controls.Add(new Label
{
Text = "Profiles",
Dock = DockStyle.Top,
Font = new Font(Font, FontStyle.Bold),
Height = 28
}, 0, 0);
leftPanel.Controls.Add(profileListBox, 0, 1);
var leftButtons = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
AutoSize = true,
WrapContents = true
};
leftPanel.Controls.Add(leftButtons, 0, 2);
var importButton = new Button { Text = "Import", AutoSize = true };
importButton.Click += (_, _) => ImportProfiles();
var newButton = new Button { Text = "New", AutoSize = true };
newButton.Click += (_, _) => CreateNewProfile();
var deleteButton = new Button { Text = "Delete", AutoSize = true };
deleteButton.Click += (_, _) => DeleteSelectedProfile();
leftButtons.Controls.Add(importButton);
leftButtons.Controls.Add(newButton);
leftButtons.Controls.Add(deleteButton);
var rightPanel = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 3
};
rightPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 60F));
rightPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
rightPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 40F));
mainSplit.Panel2.Controls.Add(rightPanel);
rightPanel.Controls.Add(BuildProfileEditor(), 0, 0);
rightPanel.Controls.Add(BuildActionsPanel(), 0, 1);
rightPanel.Controls.Add(BuildLogPanel(), 0, 2);
profileListBox.SelectedIndexChanged += (_, _) => LoadSelectedProfileIntoEditor();
}
private Control BuildProfileEditor()
{
var split = new SplitContainer
{
Dock = DockStyle.Fill,
FixedPanel = FixedPanel.Panel2,
SplitterWidth = 6
};
split.SizeChanged += (_, _) => AdjustProfileEditorSplit(split);
var profileGroup = new GroupBox
{
Text = "Profile",
Dock = DockStyle.Fill,
Padding = new Padding(12)
};
split.Panel1.Controls.Add(profileGroup);
var profileTable = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 2,
AutoScroll = true,
Padding = new Padding(0, 0, 8, 0)
};
profileTable.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 124F));
profileTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
profileGroup.Controls.Add(profileTable);
tunnelTypeComboBox.DataSource = Enum.GetValues<TunnelType>();
tunnelTypeComboBox.Format += (_, e) => e.Value = ((TunnelType)e.Value!).ToDisplayName();
congestionControlComboBox.Items.AddRange(["bbr", "dcubic"]);
dnsTransportComboBox.DataSource = Enum.GetValues<DnsTransport>();
dnsTransportComboBox.Format += (_, e) => e.Value = e.Value!.ToString()?.ToUpperInvariant();
AddRow(profileTable, "Name", nameTextBox);
AddRow(profileTable, "Tunnel Type", tunnelTypeComboBox);
AddRow(profileTable, "Domain", domainTextBox);
AddRow(profileTable, "Resolvers", resolversTextBox);
AddRow(profileTable, "Listen Host", hostTextBox);
AddRow(profileTable, "Listen Port", listenPortNumeric);
AddRow(profileTable, "Keep Alive", keepAliveNumeric);
AddRow(profileTable, "Congestion", congestionControlComboBox);
AddRow(profileTable, "DNS Transport", dnsTransportComboBox);
AddRow(profileTable, "DNSTT Key", publicKeyTextBox);
AddRow(profileTable, string.Empty, authModeCheckBox);
var runtimeGroup = new GroupBox
{
Text = "Runtime",
Dock = DockStyle.Fill,
Padding = new Padding(12)
};
split.Panel2.Controls.Add(runtimeGroup);
var runtimeTable = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 3,
Padding = new Padding(0, 0, 4, 0)
};
runtimeTable.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 96F));
runtimeTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
runtimeTable.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
runtimeGroup.Controls.Add(runtimeTable);
slipNetCliPathTextBox.Dock = DockStyle.Fill;
slipstreamClientPathTextBox.Dock = DockStyle.Fill;
AddRuntimeRow(runtimeTable, "SlipNet CLI", slipNetCliPathTextBox, CreateBrowseButton(slipNetCliPathTextBox, "Select slipnet-windows-amd64.exe"));
var downloadButton = new Button { Text = "Download", AutoSize = true };
downloadButton.Click += async (_, _) => await DownloadSlipNetCliAsync();
AddRuntimeRow(runtimeTable, "CLI Download", new Label
{
Text = "Fetch latest upstream Windows CLI into tools\\",
AutoSize = true,
Dock = DockStyle.Fill,
Padding = new Padding(0, 6, 0, 0)
}, downloadButton);
AddRuntimeRow(runtimeTable, "Slipstream", slipstreamClientPathTextBox, CreateBrowseButton(slipstreamClientPathTextBox, "Select slipstream-client.exe"));
AddRuntimeRow(runtimeTable, "HTTP Proxy", httpProxyPortNumeric, new Panel { Width = 1, Height = 1 });
AddRuntimeRow(runtimeTable, string.Empty, useLocalHttpProxyCheckBox, new Panel { Width = 1, Height = 1 });
AddRuntimeRow(runtimeTable, string.Empty, autoConfigureWindowsProxyCheckBox, new Panel { Width = 1, Height = 1 });
AdjustProfileEditorSplit(split);
return split;
}
private Control BuildActionsPanel()
{
var panel = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
AutoSize = true,
Padding = new Padding(0, 8, 0, 8)
};
saveButton.Click += (_, _) => SaveCurrentProfile();
startButton.Click += async (_, _) => await StartSelectedProfileAsync();
stopButton.Click += async (_, _) => await StopTunnelAsync();
copyConfigButton.Click += (_, _) => CopyCurrentConfig();
panel.Controls.Add(saveButton);
panel.Controls.Add(copyConfigButton);
panel.Controls.Add(startButton);
panel.Controls.Add(stopButton);
return panel;
}
private Control BuildLogPanel()
{
var group = new GroupBox
{
Text = "Status",
Dock = DockStyle.Fill,
Padding = new Padding(0)
};
group.Controls.Add(logTextBox);
return group;
}
private void BindData()
{
profileBindingSource.DataSource = profiles;
profileListBox.DataSource = profileBindingSource;
}
private void LoadSettingsIntoControls()
{
slipNetCliPathTextBox.Text = settings.SlipNetCliPath;
slipstreamClientPathTextBox.Text = settings.SlipstreamClientPath;
useLocalHttpProxyCheckBox.Checked = settings.UseLocalHttpProxy;
autoConfigureWindowsProxyCheckBox.Checked = settings.AutoConfigureWindowsProxy;
httpProxyPortNumeric.Value = settings.LocalHttpProxyPort;
}
private void AutoDetectBundledTools()
{
var changed = false;
if ((string.IsNullOrWhiteSpace(settings.SlipNetCliPath) || !File.Exists(settings.SlipNetCliPath)) &&
TryResolveBundledTool("slipnet-windows-amd64.exe", out var slipNetCliPath))
{
settings.SlipNetCliPath = slipNetCliPath;
slipNetCliPathTextBox.Text = slipNetCliPath;
Log($"Using bundled SlipNet CLI: {slipNetCliPath}");
changed = true;
}
if ((string.IsNullOrWhiteSpace(settings.SlipstreamClientPath) || !File.Exists(settings.SlipstreamClientPath)) &&
TryResolveBundledTool("slipstream-client.exe", out var slipstreamClientPath))
{
settings.SlipstreamClientPath = slipstreamClientPath;
slipstreamClientPathTextBox.Text = slipstreamClientPath;
Log($"Using bundled Slipstream client: {slipstreamClientPath}");
changed = true;
}
if (changed)
{
PersistAll();
}
}
private void LoadSelectedProfileIntoEditor()
{
if (profileListBox.SelectedItem is not SlipNetProfile profile)
{
return;
}
loadingProfile = true;
nameTextBox.Text = profile.Name;
tunnelTypeComboBox.SelectedItem = profile.TunnelType;
domainTextBox.Text = profile.Domain;
resolversTextBox.Text = string.Join(Environment.NewLine, profile.Resolvers.Select(static resolver => $"{resolver.Host}:{resolver.Port}:{(resolver.Authoritative ? "1" : "0")}"));
hostTextBox.Text = profile.TcpListenHost;
listenPortNumeric.Value = profile.TcpListenPort;
keepAliveNumeric.Value = profile.KeepAliveInterval;
congestionControlComboBox.SelectedItem = profile.CongestionControl;
dnsTransportComboBox.SelectedItem = profile.DnsTransport;
publicKeyTextBox.Text = profile.DnsttPublicKey;
authModeCheckBox.Checked = profile.AuthoritativeMode;
loadingProfile = false;
}
private void SaveCurrentProfile()
{
if (profileListBox.SelectedItem is not SlipNetProfile profile)
{
return;
}
try
{
ReadEditorIntoProfile(profile);
SaveSettingsFromControls();
PersistAll();
profileBindingSource.ResetBindings(false);
Log($"Saved profile '{profile.Name}'.");
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private async Task StartSelectedProfileAsync()
{
if (profileListBox.SelectedItem is not SlipNetProfile profile)
{
return;
}
try
{
AutoDetectBundledTools();
SaveCurrentProfile();
startButton.Enabled = false;
stopButton.Enabled = true;
await tunnelRuntime.StartAsync(profile, settings, Log);
}
catch (Exception ex)
{
startButton.Enabled = true;
stopButton.Enabled = false;
MessageBox.Show(this, ex.Message, "Tunnel Start Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log($"Start failed: {ex.Message}");
}
}
private async Task StopTunnelAsync()
{
try
{
await tunnelRuntime.StopAsync();
Log("Tunnel stopped.");
}
catch (Exception ex)
{
Log($"Stop failed: {ex.Message}");
}
finally
{
startButton.Enabled = true;
stopButton.Enabled = false;
}
}
private void CopyCurrentConfig()
{
if (profileListBox.SelectedItem is not SlipNetProfile profile)
{
return;
}
try
{
ReadEditorIntoProfile(profile);
var uri = configCodec.ExportUri(profile, profile.ResolversHidden);
Clipboard.SetText(uri);
Log($"Copied config for '{profile.Name}' to the clipboard.");
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Copy Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ImportProfiles()
{
using var dialog = new ImportProfilesForm();
if (dialog.ShowDialog(this) != DialogResult.OK)
{
return;
}
var result = configCodec.ParseMany(dialog.InputText);
foreach (var profile in result.Profiles)
{
profiles.Add(profile);
}
PersistAll();
profileBindingSource.ResetBindings(false);
if (result.Profiles.Count > 0)
{
profileListBox.SelectedItem = result.Profiles[0];
}
foreach (var warning in result.Warnings)
{
Log(warning);
}
Log($"Imported {result.Profiles.Count} profile(s).");
}
private void CreateNewProfile()
{
var profile = new SlipNetProfile
{
Name = $"New Profile {profiles.Count + 1}",
TunnelType = TunnelType.Dnstt,
Resolvers = [new DnsResolver { Host = "1.1.1.1", Port = 53 }]
};
profiles.Add(profile);
PersistAll();
profileBindingSource.ResetBindings(false);
profileListBox.SelectedItem = profile;
}
private void DeleteSelectedProfile()
{
if (profileListBox.SelectedItem is not SlipNetProfile profile)
{
return;
}
if (MessageBox.Show(this, $"Delete '{profile.Name}'?", "Delete Profile", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
{
return;
}
profiles.Remove(profile);
PersistAll();
profileBindingSource.ResetBindings(false);
}
private void ReadEditorIntoProfile(SlipNetProfile profile)
{
if (loadingProfile)
{
return;
}
profile.Name = nameTextBox.Text.Trim();
profile.TunnelType = (TunnelType)(tunnelTypeComboBox.SelectedItem ?? TunnelType.Dnstt);
profile.Domain = domainTextBox.Text.Trim();
profile.Resolvers = ParseResolversFromEditor();
profile.TcpListenHost = string.IsNullOrWhiteSpace(hostTextBox.Text) ? "127.0.0.1" : hostTextBox.Text.Trim();
profile.TcpListenPort = (int)listenPortNumeric.Value;
profile.KeepAliveInterval = (int)keepAliveNumeric.Value;
profile.CongestionControl = (congestionControlComboBox.SelectedItem?.ToString() ?? "bbr").Trim();
profile.DnsTransport = (DnsTransport)(dnsTransportComboBox.SelectedItem ?? DnsTransport.Udp);
profile.DnsttPublicKey = publicKeyTextBox.Text.Trim();
profile.AuthoritativeMode = authModeCheckBox.Checked;
profile.LastImportedUri = configCodec.ExportUri(profile, profile.ResolversHidden);
}
private List<DnsResolver> ParseResolversFromEditor()
{
var resolvers = new List<DnsResolver>();
foreach (var line in resolversTextBox.Text.Replace("\r", string.Empty).Split('\n', StringSplitOptions.RemoveEmptyEntries))
{
var parts = line.Split(':');
if (parts.Length < 2 || !int.TryParse(parts[1], out var port))
{
throw new InvalidOperationException($"Invalid resolver line: '{line}'. Expected host:port or host:port:auth.");
}
resolvers.Add(new DnsResolver
{
Host = parts[0].Trim(),
Port = port,
Authoritative = parts.Length > 2 && (parts[2].Trim() == "1" || parts[2].Contains("auth", StringComparison.OrdinalIgnoreCase))
});
}
return resolvers;
}
private void SaveSettingsFromControls()
{
settings.SlipNetCliPath = slipNetCliPathTextBox.Text.Trim();
settings.SlipstreamClientPath = slipstreamClientPathTextBox.Text.Trim();
settings.UseLocalHttpProxy = useLocalHttpProxyCheckBox.Checked;
settings.AutoConfigureWindowsProxy = autoConfigureWindowsProxyCheckBox.Checked;
settings.LocalHttpProxyPort = (int)httpProxyPortNumeric.Value;
}
private void PersistAll()
{
storage.SaveProfiles(profiles);
storage.SaveSettings(settings);
}
private void Log(string message)
{
if (InvokeRequired)
{
BeginInvoke(() => Log(message));
return;
}
var normalizedMessage = NormalizeLogMessage(message);
if (string.IsNullOrWhiteSpace(normalizedMessage))
{
return;
}
AppendLogSegment($"[{DateTime.Now:HH:mm:ss}] ", Color.FromArgb(126, 138, 153));
AppendLogSegment(normalizedMessage, GetLogColor(normalizedMessage));
AppendLogSegment(Environment.NewLine, logTextBox.ForeColor);
logTextBox.SelectionColor = logTextBox.ForeColor;
logTextBox.SelectionStart = logTextBox.TextLength;
logTextBox.ScrollToCaret();
}
private void AppendLogSegment(string text, Color color)
{
logTextBox.SelectionStart = logTextBox.TextLength;
logTextBox.SelectionLength = 0;
logTextBox.SelectionColor = color;
logTextBox.AppendText(text);
}
private static string NormalizeLogMessage(string message) =>
AnsiEscapeRegex.Replace(message, string.Empty).Trim();
private Color GetLogColor(string message)
{
var lower = message.ToLowerInvariant();
if (lower.Contains("test failed", StringComparison.Ordinal) ||
lower.Contains("failed", StringComparison.Ordinal) ||
lower.Contains("error", StringComparison.Ordinal) ||
lower.Contains("timeout", StringComparison.Ordinal))
{
return Color.FromArgb(255, 107, 107);
}
if (lower.Contains("warn", StringComparison.Ordinal) ||
lower.Contains("warning", StringComparison.Ordinal))
{
return Color.FromArgb(255, 204, 102);
}
if (lower.Contains("proxy ip", StringComparison.Ordinal) ||
lower.Contains("ready", StringComparison.Ordinal) ||
lower.Contains("listening", StringComparison.Ordinal) ||
lower.Contains("configured", StringComparison.Ordinal) ||
lower.Contains("downloaded", StringComparison.Ordinal) ||
lower.Contains("completed", StringComparison.Ordinal) ||
lower.Contains("saved profile", StringComparison.Ordinal) ||
lower.Contains("imported", StringComparison.Ordinal) ||
lower.Contains("copied config", StringComparison.Ordinal))
{
return Color.FromArgb(103, 214, 145);
}
if (lower.Contains("testing", StringComparison.Ordinal) ||
lower.Contains("launching", StringComparison.Ordinal) ||
lower.Contains("http get", StringComparison.Ordinal) ||
lower.Contains("using bundled", StringComparison.Ordinal) ||
lower.Contains("downloading", StringComparison.Ordinal))
{
return Color.FromArgb(122, 184, 255);
}
if (lower.Contains("stopped", StringComparison.Ordinal) ||
lower.Contains("closed", StringComparison.Ordinal))
{
return Color.FromArgb(186, 192, 204);
}
return logTextBox.ForeColor;
}
private Button CreateBrowseButton(TextBox targetTextBox, string title)
{
var button = new Button { Text = "Browse", AutoSize = true };
button.Click += (_, _) =>
{
using var dialog = new OpenFileDialog
{
Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*",
Title = title,
CheckFileExists = true
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
targetTextBox.Text = dialog.FileName;
SaveSettingsFromControls();
PersistAll();
}
};
return button;
}
private async Task DownloadSlipNetCliAsync()
{
try
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("SlipNetPortableLauncher", "1.0"));
using var releaseResponse = await httpClient.GetAsync("https://api.github.com/repos/anonvector/SlipNet/releases/latest");
releaseResponse.EnsureSuccessStatusCode();
var releaseJson = await releaseResponse.Content.ReadAsStringAsync();
using var document = JsonDocument.Parse(releaseJson);
var assets = document.RootElement.GetProperty("assets");
var asset = assets.EnumerateArray()
.FirstOrDefault(static item => item.GetProperty("name").GetString() == "slipnet-windows-amd64.exe");
if (asset.ValueKind == JsonValueKind.Undefined)
{
throw new InvalidOperationException("The latest upstream release does not expose slipnet-windows-amd64.exe.");
}
var downloadUrl = asset.GetProperty("browser_download_url").GetString()
?? throw new InvalidOperationException("Missing download URL.");
var toolsDirectory = Path.Combine(AppContext.BaseDirectory, "tools");
Directory.CreateDirectory(toolsDirectory);
var outputPath = Path.Combine(toolsDirectory, "slipnet-windows-amd64.exe");
Log($"Downloading SlipNet CLI from {downloadUrl}...");
using var binaryResponse = await httpClient.GetAsync(downloadUrl);
binaryResponse.EnsureSuccessStatusCode();
await using (var file = File.Create(outputPath))
{
await binaryResponse.Content.CopyToAsync(file);
}
slipNetCliPathTextBox.Text = outputPath;
SaveSettingsFromControls();
PersistAll();
Log($"SlipNet CLI downloaded to {outputPath}.");
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log($"CLI download failed: {ex.Message}");
}
}
private static void AddRow(TableLayoutPanel table, string label, Control control)
{
var row = table.RowCount++;
table.RowStyles.Add(new RowStyle(SizeType.AutoSize));
table.Controls.Add(new Label
{
Text = label,
AutoSize = true,
Padding = new Padding(0, 6, 8, 0)
}, 0, row);
control.Dock = DockStyle.Top;
table.Controls.Add(control, 1, row);
}
private static void AddRuntimeRow(TableLayoutPanel table, string label, Control mainControl, Control actionControl)
{
var row = table.RowCount++;
table.RowStyles.Add(new RowStyle(SizeType.AutoSize));
table.Controls.Add(new Label
{
Text = label,
AutoSize = true,
Padding = new Padding(0, 6, 8, 0)
}, 0, row);
mainControl.Dock = DockStyle.Top;
actionControl.Dock = DockStyle.Top;
table.Controls.Add(mainControl, 1, row);
table.Controls.Add(actionControl, 2, row);
}
private static void AdjustProfileEditorSplit(SplitContainer split)
{
const int desiredRuntimeWidth = 310;
const int desiredProfileMinWidth = 420;
const int desiredRuntimeMinWidth = 280;
var availableWidth = split.ClientSize.Width - split.SplitterWidth;
if (availableWidth <= 0)
{
return;
}
var runtimeMinWidth = Math.Min(desiredRuntimeMinWidth, Math.Max(160, availableWidth / 3));
var profileMinWidth = Math.Min(desiredProfileMinWidth, Math.Max(240, availableWidth - runtimeMinWidth));
if (profileMinWidth + runtimeMinWidth > availableWidth)
{
profileMinWidth = Math.Max(0, availableWidth - runtimeMinWidth);
}
var minDistance = profileMinWidth;
var maxDistance = availableWidth - runtimeMinWidth;
if (maxDistance < minDistance)
{
return;
}
split.Panel1MinSize = 0;
split.Panel2MinSize = 0;
var runtimeWidth = Math.Clamp(desiredRuntimeWidth, runtimeMinWidth, availableWidth - profileMinWidth);
var splitterDistance = Math.Clamp(availableWidth - runtimeWidth, minDistance, maxDistance);
if (split.SplitterDistance != splitterDistance)
{
split.SplitterDistance = splitterDistance;
}
split.Panel1MinSize = profileMinWidth;
split.Panel2MinSize = runtimeMinWidth;
}
private static bool TryResolveBundledTool(string fileName, out string path)
{
var candidates = new[]
{
Path.Combine(AppContext.BaseDirectory, "tools", fileName),
Path.Combine(AppContext.BaseDirectory, fileName),
Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "tools", fileName)
};
foreach (var candidate in candidates.Select(Path.GetFullPath))
{
if (File.Exists(candidate))
{
path = candidate;
return true;
}
}
path = string.Empty;
return false;
}
}