-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
140 lines (124 loc) · 4.38 KB
/
MainWindow.xaml.cs
File metadata and controls
140 lines (124 loc) · 4.38 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
using System;
using System.Windows;
using MessageBox = System.Windows.MessageBox;
using System.Text; // для StringBuilder
using System.Windows.Media; // для SolidColorBrush
using System.IO; // для File
using System.Web; // для HttpUtility
using System.Net.Sockets; // для NetworkStream
using System.Windows.Controls; // для TextBox, Button и т.д.
using Microsoft.AspNetCore.WebUtilities; // для QueryHelpers
namespace VlessClientApp
{
public partial class MainWindow : Window
{
private VlessClientHandler? _client;
public MainWindow()
{
InitializeComponent();
}
private async void BtnConnect_Click(object sender, RoutedEventArgs e)
{
if (_client?.IsConnected == true)
{
_client.Disconnect();
UpdateStatus("Отключено", "Red");
BtnConnect.Content = "Подключить";
}
else
{
try
{
var config = VlessUrlParser.Parse(TxtLink.Text.Trim());
_client = new VlessClientHandler(config);
_client.OnLog += Log;
UpdateStatus("Подключение...", "Orange");
BtnConnect.Content = "Отключить";
var success = await _client.ConnectAsync();
if (!success)
{
UpdateStatus("Ошибка подключения", "Red");
BtnConnect.Content = "Подключить";
}
}
catch (Exception ex)
{
MessageBox.Show("Ошибка: " + ex.Message);
UpdateStatus("Ошибка", "Red");
}
}
}
private void Log(string msg)
{
this.Dispatcher.Invoke(() =>
{
TxtLog.Text += msg + "\n";
TxtLog.ScrollToEnd();
});
}
private void UpdateStatus(string text, string color)
{
this.Dispatcher.Invoke(() =>
{
TxtStatus.Text = text;
TxtStatus.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color)!);
});
}
}
}
public partial class MainWindow : Window
{
private VlessClientHandler? _client;
private readonly ConfigManager _config;
public MainWindow(ConfigManager config)
{
InitializeComponent();
_config = config;
if (!string.IsNullOrEmpty(_config.LastConfig?.Address))
TxtLink.Text = ReconstructVlessLink(_config.LastConfig);
}
public async Task ConnectAsync(VlessConfig? config = null)
{
config ??= VlessUrlParser.Parse(TxtLink.Text.Trim());
_config.LastConfig = config;
_config.Save();
_client = new VlessClientHandler(config);
_client.OnLog += Log;
UpdateStatus("Подключение...", "Orange");
var success = await _client.ConnectAsync();
if (success)
{
UpdateStatus("✅ Подключено", "Green");
}
else
{
UpdateStatus("❌ Ошибка", "Red");
}
}
public void Disconnect()
{
_client?.Disconnect();
UpdateStatus("Отключено", "Gray");
}
private string ReconstructVlessLink(VlessConfig c)
{
var query = HttpUtility.ParseQueryString("");
query["security"] = c.Security;
query["type"] = c.Type;
query["sni"] = c.Sni;
query["host"] = c.Host;
query["path"] = c.Path;
if (c.AllowInsecure) query["allowInsecure"] = "1";
if (c.PublicKey != null) query["pbk"] = c.PublicKey;
if (c.ShortId != null) query["sid"] = c.ShortId;
if (c.Fingerprint != null) query["fp"] = c.Fingerprint;
if (c.SpiderX != null) query["spx"] = c.SpiderX;
if (!string.IsNullOrEmpty(c.Flow)) query["flow"] = c.Flow;
var sb = new StringBuilder();
sb.Append($"vless://{c.UserId}@{c.Address}:{c.Port}");
if (query.Count > 0)
sb.Append("?" + query.ToString());
sb.Append($"#Client");
return sb.ToString();
}
}