1
1
import { logWithTimestamp , errorWithTimestamp } from "./log.js" ;
2
2
import ping from "ping" ;
3
3
import { executeBashCommand } from "./bash.js" ;
4
+ import { getRcloneConfig } from "./rclone.js" ;
4
5
5
6
export const restartWifi = async ( ) => {
6
7
logWithTimestamp ( "Restarting wifi" )
7
8
await executeBashCommand ( "ifconfig wlan0 down && ifconfig wlan0 up" )
8
9
}
9
10
10
- export const checkIfArchiveIsReachable = async ( archiveServer ) => {
11
+ export const checkIfArchiveIsReachable = async ( ) => {
11
12
13
+ const config = getRcloneConfig ( )
14
+ const connectivityTestType = getConnectivityTestType ( config )
15
+
16
+ if ( connectivityTestType === ConnectivityTestType . internetConnected ) {
17
+ return await testInternetConnected ( )
18
+ }
19
+
20
+ if ( connectivityTestType === ConnectivityTestType . pingHost ) {
21
+ return await testPingHost ( config . host )
22
+ }
23
+
24
+ } ;
25
+
26
+ const testPingHost = async ( host : string ) => {
27
+ try {
28
+ const res = await ping . promise . probe ( host , {
29
+ timeout : 5 , // Timeout in seconds
30
+ extra : [ "-c" , "1" ] , // Sends only 1 packet
31
+ } ) ;
32
+
33
+ logWithTimestamp ( `${ host } is ${ res . alive ? 'reachable' : 'not reachable' } ` ) ;
34
+ return res . alive ;
35
+ } catch ( error ) {
36
+ errorWithTimestamp ( 'Error pinging host:' , error ) ;
37
+ return false ;
38
+ }
39
+ }
40
+
41
+ const testInternetConnected = async ( ) => {
12
42
try {
13
- const res = await ping . promise . probe ( archiveServer , {
43
+ const res = await ping . promise . probe ( "google.com" , {
14
44
timeout : 5 , // Timeout in seconds
15
45
extra : [ "-c" , "1" ] , // Sends only 1 packet
16
46
} ) ;
17
47
18
- logWithTimestamp ( `${ archiveServer } is ${ res . alive ? 'reachable' : 'not reachable' } ` ) ;
48
+ logWithTimestamp ( `Internet is ${ res . alive ? 'reachable' : 'not reachable' } ` ) ;
19
49
return res . alive ;
20
50
} catch ( error ) {
21
- errorWithTimestamp ( 'Error pinging archive server :' , error ) ;
51
+ errorWithTimestamp ( 'Error pinging google.com :' , error ) ;
22
52
return false ;
23
53
}
24
- } ;
54
+ }
55
+
56
+ enum ConnectivityTestType {
57
+ pingHost = "pingHost" ,
58
+ internetConnected = "internetConnected" ,
59
+ }
60
+
61
+ const getConnectivityTestType = ( config : any ) => {
62
+ let connectivityTestType : ConnectivityTestType ;
63
+ switch ( config . type ) {
64
+ case "smb" :
65
+ connectivityTestType = ConnectivityTestType . pingHost
66
+ default :
67
+ connectivityTestType = ConnectivityTestType . internetConnected
68
+ }
69
+ return connectivityTestType
70
+ }
0 commit comments