2021-07-27 13:47:13 -04:00
import { io } from "socket.io-client" ;
import { useToast } from "vue-toastification" ;
2021-09-11 07:40:03 -04:00
import axios from "axios" ;
2021-06-25 09:55:49 -04:00
const toast = useToast ( )
let socket ;
2021-09-11 07:40:03 -04:00
const noSocketIOPages = [
2021-09-11 04:22:30 -04:00
"/status-page" ,
] ;
2021-06-25 09:55:49 -04:00
export default {
data ( ) {
return {
2021-07-13 06:08:12 -04:00
info : { } ,
2021-06-25 09:55:49 -04:00
socket : {
token : null ,
firstConnect : true ,
connected : false ,
2021-06-29 04:06:20 -04:00
connectCount : 0 ,
2021-09-11 04:22:30 -04:00
initedSocketIO : false ,
2021-06-25 09:55:49 -04:00
} ,
2021-07-01 10:16:02 -04:00
remember : ( localStorage . remember !== "0" ) ,
2021-06-25 09:55:49 -04:00
allowLoginDialog : false , // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed.
loggedIn : false ,
2021-07-01 01:11:16 -04:00
monitorList : { } ,
heartbeatList : { } ,
importantHeartbeatList : { } ,
avgPingList : { } ,
2021-07-09 02:14:03 -04:00
uptimeList : { } ,
2021-07-21 00:09:09 -04:00
certInfoList : { } ,
2021-07-09 02:14:03 -04:00
notificationList : [ ] ,
2021-08-08 01:47:29 -04:00
connectionErrorMsg : "Cannot connect to the socket server. Reconnecting..." ,
2021-06-25 09:55:49 -04:00
}
} ,
created ( ) {
2021-07-27 13:47:13 -04:00
window . addEventListener ( "resize" , this . onResize ) ;
2021-09-11 04:22:30 -04:00
this . initSocketIO ( ) ;
} ,
2021-07-11 03:23:28 -04:00
2021-09-11 04:22:30 -04:00
methods : {
2021-09-11 02:37:05 -04:00
2021-09-11 04:22:30 -04:00
initSocketIO ( bypass = false ) {
// No need to re-init
if ( this . socket . initedSocketIO ) {
return ;
}
2021-07-11 02:20:31 -04:00
2021-09-11 04:22:30 -04:00
// No need to connect to the socket.io for status page
2021-09-11 07:40:03 -04:00
if ( ! bypass && noSocketIOPages . includes ( location . pathname ) ) {
2021-09-11 04:22:30 -04:00
return ;
}
this . socket . initedSocketIO = true ;
2021-06-25 09:55:49 -04:00
2021-09-11 04:22:30 -04:00
let protocol = ( location . protocol === "https:" ) ? "wss://" : "ws://" ;
2021-07-09 02:14:03 -04:00
2021-09-11 04:22:30 -04:00
let wsHost ;
const env = process . env . NODE _ENV || "production" ;
if ( env === "development" || localStorage . dev === "dev" ) {
wsHost = protocol + location . hostname + ":3001" ;
} else {
wsHost = protocol + location . host ;
2021-06-27 04:10:55 -04:00
}
2021-09-11 04:22:30 -04:00
socket = io ( wsHost , {
transports : [ "websocket" ] ,
} ) ;
2021-06-30 09:04:58 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "info" , ( info ) => {
this . info = info ;
} ) ;
2021-06-30 09:04:58 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "setup" , ( monitorID , data ) => {
this . $router . push ( "/setup" )
} ) ;
2021-06-30 09:04:58 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "autoLogin" , ( monitorID , data ) => {
this . loggedIn = true ;
this . storage ( ) . token = "autoLogin" ;
this . allowLoginDialog = false ;
} ) ;
socket . on ( "monitorList" , ( data ) => {
// Add Helper function
Object . entries ( data ) . forEach ( ( [ monitorID , monitor ] ) => {
monitor . getUrl = ( ) => {
try {
return new URL ( monitor . url ) ;
} catch ( _ ) {
return null ;
}
} ;
} ) ;
this . monitorList = data ;
} ) ;
socket . on ( "notificationList" , ( data ) => {
this . notificationList = data ;
} ) ;
socket . on ( "heartbeat" , ( data ) => {
if ( ! ( data . monitorID in this . heartbeatList ) ) {
this . heartbeatList [ data . monitorID ] = [ ] ;
2021-06-30 09:04:58 -04:00
}
2021-09-16 10:48:28 -04:00
this . heartbeatList [ data . monitorID ] . push ( data ) ;
if ( this . heartbeatList [ data . monitorID ] . length >= 150 ) {
this . heartbeatList [ data . monitorID ] . shift ( ) ;
}
2021-09-11 04:22:30 -04:00
// Add to important list if it is important
// Also toast
if ( data . important ) {
if ( data . status === 0 ) {
toast . error ( ` [ ${ this . monitorList [ data . monitorID ] . name } ] [DOWN] ${ data . msg } ` , {
timeout : false ,
} ) ;
} else if ( data . status === 1 ) {
toast . success ( ` [ ${ this . monitorList [ data . monitorID ] . name } ] [Up] ${ data . msg } ` , {
timeout : 20000 ,
} ) ;
} else {
toast ( ` [ ${ this . monitorList [ data . monitorID ] . name } ] ${ data . msg } ` ) ;
}
2021-06-27 04:10:55 -04:00
2021-09-11 04:22:30 -04:00
if ( ! ( data . monitorID in this . importantHeartbeatList ) ) {
this . importantHeartbeatList [ data . monitorID ] = [ ] ;
}
2021-06-29 04:06:20 -04:00
2021-09-11 04:22:30 -04:00
this . importantHeartbeatList [ data . monitorID ] . unshift ( data )
}
} ) ;
2021-06-30 09:04:58 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "heartbeatList" , ( monitorID , data , overwrite = false ) => {
if ( ! ( monitorID in this . heartbeatList ) || overwrite ) {
this . heartbeatList [ monitorID ] = data ;
} else {
this . heartbeatList [ monitorID ] = data . concat ( this . heartbeatList [ monitorID ] )
}
} ) ;
2021-07-01 01:11:16 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "avgPing" , ( monitorID , data ) => {
this . avgPingList [ monitorID ] = data
} ) ;
2021-07-21 00:09:09 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "uptime" , ( monitorID , type , data ) => {
this . uptimeList [ ` ${ monitorID } _ ${ type } ` ] = data
} ) ;
2021-06-25 09:55:49 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "certInfo" , ( monitorID , data ) => {
this . certInfoList [ monitorID ] = JSON . parse ( data )
} ) ;
2021-07-31 11:41:24 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "importantHeartbeatList" , ( monitorID , data , overwrite ) => {
if ( ! ( monitorID in this . importantHeartbeatList ) || overwrite ) {
this . importantHeartbeatList [ monitorID ] = data ;
2021-08-03 01:07:20 -04:00
} else {
2021-09-11 04:22:30 -04:00
this . importantHeartbeatList [ monitorID ] = data . concat ( this . importantHeartbeatList [ monitorID ] )
}
} ) ;
2021-08-03 01:07:20 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "connect_error" , ( err ) => {
console . error ( ` Failed to connect to the backend. Socket.io connect_error: ${ err . message } ` ) ;
this . connectionErrorMsg = ` Cannot connect to the socket server. [ ${ err } ] Reconnecting... ` ;
this . socket . connected = false ;
this . socket . firstConnect = false ;
} ) ;
2021-08-03 01:07:20 -04:00
2021-09-11 04:22:30 -04:00
socket . on ( "disconnect" , ( ) => {
console . log ( "disconnect" )
this . connectionErrorMsg = "Lost connection to the socket server. Reconnecting..." ;
this . socket . connected = false ;
} ) ;
socket . on ( "connect" , ( ) => {
console . log ( "connect" )
this . socket . connectCount ++ ;
this . socket . connected = true ;
// Reset Heartbeat list if it is re-connect
if ( this . socket . connectCount >= 2 ) {
this . clearData ( )
2021-07-31 11:41:24 -04:00
}
2021-06-25 09:55:49 -04:00
2021-09-11 04:22:30 -04:00
let token = this . storage ( ) . token ;
2021-06-25 09:55:49 -04:00
2021-09-11 04:22:30 -04:00
if ( token ) {
if ( token !== "autoLogin" ) {
this . loginByToken ( token )
} else {
2021-06-25 09:55:49 -04:00
2021-09-11 04:22:30 -04:00
// Timeout if it is not actually auto login
setTimeout ( ( ) => {
if ( ! this . loggedIn ) {
this . allowLoginDialog = true ;
this . $root . storage ( ) . removeItem ( "token" ) ;
}
} , 5000 ) ;
}
} else {
this . allowLoginDialog = true ;
}
this . socket . firstConnect = false ;
} ) ;
} ,
2021-06-27 04:10:55 -04:00
2021-07-01 10:16:02 -04:00
storage ( ) {
return ( this . remember ) ? localStorage : sessionStorage ;
} ,
2021-06-25 09:55:49 -04:00
getSocket ( ) {
2021-07-18 14:46:45 -04:00
return socket ;
2021-06-25 09:55:49 -04:00
} ,
2021-06-27 04:10:55 -04:00
2021-06-25 09:55:49 -04:00
toastRes ( res ) {
if ( res . ok ) {
toast . success ( res . msg ) ;
} else {
toast . error ( res . msg ) ;
}
} ,
2021-06-27 04:10:55 -04:00
2021-09-09 15:10:31 -04:00
login ( username , password , token , callback ) {
2021-06-25 09:55:49 -04:00
socket . emit ( "login" , {
username ,
password ,
2021-09-09 15:10:31 -04:00
token ,
2021-06-25 09:55:49 -04:00
} , ( res ) => {
2021-09-09 15:10:31 -04:00
if ( res . tokenRequired ) {
callback ( res )
}
2021-06-25 09:55:49 -04:00
if ( res . ok ) {
2021-07-01 10:16:02 -04:00
this . storage ( ) . token = res . token ;
2021-06-25 09:55:49 -04:00
this . socket . token = res . token ;
this . loggedIn = true ;
// Trigger Chrome Save Password
2021-07-27 13:47:13 -04:00
history . pushState ( { } , "" )
2021-06-25 09:55:49 -04:00
}
callback ( res )
} )
} ,
2021-06-27 04:10:55 -04:00
2021-06-25 09:55:49 -04:00
loginByToken ( token ) {
socket . emit ( "loginByToken" , token , ( res ) => {
this . allowLoginDialog = true ;
if ( ! res . ok ) {
this . logout ( )
} else {
this . loggedIn = true ;
}
} )
} ,
2021-06-27 04:10:55 -04:00
2021-06-25 09:55:49 -04:00
logout ( ) {
2021-07-01 10:16:02 -04:00
this . storage ( ) . removeItem ( "token" ) ;
2021-06-25 09:55:49 -04:00
this . socket . token = null ;
2021-06-30 09:04:58 -04:00
this . loggedIn = false ;
2021-06-25 09:55:49 -04:00
2021-06-30 09:04:58 -04:00
this . clearData ( )
2021-06-25 09:55:49 -04:00
} ,
2021-06-27 04:10:55 -04:00
2021-09-09 15:10:31 -04:00
prepare2FA ( callback ) {
socket . emit ( "prepare2FA" , callback )
} ,
save2FA ( secret , callback ) {
socket . emit ( "save2FA" , callback )
} ,
disable2FA ( callback ) {
socket . emit ( "disable2FA" , callback )
} ,
verifyToken ( token , callback ) {
socket . emit ( "verifyToken" , token , callback )
} ,
twoFAStatus ( callback ) {
socket . emit ( "twoFAStatus" , callback )
} ,
2021-08-26 06:55:19 -04:00
getMonitorList ( callback ) {
socket . emit ( "getMonitorList" , callback )
} ,
2021-06-25 09:55:49 -04:00
add ( monitor , callback ) {
socket . emit ( "add" , monitor , callback )
} ,
2021-06-27 04:10:55 -04:00
2021-06-25 09:55:49 -04:00
deleteMonitor ( monitorID , callback ) {
socket . emit ( "deleteMonitor" , monitorID , callback )
} ,
2021-06-30 09:04:58 -04:00
clearData ( ) {
console . log ( "reset heartbeat list" )
this . heartbeatList = { }
this . importantHeartbeatList = { }
} ,
2021-09-11 15:53:17 -04:00
uploadBackup ( uploadedJSON , importHandle , callback ) {
socket . emit ( "uploadBackup" , uploadedJSON , importHandle , callback )
2021-09-01 11:09:32 -04:00
} ,
2021-09-07 08:12:53 -04:00
2021-08-29 12:47:01 -04:00
clearEvents ( monitorID , callback ) {
socket . emit ( "clearEvents" , monitorID , callback )
} ,
clearHeartbeats ( monitorID , callback ) {
socket . emit ( "clearHeartbeats" , monitorID , callback )
} ,
2021-08-31 18:36:24 -04:00
clearStatistics ( callback ) {
socket . emit ( "clearStatistics" , callback )
} ,
2021-06-25 09:55:49 -04:00
} ,
computed : {
2021-07-01 09:47:14 -04:00
2021-06-27 04:10:55 -04:00
lastHeartbeatList ( ) {
let result = { }
for ( let monitorID in this . heartbeatList ) {
let index = this . heartbeatList [ monitorID ] . length - 1 ;
result [ monitorID ] = this . heartbeatList [ monitorID ] [ index ] ;
}
return result ;
} ,
statusList ( ) {
let result = { }
2021-06-25 09:55:49 -04:00
2021-06-27 04:10:55 -04:00
let unknown = {
text : "Unknown" ,
2021-07-27 13:47:13 -04:00
color : "secondary" ,
2021-06-27 04:10:55 -04:00
}
for ( let monitorID in this . lastHeartbeatList ) {
let lastHeartBeat = this . lastHeartbeatList [ monitorID ]
if ( ! lastHeartBeat ) {
result [ monitorID ] = unknown ;
} else if ( lastHeartBeat . status === 1 ) {
result [ monitorID ] = {
text : "Up" ,
2021-07-27 13:47:13 -04:00
color : "primary" ,
2021-06-27 04:10:55 -04:00
} ;
} else if ( lastHeartBeat . status === 0 ) {
result [ monitorID ] = {
text : "Down" ,
2021-07-27 13:47:13 -04:00
color : "danger" ,
2021-06-27 04:10:55 -04:00
} ;
2021-07-19 12:23:06 -04:00
} else if ( lastHeartBeat . status === 2 ) {
result [ monitorID ] = {
text : "Pending" ,
2021-07-27 13:47:13 -04:00
color : "warning" ,
2021-07-19 12:23:06 -04:00
} ;
2021-06-27 04:10:55 -04:00
} else {
result [ monitorID ] = unknown ;
}
}
return result ;
2021-07-27 13:47:13 -04:00
} ,
2021-07-01 10:16:02 -04:00
} ,
watch : {
2021-07-13 06:08:12 -04:00
// Reload the SPA if the server version is changed.
"info.version" ( to , from ) {
2021-07-27 13:47:13 -04:00
if ( from && from !== to ) {
window . location . reload ( )
}
2021-07-13 06:08:12 -04:00
} ,
2021-07-01 10:16:02 -04:00
remember ( ) {
localStorage . remember = ( this . remember ) ? "1" : "0"
2021-07-27 13:47:13 -04:00
} ,
2021-07-01 10:16:02 -04:00
2021-09-11 04:22:30 -04:00
// Reconnect the socket io, if status-page to dashboard
"$route.fullPath" ( newValue , oldValue ) {
2021-09-11 07:40:03 -04:00
if ( noSocketIOPages . includes ( newValue ) ) {
2021-09-11 04:22:30 -04:00
return ;
}
this . initSocketIO ( ) ;
} ,
2021-07-27 13:47:13 -04:00
} ,
2021-06-25 09:55:49 -04:00
}