2017-08-28 12:27:45 -04:00
/ * *
* @ fileoverview Meshcentral . js
* @ author Ylian Saint - Hilaire
* @ version v0 . 0.1
* /
2019-01-28 18:47:54 -05:00
var MeshServerCreateControl = function ( domain , authCookie ) {
2017-08-28 12:27:45 -04:00
var obj = { } ;
obj . State = 0 ;
obj . connectstate = 0 ;
2018-07-13 22:18:43 -04:00
obj . pingTimer = null ;
2019-01-28 18:47:54 -05:00
obj . authCookie = authCookie ;
2021-05-25 13:47:10 -04:00
//obj.trace = false;
2017-08-28 12:27:45 -04:00
2019-01-27 15:23:38 -05:00
obj . xxStateChange = function ( newstate , errCode ) {
2017-08-28 12:27:45 -04:00
if ( obj . State == newstate ) return ;
2019-01-27 15:23:38 -05:00
var previousState = obj . State ;
2017-08-28 12:27:45 -04:00
obj . State = newstate ;
2019-01-27 15:23:38 -05:00
if ( obj . onStateChanged ) obj . onStateChanged ( obj , obj . State , previousState , errCode ) ;
2017-08-28 12:27:45 -04:00
}
obj . Start = function ( ) {
2019-01-27 15:23:38 -05:00
if ( obj . connectstate != 0 ) return ;
2017-08-28 12:27:45 -04:00
obj . connectstate = 0 ;
2020-09-18 03:57:36 -04:00
var url = window . location . protocol . replace ( 'http' , 'ws' ) + '//' + window . location . host + domain + 'control.ashx' + ( urlargs . key ? ( '?key=' + urlargs . key ) : '' ) ;
2021-11-03 22:22:31 -04:00
if ( obj . authCookie && ( obj . authCookie != '' ) ) { url += '?moreargs=1' }
2019-01-28 18:47:54 -05:00
obj . socket = new WebSocket ( url ) ;
2021-11-03 21:52:11 -04:00
obj . socket . onopen = function ( e ) { obj . connectstate = 1 ; if ( obj . authCookie && ( obj . authCookie != '' ) ) { obj . send ( { 'action' : 'urlargs' , 'args' : { 'auth' : obj . authCookie } } ) ; } }
2017-08-28 12:27:45 -04:00
obj . socket . onmessage = obj . xxOnMessage ;
2019-01-27 15:23:38 -05:00
obj . socket . onclose = function ( e ) { obj . Stop ( e . code ) ; }
obj . xxStateChange ( 1 , 0 ) ;
2018-07-13 22:18:43 -04:00
if ( obj . pingTimer != null ) { clearInterval ( obj . pingTimer ) ; }
obj . pingTimer = setInterval ( function ( ) { obj . send ( { action : 'ping' } ) ; } , 29000 ) ; // Ping the server every 29 seconds, stops corporate proxies from disconnecting.
2017-08-28 12:27:45 -04:00
}
2019-01-27 15:23:38 -05:00
obj . Stop = function ( errCode ) {
2017-08-28 12:27:45 -04:00
obj . connectstate = 0 ;
if ( obj . socket ) { obj . socket . close ( ) ; delete obj . socket ; }
2018-07-13 22:18:43 -04:00
if ( obj . pingTimer != null ) { clearInterval ( obj . pingTimer ) ; obj . pingTimer = null ; }
2019-01-27 15:23:38 -05:00
obj . xxStateChange ( 0 , errCode ) ;
2017-08-28 12:27:45 -04:00
}
obj . xxOnMessage = function ( e ) {
2019-01-27 15:23:38 -05:00
if ( obj . State == 1 ) { obj . xxStateChange ( 2 ) ; }
//console.log('xxOnMessage', e.data);
2017-08-28 12:27:45 -04:00
var message ;
try { message = JSON . parse ( e . data ) ; } catch ( e ) { return ; }
2019-01-27 15:23:38 -05:00
if ( ( typeof message != 'object' ) || ( message . action == 'pong' ) ) { return ; }
2020-05-06 22:23:11 -04:00
if ( message . action == 'ping' ) { obj . send ( { action : 'pong' } ) ; }
2019-01-28 18:47:54 -05:00
if ( message . action == 'close' ) { if ( message . msg ) { console . log ( message . msg ) ; } obj . Stop ( message . cause ) ; return ; }
2021-05-25 13:47:10 -04:00
if ( obj . trace == 1 ) { console . log ( 'RECV' , message ) ; }
else if ( obj . trace == 2 ) { console . log ( 'RECV' , JSON . stringify ( message ) ) ; }
2017-08-28 12:27:45 -04:00
if ( obj . onMessage ) obj . onMessage ( obj , message ) ;
} ;
2019-06-17 20:17:23 -04:00
obj . send = function ( x ) {
if ( obj . socket != null && obj . connectstate == 1 ) {
2021-05-25 13:47:10 -04:00
if ( x . action != 'ping' ) {
if ( obj . trace == 1 ) { console . log ( 'SEND' , x ) ; }
else if ( obj . trace == 2 ) { console . log ( 'SEND' , JSON . stringify ( x ) ) ; }
}
2019-06-17 20:17:23 -04:00
obj . socket . send ( JSON . stringify ( x ) ) ;
}
}
2017-08-28 12:27:45 -04:00
return obj ;
}