-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathNetworkService.cs
292 lines (250 loc) · 8.51 KB
/
NetworkService.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
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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Threading.Tasks;
using Telegram.Common;
using Telegram.Native;
using Telegram.Td.Api;
using Windows.Foundation;
using Windows.Networking.Connectivity;
namespace Telegram.Services
{
public interface INetworkService
{
void Reconnect();
Task<int> GetSystemProxyId();
Task<Proxy> UpdateSystemProxy();
NetworkType Type { get; }
bool IsMetered { get; }
bool UseSystemProxy { get; set; }
event EventHandler<Proxy> ProxyChanged;
}
public partial class NetworkService : INetworkService
{
private readonly IClientService _clientService;
private readonly ISettingsService _settingsService;
private readonly IEventAggregator _aggregator;
private readonly HttpProxyWatcher _watcher;
private readonly EventDebouncer<bool> _debouncer;
public NetworkService(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
{
_clientService = clientService;
_settingsService = settingsService;
_aggregator = aggregator;
_watcher = HttpProxyWatcher.Current;
_debouncer = new EventDebouncer<bool>(Constants.HoldingThrottle,
handler => _watcher.Changed += new TypedEventHandler<HttpProxyWatcher, bool>(handler),
handler => _watcher.Changed -= new TypedEventHandler<HttpProxyWatcher, bool>(handler), true);
NetworkInformation.NetworkStatusChanged += OnNetworkStatusChanged;
try
{
Update(NetworkInformation.GetInternetConnectionProfile());
}
catch { }
Initialize();
}
public bool UseSystemProxy
{
get => _settingsService.UseSystemProxy;
set
{
if (_settingsService.UseSystemProxy != value)
{
_settingsService.UseSystemProxy = value;
if (value)
{
ProxyChanged += OnProxyChanged;
}
else
{
ProxyChanged -= OnProxyChanged;
}
}
}
}
private void OnProxyChanged(object sender, Proxy e)
{
// This is used only to keep the event subscribed.
// No action should be performed here.
}
private event EventHandler<Proxy> _proxyChanged;
public event EventHandler<Proxy> ProxyChanged
{
add
{
if (_proxyChanged == null)
{
_debouncer.Invoked += OnProxyChanged;
}
_proxyChanged += value;
}
remove
{
_proxyChanged -= value;
if (_proxyChanged == null)
{
_debouncer.Invoked -= OnProxyChanged;
}
}
}
private async void OnProxyChanged(object sender, bool args)
{
_proxyChanged?.Invoke(this, await UpdateSystemProxy());
}
private async void Initialize()
{
if (UseSystemProxy)
{
await UpdateSystemProxy();
ProxyChanged += OnProxyChanged;
}
}
public async Task<int> GetSystemProxyId()
{
var response = await _clientService.SendAsync(new GetOption("x_system_proxy"));
if (response is OptionValueInteger integer)
{
return (int)integer.Value;
}
string host;
int port;
if (TryCreateUri(_watcher.Server, out Uri result))
{
host = result.Host;
port = result.Port;
}
else
{
host = "localhost";
port = 80;
}
var proxy = await _clientService.SendAsync(new AddProxy(host, port, false, new ProxyTypeHttp())) as Proxy;
if (proxy != null)
{
_clientService.Send(new SetOption("x_system_proxy", new OptionValueInteger(proxy.Id)));
return proxy.Id;
}
return 0;
}
public async Task<Proxy> UpdateSystemProxy()
{
if (_settingsService.UseSystemProxy && !_watcher.IsEnabled)
{
_clientService.Send(new DisableProxy());
}
string host;
int port;
if (TryCreateUri(_watcher.Server, out Uri result))
{
host = result.Host;
port = result.Port;
}
else
{
host = "localhost";
port = 80;
}
var enabled = _settingsService.UseSystemProxy && _watcher.IsEnabled;
var response = await _clientService.SendAsync(new GetOption("x_system_proxy"));
if (response is OptionValueInteger integer)
{
return await _clientService.SendAsync(new EditProxy((int)integer.Value, host, port, enabled, new ProxyTypeHttp())) as Proxy;
}
var proxy = await _clientService.SendAsync(new AddProxy(host, port, enabled, new ProxyTypeHttp())) as Proxy;
if (proxy != null)
{
_clientService.Send(new SetOption("x_system_proxy", new OptionValueInteger(proxy.Id)));
return proxy;
}
return null;
}
private bool TryCreateUri(string server, out Uri result)
{
var query = server.Split(';');
foreach (var part in query)
{
var split = part.Split('=');
if (split.Length == 2 && string.Equals(split[0], "http"))
{
return MessageHelper.TryCreateUri(split[1], out result);
}
else if (split.Length == 1)
{
return MessageHelper.TryCreateUri(split[0], out result);
}
}
result = null;
return false;
}
public void Reconnect()
{
_clientService.Send(new SetNetworkType(_type));
}
private void OnNetworkStatusChanged(object sender)
{
try
{
Update(NetworkInformation.GetInternetConnectionProfile());
}
catch { }
}
private void Update(ConnectionProfile profile)
{
_clientService.Send(new SetNetworkType(_type = GetNetworkType(profile)));
}
private NetworkType GetNetworkType(ConnectionProfile profile)
{
if (profile == null)
{
//return new NetworkTypeNone();
return new NetworkTypeWiFi();
}
var cost = profile.GetConnectionCost();
if (cost != null)
{
IsMetered = cost.NetworkCostType is not NetworkCostType.Unrestricted and not NetworkCostType.Unknown;
}
else
{
IsMetered = false;
}
var level = profile.GetNetworkConnectivityLevel();
if (level is NetworkConnectivityLevel.LocalAccess or NetworkConnectivityLevel.None)
{
//return new NetworkTypeNone();
return new NetworkTypeWiFi();
}
if (cost != null && cost.Roaming)
{
return new NetworkTypeMobileRoaming();
}
else if (profile.IsWlanConnectionProfile)
{
return new NetworkTypeWiFi();
}
else if (profile.IsWwanConnectionProfile)
{
return new NetworkTypeMobile();
}
// This is most likely cable connection.
//return new NetworkTypeOther();
return new NetworkTypeWiFi();
}
private NetworkType _type = new NetworkTypeOther();
public NetworkType Type
{
get => _type;
private set => _type = value;
}
private bool _isMetered;
public bool IsMetered
{
get => _isMetered;
set => _isMetered = value;
}
}
}