@@ -4,11 +4,13 @@ import 'dart:io';
4
4
5
5
import 'package:connectivity_plus/connectivity_plus.dart' ;
6
6
import 'package:flutter/material.dart' ;
7
+ import 'package:flutter_speedtest/flutter_speedtest.dart' ;
7
8
import 'package:http/http.dart' as http;
8
9
import 'package:http/io_client.dart' ;
9
10
import 'package:ir_net/data/leak_item.dart' ;
10
11
import 'package:ir_net/data/shared_preferences.dart' ;
11
12
import 'package:ir_net/utils/cmd.dart' ;
13
+ import 'package:ir_net/utils/http.dart' ;
12
14
import 'package:ir_net/utils/system_tray.dart' ;
13
15
import 'package:latlng/latlng.dart' ;
14
16
import 'package:live_event/live_event.dart' ;
@@ -21,6 +23,10 @@ class AppBloc with AppSystemTray {
21
23
final _clearLeakInput = LiveEvent ();
22
24
final _leakChecklist = BehaviorSubject <List <LeakItem >>();
23
25
final _localNetwork = BehaviorSubject <LocalNetworksResult >();
26
+ final _ping = BehaviorSubject <double ?>();
27
+ final _downloadSpeed = BehaviorSubject <double ?>();
28
+ final _uploadSpeed = BehaviorSubject <double ?>();
29
+ final _speedTestStatus = BehaviorSubject <String >();
24
30
25
31
bool _isPingingGoogle = false ;
26
32
bool _foundALeakedSite = false ;
@@ -32,6 +38,17 @@ class AppBloc with AppSystemTray {
32
38
Stream get clearLeakInput => _clearLeakInput.stream;
33
39
Stream <List <LeakItem >> get leakChecklist => _leakChecklist.stream;
34
40
Stream <LocalNetworksResult > get localNetwork => _localNetwork.stream;
41
+ Stream <double ?> get ping => _ping.stream;
42
+ Stream <double ?> get downloadSpeed => _downloadSpeed.stream;
43
+ Stream <double ?> get uploadSpeed => _uploadSpeed.stream;
44
+ Stream <String > get speedTestStatus => _speedTestStatus.stream;
45
+
46
+ final speedtest = FlutterSpeedtest (
47
+ baseUrl: 'http://speedtest.jaosing.com:8080' ,
48
+ pathDownload: '/download' ,
49
+ pathUpload: '/upload' ,
50
+ pathResponseTime: '/ping' ,
51
+ );
35
52
36
53
void onLeakInputChanged (String value) {
37
54
_leakInput = value;
@@ -110,7 +127,7 @@ class AppBloc with AppSystemTray {
110
127
item.status = LeakStatus .failed;
111
128
}
112
129
debugPrint ('leak detection for $url => ${response .bodyBytes .length ~/ 1024 } Kilobytes' );
113
- } on Exception catch (ex) {
130
+ } on Exception catch (ex) {
114
131
item.status = LeakStatus .failed;
115
132
_checkNetworkRefuseException (ex);
116
133
}
@@ -131,12 +148,17 @@ class AppBloc with AppSystemTray {
131
148
await _checkProxySettings ();
132
149
_checkIpLocation ();
133
150
_verifyLeakedSites ();
151
+ _checkPing ();
134
152
} else {
135
153
setSystemTrayStatusToOffline ();
136
154
}
137
155
});
138
156
}
139
157
158
+ void _checkPing () async {
159
+ _ping.value = await HttpUtils .measureHttpPing ();
160
+ }
161
+
140
162
void _runIpCheckInfinitely () async {
141
163
while (true ) {
142
164
await _checkProxySettings ();
@@ -154,14 +176,15 @@ class AppBloc with AppSystemTray {
154
176
_isPingingGoogle = false ;
155
177
_checkNetworkConnectivity ();
156
178
return ;
157
- } on SocketException catch (ex) {
179
+ } on SocketException catch (ex) {
158
180
_checkNetworkRefuseException (ex);
159
181
}
160
182
_isPingingGoogle = false ;
161
183
}
162
184
163
185
void _checkNetworkRefuseException (Exception ex) {
164
- if (ex is SocketException && ex.message.startsWith ('The remote computer refused the network connection.' )) {
186
+ if (ex is SocketException &&
187
+ ex.message.startsWith ('The remote computer refused the network connection.' )) {
165
188
_proxyServer = null ;
166
189
}
167
190
}
@@ -222,6 +245,31 @@ class AppBloc with AppSystemTray {
222
245
}
223
246
}
224
247
248
+ void onConnectionTestClick () async {
249
+ _downloadSpeed.value = 0 ;
250
+ _uploadSpeed.value = 0 ;
251
+ _speedTestStatus.value = 'Running' ;
252
+ speedtest.getDataspeedtest (
253
+ downloadOnProgress: ((percent, transferRate) {
254
+ _downloadSpeed.value = transferRate;
255
+ }),
256
+ uploadOnProgress: ((percent, transferRate) {
257
+ _uploadSpeed.value = transferRate;
258
+ }),
259
+ progressResponse: ((responseTime, jitter) {
260
+ // nothing to do
261
+ }),
262
+ onError: ((errorMessage) {
263
+ _downloadSpeed.value = 0 ;
264
+ _uploadSpeed.value = 0 ;
265
+ _speedTestStatus.value = 'Error' ;
266
+ }),
267
+ onDone: () {
268
+ _speedTestStatus.value = 'Done' ;
269
+ },
270
+ );
271
+ }
272
+
225
273
void onExitClick () {
226
274
destroySystemTray ();
227
275
exit (0 );
@@ -238,6 +286,7 @@ class AppBloc with AppSystemTray {
238
286
239
287
void _handleRefresh () async {
240
288
await _checkProxySettings ();
289
+ _checkPing ();
241
290
_pingGoogle ();
242
291
_checkIpLocation ();
243
292
_verifyLeakedSites ();
0 commit comments