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
/
Copy pathAddNewTunnelWindow.xaml.cs
99 lines (82 loc) · 2.98 KB
/
AddNewTunnelWindow.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
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
namespace ngrokGUI
{
/// <summary>
/// Interaction logic for AddNewTunnelWindow.xaml
/// </summary>
public partial class AddNewTunnelWindow : Window
{
private readonly List<TunnelDescription> _currentTunnelDescriptions;
private static readonly Regex _regex = new("[^0-9.-]+");
public AddNewTunnelWindow(List<TunnelDescription> currentTunnelDescriptions ,bool paidAccount = false)
{
_currentTunnelDescriptions = currentTunnelDescriptions;
InitializeComponent();
if (!paidAccount)
{
tbSubdomain.Visibility = Visibility.Hidden;
tbCustomDomain.Visibility = Visibility.Hidden;
llsubdomain.Visibility = Visibility.Hidden;
llCustomDomain.Visibility = Visibility.Hidden;
wdSubdomain.MaxHeight = 0;
wdCustomdomain.MaxHeight = 0;
Height = 170;
}
cobProtocol.Items.Add("https");
cobProtocol.Items.Add("http");
cobProtocol.Items.Add("tcp");
cobProtocol.SelectedIndex = cobProtocol.Items.IndexOf("https");
}
private static bool IsTextAllowed(string text)
{
return !_regex.IsMatch(text);
}
private void NumberValidationTextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private void BtnAddNewTunnel_OnClick(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tbName.Text))
{
MessageBox.Show("The tunnel must have a name");
return;
}
if (_currentTunnelDescriptions.Any(x => x.Name == tbName.Text) )
{
MessageBox.Show($"A tunnel with name: {tbName.Text} already exists");
return;
}
if (string.IsNullOrWhiteSpace(tbLocalPort.Text))
{
MessageBox.Show("There must be a local Port");
return;
}
if (int.TryParse(tbLocalPort.Text, out var i))
if (i < 1 || i > 65353)
{
MessageBox.Show("Local Port must be between 1 and 65353");
return;
}
DialogResult = true;
Close();
}
private void TbLocalPort_OnPasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
var text = (string) e.DataObject.GetData(typeof(string));
if (!IsTextAllowed(text)) e.CancelCommand();
}
else
{
e.CancelCommand();
}
}
}
}