@@ -2,62 +2,56 @@ import { net } from 'electron';
2
2
3
3
import { DiagnosticsCategory , DiagnosticsChecker } from './types' ;
4
4
5
+ import mainEvents from '@pkg/main/mainEvents' ;
5
6
import Logging from '@pkg/utils/logging' ;
6
7
7
8
const console = Logging . diagnostics ;
8
9
9
- // Returns the timeout, in milliseconds, for the network connectivity check.
10
- function getTimeout ( ) : number {
11
- if ( process . env . RD_CONNECTED_TO_INTERNET_TIMEOUT ) {
12
- const parsedTimeout = parseInt ( process . env . RD_CONNECTED_TO_INTERNET_TIMEOUT ) ;
10
+ let pollingInterval : NodeJS . Timeout ;
11
+ let timeout = 5_000 ;
13
12
14
- if ( parsedTimeout > 0 ) {
15
- return parsedTimeout ;
16
- }
17
- }
13
+ // Since this is just a status check, it's fine to just reset the timer every
14
+ // time _any_ setting has been updated.
15
+ mainEvents . on ( 'settings-update' , settings => {
16
+ clearInterval ( pollingInterval ) ;
18
17
19
- return 5000 ;
20
- }
18
+ const { timeout : localTimeout , interval } = settings . diagnostics . connectivity ;
19
+
20
+ timeout = localTimeout ;
21
+ if ( interval > 0 ) {
22
+ pollingInterval = setInterval ( ( ) => {
23
+ mainEvents . invoke ( 'diagnostics-trigger' , CheckConnectedToInternet . id ) ;
24
+ } , interval ) ;
25
+ }
26
+ } ) ;
21
27
22
28
/**
23
29
* Checks whether we can perform an HTTP request to a host on the internet,
24
30
* with a reasonably short timeout.
25
31
*/
26
32
async function checkNetworkConnectivity ( ) : Promise < boolean > {
27
- const controller = new AbortController ( ) ;
28
- const timeout = getTimeout ( ) ;
29
- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeout ) ;
30
- let connected : boolean ;
31
-
32
- try {
33
+ const request = net . request ( {
33
34
// Using HTTP request that returns a 301 redirect response instead of a 20+ kB web page
34
- const resp = await net . fetch ( 'http://docs.rancherdesktop.io/' , { signal : controller . signal , redirect : 'manual' } ) ;
35
- const location = resp . headers . get ( 'Location' ) || '' ;
36
-
37
- // Verify that we get the original redirect and not a captive portal
38
- if ( resp . status !== 301 || ! location . includes ( 'docs.rancherdesktop.io' ) ) {
39
- throw new Error ( `expected status 301 (was ${ resp . status } ) and location including docs.rancherdesktop.io (was ${ location } )` ) ;
40
- }
41
- connected = true ;
42
- } catch ( error : any ) {
43
- let errorMessage = error ;
44
-
45
- if ( / R e d i r e c t w a s c a n c e l l e d / . test ( error ) ) {
46
- // Electron does not currently handle manual redirects correctly.
47
- // https://github.com/electron/electron/issues/43715
48
- connected = true ;
49
- } else {
50
- if ( error . name === 'AbortError' ) {
51
- errorMessage = `timed out after ${ timeout } ms` ;
52
- }
53
- console . log ( `Got error while checking connectivity: ${ errorMessage } ` ) ;
54
- connected = false ;
55
- }
35
+ url : 'http://docs.rancherdesktop.io/' ,
36
+ credentials : 'omit' ,
37
+ redirect : 'manual' ,
38
+ cache : 'no-cache' ,
39
+ } ) ;
40
+ const timeoutId = setTimeout ( ( ) => {
41
+ console . log ( `${ CheckConnectedToInternet . id } : aborting due to timeout after ${ timeout } milliseconds.` ) ;
42
+ request . abort ( ) ;
43
+ } , timeout ) ;
44
+ try {
45
+ return await new Promise < boolean > ( resolve => {
46
+ request . on ( 'response' , ( ) => resolve ( true ) ) ;
47
+ request . on ( 'redirect' , ( ) => resolve ( true ) ) ;
48
+ request . on ( 'error' , ( ) => resolve ( false ) ) ;
49
+ request . on ( 'abort' , ( ) => resolve ( false ) ) ;
50
+ request . end ( ) ;
51
+ } ) ;
56
52
} finally {
57
53
clearTimeout ( timeoutId ) ;
58
54
}
59
-
60
- return connected ;
61
55
}
62
56
63
57
/**
@@ -71,7 +65,9 @@ const CheckConnectedToInternet: DiagnosticsChecker = {
71
65
return Promise . resolve ( true ) ;
72
66
} ,
73
67
async check ( ) {
74
- if ( await checkNetworkConnectivity ( ) ) {
68
+ const connected = await checkNetworkConnectivity ( ) ;
69
+ mainEvents . emit ( 'diagnostics-event' , { id : 'network-connectivity' , connected } ) ;
70
+ if ( connected ) {
75
71
return {
76
72
description : 'The application can reach the internet successfully.' ,
77
73
passed : true ,
0 commit comments