This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MainWindow.xaml.cs
258 lines (207 loc) · 9.92 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Windows;
using NgrokSharp;
using NgrokSharp.DTO;
namespace ngrokGUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly INgrokManager _ngrokManager;
private readonly ObservableCollection<TunnelDescription> _tunnelDescriptions;
private readonly string _downloadFolder =
$"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar}NgrokSharp{Path.DirectorySeparatorChar}";
private bool PaidAccount;
public MainWindow()
{
InitializeComponent();
_tunnelDescriptions = new ObservableCollection<TunnelDescription>();
DataContext = _tunnelDescriptions;
_ngrokManager = new NgrokManager();
if (!File.Exists($"{_downloadFolder}Settings.json"))
{
Directory.CreateDirectory(_downloadFolder);
File.WriteAllText($"{_downloadFolder}Settings.json", "{\r\n \"firstTimeSetupDone\": false\r\n}");
}
Settings settings;
try
{
//Load settings
settings = JsonSerializer.Deserialize<Settings>(File.ReadAllText($"{_downloadFolder}Settings.json"));
if (settings.FirstTimeSetupDone == false)
{
var firstTimeWizard = new FirstTimeWizard(_ngrokManager);
firstTimeWizard.ShowDialog();
if (firstTimeWizard.DialogResult == true)
{
settings.FirstTimeSetupDone = true;
settings.DataCenterRegion = firstTimeWizard.cmbTunnelExit.SelectedIndex;
settings.PaidAccount = (bool) firstTimeWizard.cbxPaidAccount.IsChecked;
File.WriteAllText($"{_downloadFolder}Settings.json", JsonSerializer.Serialize(settings));
}
}
PaidAccount = settings.PaidAccount;
sbStatus.Content = $"connected to {(NgrokManager.Region)settings.DataCenterRegion}";
_ngrokManager.StartNgrok((NgrokManager.Region)settings.DataCenterRegion);
if (File.Exists($"{_downloadFolder}SavedTunnels.json"))
{
JsonSerializer.Deserialize<List<TunnelDescription>>(File.ReadAllText(
$"{_downloadFolder}SavedTunnels.json"))?.ForEach( x => _tunnelDescriptions.Add(x));
}
}
catch (Exception e)
{
MessageBox.Show($"Something went wrong while loading the settings: {e}");
Close();
}
}
private void btnMenuItemExit_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private async void btnMenuItemAddNew_OnClick(object sender, RoutedEventArgs e)
{
var addNewTunnelWindow = new AddNewTunnelWindow(_tunnelDescriptions.ToList(), PaidAccount);
addNewTunnelWindow.ShowDialog();
if (addNewTunnelWindow.DialogResult != null && addNewTunnelWindow.DialogResult.Value)
{
var startTunnelDto = new StartTunnelDTO
{
name = addNewTunnelWindow.tbName.Text,
proto = addNewTunnelWindow.cobProtocol.SelectionBoxItem.ToString(),
addr = addNewTunnelWindow.tbLocalPort.Text,
bind_tls = "false",
subdomain = addNewTunnelWindow.tbSubdomain.Text,
hostname = addNewTunnelWindow.tbCustomDomain.Text
};
// bind_tls http bind an HTTPS or HTTP endpoint or both true, false, or both
if (startTunnelDto.proto == "https")
{
startTunnelDto.proto = "http";
startTunnelDto.bind_tls = "true";
}
var httpResponseMessage = await _ngrokManager.StartTunnelAsync(startTunnelDto);
if ((int) httpResponseMessage.StatusCode == 201)
{
var tunnelDetail =
JsonSerializer.Deserialize<TunnelDetailDTO>(
await httpResponseMessage.Content.ReadAsStringAsync());
var tunnel = new TunnelDescription
{
Name = addNewTunnelWindow.tbName.Text,
Protocol = addNewTunnelWindow.cobProtocol.SelectionBoxItem.ToString(),
Port = Convert.ToInt32(addNewTunnelWindow.tbLocalPort.Text),
Url = tunnelDetail.PublicUrl,
Active = true,
SubDomain = addNewTunnelWindow.tbSubdomain.Text,
CustomDomain = addNewTunnelWindow.tbCustomDomain.Text
};
_tunnelDescriptions.Add(tunnel);
}
else
{
var tunnelError =
JsonSerializer.Deserialize<TunnelErrorDTO>(
await httpResponseMessage.Content.ReadAsStringAsync());
MessageBox.Show(tunnelError.Details.Err);
}
}
}
private void Window_Closed(object sender, EventArgs e)
{
_ngrokManager.StopNgrok();
foreach (var tunnelDescription in _tunnelDescriptions)
{
tunnelDescription.Active = false;
if (string.IsNullOrWhiteSpace(tunnelDescription.SubDomain))
{
tunnelDescription.Url = null;
}
}
File.WriteAllText($"{_downloadFolder}SavedTunnels.json", JsonSerializer.Serialize(_tunnelDescriptions));
}
private void btnMenuItemCopy_OnClick(object sender, RoutedEventArgs e)
{
if (lwTunnels.SelectedIndex == -1) return;
if (_tunnelDescriptions[lwTunnels.SelectedIndex].Url == null) return;
Clipboard.SetText(_tunnelDescriptions[lwTunnels.SelectedIndex].Url.ToString());
}
private async void btnMenuItemStopTunnel_OnClick(object sender, RoutedEventArgs e)
{
if (lwTunnels.SelectedIndex == -1) return;
var result = await _ngrokManager.StopTunnelAsync(_tunnelDescriptions[lwTunnels.SelectedIndex].Name);
if (result.StatusCode == HttpStatusCode.NoContent)
{
_tunnelDescriptions[lwTunnels.SelectedIndex].Active = false;
if (string.IsNullOrWhiteSpace(_tunnelDescriptions[lwTunnels.SelectedIndex].SubDomain))
{
_tunnelDescriptions[lwTunnels.SelectedIndex].Url = null;
}
}
}
private void BtnMenuItemRunFirstTimeWizard_OnClick(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show("Are you sure you want to close NgrokGUI in order to run the First Time Wizard, again?", "Are you sure?", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
Settings settings = new Settings {FirstTimeSetupDone = false};
File.WriteAllText($"{_downloadFolder}Settings.json", JsonSerializer.Serialize(settings));
Close();
}
}
private async void btnMenuItemStartTunnel_OnClick(object sender, RoutedEventArgs e)
{
if (lwTunnels.SelectedIndex == -1) return;
if (_tunnelDescriptions[lwTunnels.SelectedIndex].Active) return;
var startTunnelDto = new StartTunnelDTO
{
name = _tunnelDescriptions[lwTunnels.SelectedIndex].Name,
proto = _tunnelDescriptions[lwTunnels.SelectedIndex].Protocol,
addr = _tunnelDescriptions[lwTunnels.SelectedIndex].Port.ToString(),
bind_tls = "false",
subdomain = _tunnelDescriptions[lwTunnels.SelectedIndex].SubDomain,
hostname = _tunnelDescriptions[lwTunnels.SelectedIndex].CustomDomain
};
// bind_tls http bind an HTTPS or HTTP endpoint or both true, false, or both
if (startTunnelDto.proto == "https")
{
startTunnelDto.proto = "http";
startTunnelDto.bind_tls = "true";
}
var httpResponseMessage = await _ngrokManager.StartTunnelAsync(startTunnelDto);
if ((int)httpResponseMessage.StatusCode == 201)
{
var tunnelDetail =
JsonSerializer.Deserialize<TunnelDetailDTO>(
await httpResponseMessage.Content.ReadAsStringAsync());
_tunnelDescriptions[lwTunnels.SelectedIndex].Active = true;
_tunnelDescriptions[lwTunnels.SelectedIndex].Url = tunnelDetail.PublicUrl;
}
else
{
var tunnelError =
JsonSerializer.Deserialize<TunnelErrorDTO>(
await httpResponseMessage.Content.ReadAsStringAsync());
MessageBox.Show(tunnelError.Details.Err);
}
}
private async void btnMenuItemDeleteTunnel_OnClick(object sender, RoutedEventArgs e)
{
if (lwTunnels.SelectedIndex == -1) return;
if (_tunnelDescriptions[lwTunnels.SelectedIndex].Active)
{
var result = await _ngrokManager.StopTunnelAsync(_tunnelDescriptions[lwTunnels.SelectedIndex].Name);
}
var tunnel = _tunnelDescriptions.SingleOrDefault(x => x.Name == _tunnelDescriptions[lwTunnels.SelectedIndex].Name);
_tunnelDescriptions.Remove(tunnel);
}
}
}